summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2020-08-27 13:49:00 -0700
committerGitHub <noreply@github.com>2020-08-27 13:49:00 -0700
commite9bf8de3123563df6f2ca4d3b99291c6a8c99d5d (patch)
tree4e88219698dc7401ef564269ddc4d3eede83cc5a /tests
parentbc0d0f9e2a5a7297d8559a2ee1e3bd59454813cf (diff)
Enable simple extensions of interface types (#1521)
The big picture here is that an `extension` can now apply to an interface type and provide convenience methods for all types that implement that interface. Suppose you have an interface for counters: interface ICounter { [mutating] void add(int val); } and a type that implements it: struct SimpleCounter : ICounter { int _state = 0; ... } If a common operation in your codebase is to increment a counter by adding one, you would be faced with the problem of either: * Add the `increment()` operation to `ICounter`, and force every implementation to implement the new requirement * Add the `increment()` operation to concrete counter types as needed, and thus not be able to use it in generic code * Make `increment()` a global ("free") function, and force clients of counters to have to know which operations use member syntax (`c.add(...)`) and which use global function call syntax (`increment(c)`). The whole idea of `extension`s is to allow for another option that is better than all of the above: extension ICounter { [mutating] void increment() { this.add(1); } } The core of the implementation is relatively straightforward, and consists of two complementary pieces. The first piece is that when emitting a concrete IR entity (function/type/whatever) we treat any enclosing `interface` type (or `extension` thereof) a bit like an enclosing `GenericDecl`, and introduce an `IRGeneric` to wrap things. The generic `IRGeneric` has parameters representing the `This` type for the interface, along with the witness table that shows how `This` conforms to the interface itself. We thus end up with an IR version of `increment()` something like: void increment<This : ICounter>(This this) { this.add(1); } The second (complementary) fix is that when there is code that references this `increment()` operation, we don't treat it like an interface requirement (look up based on its key), and instead treat it like a generic (since that is how it is lowered now) and speciaize it to the information we can glean from the `ThisTypeSubstitution`. A related fix that is required here is that within the body of `increment`, when we perform `this.add`, we need to ensure that the lookup of `add` in the base interface properly takes into account the subtype relationship (`This : ICounter`) and encodes it into the lookup result, so that we get `((ICounter) this).add`, and properly generate code that looks up the `add` method in the witness table for `This`.
Diffstat (limited to 'tests')
-rw-r--r--tests/language-feature/extensions/interface-extension.slang50
-rw-r--r--tests/language-feature/extensions/interface-extension.slang.expected.txt4
2 files changed, 54 insertions, 0 deletions
diff --git a/tests/language-feature/extensions/interface-extension.slang b/tests/language-feature/extensions/interface-extension.slang
new file mode 100644
index 000000000..824aa3450
--- /dev/null
+++ b/tests/language-feature/extensions/interface-extension.slang
@@ -0,0 +1,50 @@
+// interface-extension.slang
+
+// Test that an `extension` applied to an interface type works as users expect
+
+//TEST(compute):COMPARE_COMPUTE:
+
+interface ICounter
+{
+ [mutating] void add(int value);
+}
+
+struct MyCounter : ICounter
+{
+ int _state = 0;
+
+ [mutating] void add(int value) { _state += value; }
+}
+
+extension ICounter
+{
+ [mutating] void increment()
+ {
+ this.add(1);
+ }
+}
+
+void helper<T : ICounter>(in out T counter)
+{
+ counter.increment();
+}
+
+int test(int value)
+{
+ MyCounter counter = { value };
+ counter.increment();
+ helper(counter);
+ return counter._state;
+}
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+[numthreads(4, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ uint tid = dispatchThreadID.x;
+ int inVal = tid;
+ int outVal = test(inVal);
+ outputBuffer[tid] = outVal;
+}
diff --git a/tests/language-feature/extensions/interface-extension.slang.expected.txt b/tests/language-feature/extensions/interface-extension.slang.expected.txt
new file mode 100644
index 000000000..f8affbc14
--- /dev/null
+++ b/tests/language-feature/extensions/interface-extension.slang.expected.txt
@@ -0,0 +1,4 @@
+2
+3
+4
+5