From c260e6aa3a7dc3e6794442daacde3ae23f029e0b Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Wed, 16 Jan 2019 09:15:02 -0800 Subject: Fix a bug in IR linking (#777) The IR linking logic was recently rewritten to use the (optional) `IRLinkageDecoration`s instead of assuming `IRGlobalVals` always have a mangled name field, and in that process a bug seems to have crept in where in the case that an instruction that would usually quality as a "global value" does *not* have linkage, we were failing to register the instruction we create in the output module as a replacement for the original instruction. This problem affects `static` variables inside of functions, leading to them potentially getting emitted multiple times. --- source/slang/ir-link.cpp | 2 +- tests/bugs/static-var.slang | 22 ++++++++++++++++++++++ tests/bugs/static-var.slang.expected.txt | 4 ++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/bugs/static-var.slang create mode 100644 tests/bugs/static-var.slang.expected.txt diff --git a/source/slang/ir-link.cpp b/source/slang/ir-link.cpp index 586172cb2..25d3b40b6 100644 --- a/source/slang/ir-link.cpp +++ b/source/slang/ir-link.cpp @@ -990,7 +990,7 @@ IRInst* cloneGlobalValueWithLinkage( { // If there is no mangled name, then we assume this is a local symbol, // and it can't possibly have multiple declarations. - return cloneGlobalValueImpl(context, originalVal, IROriginalValuesForClone()); + return cloneGlobalValueImpl(context, originalVal, IROriginalValuesForClone(originalVal)); } // diff --git a/tests/bugs/static-var.slang b/tests/bugs/static-var.slang new file mode 100644 index 000000000..f060586ad --- /dev/null +++ b/tests/bugs/static-var.slang @@ -0,0 +1,22 @@ +// gh-775-ext.slang +//TEST(compute):COMPARE_COMPUTE: + +int test(int inVal) +{ + static int kVal = 16; + return inVal + kVal; +} + +//TEST_INPUT:ubuffer(data=[9 9 9 9], stride=4):dxbinding(0),glbinding(0),out +RWStructuredBuffer outputBuffer : register(u0); + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + + int inVal = int(tid); + int outVal = test(inVal); + + outputBuffer[tid] = outVal; +} \ No newline at end of file diff --git a/tests/bugs/static-var.slang.expected.txt b/tests/bugs/static-var.slang.expected.txt new file mode 100644 index 000000000..a0d427709 --- /dev/null +++ b/tests/bugs/static-var.slang.expected.txt @@ -0,0 +1,4 @@ +10 +11 +12 +13 -- cgit v1.2.3