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

[Lang] [bug] Make comparisons always return i32 #5487

Merged
merged 2 commits into from
Jul 22, 2022
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
2 changes: 2 additions & 0 deletions python/taichi/lang/ast/ast_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,8 @@ def build_Compare(ctx, node):
f'"{type(node_op).__name__}" is not supported in Taichi kernels.'
)
val = ti_ops.bit_and(val, op(l, r))
if not isinstance(val, bool):
val = ti_ops.cast(val, primitive_types.i32)
node.ptr = val
return node.ptr

Expand Down
14 changes: 14 additions & 0 deletions tests/python/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,17 @@ def is_f32(tp: ti.template()) -> ti.i32:
return tp is ti.f32

is_f32(ti.f32)


@test_utils.test(default_ip=ti.i64, require=ti.extension.data64)
def test_compare_ret_type():
# The purpose of this test is to make sure a comparison returns i32
# regardless of default_ip so that it can always serve as the condition of
# an if statement.
@ti.kernel
def foo():
for i in range(100):
if i == 0:
pass

foo()