diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2019-01-16 08:16:53 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-16 08:16:53 -0800 |
| commit | f0633a7d0b1615f6b115d53763bedeeb857b7f3c (patch) | |
| tree | f3871140fdafe54dfcc412df6a828840c890f17f /tests | |
| parent | 29ffac963e9c4b02d3cbc9d4b281512ed820e67d (diff) | |
Add proper IR codegen support for local static const variables (#779)
Previously the IR codegen logic was treating function-scope `static const` variables just like `static` variables, which results in them generating less efficient output HLSL/GLSL.
This change special-cases function-local `static const` variables with logic that mirrors how we handle global-scope `static const` variables.
The approach in this change attempts to find a simpler solution to deal with `static const` variables inside of generic functions than what is currently done for `static` variables in generic functions, but I haven't tested whether that works in practice, so I didn't apply the same approach to the plain `static` case. That would make a good follow-on change.
I've included a single test case to demonstrate that with this fix the Slang compiler generates output DXBC that uses an indexable "immediate" constant buffer, whereas without the fix it generates an array in local memory (slow).
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/cross-compile/function-static-const.slang | 27 | ||||
| -rw-r--r-- | tests/cross-compile/function-static-const.slang.hlsl | 27 |
2 files changed, 54 insertions, 0 deletions
diff --git a/tests/cross-compile/function-static-const.slang b/tests/cross-compile/function-static-const.slang new file mode 100644 index 000000000..7d933a04a --- /dev/null +++ b/tests/cross-compile/function-static-const.slang @@ -0,0 +1,27 @@ +// function-static-const.slang + +//TEST:CROSS_COMPILE:-target dxbc-assembly -entry main -stage fragment -profile sm_5_0 + +// This test ensures that we compile `static const` variables inside +// of functions to reasonable HLSL output so that we don't introduce +// unexpected overhead by treating them as just `static`. +// + +int test(int val) +{ + static const int kArray[] = { + 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16 + }; + return kArray[val]; +} + +cbuffer C +{ + int index; +} + +float4 main() : SV_Target +{ + return test(index); +} diff --git a/tests/cross-compile/function-static-const.slang.hlsl b/tests/cross-compile/function-static-const.slang.hlsl new file mode 100644 index 000000000..a4f1118eb --- /dev/null +++ b/tests/cross-compile/function-static-const.slang.hlsl @@ -0,0 +1,27 @@ +// function-static-const.slang +//TEST_IGNORE_FILE + +#pragma pack_matrix(column_major) + +struct SLANG_ParameterGroup_C_0 +{ + int index_0; +}; + +cbuffer C_0 : register(b0) +{ + SLANG_ParameterGroup_C_0 C_0; +} + +static const int kArray_0[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; + +int test_0(int val_0) +{ + return kArray_0[val_0]; +} + +vector<float,4> main() : SV_TARGET +{ + int _S1 = test_0(C_0.index_0); + return (vector<float,4>) _S1; +} |
