From 5c9f011fa4948d1f70689b03ddcd203cb2525b2b Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 20 Dec 2024 00:53:24 -0800 Subject: Fix crash during loop unrolling. (#5920) --- source/slang/slang-ir-loop-unroll.cpp | 3 +++ tests/ir/loop-unroll-3.slang | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/ir/loop-unroll-3.slang diff --git a/source/slang/slang-ir-loop-unroll.cpp b/source/slang/slang-ir-loop-unroll.cpp index 5f9c53bc7..6b001a562 100644 --- a/source/slang/slang-ir-loop-unroll.cpp +++ b/source/slang/slang-ir-loop-unroll.cpp @@ -463,6 +463,9 @@ bool unrollLoopsInFunc( for (auto loop : loops) { + if (!loop->parent) + continue; + // Remove any continue jumps from the loop. eliminateContinueBlocks(module, loop); diff --git a/tests/ir/loop-unroll-3.slang b/tests/ir/loop-unroll-3.slang new file mode 100644 index 000000000..4e5275e67 --- /dev/null +++ b/tests/ir/loop-unroll-3.slang @@ -0,0 +1,38 @@ +//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK): -output-using-type + +//TEST_INPUT:set buffer = ubuffer(data=[0 0 0 0], stride=4) +RWStructuredBuffer buffer; + +struct Foo +{ + float load() + { + int idx[D]; + [ForceUnroll] + for (int i = 0; i < D; ++i) + idx[i] = 0; + [ForceUnroll] + for (int i = 0; i < D-2; ++i) + idx[i] = 0; + float result = 100.0; + [ForceUnroll] + for (int i = 0; i < 1; i++) + result += buffer[idx[0]]; + + return result; + } +} + +//TEST_INPUT:set result = out ubuffer(data=[0 0 0 0], stride=4) +uniform RWStructuredBuffer result; + +[shader("compute")] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + Foo<2> biases = {}; + + // Test that we can unroll loops of 0 iterations. + result[0] = biases.load(); + + // CHECK: 100.0 +} \ No newline at end of file -- cgit v1.2.3