From 85f005888cadeb4b1d957b57a86cbad6cc9ea313 Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 23 Mar 2023 11:37:29 -0700 Subject: Fix scope fixing for address insts. (#2724) Co-authored-by: Yong He --- tests/bugs/addr-scope-fix.slang | 76 ++++++++++++++++++++++++++++ tests/bugs/addr-scope-fix.slang.expected.txt | 5 ++ 2 files changed, 81 insertions(+) create mode 100644 tests/bugs/addr-scope-fix.slang create mode 100644 tests/bugs/addr-scope-fix.slang.expected.txt (limited to 'tests') diff --git a/tests/bugs/addr-scope-fix.slang b/tests/bugs/addr-scope-fix.slang new file mode 100644 index 000000000..8a58b7daf --- /dev/null +++ b/tests/bugs/addr-scope-fix.slang @@ -0,0 +1,76 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +struct HitInfo +{ + float val; + float getVal() { return val; } +} +struct PathInfo +{ + HitInfo hit; + [mutating] + void setVal(float x) + { + hit.val = x; + } +} + +void someOp(HitInfo hitInfo) +{ + outputBuffer[0] = hitInfo.val; +} + +void func(int x) +{ + PathInfo path = {}; + int i = 0; + if (x < 10) + { + // The `elementAddr(path, hit)` is first defined in this block. + // Note that this block dominates all remaining blocks in this + // function, so all future references to path.hit will use + // `elementAddr` inst defined here. + // + // The bug here is that when we emit code, the emit logic will + // find that the elementAddr inst is not in a valid region for + // future use and will try to create a local var for it. This will + // result a pointer typed var being created, and leads to invalid + // hlsl/glsl code. + // + // The fix is to hoist all always-fold insts to earliest point + // in the program instead of creating a var for it. + // + someOp(path.hit); + } + else + { + // This return makes the true block dominate the rest of the blocks + // from a region that does not "dominate" the rest code regions. + return; + } + + while (i < 3) + { + if (i < 2) + { + path.setVal(1); + } + else + { + path.setVal(2); + } + + // This is the point where we are using path.hit again. + outputBuffer[i] = path.hit.getVal(); + i++; + } +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + func(4); +} \ No newline at end of file diff --git a/tests/bugs/addr-scope-fix.slang.expected.txt b/tests/bugs/addr-scope-fix.slang.expected.txt new file mode 100644 index 000000000..73616c399 --- /dev/null +++ b/tests/bugs/addr-scope-fix.slang.expected.txt @@ -0,0 +1,5 @@ +type: float +1.0 +1.0 +2.0 + -- cgit v1.2.3