Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
[IR] [lang] Support SHR operator: ti.bit_shr(x, y) #1871
[IR] [lang] Support SHR operator: ti.bit_shr(x, y) #1871
Changes from 5 commits
59cba6f
98c1b7e
8eaea77
0ff726d
cc216bf
fae7a3b
8fc51d9
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test will fail on backends other than LLVM due to unimplemented op.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This includes(but not limited to...) cc, metal and opengl.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch... I believe on these backends SHR is not implemented (but SAR is). How about this: let's demote
bit_shr
into a series of three operationsUnaryOpStmt
bit_cast
into unsignedBinaryOpStmt
bit_sar
UnaryOpStmt
bit_cast
into signedin this pass
taichi/taichi/transforms/demote_operations.cpp
Line 17 in edb8ed7
https://github.com/yuanming-hu/taichi/blob/2cc50cfaeb84c6ba4f3df7d3a0caa028daadb51f/taichi/transforms/demote_operations.cpp#L17
So that backend developers don't need to worry about SAR. This is a very late pass in the compilation process - we still need SAR in the IR for certain domain-specific optimizations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! I'll later add uint support to OpenGL so that SHR works.
EDIT: So
bit_sar
should act as SHR when operand is uint to make this method work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@archibate That's what I'd also like to discuss. I did some experiments, in the LLVM backend,
bit_sar
is implemented usingCreateAShr
which simply copies the MSB without considering the type. While on other backends,bit_sar
is expressed with>>
which will behave differently according to unsigned/signed information. I think we need to decide which kind of SAR operation we truly want, either a pure one copying the MSB or a one that is more similar to the>>
in C.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all the discussions. My two cents:
>>
always translates toBinaryOpType::bit_sar
andti.bit_shr
always translates toBinaryOpType::bit_shr
.demote_operations.cpp
we convertbit_shr
on signed integers into three sub-operations as discussed above. Then we only havebit_sar
for the backend.bit_sar
, its behavior is determined by the type of its operands.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yuanming-hu Thanks for the clarification.
The point(or problem) I'd like to discuss is the behavior of
bit_sar
whichbit_shr
will eventually rely on. As I wrote in the above comment, in the LLVM backend,bit_sar
is implemented with aCreateAShr
which directly maps to asar
instruction and simply copies the MSB, that is, it will ignore the type information so casting signed to unsigned will not work since the low-level bits are not changed by type casts. In other backends (at least Metal),bit_sar
is implemented using the operator>>
, which will consider the type information, that is, it will map to azext
andsar
when working on unsigned integers. This is creating different behaviors across backends. We should first decide what kind of behavior we want forbit_sar
before we move on.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's follow the Metal behavior
bit_sar
in backends. And providebit_shr
usingdemote_operations
as described above.The LLVM backend should do some branches to match the behavior of Metal, e.g.:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps one source of confusion is that LLVM doesn't distinguish signed/unsigned integers (so does hardware such x64). So
SHR
always shifts everything andSAR
copies the MSB in LLVM.In Taichi, on unsigned integers
bit_shr=bit_sar
and they both map toSHR
in LLVM. On signed integers,bit_shr
maps toSHR
in LLVM andbit_sar
maps toSAR
in LLVM.