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

[Opt] Make merging casts int(int(x)) less aggressive #7944

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions taichi/transforms/alg_simp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ class AlgSimp : public BasicStmtVisitor {
data_type_bits(second_cast) <= data_type_bits(first_cast);
}
if (is_integral(first_cast)) {
// int(int(a))
return data_type_bits(second_cast) <= data_type_bits(first_cast);
// int(int(a)), note it's not always equivalent when signedness differ,
// see #7915
return data_type_bits(second_cast) <= data_type_bits(first_cast) &&
is_signed(second_cast) == is_signed(first_cast);
}
// int(float(a))
if (data_type_bits(second_cast) <= data_type_bits(first_cast) * 2) {
Expand Down
10 changes: 10 additions & 0 deletions tests/python/test_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,13 @@ def func():
for i in range(3):
for j in range(4):
assert mat[i, j] == i + 1


@test_utils.test()
def test_casts_int_uint():
@ti.kernel
def my_cast(x: ti.f32) -> ti.u32:
y = ti.floor(x, ti.i32)
return ti.cast(y, ti.u32)

assert my_cast(-1) == 4294967295