From 87cba147d513fcbc4b69a083dc10557a6c1b42ba Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 27 Dec 2017 18:47:40 -0500 Subject: Support nested generic types (e.g. L>) fixes #325 This commit includes following changes: 1. Including a default DeclaredSubtypeWitness argument when creating a default GenericSubstitution for a DeclRefType, so that the witness argument can be successfully replaced with an actual witness table after specialization. (check,cpp) 2. Not emitting full mangled name for struct field members. Since the declref of the member access instruction do not include necessary generic substitutions for its parent generic parameters, so the mangled names of the declaration site and use site mismatches. Instead we just emit the original name for struct fields. (emit.cpp) 3. Allow IRWitnessTable to represent a generic witness table for generic structs. Adds necessary fields to IRWitnessTable for generic specialization. For now, the user field of the IRUse is not used and is nullptr. (ir-inst.h) 4. Make IRProxyVal use an IRUse instead of an IRValue*, so that an IRValue referenced by IRProxyVal (as a substitution argument) can be managed by the def-use chain for easy replacement. This is used for specializing witness tables. (ir.cpp, ir.h) 5. Add a `String dumpIRFunc(IRFunc*)` function for debugging. 6. Add name mangling for generic / specialized witness tables (mangle.cpp) 7. improved natvis file for inspecting witness tables. 8. Add specialization of witness tables: 1) `findWitnessTable` will simply return the specialize IRInst for a generic witness table. 2) make `cloneSubstitutionArg` call `cloneValue` to clone the argument instead of calling `context->maybeCloneValue`, so we can make use of the cloned value lookup machanism to directly return the specialized witness table (which is done when we process the `specialize` instruction on the generic witness table before process the decl ref). 3) bug fix: the argument in ir.cpp:3338 should be `newArg` instead of `arg`. 4) add `specializeWitnessTable` function to specailize a generic witness table. It clones the witness table, and recursively calls `getSpecailizedFunc` for the witness table entries. 5) make `specailizeGenerics` function also handle the case when an operand of the `specialize` instruction is a witness table. We will call `specializeWitnessTable` here and replace the `specialize` instruction with the specialized witness table. The replacement mechanism based on IR def-use chain works here because we have already make IRProxyVal a part of the def-use chain. 9. Add two more test cases for nested generics with constraints. (generic-list.slang and generic-struct-with-constraint.slang) --- tests/compute/generic-list.slang | 48 ++++++++++++++++++++++ tests/compute/generic-list.slang.expected.txt | 4 ++ tests/compute/generic-struct-with-constraint.slang | 44 ++++++++++++++++++++ ...neric-struct-with-constraint.slang.expected.txt | 4 ++ 4 files changed, 100 insertions(+) create mode 100644 tests/compute/generic-list.slang create mode 100644 tests/compute/generic-list.slang.expected.txt create mode 100644 tests/compute/generic-struct-with-constraint.slang create mode 100644 tests/compute/generic-struct-with-constraint.slang.expected.txt (limited to 'tests') diff --git a/tests/compute/generic-list.slang b/tests/compute/generic-list.slang new file mode 100644 index 000000000..256e02d33 --- /dev/null +++ b/tests/compute/generic-list.slang @@ -0,0 +1,48 @@ +//TEST(compute):COMPARE_COMPUTE:-xslang -use-ir +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out +// Confirm that generics syntax can be used in user +// code and generates valid output. + +RWStructuredBuffer outputBuffer; + +interface IElement +{ + float4 getValue(); +}; + +struct SingleElement : IElement +{ + float4 value; + float4 getValue() + { + return value; + } +}; + +struct ListElement : IElement +{ + THead head; + TTail tail; + float4 getValue() + { + return head.getValue() + tail.getValue(); + } +}; + +float4 test(T val) +{ + return val.getValue(); +} + + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + ListElement > > list; + list.head.value = float4(1.0); + list.tail.head.value = float4(2.0); + list.tail.tail.head.value = float4(3.0); + list.tail.tail.tail.value = float4(4.0); + float4 outVal = test(list); + outputBuffer[0] = outVal; +} \ No newline at end of file diff --git a/tests/compute/generic-list.slang.expected.txt b/tests/compute/generic-list.slang.expected.txt new file mode 100644 index 000000000..7b383b304 --- /dev/null +++ b/tests/compute/generic-list.slang.expected.txt @@ -0,0 +1,4 @@ +41200000 +41200000 +41200000 +41200000 \ No newline at end of file diff --git a/tests/compute/generic-struct-with-constraint.slang b/tests/compute/generic-struct-with-constraint.slang new file mode 100644 index 000000000..0c81ad176 --- /dev/null +++ b/tests/compute/generic-struct-with-constraint.slang @@ -0,0 +1,44 @@ +//TEST(compute):COMPARE_COMPUTE:-xslang -use-ir +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out +// Confirm that generics syntax can be used in user +// code and generates valid output. + +RWStructuredBuffer outputBuffer; + +interface IElement +{ + float4 getValue(); +}; + +struct SingleElement : IElement +{ + float4 value; + float4 getValue() + { + return value; + } +}; + +struct ListElement : IElement +{ + THead head; + float4 getValue() + { + return head.getValue(); + } +}; + +float4 test(T val) +{ + return val.getValue(); +} + + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + ListElement list; + list.head.value = float4(1.0); + float4 outVal = test >(list); + outputBuffer[0] = outVal; +} \ No newline at end of file diff --git a/tests/compute/generic-struct-with-constraint.slang.expected.txt b/tests/compute/generic-struct-with-constraint.slang.expected.txt new file mode 100644 index 000000000..e143b7f20 --- /dev/null +++ b/tests/compute/generic-struct-with-constraint.slang.expected.txt @@ -0,0 +1,4 @@ +3F800000 +3F800000 +3F800000 +3F800000 \ No newline at end of file -- cgit v1.2.3