Skip to content

Commit

Permalink
[Lang] [bug] Make comparisons always return i32 (#5487)
Browse files Browse the repository at this point in the history
* [Lang] [bug] Make comparisons independent of default_ip

* Fix tests
  • Loading branch information
strongoier authored Jul 22, 2022
1 parent eea6f74 commit 3d9da17
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
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()

0 comments on commit 3d9da17

Please sign in to comment.