Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] Replace std::map with switch case statements in lang_util.cpp #1755

Merged
merged 26 commits into from
Aug 29, 2020
Merged

Conversation

aryansoman
Copy link
Contributor

@aryansoman aryansoman commented Aug 23, 2020

Related issue = #1750

[Click here for the format server]


Hello, I tried refactoring the first two functions. Once these changes look good, I'll apply them to every function with std::map.

@yuanming-hu yuanming-hu self-requested a review August 23, 2020 14:54
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool!!! I believe your strategy mostly works. For the special cases, let's use a case instead of macros :-)

Please go ahead and refactor more functions. Feel free to ask me to take another look if there's anything you are unsure of during the refactoring.

Thanks again for contributing! Looking forward to having this PR merged :-)

case DataType::i: \
return sizeof(j);

REGISTER_DATA_TYPE(DataType::f16, 2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, sizeof(2) gives you 4 since 2 is treated as int32. Since this option is not regular, maybe we can use an explicit case DataType::f16: return 2 instead of the macro :-)

Comment on lines 124 to 132
type_sizes[DataType::gen] = 0;
type_sizes[DataType::unknown] = -1;
REGISTER_DATA_TYPE(DataType::gen, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Comment on lines 135 to 136
default: //unknown
return -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's treat unknown in a switch branch, and use TI_NOT_IMPLEMENTED in the default case: it's not impossible in C++ that someone triggered undefined a behavior and DataType's value equals non of its options. We should fail loudly in that case.

@aryansoman
Copy link
Contributor Author

I've refactored the functions and put TI_NOT_IMPLEMENTED in the default blocks, and corrected the sizing errors. Let me know if anything else needs work. Thank you!

@yuanming-hu
Copy link
Member

yuanming-hu commented Aug 24, 2020

Very cool! Could you fix the compilation errors? https://github.com/taichi-dev/taichi/runs/1018642631#step:5:200

It's highly recommended to have a local Taichi built from source: https://taichi.readthedocs.io/en/latest/dev_install.html#developer-installation so that you can fix the compilation errors locally.

@@ -0,0 +1,519 @@
// Definitions of utility functions and enums
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this file to misc/lang_util_backup.cpp to prevent confusion.

@yuanming-hu yuanming-hu changed the title [Refactor] replaced std::map with switch case statements [refactor] Replace std::map with switch case statements in lang_util.cpp Aug 26, 2020
@aryansoman
Copy link
Contributor Author

@yuanming-hu The switch-case statement doesn't work for pairs of DataTypes, so should I leave the promoted_type method as-is?

@yuanming-hu
Copy link
Member

@yuanming-hu The switch-case statement doesn't work for pairs of DataTypes, so should I leave the promoted_type method as-is?

Sure, sounds good to me! We can leave it to a future PR. Thank you.

@codecov
Copy link

codecov bot commented Aug 29, 2020

Codecov Report

Merging #1755 into master will decrease coverage by 0.07%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1755      +/-   ##
==========================================
- Coverage   43.51%   43.43%   -0.08%     
==========================================
  Files          44       44              
  Lines        6023     6011      -12     
  Branches     1078     1075       -3     
==========================================
- Hits         2621     2611      -10     
+ Misses       3248     3246       -2     
  Partials      154      154              
Impacted Files Coverage Δ
python/taichi/lang/impl.py 66.37% <0.00%> (-0.94%) ⬇️
python/taichi/diagnose.py 0.00% <0.00%> (ø)
python/taichi/lang/ops.py 52.65% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b5c3ee2...d29bc6e. Read the comment docs.

@yuanming-hu yuanming-hu marked this pull request as ready for review August 29, 2020 15:00
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! I believe we are mostly done. Please apply the suggestions if those make sense - then I can merge this once the tests pass. Currently, the tests fail because of the deletion of bit_shl and bit_shr.

Thank you so much for contributing! This is a very very helpful refactoring to have since we will have parallel compilation very soon.


REGISTER_DATA_TYPE(f32, float32);
REGISTER_DATA_TYPE(f64, float64);
// REGISTER_DATA_TYPE(i8, bool); // duplicate value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// REGISTER_DATA_TYPE(i8, bool); // duplicate value

Nice catch! Let's remove this line then :-)

Comment on lines 199 to 200
REGISTER_TYPE(bit_shl, <<);
REGISTER_TYPE(bit_sar, >>);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we still need these lines?

@yuanming-hu yuanming-hu merged commit c3f1a5d into taichi-dev:master Aug 29, 2020
@yuanming-hu yuanming-hu mentioned this pull request Sep 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants