Skip to content

Incorrect if/else result when a loop in a branch reassigns the condition variable #2156

Open

Description

@shi-eric

When a loop inside the if branch reassigns the variable used as the condition, the if/else gives the wrong result.

import warp as wp


@wp.kernel
def repro(flags: wp.array[int], n: int, out: wp.array[int]):
    i = wp.tid()
    flag = int(flags[i])
    if flag:
        for _ in range(n):
            flag = 0
        value = 11
    else:
        value = 22
    out[i] = value


flags = wp.array([1, 0], dtype=int)
out = wp.empty(2, dtype=int)
wp.launch(repro, dim=2, inputs=[flags, 1], outputs=[out])
print(out.numpy().tolist())

Expected: [11, 22]

Actual: [22, 22]

The first flag starts as 1, so the first result should be 11. Clearing flag inside the loop should not change which result is selected.

The reverse case fails too. Moving the loop into the else branch and setting flag = 1 there gives [11, 11]. A while loop behaves the same way. The output is correct when n is 0, or when the loop uses a constant count such as range(1).

Reproduced with Warp 1.12.0, 1.17.0, and 1.18.0 on CPU and CUDA, in both release and debug modes, using Linux and Python 3.12.

Activity

  1. added
    bugSomething isn't working
    languageWarp syntax, types, annotations, inference, and kernel programming behavior
    on Oct 25, 2026
  2. self-assigned this
    on Oct 25, 2026
  3. added theissue type on Oct 25, 2026
  4. added this to the 1.20.0 milestone on Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinglanguageWarp syntax, types, annotations, inference, and kernel programming behavior

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions