From 0ca75fe002f346f6ab9b77f40c0576d2905560f1 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 24 Jun 2020 13:16:11 -0700 Subject: Dynamic dispatch for generic interface requirements. -Lower interfaces into actual `IRInterfaceType` insts. -Lower `DeclRef` into `IRAssociatedType` -Generate proper IRType for generic functions. -Add a test case exercising dynamic dispatching a generic static function through an associated type. -Bug fixes for the test case. --- tests/compute/dynamic-dispatch-3.slang | 60 ++++++++++++++++++++++ .../compute/dynamic-dispatch-3.slang.expected.txt | 4 ++ 2 files changed, 64 insertions(+) create mode 100644 tests/compute/dynamic-dispatch-3.slang create mode 100644 tests/compute/dynamic-dispatch-3.slang.expected.txt (limited to 'tests/compute') diff --git a/tests/compute/dynamic-dispatch-3.slang b/tests/compute/dynamic-dispatch-3.slang new file mode 100644 index 000000000..7011a2f4e --- /dev/null +++ b/tests/compute/dynamic-dispatch-3.slang @@ -0,0 +1,60 @@ +//TEST(compute):COMPARE_COMPUTE:-cpu -xslang -allow-dynamic-code + +// Test dynamic dispatch code gen for static member functions +// of associated type. +interface IGetter +{ + int getVal(); +}; +interface IAssoc +{ + int get(); + static int getBase(T getter); +} +interface IInterface +{ + associatedtype Assoc : IAssoc; + int Compute(int inVal); +}; + +struct GetterImpl : IGetter +{ + int getVal() { return 1; } +}; + +int GenericCompute(T obj, int inVal) +{ + GetterImpl getter; + return obj.Compute(inVal) + T.Assoc.getBase(getter); +} + +struct Impl : IInterface +{ + struct Assoc : IAssoc + { + int val; + int get() { return val; } + static int getBase(T t) { return t.getVal(); } + }; + int base; + int Compute(int inVal) { return base + inVal * inVal; } +}; + +int test(int inVal) +{ + Impl obj; + obj.base = 1; + return GenericCompute(obj, inVal); +} + +//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; +} diff --git a/tests/compute/dynamic-dispatch-3.slang.expected.txt b/tests/compute/dynamic-dispatch-3.slang.expected.txt new file mode 100644 index 000000000..a6bafb7ca --- /dev/null +++ b/tests/compute/dynamic-dispatch-3.slang.expected.txt @@ -0,0 +1,4 @@ +2 +3 +6 +B -- cgit v1.2.3