lib/accy/src/tensor/interpret/context.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const program_mod = @import("../root.zig").program;
2 const trace = @import("../root.zig").trace;
3
4 fn pointerChild(comptime Pointer: type) type {
5 return switch (@typeInfo(Pointer)) {
6 .pointer => |info| info.child,
7 else => @compileError("tensor interpreter layer handles must be pointers"),
8 };
9 }
10
11 pub fn BindContext(comptime BoundValue: type, comptime Handle: type) type {
12 return struct {
13 next: Handle,
14 op: *const program_mod.Operation,
15 args: []const BoundValue,
16
17 pub fn default(self: *@This()) !BoundValue {
18 return self.next.bind(self.op, self.args);
19 }
20
21 pub fn arg(self: *@This(), index: usize) BoundValue {
22 return self.args[index];
23 }
24
25 pub fn builderHandle(self: *@This()) *trace.Builder {
26 return self.next.builderHandle();
27 }
28 };
29 }
30
31 pub fn FinishContext(comptime BoundValue: type, comptime Handle: type) type {
32 return struct {
33 next: Handle,
34
35 pub fn default(self: *@This(), outputs: []const BoundValue) !pointerChild(Handle).Result {
36 return self.next.finish(outputs);
37 }
38
39 pub fn builderHandle(self: *@This()) *trace.Builder {
40 return self.next.builderHandle();
41 }
42 };
43 }
44
45 pub fn LeafBindContext(comptime BoundValue: type) type {
46 return struct {
47 op: *const program_mod.Operation,
48 args: []const BoundValue,
49
50 pub fn arg(self: *@This(), index: usize) BoundValue {
51 return self.args[index];
52 }
53 };
54 }