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

ti.bit_shr is broken when the input is unsigned #7650

Closed
lin-hitonami opened this issue Mar 24, 2023 · 0 comments · Fixed by #7757
Closed

ti.bit_shr is broken when the input is unsigned #7650

lin-hitonami opened this issue Mar 24, 2023 · 0 comments · Fixed by #7757
Assignees

Comments

@lin-hitonami
Copy link
Contributor

Describe the bug
ti.bit_shr is broken when the input is unsigned because we only transform it to bit_sar(unsigned) when the input is signed, and the codegen assumes that bit_shr should not exist when the ir reaches the codegen.

} else if (stmt->op_type == BinaryOpType::bit_shr &&
is_integral(lhs->element_type()) &&
is_integral(rhs->element_type()) &&
is_signed(lhs->element_type())) {
// @ti.func
// def bit_shr(a, b):
// unsigned_a = ti.cast(a, ti.uXX)
// shifted = ti.bit_sar(unsigned_a, b)
// ret = ti.cast(shifted, ti.iXX)
// return ret
auto unsigned_cast = Stmt::make<UnaryOpStmt>(UnaryOpType::cast_bits, lhs);
unsigned_cast->as<UnaryOpStmt>()->cast_type =
to_unsigned(lhs->element_type());
auto shift = Stmt::make<BinaryOpStmt>(BinaryOpType::bit_sar,
unsigned_cast.get(), rhs);
auto signed_cast =
Stmt::make<UnaryOpStmt>(UnaryOpType::cast_bits, shift.get());
signed_cast->as<UnaryOpStmt>()->cast_type = lhs->element_type();
signed_cast->ret_type = stmt->ret_type;
stmt->replace_usages_with(signed_cast.get());
modifier.insert_before(stmt, std::move(unsigned_cast));
modifier.insert_before(stmt, std::move(shift));
modifier.insert_before(stmt, std::move(signed_cast));
modifier.erase(stmt);

To Reproduce

import taichi as ti
ti.init()                                                                                                                        

@ti.kernel
def func(x: ti.u32, y: ti.i32)->ti.u32:
    return ti.bit_shr(x, y)

print(func(5, 2))

Log/Screenshots
Please post the full log of the program (instead of just a few lines around the error message, unless the log is > 1000 lines). This will help us diagnose what's happening. For example:

[Taichi] version 1.6.0, llvm 15.0.4, commit 2467d918, linux, python 3.10.0
[Taichi] Starting on arch=x64
[I 03/24/23 17:04:04.280 1754876] [codegen_llvm.cpp:visit@834] 
"binary_op_type_name(op)": "bit_shr"
[E 03/24/23 17:04:04.280 1754876] [codegen_llvm.cpp:visit@835] Not supported.
@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang Mar 24, 2023
@jim19930609 jim19930609 moved this from Untriaged to Todo in Taichi Lang Mar 31, 2023
ailzhang added a commit that referenced this issue Apr 7, 2023
Closes #7650

### Brief Summary

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at b81d127</samp>

Fix the demotion transformation for the bit shift right operation with
unsigned integers and add a test case for it. This improves the
correctness and test coverage for issue `#2840`.

### Walkthrough

<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at b81d127</samp>

* Relax demotion condition for bit shift right operation to apply to
both signed and unsigned integers
([link](https://github.com/taichi-dev/taichi/pull/7757/files?diff=unified&w=0#diff-d217f2b07d4578612dc805b0f01e5dc1883be9acb906b222a8762313cfd0596bL154-R154))
* Modify demotion transformation for bit shift right operation to cast
left operand to unsigned only if originally signed and perform bit shift
arithmetic right
([link](https://github.com/taichi-dev/taichi/pull/7757/files?diff=unified&w=0#diff-d217f2b07d4578612dc805b0f01e5dc1883be9acb906b222a8762313cfd0596bL163-R164))
* Add test case for bit shift right operation with unsigned integers in
`test_bit_operations.py`
([link](https://github.com/taichi-dev/taichi/pull/7757/files?diff=unified&w=0#diff-ea8fa4876c4d3e866c5c18e9d9d796364c5debf5eae6447e655326ed44f4257cR63-R71))
@github-project-automation github-project-automation bot moved this from Todo to Done in Taichi Lang Apr 7, 2023
ailzhang added a commit to ailzhang/taichi that referenced this issue Apr 12, 2023
Closes taichi-dev#7650

### Brief Summary

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at b81d127</samp>

Fix the demotion transformation for the bit shift right operation with
unsigned integers and add a test case for it. This improves the
correctness and test coverage for issue `taichi-dev#2840`.

### Walkthrough

<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at b81d127</samp>

* Relax demotion condition for bit shift right operation to apply to
both signed and unsigned integers
([link](https://github.com/taichi-dev/taichi/pull/7757/files?diff=unified&w=0#diff-d217f2b07d4578612dc805b0f01e5dc1883be9acb906b222a8762313cfd0596bL154-R154))
* Modify demotion transformation for bit shift right operation to cast
left operand to unsigned only if originally signed and perform bit shift
arithmetic right
([link](https://github.com/taichi-dev/taichi/pull/7757/files?diff=unified&w=0#diff-d217f2b07d4578612dc805b0f01e5dc1883be9acb906b222a8762313cfd0596bL163-R164))
* Add test case for bit shift right operation with unsigned integers in
`test_bit_operations.py`
([link](https://github.com/taichi-dev/taichi/pull/7757/files?diff=unified&w=0#diff-ea8fa4876c4d3e866c5c18e9d9d796364c5debf5eae6447e655326ed44f4257cR63-R71))
quadpixels pushed a commit to quadpixels/taichi that referenced this issue May 13, 2023
Closes taichi-dev#7650

### Brief Summary

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at b81d127</samp>

Fix the demotion transformation for the bit shift right operation with
unsigned integers and add a test case for it. This improves the
correctness and test coverage for issue `taichi-dev#2840`.

### Walkthrough

<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at b81d127</samp>

* Relax demotion condition for bit shift right operation to apply to
both signed and unsigned integers
([link](https://github.com/taichi-dev/taichi/pull/7757/files?diff=unified&w=0#diff-d217f2b07d4578612dc805b0f01e5dc1883be9acb906b222a8762313cfd0596bL154-R154))
* Modify demotion transformation for bit shift right operation to cast
left operand to unsigned only if originally signed and perform bit shift
arithmetic right
([link](https://github.com/taichi-dev/taichi/pull/7757/files?diff=unified&w=0#diff-d217f2b07d4578612dc805b0f01e5dc1883be9acb906b222a8762313cfd0596bL163-R164))
* Add test case for bit shift right operation with unsigned integers in
`test_bit_operations.py`
([link](https://github.com/taichi-dev/taichi/pull/7757/files?diff=unified&w=0#diff-ea8fa4876c4d3e866c5c18e9d9d796364c5debf5eae6447e655326ed44f4257cR63-R71))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants