From 579870b714e76cc92300cef1fdf091993bb55954 Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 27 Mar 2023 19:51:21 -0700 Subject: Apply IR simplifcation immediately after specialization to avoid duplicates. (#2739) * Apply IR simplifcation immediately after specialization to avoid duplicates. * Update source/slang/slang-ir-specialize.cpp Co-authored-by: Ellie Hermaszewska --------- Co-authored-by: Yong He Co-authored-by: Ellie Hermaszewska --- source/slang/slang-ir-specialize.cpp | 10 ++++++++ tests/bugs/generic-groupshared.slang | 29 +++++++++++++++++++++++ tests/bugs/generic-groupshared.slang.expected.txt | 4 ++++ 3 files changed, 43 insertions(+) create mode 100644 tests/bugs/generic-groupshared.slang create mode 100644 tests/bugs/generic-groupshared.slang.expected.txt diff --git a/source/slang/slang-ir-specialize.cpp b/source/slang/slang-ir-specialize.cpp index 05c28d131..d2e042363 100644 --- a/source/slang/slang-ir-specialize.cpp +++ b/source/slang/slang-ir-specialize.cpp @@ -2439,6 +2439,16 @@ IRInst* specializeGenericImpl( // value. cloneInstDecorationsAndChildren( &env, module, specializeInst, specializedVal); + + // Perform IR simplifications to fold constants in this specialized value if it is a function, so + // further specializations from the specialized function will have as simple specialization + // arguments as possible to avoid creating specializations that eventually simplified into + // the same thing. + if (auto func = as(specializedVal)) + { + simplifyFunc(func); + } + return specializedVal; } diff --git a/tests/bugs/generic-groupshared.slang b/tests/bugs/generic-groupshared.slang new file mode 100644 index 000000000..c5ed8cf40 --- /dev/null +++ b/tests/bugs/generic-groupshared.slang @@ -0,0 +1,29 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj +//TEST(compute,vulkan):COMPARE_COMPUTE_EX:-vk -slang -compute -shaderobj + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer +RWStructuredBuffer outputBuffer; + +[ForceInline] +Ref table(int index) +{ + static groupshared uint array[n]; + return array[index]; +} + +struct S +{ + static const int M = n * 2; + int doSomething() + { + table(0) = M; + return table(0); + } +} + +[numthreads(1, 1, 1)] +void computeMain(int3 dispatchThreadID: SV_DispatchThreadID) +{ + S<2> s; + outputBuffer[0] = s.doSomething(); +} \ No newline at end of file diff --git a/tests/bugs/generic-groupshared.slang.expected.txt b/tests/bugs/generic-groupshared.slang.expected.txt new file mode 100644 index 000000000..a358987a6 --- /dev/null +++ b/tests/bugs/generic-groupshared.slang.expected.txt @@ -0,0 +1,4 @@ +4 +0 +0 +0 -- cgit v1.2.3