diff options
| author | Yong He <yonghe@outlook.com> | 2025-06-04 13:07:11 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-04 13:07:11 -0700 |
| commit | 2d7106640addf0ac88e0a5462117cd90b13a5e73 (patch) | |
| tree | 6ce27db4d6c5bf06db84dee755a82e09b1b3a2ca /tests/language-feature | |
| parent | 812e478989e27983b8dea7ab11964de751654ba2 (diff) | |
Add legalization for 0-sized arrays. (#7327)
* Add legalization for 0-sized arrays.
* Allow 0-sized arrays in the front-end.
* More tests.
* Add `Conditional<T, hasValue>` type to core module.
* Update toc.
* Fix wording.
* Update test.
Diffstat (limited to 'tests/language-feature')
| -rw-r--r-- | tests/language-feature/0-array-1.slang | 31 | ||||
| -rw-r--r-- | tests/language-feature/0-array.slang | 64 | ||||
| -rw-r--r-- | tests/language-feature/cond-field.slang | 64 |
3 files changed, 159 insertions, 0 deletions
diff --git a/tests/language-feature/0-array-1.slang b/tests/language-feature/0-array-1.slang new file mode 100644 index 000000000..327f71444 --- /dev/null +++ b/tests/language-feature/0-array-1.slang @@ -0,0 +1,31 @@ +//TEST:SIMPLE(filecheck=SPV): -target spirv +//TEST:SIMPLE(filecheck=HLSL): -target hlsl -profile cs_6_0 -entry computeMain + +struct MyData +{ + int a[0][0][0]; +} + +uniform MyData* myData; +uniform int * output; + +[numthreads(1, 1, 1)] +void computeMain() +{ + // These are all ill-formed, but we want to still ensure our backend + // can handle them gracefully without crashing. + // In actual user code, any access to 0-sized arrays should be protected + // by a `if` statement that checks the size before accessing. + // The condition would then evaluate to false and causing all the accessing + // code to be optimized out. + InterlockedAdd(myData.a[0][0][0], 1); + myData.a[0][0][0] += 1; + output[0] = myData.a[0][0][0]; +} + +//SPV: OpEntryPoint +//SPV-NOT: OpAtomic +//SPV-NOT: OpStore +//SPV-NOT: OpLoad + +//HLSL: computeMain diff --git a/tests/language-feature/0-array.slang b/tests/language-feature/0-array.slang new file mode 100644 index 000000000..7379faace --- /dev/null +++ b/tests/language-feature/0-array.slang @@ -0,0 +1,64 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk + +interface IVertex +{ + property float3 position{get;} + property Optional<float3> normal{get;} + property Optional<float3> color{get;} +} + +struct Vertex<bool hasNormal, bool hasColor> : IVertex +{ + private float3 m_position; + private float3 m_normal[hasNormal]; + private float3 m_color[hasColor]; + + __init(float3 position, float3 normal, float3 color) + { + m_position = position; + if (hasNormal) m_normal[0] = normal; + if (hasColor) m_color[0] = color; + } + + property float3 position + { + get { return m_position; } + } + property Optional<float3> normal + { + get { if (hasNormal) return m_normal[0]; else return none; } + } + property Optional<float3> color + { + get { if (hasColor) return m_color[0]; else return none; } + } +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<int> outputBuffer; + +void test<V:IVertex>(V vert) +{ + // CHECK: 0 + // CHECK: 0 + // CHECK: 1 + // CHECK: 3 + if (let normal = vert.normal) + { + outputBuffer[0] = 1; + outputBuffer[1] = (int)normal.x; + } + + if (let color = vert.color) + { + outputBuffer[2] = 1; + outputBuffer[3] = (int)color.x; + } +} + +[numthreads(1,1,1)] +void computeMain() +{ + test<Vertex<false, true>>(Vertex<false, true>(1.0, 2.0, 3.0)); +}
\ No newline at end of file diff --git a/tests/language-feature/cond-field.slang b/tests/language-feature/cond-field.slang new file mode 100644 index 000000000..5e9f4a06e --- /dev/null +++ b/tests/language-feature/cond-field.slang @@ -0,0 +1,64 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk + +interface IVertex +{ + property float3 position{get;} + property Optional<float3> normal{get;} + property Optional<float3> color{get;} +} + +struct Vertex<bool hasNormal, bool hasColor> : IVertex +{ + private float3 m_position; + private Conditional<float3, hasNormal> m_normal; + private Conditional<float3, hasColor> m_color; + + __init(float3 position, float3 normal, float3 color) + { + m_position = position; + m_normal = normal; + m_color = color; + } + + property float3 position + { + get { return m_position; } + } + property Optional<float3> normal + { + get { return m_normal; } + } + property Optional<float3> color + { + get { return m_color; } + } +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<int> outputBuffer; + +void test<V:IVertex>(V vert) +{ + // CHECK: 0 + // CHECK: 0 + // CHECK: 1 + // CHECK: 3 + if (let normal = vert.normal) + { + outputBuffer[0] = 1; + outputBuffer[1] = (int)normal.x; + } + + if (let color = vert.color) + { + outputBuffer[2] = 1; + outputBuffer[3] = (int)color.x; + } +} + +[numthreads(1,1,1)] +void computeMain() +{ + test<Vertex<false, true>>(Vertex<false, true>(1.0, 2.0, 3.0)); +}
\ No newline at end of file |
