From 433ce869481b72ad44897dcc91d7038b03ba45e2 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 21 Feb 2020 08:18:31 -0800 Subject: Initial support for explicit default initializers (#1235) This change makes it so that for a suitable type `MyType`, a variable declaration like: MyType v; is treated as if it were written: MyType v = MyType(); The definition of "suitable" here is that `MyType` needs to have an available `__init` declaration that can be invoked with zero arguments. I've added a test to confirm that the new behavior works in this specific case. There are a bunch of caveats to the feature as it stands today: * Just because `MyType` has a zero-parameter `__init`, that doesn't mean an array type like `MyType[10]` does, so arrays currently remain uninitialized by default. Fixing this gap requires careful consideration because some, but not all, array types should be default-initializable. * The change here should mean that a `struct` type with a field like `MyType f;` should count as having a default initial-value expression for that field, but I haven't confirmed that. * Even if a `struct` provides initial values for all its fields (e.g., `struct S { float f = 0; }`), that doesn't mean it has a default `__init` right now, so those `struct` types will still be left uninitialized by default. Converging all this behavior is still TBD. Just to be clear: there is no provision or plan in Slang to support destructors, RAII, copy constructors, move constructors, overloaded assignment operations, or any other features that buy heavily into the C++ model of how construction and destruction of values gets done. In fact, I'm not even 100% sure I like having this change in place at all, and I think we should reserve the right to revert it and say that only specific stdlib types get to opt in to default initialization along these lines. --- tests/compute/default-initializer.slang | 35 ++++++++++++++++++++++ .../compute/default-initializer.slang.expected.txt | 4 +++ 2 files changed, 39 insertions(+) create mode 100644 tests/compute/default-initializer.slang create mode 100644 tests/compute/default-initializer.slang.expected.txt (limited to 'tests') diff --git a/tests/compute/default-initializer.slang b/tests/compute/default-initializer.slang new file mode 100644 index 000000000..1cda60084 --- /dev/null +++ b/tests/compute/default-initializer.slang @@ -0,0 +1,35 @@ +// default-initializer.slang + +// Confirm that a type with a default initializer gets +// default-initialized where appropriate. + +//TEST(compute):COMPARE_COMPUTE: +//TEST(compute):COMPARE_COMPUTE:-cpu + +struct Stuff +{ + int value; + + __init() + { + value = 16; + } +} + +int test(int value) +{ + Stuff s; + return value * s.value + value; +} + +//TEST_INPUT:ubuffer(data=[0 1 2 3], stride=4):out,name=outputBuffer +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/default-initializer.slang.expected.txt b/tests/compute/default-initializer.slang.expected.txt new file mode 100644 index 000000000..d4cb1cc00 --- /dev/null +++ b/tests/compute/default-initializer.slang.expected.txt @@ -0,0 +1,4 @@ +0 +11 +22 +33 -- cgit v1.2.3