diff options
| -rw-r--r-- | source/slang/slang-lower-to-ir.cpp | 21 | ||||
| -rw-r--r-- | tests/compute/dynamic-dispatch-5.slang | 39 | ||||
| -rw-r--r-- | tests/compute/dynamic-dispatch-5.slang.expected.txt | 4 |
3 files changed, 50 insertions, 14 deletions
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index a744d9f38..d14fbfed5 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -1629,20 +1629,13 @@ struct ValLoweringVisitor : ValVisitor<ValLoweringVisitor, LoweredValInfo, Lower LoweredValInfo visitThisType(ThisType* type) { - // TODO: In theory, we should only run into a `ThisType` when lowering a concrete - // declaration defined on an interface type (e.g., via an `extension`). - // - // There is an open question of how we should emit a concrete method (say) defined - // on an `interface` type. We could emit the code in "object-oriented" style, - // passing in a `this` parameter of type `IFoo`, or we could emit it in a "generic" - // type where the whole member is wrapped in a generic on `<This : IFoo>`. - // - // The generic option has the benefit of having a clear solution in the case of - // static members that don't have a `this` parameter, but might still need `This`, - // but we have so far favored the "object-oriented" lowering for code involving - // bare interface types. - // - // For now we punt and emit the `ThisType` of an interface `IFoo` as `IFoo`. + // A `This` type in an interface decl should lower to `IRThisType`, + // while `This` type in a concrete `struct` should lower to the `struct` type + // itself. A `This` type reference in a concrete type is already translated to that + // type in semantics checking in this setting. + // If we see `This` type here, we are dealing with `This` inside an interface decl. + // Therefore, `context->thisType` should have been set to `IRThisType` + // in `visitInterfaceDecl`, and we can just use that value here. // if (context->thisType != nullptr) return LoweredValInfo::simple(context->thisType); diff --git a/tests/compute/dynamic-dispatch-5.slang b/tests/compute/dynamic-dispatch-5.slang new file mode 100644 index 000000000..6c403860b --- /dev/null +++ b/tests/compute/dynamic-dispatch-5.slang @@ -0,0 +1,39 @@ +//TEST(compute):COMPARE_COMPUTE:-cpu -xslang -allow-dynamic-code +//DISABLE_TEST(compute):COMPARE_COMPUTE:-cuda -xslang -allow-dynamic-code + +// Test dynamic dispatch code gen for general `This` type. + +interface IInterface +{ + int Compute(int inVal, This other); +}; + +int GenericCompute<T:IInterface>(int inVal, T obj, T other) +{ + return obj.Compute(inVal, other); +} + +struct Impl : IInterface +{ + int base; + int Compute(int inVal, This other) { return other.base + base + inVal; } +}; +int test(int inVal) +{ + Impl obj1, obj2; + obj1.base = 1; + obj2.base = 2; + return GenericCompute<Impl>(inVal, obj1, obj2); +} + +//TEST_INPUT:ubuffer(data=[0 1 2 3], stride=4):out,name=outputBuffer +RWStructuredBuffer<int> 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-5.slang.expected.txt b/tests/compute/dynamic-dispatch-5.slang.expected.txt new file mode 100644 index 000000000..dc5f2efd9 --- /dev/null +++ b/tests/compute/dynamic-dispatch-5.slang.expected.txt @@ -0,0 +1,4 @@ +3 +4 +5 +6 |
