Skip to content

Reusing for-loop variable name in while loop gives wrong result #1534

Description

@shi-eric

Bug Description

Warp gives an incorrect result when a variable name used in a for loop is reused later as the induction variable of a while loop.

In the example below, both kernels should compute the same value. The only difference is that same_name() reuses i in the while loop, while different_name() uses j.

import warp as wp

wp.init()


@wp.kernel
def same_name(out: wp.array[wp.int32]):
    acc = int(0)

    for i in range(2):
        acc = acc + i

    i = int(0)
    while i < 3:
        acc = acc + 10
        i = i + 1

    out[0] = acc


@wp.kernel
def different_name(out: wp.array[wp.int32]):
    acc = int(0)

    for i in range(2):
        acc = acc + i

    j = int(0)
    while j < 3:
        acc = acc + 10
        j = j + 1

    out[0] = acc


device = "cpu"

out = wp.zeros(1, dtype=wp.int32, device=device)
wp.launch(same_name, dim=1, inputs=[out], device=device)
print("same_name", int(out.numpy()[0]))

out = wp.zeros(1, dtype=wp.int32, device=device)
wp.launch(different_name, dim=1, inputs=[out], device=device)
print("different_name", int(out.numpy()[0]))

Expected:

same_name 31
different_name 31

Observed locally on current main:

same_name 0
different_name 31

Expected calculation:

for loop:   acc = 0 + 0 + 1 = 1
while loop: acc = 1 + 10 + 10 + 10 = 31

System Information

Confirmed locally on:

  • Warp: 1.15.0.dev0
  • Python: 3.12.13
  • OS/platform: Linux-6.8.0-1051-gke-x86_64-with-glibc2.35
  • Device: CPU, x86_64

Metadata

Metadata

Labels

bugSomething isn't working

Type

Fields

No fields configured for Bug.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions