Skip to content

Commit f8cc28c

Browse files
csyongheTim Foley
and
Tim Foley
authored
Add a test case for dynamic dispatch with This type in interface decl. (shader-slang#1431)
* Add a test case for dynamic dispatch with `This` type in interface decl. * Update comments * fix typo in comments Co-authored-by: Tim Foley <tfoleyNV@users.noreply.github.com>
1 parent 1301f6b commit f8cc28c

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

source/slang/slang-lower-to-ir.cpp

+7-14
Original file line numberDiff line numberDiff line change
@@ -1629,20 +1629,13 @@ struct ValLoweringVisitor : ValVisitor<ValLoweringVisitor, LoweredValInfo, Lower
16291629

16301630
LoweredValInfo visitThisType(ThisType* type)
16311631
{
1632-
// TODO: In theory, we should only run into a `ThisType` when lowering a concrete
1633-
// declaration defined on an interface type (e.g., via an `extension`).
1634-
//
1635-
// There is an open question of how we should emit a concrete method (say) defined
1636-
// on an `interface` type. We could emit the code in "object-oriented" style,
1637-
// passing in a `this` parameter of type `IFoo`, or we could emit it in a "generic"
1638-
// type where the whole member is wrapped in a generic on `<This : IFoo>`.
1639-
//
1640-
// The generic option has the benefit of having a clear solution in the case of
1641-
// static members that don't have a `this` parameter, but might still need `This`,
1642-
// but we have so far favored the "object-oriented" lowering for code involving
1643-
// bare interface types.
1644-
//
1645-
// For now we punt and emit the `ThisType` of an interface `IFoo` as `IFoo`.
1632+
// A `This` type in an interface decl should lower to `IRThisType`,
1633+
// while `This` type in a concrete `struct` should lower to the `struct` type
1634+
// itself. A `This` type reference in a concrete type is already translated to that
1635+
// type in semantics checking in this setting.
1636+
// If we see `This` type here, we are dealing with `This` inside an interface decl.
1637+
// Therefore, `context->thisType` should have been set to `IRThisType`
1638+
// in `visitInterfaceDecl`, and we can just use that value here.
16461639
//
16471640
if (context->thisType != nullptr)
16481641
return LoweredValInfo::simple(context->thisType);
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//TEST(compute):COMPARE_COMPUTE:-cpu -xslang -allow-dynamic-code
2+
//DISABLE_TEST(compute):COMPARE_COMPUTE:-cuda -xslang -allow-dynamic-code
3+
4+
// Test dynamic dispatch code gen for general `This` type.
5+
6+
interface IInterface
7+
{
8+
int Compute(int inVal, This other);
9+
};
10+
11+
int GenericCompute<T:IInterface>(int inVal, T obj, T other)
12+
{
13+
return obj.Compute(inVal, other);
14+
}
15+
16+
struct Impl : IInterface
17+
{
18+
int base;
19+
int Compute(int inVal, This other) { return other.base + base + inVal; }
20+
};
21+
int test(int inVal)
22+
{
23+
Impl obj1, obj2;
24+
obj1.base = 1;
25+
obj2.base = 2;
26+
return GenericCompute<Impl>(inVal, obj1, obj2);
27+
}
28+
29+
//TEST_INPUT:ubuffer(data=[0 1 2 3], stride=4):out,name=outputBuffer
30+
RWStructuredBuffer<int> outputBuffer : register(u0);
31+
32+
[numthreads(4, 1, 1)]
33+
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
34+
{
35+
uint tid = dispatchThreadID.x;
36+
int inVal = outputBuffer[tid];
37+
int outVal = test(inVal);
38+
outputBuffer[tid] = outVal;
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
3
2+
4
3+
5
4+
6

0 commit comments

Comments
 (0)