From 6e591ada0eb652c320bba4bd8a46cd579946df01 Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 7 Nov 2017 19:09:40 -0500 Subject: Support generic interface methods (#251) * improve diagnostic messages and prevent fatal errors from crashing the compiler. * fix top level exception catching. * spelling fix * change wording of invalidSwizzleExpr diagnostic * add speculative GenericsApp expr parsing * add new test case of cascading generics call. * Fixing bugs in compiling cascaded generic function calls. Add implementation of DeclaredSubTypeWitness::SubstituteImpl() This is not needed by the type checker, but needed by IR specialization. When input source contains cascading generic function call, the arguments to `specialize` instruction is currently represented as a substitution. The arg values of this subsittution can be a `DeclaredSubTypeWitness` when a generic function uses one of its generic parameter to specialize another generic function. When the top level generics function is being specialized, this substitution argument, which is a `DeclaredSubTypeWitness`, needs to be substituted with the witness that used to specialize the top level function in the specialized specialize instruction as well. * add a test case for cascading generic function call. * parser bug fix * fixes #255 * add test case for issue #255 * Generate missing `specialize` instruction when calling a generic method from an interface constraint. When calling a generic method via an interface, we should be generating the following ir: ... f = lookup_interface_method(...) f_s = specailize(f, declRef) ... This commit fixes this `emitFuncRef` function to emit the needed `specialize` instruction. * fixes #260 This fix follows the second apporach in the disucssion. It generated mangled name for specialized functions by appending new substitution type names to the original mangled name. * Disabling removing and re-inserting specailized functions in getSpecalizeFunc() I am not sure why it is needed, it seems HLSL and GLSL backends are generating forward declarations anyways, so the order of functions in IRModule shouldn't matter. * cleanup and complete test cases. * fix warnings --- tests/bugs/nested-generics-call.slang | 34 +++++++++++++++++++ tests/bugs/nested-generics-call.slang.expected.txt | 4 +++ tests/bugs/nested-generics-method-call.slang | 38 ++++++++++++++++++++++ .../nested-generics-method-call.slang.expected.txt | 4 +++ 4 files changed, 80 insertions(+) create mode 100644 tests/bugs/nested-generics-call.slang create mode 100644 tests/bugs/nested-generics-call.slang.expected.txt create mode 100644 tests/bugs/nested-generics-method-call.slang create mode 100644 tests/bugs/nested-generics-method-call.slang.expected.txt (limited to 'tests/bugs') diff --git a/tests/bugs/nested-generics-call.slang b/tests/bugs/nested-generics-call.slang new file mode 100644 index 000000000..2c6df59c6 --- /dev/null +++ b/tests/bugs/nested-generics-call.slang @@ -0,0 +1,34 @@ +//TEST(compute):COMPARE_COMPUTE:-xslang -use-ir +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out + +RWStructuredBuffer outputBuffer; + +interface IBase +{ + float get(); +} + +struct BaseImpl : IBase +{ + float get() { return 1.0; } +}; + +__generic +float eval(T obj) +{ + return obj.get(); +} + +__generic +float test(T obj) +{ + return eval(obj); +} + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + BaseImpl base; + float outVal = test(base); + outputBuffer[dispatchThreadID.x] = outVal; +} \ No newline at end of file diff --git a/tests/bugs/nested-generics-call.slang.expected.txt b/tests/bugs/nested-generics-call.slang.expected.txt new file mode 100644 index 000000000..cc5e55ab6 --- /dev/null +++ b/tests/bugs/nested-generics-call.slang.expected.txt @@ -0,0 +1,4 @@ +3F800000 +3F800000 +3F800000 +3F800000 diff --git a/tests/bugs/nested-generics-method-call.slang b/tests/bugs/nested-generics-method-call.slang new file mode 100644 index 000000000..d1e80da57 --- /dev/null +++ b/tests/bugs/nested-generics-method-call.slang @@ -0,0 +1,38 @@ +//TEST(compute):COMPARE_COMPUTE:-xslang -use-ir +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out + +RWStructuredBuffer outputBuffer; + +interface IBase +{ + float get(); +} + +struct BaseImpl : IBase +{ + float get() { return 1.0; } +}; + +struct S +{ + __generic + float eval(T obj) + { + return obj.get(); + } +}; + +__generic +float test(T obj) +{ + S s; + return s.eval(obj); +} + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + BaseImpl base; + float outVal = test(base); + outputBuffer[dispatchThreadID.x] = outVal; +} \ No newline at end of file diff --git a/tests/bugs/nested-generics-method-call.slang.expected.txt b/tests/bugs/nested-generics-method-call.slang.expected.txt new file mode 100644 index 000000000..cc5e55ab6 --- /dev/null +++ b/tests/bugs/nested-generics-method-call.slang.expected.txt @@ -0,0 +1,4 @@ +3F800000 +3F800000 +3F800000 +3F800000 -- cgit v1.2.3