diff --git a/CHANGELOG.md b/CHANGELOG.md index 9413eea2..c9956089 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ - Fix handling of `stream` argument in `array.__dlpack__()`. - Fix a bug related to reloading CPU modules. - Fix a crash when kernel functions are not found in CPU modules. +- Fix conditions not being evaluated as expected in `while` statements. ## [1.3.3] - 2024-09-04 diff --git a/warp/codegen.py b/warp/codegen.py index df6e4427..1c831156 100644 --- a/warp/codegen.py +++ b/warp/codegen.py @@ -1465,6 +1465,7 @@ def begin_while(adj, cond): cond_block.body_forward.append(f"start_{cond_block.label}:;") c = adj.eval(cond) + c = adj.load(c) cond_block.body_forward.append(f"if (({c.emit()}) == false) goto end_{cond_block.label};") diff --git a/warp/tests/test_codegen.py b/warp/tests/test_codegen.py index 6787a4a1..beb0cf03 100644 --- a/warp/tests/test_codegen.py +++ b/warp/tests/test_codegen.py @@ -518,6 +518,19 @@ def test_shadow_builtin(): wp.expect_eq(sum(wp.vec3(1.0)), 3.0) +@wp.struct +class Iterator: + valid: wp.bool + + +@wp.kernel(enable_backward=False) +def test_while_condition_eval(): + it = Iterator() + it.valid = True + while it.valid: + it.valid = False + + class TestCodeGen(unittest.TestCase): pass @@ -657,6 +670,7 @@ class TestCodeGen(unittest.TestCase): add_kernel_test(TestCodeGen, name="test_call_syntax", kernel=test_call_syntax, dim=1, devices=devices) add_kernel_test(TestCodeGen, name="test_shadow_builtin", kernel=test_shadow_builtin, dim=1, devices=devices) +add_kernel_test(TestCodeGen, name="test_while_condition_eval", kernel=test_while_condition_eval, dim=1, devices=devices) if __name__ == "__main__":