Taichi: [Bug]statement "return" seems not to "break" a loop in taichi's function

Created on 19 Oct 2020  路  2Comments  路  Source: taichi-dev/taichi

Describe the bug

Hello, I seem to find a dangeous trap while using a infinite loop in taichi's function.

I was used to use "return" to break an infinite loop and return what I want simultaneously, however, when it comes to taichi's function, my system CRASHED! It seems that, statement "return" don't break the loop, and finally run out the memory.

To test my guess, every time I ran my program in VScode, my system crashed and I had to restart my computer, therefore, I could not got the error message.

To Reproduce

I make two examples here. Please BE CAUTIOUS, It may run out your memory and your computer may crash

@ti.kernel
def foo1():
  foo2()

@ti.func
def foo2():
  a = 1
  while 1:
    a += 1
    if a > 10: # when this number is small, my system got extremely slow and taichi would simply quit and print "None"; when a big number, my system crashed.
      print(a)# no output
      return a

print(foo1())# print "None" 

If we put a "break" there, the loop ends normally, but no return value.

@ti.kernel
def foo1():
  foo2()

@ti.func
def foo2():
  a = 1
  while 1:
    a += 1
    if a > 10: 
      print(a)# print "11"
      break
      return a

print(foo1())# print "None"

Environment

  • Taichi version 0.6.41
  • llvm 10.0.0
  • osx 10.15.7
  • python 3.7.7

I wonder if it is a bug exactly, or simply out of my poor understanding for taichi? Thanks for checking.

Best regards.

potential bug

All 2 comments

This is one of the gotchas in Taichi. Right now you can only have a single, top-level return stmt at the end of a @ti.func, see https://taichi.graphics/docs/develop/documentation/basic/syntax.html#advanced-arguments-2. During the python AST transformation, all functions are inlined, so it's a bit difficult for Taichi's function to support advanced return control flows...

Ok I got it thank you XD.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

liaopeiyuan picture liaopeiyuan  路  3Comments

yuanming-hu picture yuanming-hu  路  3Comments

jackalcooper picture jackalcooper  路  4Comments

archibate picture archibate  路  4Comments

yuanming-hu picture yuanming-hu  路  3Comments