From 3072cfea95aad2a9ddab0f517c8f18f634442a27 Mon Sep 17 00:00:00 2001 From: kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> Date: Thu, 22 May 2025 21:29:06 -0500 Subject: Implement default initializer list for C-Style type member (#7079) * Implement default initializer list for C-Style type member Close #6189. Previsouly, for the C-Style member in a struct, if it doesn't have any initialize expression, when we synthesize the ctor, we will not associate the default value for the parameter corresponding to that member. This bring some trouble that existing slang users has to add '= {}' to every struct fields in order to make all the parameters in the synthesized ctor having a default value, so people can still use `Struct a = {}` to create a struct. To make this use case convenience, we will automatically associated a '= {}' as the default value for this case. This PR also add support for empty initializing link-time sized vector/matrix by "= {}". In addition, this PR also fix a bug in auto diff where we should not report error when proccessing transpose on an empty struct. --- tests/initializer-list/c-style-type.slang | 2 +- .../default-init-c-style-member.slang | 62 ++++++++++++++++++++++ tests/initializer-list/generic-array-init.slang | 26 ++++++++- tests/initializer-list/partial-init.slang | 18 +++++-- .../struct-visibility-diagnostic-1.slang | 8 +-- .../struct-visibility-diagnostic-2.slang | 8 +-- .../struct-visibility-diagnostic-3.slang | 12 ++--- 7 files changed, 111 insertions(+), 25 deletions(-) create mode 100644 tests/initializer-list/default-init-c-style-member.slang (limited to 'tests/initializer-list') diff --git a/tests/initializer-list/c-style-type.slang b/tests/initializer-list/c-style-type.slang index 500bf37bb..8afa1e282 100644 --- a/tests/initializer-list/c-style-type.slang +++ b/tests/initializer-list/c-style-type.slang @@ -6,7 +6,7 @@ struct CLike int x; int y; // compiler synthesizes: - // __init(int x, int y); + // __init(int x = {}, int y = {}); } //TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):out,name=outputBuffer diff --git a/tests/initializer-list/default-init-c-style-member.slang b/tests/initializer-list/default-init-c-style-member.slang new file mode 100644 index 000000000..41dba5bd0 --- /dev/null +++ b/tests/initializer-list/default-init-c-style-member.slang @@ -0,0 +1,62 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUFFER):-shaderobj -vk +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUFFER):-shaderobj + +struct NonCStyle +{ + int x; + __init(int x) + { + this.x = x; + } +} + +struct CLike +{ + NonCStyle nonCStyleMember; + int x; + int y; + // compiler synthesizes: + // __init(NonCStyle nonCStyleMember, int x = {}, int y = {}); +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; +void test() +{ + // case 1: initialized with synthesized ctor call, but CLike::x and CLike::y are default initialized + // and `c1` is now `{1, 0, 0}`. + CLike c1 = {NonCStyle(1)}; + + // case 2: initialized with synthesized ctor call, CLike::y are default initialized + CLike c2 = {NonCStyle(1), 2}; + + // case 3: initialized with synthesized ctor call + CLike c3 = {NonCStyle(1), 2, 3}; + + outputBuffer[0] = c1.nonCStyleMember.x; + // BUFFER: 1 + outputBuffer[1] = c1.x; + // BUFFER-NEXT: 0 + outputBuffer[2] = c1.y; + // BUFFER-NEXT: 0 + + outputBuffer[3] = c2.nonCStyleMember.x; + // BUFFER-NEXT: 1 + outputBuffer[4] = c2.x; + // BUFFER-NEXT: 2 + outputBuffer[5] = c2.y; + // BUFFER-NEXT: 0 + + outputBuffer[6] = c3.nonCStyleMember.x; + // BUFFER-NEXT: 1 + outputBuffer[7] = c3.x; + // BUFFER-NEXT: 2 + outputBuffer[8] = c3.y; + // BUFFER-NEXT: 3 +} + +[shader("compute")] +void computeMain() +{ + test(); +} diff --git a/tests/initializer-list/generic-array-init.slang b/tests/initializer-list/generic-array-init.slang index 4257e2972..83c14ebd2 100644 --- a/tests/initializer-list/generic-array-init.slang +++ b/tests/initializer-list/generic-array-init.slang @@ -2,7 +2,7 @@ //TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUFFER):-shaderobj -mtl //TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUFFER):-cpu -compute -entry computeMain -//TEST_INPUT:ubuffer(data=[9 9 9 9], stride=4):out,name=outputBuffer +//TEST_INPUT:ubuffer(data=[9 9 9 9 9 9 9 9 9 9 9 9], stride=4):out,name=outputBuffer RWStructuredBuffer outputBuffer; void test() @@ -18,6 +18,28 @@ void test() outputBuffer[2] = array[2]; // BUFFER-NEXT: 3 outputBuffer[3] = array.getCount(); + + vector vec = {}; + + // BUFFER-NEXT: 0 + outputBuffer[4] = vec[0]; + // BUFFER-NEXT: 0 + outputBuffer[5] = vec[1]; + // BUFFER-NEXT: 0 + outputBuffer[6] = vec[2]; + // BUFFER-NEXT: 3 + outputBuffer[7] = vec.getCount(); + + matrix mat = {}; + + // BUFFER-NEXT: 0 + outputBuffer[8] = (int)mat[0][0]; + // BUFFER-NEXT: 0 + outputBuffer[9] = (int)mat[1][1]; + // BUFFER-NEXT: 0 + outputBuffer[10] = (int)mat[2][1]; + // BUFFER-NEXT: 3 + outputBuffer[11] = mat.getCount(); // this is a bad name, getCount() is actually get the number of row. } [shader("compute")] @@ -25,4 +47,4 @@ void test() void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) { test<3>(); -} +} diff --git a/tests/initializer-list/partial-init.slang b/tests/initializer-list/partial-init.slang index 55f3816fb..a4739f041 100644 --- a/tests/initializer-list/partial-init.slang +++ b/tests/initializer-list/partial-init.slang @@ -8,15 +8,15 @@ struct PartialInit { int x; int y = 1; // compiler synthesizes: - // __init(int x, int y = 1); + // __init(int x = {}, int y = 1); } struct PartialInit2 { int x = 1; int y; - // __init(int x, int y); + // __init(int x, int y = {}); } -//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):out,name=outputBuffer +//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0], stride=4):out,name=outputBuffer RWStructuredBuffer outputBuffer; void test() { @@ -38,6 +38,18 @@ void test() outputBuffer[4] = p3.x; // BUFFER-NEXT: 5 outputBuffer[5] = p3.y; + + PartialInit p4 = {}; // calls `__init`, result is {0, 1} + // BUFFER-NEXT: 0 + outputBuffer[6] = p4.x; + // BUFFER-NEXT: 1 + outputBuffer[7] = p4.y; + + PartialInit2 p5 = {}; // calls `__init`, result is {1, 0} + // BUFFER-NEXT: 1 + outputBuffer[8] = p5.x; + // BUFFER-NEXT: 0 + outputBuffer[9] = p5.y; } [shader("compute")] diff --git a/tests/initializer-list/struct-visibility-diagnostic-1.slang b/tests/initializer-list/struct-visibility-diagnostic-1.slang index ab11933ae..1670860ad 100644 --- a/tests/initializer-list/struct-visibility-diagnostic-1.slang +++ b/tests/initializer-list/struct-visibility-diagnostic-1.slang @@ -4,12 +4,8 @@ public struct Visibility { internal int x; public int y = 0; - // the compiler does not synthesize any ctor. - // the compiler will try to synthesize: - // public __init(int y); - // but then it will find that `x` cannot be initialized. - // so this synthesis will fail and no ctor will be added - // to the type. + // will synthesize a constructor + // __init(int y = 0) } void test() diff --git a/tests/initializer-list/struct-visibility-diagnostic-2.slang b/tests/initializer-list/struct-visibility-diagnostic-2.slang index a3255de5e..b9e92555b 100644 --- a/tests/initializer-list/struct-visibility-diagnostic-2.slang +++ b/tests/initializer-list/struct-visibility-diagnostic-2.slang @@ -4,12 +4,8 @@ public struct Visibility { internal int x; public int y = 5; - // the compiler does not synthesize any ctor. - // the compiler will try to synthesize: - // public __init(int y); - // but then it will find that `x` cannot be initialized. - // so this synthesis will fail and no ctor will be added - // to the type. + // will synthesize a constructor + // __init(int y = 0) } void test() diff --git a/tests/initializer-list/struct-visibility-diagnostic-3.slang b/tests/initializer-list/struct-visibility-diagnostic-3.slang index 75ac5919d..e36bd4e4e 100644 --- a/tests/initializer-list/struct-visibility-diagnostic-3.slang +++ b/tests/initializer-list/struct-visibility-diagnostic-3.slang @@ -1,15 +1,13 @@ //DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): +public struct NonCStyle +{} public struct Visibility { internal int x; - public int y; - // the compiler does not synthesize any ctor. - // the compiler will try to synthesize: - // public __init(int y); - // but then it will find that `x` cannot be initialized. - // so this synthesis will fail and no ctor will be added - // to the type. + public int[] y; // define a unsized array here on purpose so that the ctor will not associate it with a '{}' initializer + // will synthesize a constructor + // __init(int[] y) } void test() -- cgit v1.2.3