From ce238dd878038bf857968931773cc9b10f3b225d Mon Sep 17 00:00:00 2001 From: Julius Ikkala Date: Thu, 22 May 2025 22:10:42 +0300 Subject: Make sizeof(T) & alignof(T) of generic types work as compile-time constants (#7213) * Make sizeof(generic) work as compile-time constant * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- .../size-of/size-of-compile-time.slang | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/hlsl-intrinsic/size-of/size-of-compile-time.slang (limited to 'tests') diff --git a/tests/hlsl-intrinsic/size-of/size-of-compile-time.slang b/tests/hlsl-intrinsic/size-of/size-of-compile-time.slang new file mode 100644 index 000000000..64e2f640b --- /dev/null +++ b/tests/hlsl-intrinsic/size-of/size-of-compile-time.slang @@ -0,0 +1,41 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-cpu -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-slang -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-slang -compute -dx12 -shaderobj +//TEST(compute, vulkan):COMPARE_COMPUTE(filecheck-buffer=CHECK):-vk -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-cuda -compute -shaderobj + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer + +RWStructuredBuffer outputBuffer; + +struct Thing +{ + uint8_t data[sizeof(T)]; +}; + +struct AlignThing +{ + uint8_t data[alignof(T)]; +}; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + const int idx = asint(dispatchThreadID.x); + + int size = 0; + + switch (idx) + { + case 0: size = sizeof(Thing); break; + case 1: size = sizeof(Thing); break; + case 2: size = sizeof(AlignThing); break; + case 3: size = sizeof(AlignThing); break; + } + + // CHECK: 4 + // CHECK-NEXT: C + // CHECK: 4 + // CHECK-NEXT: 4 + outputBuffer[idx] = size; +} -- cgit v1.2.3