mitsuba-renderer / drjit

Dr.Jit — A Just-In-Time-Compiler for Differentiable Rendering
BSD 3-Clause "New" or "Revised" License
603 stars 45 forks source link

Issue in `dr.syntax` transformation with scalar loops and list comprehensions #252

Closed dvicini closed 3 months ago

dvicini commented 3 months ago

I ran into an issue when using dr.syntax on a longer function that mixes several types of loops (some symbolic, some scalar/evaluated) and list comprehension.

I haven't fully narrowed this down, but it seems to be some combination of having a list expression and a scalar loop reuse a variable name (here, j).

Here is a reproducer:

import drjit as dr

@dr.syntax
def f():
  n = 10
  k = 3

  print([j for j in range(k)])

  i = dr.zeros(dr.llvm.Int32, n)
  result = dr.zeros(dr.llvm.Float)
  while i < 10:
    for j in range(k):
      result += i * j
    i += 1
  print(result)

f()

This prints:

Traceback (most recent call last):
  File ".../mitsuba3/build/drjit_loop.py", line 19, in <module>
    f()
  File ".../mitsuba3/build/drjit_loop.py", line 13, in f
    while i < 10:
UnboundLocalError: cannot access local variable 'j' where it is not associated with a value