taichi icon indicating copy to clipboard operation
taichi copied to clipboard

default_ip=ti.i64 doesn't work with if

Open lin-hitonami opened this issue 2 years ago • 5 comments

To Reproduce

import taichi as ti
ti.init(default_ip=ti.i64)

@ti.kernel
def test_kernel():
    stop = 0
    if stop:
        stop = 1

test_kernel()

Log/Screenshots

[Taichi] version 1.0.5, llvm 10.0.0, commit 40712d7d, linux, python 3.9.12
[Taichi] Starting on arch=x64
Traceback (most recent call last):
  File "/home/lin/test/yu.py", line 10, in <module>
    test_kernel()
  File "/home/lin/taichi/python/taichi/lang/kernel_impl.py", line 926, in wrapped
    raise type(e)('\n' + str(e)) from None
taichi.lang.exception.TaichiTypeError: 
`if` conditions must be of type i32; found i64. Consider using `if x != 0` instead of `if x` for float values.

lin-hitonami avatar Aug 08 '22 09:08 lin-hitonami

maybe @strongoier

lin-hitonami avatar Aug 08 '22 09:08 lin-hitonami

cc: @re-xyr

strongoier avatar Aug 08 '22 09:08 strongoier

I think we should make Expr(True/False) return i32 and then inform user of their difference from using 0 and 1 directly (which returns default_ip). Are there any cases where we must use 0/1 instead of True/False?

re-xyr avatar Aug 08 '22 09:08 re-xyr

Many people use if x to determine whether x is zero or not, x can be any integer. It's weird only i32 works while i64 doesn't.

lin-hitonami avatar Aug 08 '22 09:08 lin-hitonami

Boolean values being i32 is a workaround; we will make it proper i1 in the future. In the meantime I think we'd better keep enforcing one single type for conditionals; using if x to see whether x is zero is not good software engineering practice anyway, and we've advised users to write if x != 0 in the error message.

re-xyr avatar Aug 08 '22 09:08 re-xyr