From 37315c96ea48045fae60f0e1cb1a3293f3ddd962 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 20 Nov 2017 13:45:10 -0800 Subject: IR: support global variable with initializers (#294) The big change here is that the ability to contain basic blocks with instructions in them has been hoisted from `IRFunc` into a new base type `IRGlobalValueWithCode` shared with `IRGlobalVar`. The basic blocks of a global variable define initialization logic for it; they can be looked at like a function that returns the initial value. Places in the IR that used to assume functions contain all the code need to be updated, but so far I only handled the cloning step. The emit logic currently handles an initializer for a global variable by outputting its logic as a separate function, and then having the variable call that function to initialize itself. This should be cleaned up over time so that we generate an ordinary expression whenever possible. I also made the emit logic correctly label any global variable without a layout (that is, any that don't represent a shader parameter) as `static` so that the downstream HLSL compiler sees them as variables rather than parameters. --- tests/compute/global-init.slang | 23 +++++++++++++++++++++++ tests/compute/global-init.slang.expected.txt | 4 ++++ 2 files changed, 27 insertions(+) create mode 100644 tests/compute/global-init.slang create mode 100644 tests/compute/global-init.slang.expected.txt (limited to 'tests') diff --git a/tests/compute/global-init.slang b/tests/compute/global-init.slang new file mode 100644 index 000000000..909cdf7e6 --- /dev/null +++ b/tests/compute/global-init.slang @@ -0,0 +1,23 @@ +//TEST(compute):COMPARE_COMPUTE:-xslang -use-ir +//TEST_INPUT:ubuffer(data=[0 1 2 3], stride=4):dxbinding(0),glbinding(0),out + +// Test that a global variable (not a shader parameter) +// with an initializer works. + +static int gVar = 16; + +int test(int inVal) +{ + return inVal + gVar; +} + +RWStructuredBuffer outputBuffer : register(u0); + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + int inVal = outputBuffer[tid]; + int outVal = test(inVal); + outputBuffer[tid] = outVal; +} \ No newline at end of file diff --git a/tests/compute/global-init.slang.expected.txt b/tests/compute/global-init.slang.expected.txt new file mode 100644 index 000000000..a0d427709 --- /dev/null +++ b/tests/compute/global-init.slang.expected.txt @@ -0,0 +1,4 @@ +10 +11 +12 +13 -- cgit v1.2.3