@mun-ui/compiler 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/analysis.d.ts +11 -0
- package/dist/analysis.d.ts.map +1 -0
- package/dist/analysis.js +80 -0
- package/dist/analysis.js.map +1 -0
- package/dist/ast.d.ts +121 -0
- package/dist/ast.d.ts.map +1 -0
- package/dist/ast.js +1017 -0
- package/dist/ast.js.map +1 -0
- package/dist/builtins.d.ts +47 -0
- package/dist/builtins.d.ts.map +1 -0
- package/dist/builtins.js +259 -0
- package/dist/builtins.js.map +1 -0
- package/dist/collection-specialization.d.ts +2 -0
- package/dist/collection-specialization.d.ts.map +1 -0
- package/dist/collection-specialization.js +475 -0
- package/dist/collection-specialization.js.map +1 -0
- package/dist/diagnostics.d.ts +12 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/diagnostics.js +182 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/effect-analysis.d.ts +54 -0
- package/dist/effect-analysis.d.ts.map +1 -0
- package/dist/effect-analysis.js +318 -0
- package/dist/effect-analysis.js.map +1 -0
- package/dist/execution-plan.d.ts +63 -0
- package/dist/execution-plan.d.ts.map +1 -0
- package/dist/execution-plan.js +223 -0
- package/dist/execution-plan.js.map +1 -0
- package/dist/gpu-island-ir.d.ts +85 -0
- package/dist/gpu-island-ir.d.ts.map +1 -0
- package/dist/gpu-island-ir.js +159 -0
- package/dist/gpu-island-ir.js.map +1 -0
- package/dist/gpu-region.d.ts +28 -0
- package/dist/gpu-region.d.ts.map +1 -0
- package/dist/gpu-region.js +136 -0
- package/dist/gpu-region.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +56 -0
- package/dist/index.js.map +1 -0
- package/dist/kernel-ir.d.ts +13 -0
- package/dist/kernel-ir.d.ts.map +1 -0
- package/dist/kernel-ir.js +221 -0
- package/dist/kernel-ir.js.map +1 -0
- package/dist/language-catalog.d.ts +26 -0
- package/dist/language-catalog.d.ts.map +1 -0
- package/dist/language-catalog.js +27 -0
- package/dist/language-catalog.js.map +1 -0
- package/dist/lifecycle.d.ts +8 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +84 -0
- package/dist/lifecycle.js.map +1 -0
- package/dist/native-contract.d.ts +27 -0
- package/dist/native-contract.d.ts.map +1 -0
- package/dist/native-contract.js +163 -0
- package/dist/native-contract.js.map +1 -0
- package/dist/native-member-contract.d.ts +13 -0
- package/dist/native-member-contract.d.ts.map +1 -0
- package/dist/native-member-contract.js +10 -0
- package/dist/native-member-contract.js.map +1 -0
- package/dist/native-values.d.ts +40 -0
- package/dist/native-values.d.ts.map +1 -0
- package/dist/native-values.js +394 -0
- package/dist/native-values.js.map +1 -0
- package/dist/observable.d.ts +19 -0
- package/dist/observable.d.ts.map +1 -0
- package/dist/observable.js +203 -0
- package/dist/observable.js.map +1 -0
- package/dist/pipeline.d.ts +11 -0
- package/dist/pipeline.d.ts.map +1 -0
- package/dist/pipeline.js +2183 -0
- package/dist/pipeline.js.map +1 -0
- package/dist/resident-fusion.d.ts +102 -0
- package/dist/resident-fusion.d.ts.map +1 -0
- package/dist/resident-fusion.js +423 -0
- package/dist/resident-fusion.js.map +1 -0
- package/dist/resident-js.d.ts +7 -0
- package/dist/resident-js.d.ts.map +1 -0
- package/dist/resident-js.js +106 -0
- package/dist/resident-js.js.map +1 -0
- package/dist/resident-planner.d.ts +58 -0
- package/dist/resident-planner.d.ts.map +1 -0
- package/dist/resident-planner.js +101 -0
- package/dist/resident-planner.js.map +1 -0
- package/dist/resident-wasm.d.ts +56 -0
- package/dist/resident-wasm.d.ts.map +1 -0
- package/dist/resident-wasm.js +867 -0
- package/dist/resident-wasm.js.map +1 -0
- package/dist/scanner.d.ts +63 -0
- package/dist/scanner.d.ts.map +1 -0
- package/dist/scanner.js +755 -0
- package/dist/scanner.js.map +1 -0
- package/dist/semantic.d.ts +100 -0
- package/dist/semantic.d.ts.map +1 -0
- package/dist/semantic.js +669 -0
- package/dist/semantic.js.map +1 -0
- package/dist/shorthand.d.ts +16 -0
- package/dist/shorthand.d.ts.map +1 -0
- package/dist/shorthand.js +225 -0
- package/dist/shorthand.js.map +1 -0
- package/dist/source-map.d.ts +26 -0
- package/dist/source-map.d.ts.map +1 -0
- package/dist/source-map.js +228 -0
- package/dist/source-map.js.map +1 -0
- package/dist/specialization.d.ts +34 -0
- package/dist/specialization.d.ts.map +1 -0
- package/dist/specialization.js +1977 -0
- package/dist/specialization.js.map +1 -0
- package/dist/state-specialization.d.ts +3 -0
- package/dist/state-specialization.d.ts.map +1 -0
- package/dist/state-specialization.js +227 -0
- package/dist/state-specialization.js.map +1 -0
- package/dist/struct-state.d.ts +32 -0
- package/dist/struct-state.d.ts.map +1 -0
- package/dist/struct-state.js +170 -0
- package/dist/struct-state.js.map +1 -0
- package/dist/types.d.ts +70 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/ui-ir.d.ts +151 -0
- package/dist/ui-ir.d.ts.map +1 -0
- package/dist/ui-ir.js +2685 -0
- package/dist/ui-ir.js.map +1 -0
- package/dist/value-types.d.ts +74 -0
- package/dist/value-types.d.ts.map +1 -0
- package/dist/value-types.js +246 -0
- package/dist/value-types.js.map +1 -0
- package/dist/vite.d.ts +33 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +233 -0
- package/dist/vite.js.map +1 -0
- package/dist/vue-host.d.ts +25 -0
- package/dist/vue-host.d.ts.map +1 -0
- package/dist/vue-host.js +103 -0
- package/dist/vue-host.js.map +1 -0
- package/dist/web-analysis.d.ts +7 -0
- package/dist/web-analysis.d.ts.map +1 -0
- package/dist/web-analysis.js +81 -0
- package/dist/web-analysis.js.map +1 -0
- package/package.json +29 -3
- package/README.md +0 -3
|
@@ -0,0 +1,867 @@
|
|
|
1
|
+
const OP = Object.freeze({
|
|
2
|
+
End: 0,
|
|
3
|
+
Kernel: 1,
|
|
4
|
+
ConstF32: 2,
|
|
5
|
+
LoadColumn: 3,
|
|
6
|
+
Index: 4,
|
|
7
|
+
Capture: 5,
|
|
8
|
+
Positive: 6,
|
|
9
|
+
Negative: 7,
|
|
10
|
+
Not: 8,
|
|
11
|
+
Add: 9,
|
|
12
|
+
Subtract: 10,
|
|
13
|
+
Multiply: 11,
|
|
14
|
+
Divide: 12,
|
|
15
|
+
LessThan: 13,
|
|
16
|
+
LessEqual: 14,
|
|
17
|
+
GreaterThan: 15,
|
|
18
|
+
GreaterEqual: 16,
|
|
19
|
+
Equal: 17,
|
|
20
|
+
NotEqual: 18,
|
|
21
|
+
And: 19,
|
|
22
|
+
Or: 20,
|
|
23
|
+
Select: 21,
|
|
24
|
+
StoreTemp: 22,
|
|
25
|
+
Commit: 23,
|
|
26
|
+
});
|
|
27
|
+
const BINARY_OPCODE = Object.freeze({
|
|
28
|
+
"+": OP.Add,
|
|
29
|
+
"-": OP.Subtract,
|
|
30
|
+
"*": OP.Multiply,
|
|
31
|
+
"/": OP.Divide,
|
|
32
|
+
"<": OP.LessThan,
|
|
33
|
+
"<=": OP.LessEqual,
|
|
34
|
+
">": OP.GreaterThan,
|
|
35
|
+
">=": OP.GreaterEqual,
|
|
36
|
+
"==": OP.Equal,
|
|
37
|
+
"===": OP.Equal,
|
|
38
|
+
"!=": OP.NotEqual,
|
|
39
|
+
"!==": OP.NotEqual,
|
|
40
|
+
"&&": OP.And,
|
|
41
|
+
"||": OP.Or,
|
|
42
|
+
});
|
|
43
|
+
const WASM = Object.freeze({
|
|
44
|
+
i32: 0x7f,
|
|
45
|
+
f32: 0x7d,
|
|
46
|
+
v128: 0x7b,
|
|
47
|
+
emptyBlock: 0x40,
|
|
48
|
+
block: 0x02,
|
|
49
|
+
loop: 0x03,
|
|
50
|
+
end: 0x0b,
|
|
51
|
+
br: 0x0c,
|
|
52
|
+
brIf: 0x0d,
|
|
53
|
+
select: 0x1b,
|
|
54
|
+
localGet: 0x20,
|
|
55
|
+
localSet: 0x21,
|
|
56
|
+
i32Load: 0x28,
|
|
57
|
+
f32Load: 0x2a,
|
|
58
|
+
f32Store: 0x38,
|
|
59
|
+
i32Const: 0x41,
|
|
60
|
+
f32Const: 0x43,
|
|
61
|
+
i32Eqz: 0x45,
|
|
62
|
+
i32GtU: 0x4b,
|
|
63
|
+
i32GeU: 0x4f,
|
|
64
|
+
f32Eq: 0x5b,
|
|
65
|
+
f32Ne: 0x5c,
|
|
66
|
+
f32Lt: 0x5d,
|
|
67
|
+
f32Gt: 0x5e,
|
|
68
|
+
f32Le: 0x5f,
|
|
69
|
+
f32Ge: 0x60,
|
|
70
|
+
i32Add: 0x6a,
|
|
71
|
+
i32Mul: 0x6c,
|
|
72
|
+
i32And: 0x71,
|
|
73
|
+
i32Or: 0x72,
|
|
74
|
+
f32Neg: 0x8c,
|
|
75
|
+
f32Add: 0x92,
|
|
76
|
+
f32Sub: 0x93,
|
|
77
|
+
f32Mul: 0x94,
|
|
78
|
+
f32Div: 0x95,
|
|
79
|
+
f32ConvertI32S: 0xb2,
|
|
80
|
+
f32ConvertI32U: 0xb3,
|
|
81
|
+
});
|
|
82
|
+
const SIMD = Object.freeze({
|
|
83
|
+
prefix: 0xfd,
|
|
84
|
+
v128Load: 0x00,
|
|
85
|
+
v128Store: 0x0b,
|
|
86
|
+
v128Const: 0x0c,
|
|
87
|
+
i32x4Splat: 0x11,
|
|
88
|
+
f32x4Splat: 0x13,
|
|
89
|
+
f32x4Eq: 0x41,
|
|
90
|
+
f32x4Ne: 0x42,
|
|
91
|
+
f32x4Lt: 0x43,
|
|
92
|
+
f32x4Gt: 0x44,
|
|
93
|
+
f32x4Le: 0x45,
|
|
94
|
+
f32x4Ge: 0x46,
|
|
95
|
+
v128And: 0x4e,
|
|
96
|
+
v128Or: 0x50,
|
|
97
|
+
v128Bitselect: 0x52,
|
|
98
|
+
i32x4Add: 0xae,
|
|
99
|
+
f32x4Neg: 0xe1,
|
|
100
|
+
f32x4Add: 0xe4,
|
|
101
|
+
f32x4Sub: 0xe5,
|
|
102
|
+
f32x4Mul: 0xe6,
|
|
103
|
+
f32x4Div: 0xe7,
|
|
104
|
+
f32x4ConvertI32x4U: 0xfb,
|
|
105
|
+
});
|
|
106
|
+
function encodeU32(value) {
|
|
107
|
+
if (!Number.isSafeInteger(value) || value < 0 || value > 0xffff_ffff) {
|
|
108
|
+
throw new RangeError(`WASM u32 immediate is outside range: ${value}`);
|
|
109
|
+
}
|
|
110
|
+
const bytes = [];
|
|
111
|
+
let remaining = value >>> 0;
|
|
112
|
+
do {
|
|
113
|
+
let byte = remaining & 0x7f;
|
|
114
|
+
remaining >>>= 7;
|
|
115
|
+
if (remaining !== 0)
|
|
116
|
+
byte |= 0x80;
|
|
117
|
+
bytes.push(byte);
|
|
118
|
+
} while (remaining !== 0);
|
|
119
|
+
return bytes;
|
|
120
|
+
}
|
|
121
|
+
function wasmName(value) {
|
|
122
|
+
// Every name emitted by this module is compiler-owned ASCII. Keeping the
|
|
123
|
+
// encoder tiny avoids pulling TextEncoder or a binary dependency into the
|
|
124
|
+
// compiler package.
|
|
125
|
+
const bytes = [...value].map(character => character.charCodeAt(0));
|
|
126
|
+
if (bytes.some(byte => byte > 0x7f))
|
|
127
|
+
throw new TypeError("direct resident WASM names must be ASCII");
|
|
128
|
+
return [...encodeU32(bytes.length), ...bytes];
|
|
129
|
+
}
|
|
130
|
+
function wasmVector(items) {
|
|
131
|
+
return [...encodeU32(items.length), ...items.flatMap(item => [...item])];
|
|
132
|
+
}
|
|
133
|
+
function wasmSection(id, payload) {
|
|
134
|
+
return [id, ...encodeU32(payload.length), ...payload];
|
|
135
|
+
}
|
|
136
|
+
function wasmF32(value) {
|
|
137
|
+
const buffer = new ArrayBuffer(4);
|
|
138
|
+
new DataView(buffer).setFloat32(0, typeof value === "boolean" ? (value ? 1 : 0) : value, true);
|
|
139
|
+
return [...new Uint8Array(buffer)];
|
|
140
|
+
}
|
|
141
|
+
function simdOpcode(opcode) {
|
|
142
|
+
return [SIMD.prefix, ...encodeU32(opcode)];
|
|
143
|
+
}
|
|
144
|
+
function wasmI32x4Const(values) {
|
|
145
|
+
const buffer = new ArrayBuffer(16);
|
|
146
|
+
const view = new DataView(buffer);
|
|
147
|
+
values.forEach((value, index) => view.setInt32(index * 4, value, true));
|
|
148
|
+
return [...simdOpcode(SIMD.v128Const), ...new Uint8Array(buffer)];
|
|
149
|
+
}
|
|
150
|
+
function directExpressionKey(expression) {
|
|
151
|
+
if (expression.op === "const")
|
|
152
|
+
return `c:${String(expression.value)}`;
|
|
153
|
+
if (expression.op === "index")
|
|
154
|
+
return "i";
|
|
155
|
+
if (expression.op === "capture")
|
|
156
|
+
return `p:${expression.name}`;
|
|
157
|
+
if (expression.op === "load")
|
|
158
|
+
return `l:${expression.path.map(String).join(".")}`;
|
|
159
|
+
if (expression.op === "unary")
|
|
160
|
+
return `u:${expression.operator}:${directExpressionKey(expression.value)}`;
|
|
161
|
+
if (expression.op === "binary")
|
|
162
|
+
return `b:${expression.operator}:${directExpressionKey(expression.left)}:${directExpressionKey(expression.right)}`;
|
|
163
|
+
return `s:${directExpressionKey(expression.condition)}:${directExpressionKey(expression.whenTrue)}:${directExpressionKey(expression.whenFalse)}`;
|
|
164
|
+
}
|
|
165
|
+
function directColumnAddress(pointerLocal, rowLocal) {
|
|
166
|
+
return [
|
|
167
|
+
WASM.localGet, ...encodeU32(pointerLocal),
|
|
168
|
+
WASM.localGet, ...encodeU32(rowLocal),
|
|
169
|
+
WASM.i32Const, ...encodeU32(4),
|
|
170
|
+
WASM.i32Mul,
|
|
171
|
+
WASM.i32Add,
|
|
172
|
+
];
|
|
173
|
+
}
|
|
174
|
+
function emitDirectTruthiness(expression, context) {
|
|
175
|
+
return [
|
|
176
|
+
...emitDirectExpression(expression, context),
|
|
177
|
+
WASM.f32Const, ...wasmF32(0),
|
|
178
|
+
WASM.f32Ne,
|
|
179
|
+
];
|
|
180
|
+
}
|
|
181
|
+
function emitDirectExpression(expression, context) {
|
|
182
|
+
if (expression.op === "const")
|
|
183
|
+
return [WASM.f32Const, ...wasmF32(expression.value)];
|
|
184
|
+
if (expression.op === "index") {
|
|
185
|
+
return [WASM.localGet, ...encodeU32(context.rowLocal), WASM.f32ConvertI32U];
|
|
186
|
+
}
|
|
187
|
+
if (expression.op === "capture") {
|
|
188
|
+
const capture = context.captures.get(expression.name);
|
|
189
|
+
if (capture === undefined)
|
|
190
|
+
throw new CompileFailure("undeclared-capture", `resident capture is undeclared: ${expression.name}`);
|
|
191
|
+
return [
|
|
192
|
+
WASM.localGet, ...encodeU32(3),
|
|
193
|
+
WASM.i32Const, ...encodeU32(capture * 4),
|
|
194
|
+
WASM.i32Add,
|
|
195
|
+
WASM.f32Load, 0x02, 0x00,
|
|
196
|
+
];
|
|
197
|
+
}
|
|
198
|
+
if (expression.op === "load") {
|
|
199
|
+
if (expression.path.length !== 1 || typeof expression.path[0] !== "string") {
|
|
200
|
+
throw new CompileFailure("unsupported-expression", "direct resident WASM requires a statically proven column load");
|
|
201
|
+
}
|
|
202
|
+
const pointerLocal = context.fieldPointers.get(expression.path[0]);
|
|
203
|
+
if (pointerLocal === undefined)
|
|
204
|
+
throw new CompileFailure("unknown-column", `resident WASM reads unknown column: ${expression.path[0]}`);
|
|
205
|
+
return [...directColumnAddress(pointerLocal, context.rowLocal), WASM.f32Load, 0x02, 0x00];
|
|
206
|
+
}
|
|
207
|
+
if (expression.op === "unary") {
|
|
208
|
+
if (expression.operator === "+")
|
|
209
|
+
return emitDirectExpression(expression.value, context);
|
|
210
|
+
if (expression.operator === "-")
|
|
211
|
+
return [...emitDirectExpression(expression.value, context), WASM.f32Neg];
|
|
212
|
+
if (expression.operator === "!") {
|
|
213
|
+
return [
|
|
214
|
+
...emitDirectExpression(expression.value, context),
|
|
215
|
+
WASM.f32Const, ...wasmF32(0),
|
|
216
|
+
WASM.f32Eq,
|
|
217
|
+
WASM.f32ConvertI32S,
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
throw new CompileFailure("unsupported-expression", `direct resident WASM does not support unary ${expression.operator}`);
|
|
221
|
+
}
|
|
222
|
+
if (expression.op === "binary") {
|
|
223
|
+
const arithmetic = {
|
|
224
|
+
"+": WASM.f32Add,
|
|
225
|
+
"-": WASM.f32Sub,
|
|
226
|
+
"*": WASM.f32Mul,
|
|
227
|
+
"/": WASM.f32Div,
|
|
228
|
+
};
|
|
229
|
+
const comparison = {
|
|
230
|
+
"<": WASM.f32Lt,
|
|
231
|
+
"<=": WASM.f32Le,
|
|
232
|
+
">": WASM.f32Gt,
|
|
233
|
+
">=": WASM.f32Ge,
|
|
234
|
+
"==": WASM.f32Eq,
|
|
235
|
+
"===": WASM.f32Eq,
|
|
236
|
+
"!=": WASM.f32Ne,
|
|
237
|
+
"!==": WASM.f32Ne,
|
|
238
|
+
};
|
|
239
|
+
const arithmeticOpcode = arithmetic[expression.operator];
|
|
240
|
+
if (arithmeticOpcode !== undefined) {
|
|
241
|
+
return [
|
|
242
|
+
...emitDirectExpression(expression.left, context),
|
|
243
|
+
...emitDirectExpression(expression.right, context),
|
|
244
|
+
arithmeticOpcode,
|
|
245
|
+
];
|
|
246
|
+
}
|
|
247
|
+
const comparisonOpcode = comparison[expression.operator];
|
|
248
|
+
if (comparisonOpcode !== undefined) {
|
|
249
|
+
return [
|
|
250
|
+
...emitDirectExpression(expression.left, context),
|
|
251
|
+
...emitDirectExpression(expression.right, context),
|
|
252
|
+
comparisonOpcode,
|
|
253
|
+
WASM.f32ConvertI32S,
|
|
254
|
+
];
|
|
255
|
+
}
|
|
256
|
+
if (expression.operator === "&&" || expression.operator === "||") {
|
|
257
|
+
return [
|
|
258
|
+
...emitDirectTruthiness(expression.left, context),
|
|
259
|
+
...emitDirectTruthiness(expression.right, context),
|
|
260
|
+
expression.operator === "&&" ? WASM.i32And : WASM.i32Or,
|
|
261
|
+
WASM.f32ConvertI32S,
|
|
262
|
+
];
|
|
263
|
+
}
|
|
264
|
+
throw new CompileFailure("unsupported-expression", `direct resident WASM does not support binary ${expression.operator}`);
|
|
265
|
+
}
|
|
266
|
+
return [
|
|
267
|
+
...emitDirectExpression(expression.whenTrue, context),
|
|
268
|
+
...emitDirectExpression(expression.whenFalse, context),
|
|
269
|
+
...emitDirectTruthiness(expression.condition, context),
|
|
270
|
+
WASM.select,
|
|
271
|
+
];
|
|
272
|
+
}
|
|
273
|
+
function emitVectorSplat(value) {
|
|
274
|
+
return [WASM.f32Const, ...wasmF32(value), ...simdOpcode(SIMD.f32x4Splat)];
|
|
275
|
+
}
|
|
276
|
+
function emitVectorTruthinessMask(expression, context) {
|
|
277
|
+
return [
|
|
278
|
+
...emitDirectVectorExpression(expression, context),
|
|
279
|
+
...emitVectorSplat(0),
|
|
280
|
+
...simdOpcode(SIMD.f32x4Ne),
|
|
281
|
+
];
|
|
282
|
+
}
|
|
283
|
+
function emitVectorBoolean(mask) {
|
|
284
|
+
return [
|
|
285
|
+
...emitVectorSplat(1),
|
|
286
|
+
...emitVectorSplat(0),
|
|
287
|
+
...mask,
|
|
288
|
+
...simdOpcode(SIMD.v128Bitselect),
|
|
289
|
+
];
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Emit one f32x4 expression. Boolean-valued Kernel IR remains represented as
|
|
293
|
+
* numeric 0/1 lanes, matching the scalar resident ABI rather than leaking mask
|
|
294
|
+
* bit patterns into later arithmetic or stores.
|
|
295
|
+
*/
|
|
296
|
+
function emitDirectVectorExpression(expression, context) {
|
|
297
|
+
if (expression.op === "const")
|
|
298
|
+
return emitVectorSplat(expression.value);
|
|
299
|
+
if (expression.op === "index") {
|
|
300
|
+
return [
|
|
301
|
+
WASM.localGet, ...encodeU32(context.rowLocal),
|
|
302
|
+
...simdOpcode(SIMD.i32x4Splat),
|
|
303
|
+
...wasmI32x4Const([0, 1, 2, 3]),
|
|
304
|
+
...simdOpcode(SIMD.i32x4Add),
|
|
305
|
+
...simdOpcode(SIMD.f32x4ConvertI32x4U),
|
|
306
|
+
];
|
|
307
|
+
}
|
|
308
|
+
if (expression.op === "capture") {
|
|
309
|
+
const capture = context.captures.get(expression.name);
|
|
310
|
+
if (capture === undefined)
|
|
311
|
+
throw new CompileFailure("undeclared-capture", `resident capture is undeclared: ${expression.name}`);
|
|
312
|
+
return [
|
|
313
|
+
WASM.localGet, ...encodeU32(3),
|
|
314
|
+
WASM.i32Const, ...encodeU32(capture * 4),
|
|
315
|
+
WASM.i32Add,
|
|
316
|
+
WASM.f32Load, 0x02, 0x00,
|
|
317
|
+
...simdOpcode(SIMD.f32x4Splat),
|
|
318
|
+
];
|
|
319
|
+
}
|
|
320
|
+
if (expression.op === "load") {
|
|
321
|
+
if (expression.path.length !== 1 || typeof expression.path[0] !== "string") {
|
|
322
|
+
throw new CompileFailure("unsupported-expression", "direct SIMD resident WASM requires a statically proven column load");
|
|
323
|
+
}
|
|
324
|
+
const pointerLocal = context.fieldPointers.get(expression.path[0]);
|
|
325
|
+
if (pointerLocal === undefined)
|
|
326
|
+
throw new CompileFailure("unknown-column", `resident SIMD WASM reads unknown column: ${expression.path[0]}`);
|
|
327
|
+
return [...directColumnAddress(pointerLocal, context.rowLocal), ...simdOpcode(SIMD.v128Load), 0x02, 0x00];
|
|
328
|
+
}
|
|
329
|
+
if (expression.op === "unary") {
|
|
330
|
+
if (expression.operator === "+")
|
|
331
|
+
return emitDirectVectorExpression(expression.value, context);
|
|
332
|
+
if (expression.operator === "-")
|
|
333
|
+
return [...emitDirectVectorExpression(expression.value, context), ...simdOpcode(SIMD.f32x4Neg)];
|
|
334
|
+
if (expression.operator === "!") {
|
|
335
|
+
return emitVectorBoolean([
|
|
336
|
+
...emitDirectVectorExpression(expression.value, context),
|
|
337
|
+
...emitVectorSplat(0),
|
|
338
|
+
...simdOpcode(SIMD.f32x4Eq),
|
|
339
|
+
]);
|
|
340
|
+
}
|
|
341
|
+
throw new CompileFailure("unsupported-expression", `direct SIMD resident WASM does not support unary ${expression.operator}`);
|
|
342
|
+
}
|
|
343
|
+
if (expression.op === "binary") {
|
|
344
|
+
const arithmetic = {
|
|
345
|
+
"+": SIMD.f32x4Add,
|
|
346
|
+
"-": SIMD.f32x4Sub,
|
|
347
|
+
"*": SIMD.f32x4Mul,
|
|
348
|
+
"/": SIMD.f32x4Div,
|
|
349
|
+
};
|
|
350
|
+
const comparison = {
|
|
351
|
+
"<": SIMD.f32x4Lt,
|
|
352
|
+
"<=": SIMD.f32x4Le,
|
|
353
|
+
">": SIMD.f32x4Gt,
|
|
354
|
+
">=": SIMD.f32x4Ge,
|
|
355
|
+
"==": SIMD.f32x4Eq,
|
|
356
|
+
"===": SIMD.f32x4Eq,
|
|
357
|
+
"!=": SIMD.f32x4Ne,
|
|
358
|
+
"!==": SIMD.f32x4Ne,
|
|
359
|
+
};
|
|
360
|
+
const arithmeticOpcode = arithmetic[expression.operator];
|
|
361
|
+
if (arithmeticOpcode !== undefined) {
|
|
362
|
+
return [
|
|
363
|
+
...emitDirectVectorExpression(expression.left, context),
|
|
364
|
+
...emitDirectVectorExpression(expression.right, context),
|
|
365
|
+
...simdOpcode(arithmeticOpcode),
|
|
366
|
+
];
|
|
367
|
+
}
|
|
368
|
+
const comparisonOpcode = comparison[expression.operator];
|
|
369
|
+
if (comparisonOpcode !== undefined) {
|
|
370
|
+
return emitVectorBoolean([
|
|
371
|
+
...emitDirectVectorExpression(expression.left, context),
|
|
372
|
+
...emitDirectVectorExpression(expression.right, context),
|
|
373
|
+
...simdOpcode(comparisonOpcode),
|
|
374
|
+
]);
|
|
375
|
+
}
|
|
376
|
+
if (expression.operator === "&&" || expression.operator === "||") {
|
|
377
|
+
return emitVectorBoolean([
|
|
378
|
+
...emitVectorTruthinessMask(expression.left, context),
|
|
379
|
+
...emitVectorTruthinessMask(expression.right, context),
|
|
380
|
+
...simdOpcode(expression.operator === "&&" ? SIMD.v128And : SIMD.v128Or),
|
|
381
|
+
]);
|
|
382
|
+
}
|
|
383
|
+
throw new CompileFailure("unsupported-expression", `direct SIMD resident WASM does not support binary ${expression.operator}`);
|
|
384
|
+
}
|
|
385
|
+
return [
|
|
386
|
+
...emitDirectVectorExpression(expression.whenTrue, context),
|
|
387
|
+
...emitDirectVectorExpression(expression.whenFalse, context),
|
|
388
|
+
...emitVectorTruthinessMask(expression.condition, context),
|
|
389
|
+
...simdOpcode(SIMD.v128Bitselect),
|
|
390
|
+
];
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Emit a region-specific f32x4 loop with a scalar tail for arbitrary dirty
|
|
394
|
+
* range boundaries. This keeps one semantic implementation per region while
|
|
395
|
+
* letting the engine execute four packed rows per vector iteration.
|
|
396
|
+
*/
|
|
397
|
+
function emitDirectResidentWasmSimdModule(region, fields, captures, sharedMemory = false) {
|
|
398
|
+
const maxOutputs = Math.max(0, ...region.kernels.map(kernel => kernel.kind === "map" ? kernel.outputs.length : 0));
|
|
399
|
+
const rowLocal = 4;
|
|
400
|
+
const rangeLocal = 5;
|
|
401
|
+
const endLocal = 6;
|
|
402
|
+
const pointerBase = 7;
|
|
403
|
+
const fieldPointers = new Map();
|
|
404
|
+
for (const [name, index] of fields)
|
|
405
|
+
fieldPointers.set(name, pointerBase + index);
|
|
406
|
+
const scalarTempBase = pointerBase + fields.size;
|
|
407
|
+
const vectorTempBase = scalarTempBase + maxOutputs;
|
|
408
|
+
const context = { fieldPointers, captures, rowLocal };
|
|
409
|
+
const instructions = [
|
|
410
|
+
...[...fields.entries()].flatMap(([, column], index) => [
|
|
411
|
+
WASM.localGet, ...encodeU32(0),
|
|
412
|
+
WASM.i32Const, ...encodeU32(column * 4),
|
|
413
|
+
WASM.i32Add,
|
|
414
|
+
WASM.i32Load, 0x02, 0x00,
|
|
415
|
+
WASM.localSet, ...encodeU32(pointerBase + index),
|
|
416
|
+
]),
|
|
417
|
+
WASM.i32Const, ...encodeU32(0),
|
|
418
|
+
WASM.localSet, ...encodeU32(rangeLocal),
|
|
419
|
+
WASM.block, WASM.emptyBlock,
|
|
420
|
+
WASM.loop, WASM.emptyBlock,
|
|
421
|
+
WASM.localGet, ...encodeU32(rangeLocal),
|
|
422
|
+
WASM.localGet, ...encodeU32(2),
|
|
423
|
+
WASM.i32GeU,
|
|
424
|
+
WASM.brIf, ...encodeU32(1),
|
|
425
|
+
WASM.localGet, ...encodeU32(1),
|
|
426
|
+
WASM.localGet, ...encodeU32(rangeLocal),
|
|
427
|
+
WASM.i32Const, ...encodeU32(8),
|
|
428
|
+
WASM.i32Mul,
|
|
429
|
+
WASM.i32Add,
|
|
430
|
+
WASM.i32Load, 0x02, 0x00,
|
|
431
|
+
WASM.localSet, ...encodeU32(rowLocal),
|
|
432
|
+
WASM.localGet, ...encodeU32(1),
|
|
433
|
+
WASM.localGet, ...encodeU32(rangeLocal),
|
|
434
|
+
WASM.i32Const, ...encodeU32(8),
|
|
435
|
+
WASM.i32Mul,
|
|
436
|
+
WASM.i32Add,
|
|
437
|
+
WASM.i32Const, ...encodeU32(4),
|
|
438
|
+
WASM.i32Add,
|
|
439
|
+
WASM.i32Load, 0x02, 0x00,
|
|
440
|
+
WASM.localSet, ...encodeU32(endLocal),
|
|
441
|
+
// Vector body: stop when fewer than four rows remain in this dirty range.
|
|
442
|
+
WASM.block, WASM.emptyBlock,
|
|
443
|
+
WASM.loop, WASM.emptyBlock,
|
|
444
|
+
WASM.localGet, ...encodeU32(rowLocal),
|
|
445
|
+
WASM.i32Const, ...encodeU32(4),
|
|
446
|
+
WASM.i32Add,
|
|
447
|
+
WASM.localGet, ...encodeU32(endLocal),
|
|
448
|
+
WASM.i32GtU,
|
|
449
|
+
WASM.brIf, ...encodeU32(1),
|
|
450
|
+
];
|
|
451
|
+
for (const kernel of region.kernels) {
|
|
452
|
+
if (kernel.kind !== "map")
|
|
453
|
+
throw new CompileFailure("non-map-kernel", "direct SIMD resident WASM currently supports fused map kernels");
|
|
454
|
+
const computed = new Map();
|
|
455
|
+
kernel.outputs.forEach((output, index) => {
|
|
456
|
+
const key = directExpressionKey(output.value);
|
|
457
|
+
const existing = computed.get(key);
|
|
458
|
+
instructions.push(...(existing === undefined
|
|
459
|
+
? emitDirectVectorExpression(output.value, context)
|
|
460
|
+
: [WASM.localGet, ...encodeU32(vectorTempBase + existing)]), WASM.localSet, ...encodeU32(vectorTempBase + index));
|
|
461
|
+
if (existing === undefined)
|
|
462
|
+
computed.set(key, index);
|
|
463
|
+
});
|
|
464
|
+
kernel.outputs.forEach((output, index) => {
|
|
465
|
+
const pointerLocal = fieldPointers.get(output.name);
|
|
466
|
+
if (pointerLocal === undefined)
|
|
467
|
+
throw new CompileFailure("unknown-column", `resident SIMD WASM writes unknown column: ${output.name}`);
|
|
468
|
+
instructions.push(...directColumnAddress(pointerLocal, rowLocal), WASM.localGet, ...encodeU32(vectorTempBase + index), ...simdOpcode(SIMD.v128Store), 0x02, 0x00);
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
instructions.push(WASM.localGet, ...encodeU32(rowLocal), WASM.i32Const, ...encodeU32(4), WASM.i32Add, WASM.localSet, ...encodeU32(rowLocal), WASM.br, ...encodeU32(0), WASM.end, WASM.end,
|
|
472
|
+
// Scalar tail retains exact semantics for 0..3 remaining rows.
|
|
473
|
+
WASM.block, WASM.emptyBlock, WASM.loop, WASM.emptyBlock, WASM.localGet, ...encodeU32(rowLocal), WASM.localGet, ...encodeU32(endLocal), WASM.i32GeU, WASM.brIf, ...encodeU32(1));
|
|
474
|
+
const scalarContext = { fieldPointers, captures, rowLocal };
|
|
475
|
+
for (const kernel of region.kernels) {
|
|
476
|
+
if (kernel.kind !== "map")
|
|
477
|
+
throw new CompileFailure("non-map-kernel", "direct SIMD resident WASM currently supports fused map kernels");
|
|
478
|
+
const computed = new Map();
|
|
479
|
+
kernel.outputs.forEach((output, index) => {
|
|
480
|
+
const key = directExpressionKey(output.value);
|
|
481
|
+
const existing = computed.get(key);
|
|
482
|
+
instructions.push(...(existing === undefined
|
|
483
|
+
? emitDirectExpression(output.value, scalarContext)
|
|
484
|
+
: [WASM.localGet, ...encodeU32(scalarTempBase + existing)]), WASM.localSet, ...encodeU32(scalarTempBase + index));
|
|
485
|
+
if (existing === undefined)
|
|
486
|
+
computed.set(key, index);
|
|
487
|
+
});
|
|
488
|
+
kernel.outputs.forEach((output, index) => {
|
|
489
|
+
const pointerLocal = fieldPointers.get(output.name);
|
|
490
|
+
if (pointerLocal === undefined)
|
|
491
|
+
throw new CompileFailure("unknown-column", `resident SIMD WASM writes unknown column: ${output.name}`);
|
|
492
|
+
instructions.push(...directColumnAddress(pointerLocal, rowLocal), WASM.localGet, ...encodeU32(scalarTempBase + index), WASM.f32Store, 0x02, 0x00);
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
instructions.push(WASM.localGet, ...encodeU32(rowLocal), WASM.i32Const, ...encodeU32(1), WASM.i32Add, WASM.localSet, ...encodeU32(rowLocal), WASM.br, ...encodeU32(0), WASM.end, WASM.end, WASM.localGet, ...encodeU32(rangeLocal), WASM.i32Const, ...encodeU32(1), WASM.i32Add, WASM.localSet, ...encodeU32(rangeLocal), WASM.br, ...encodeU32(0), WASM.end, WASM.end, WASM.i32Const, ...encodeU32(0), WASM.end);
|
|
496
|
+
const localGroups = [[...encodeU32(3 + fields.size), WASM.i32]];
|
|
497
|
+
if (maxOutputs > 0) {
|
|
498
|
+
localGroups.push([...encodeU32(maxOutputs), WASM.f32]);
|
|
499
|
+
localGroups.push([...encodeU32(maxOutputs), WASM.v128]);
|
|
500
|
+
}
|
|
501
|
+
const body = [...encodeU32(localGroups.length), ...localGroups.flat(), ...instructions];
|
|
502
|
+
const functionType = [
|
|
503
|
+
0x60,
|
|
504
|
+
...encodeU32(4), WASM.i32, WASM.i32, WASM.i32, WASM.i32,
|
|
505
|
+
...encodeU32(1), WASM.i32,
|
|
506
|
+
];
|
|
507
|
+
const importMemory = sharedMemory
|
|
508
|
+
? [...wasmName("env"), ...wasmName("memory"), 0x02, 0x03, ...encodeU32(0), ...encodeU32(65_536)]
|
|
509
|
+
: [...wasmName("env"), ...wasmName("memory"), 0x02, 0x00, ...encodeU32(0)];
|
|
510
|
+
const exportedFunction = [...wasmName("resident_execute_direct_simd"), 0x00, ...encodeU32(0)];
|
|
511
|
+
return new Uint8Array([
|
|
512
|
+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
|
|
513
|
+
...wasmSection(1, wasmVector([functionType])),
|
|
514
|
+
...wasmSection(2, wasmVector([importMemory])),
|
|
515
|
+
...wasmSection(3, wasmVector([[...encodeU32(0)]])),
|
|
516
|
+
...wasmSection(7, wasmVector([exportedFunction])),
|
|
517
|
+
...wasmSection(10, wasmVector([[...encodeU32(body.length), ...body]])),
|
|
518
|
+
]);
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Emit a tiny region-specific scalar WASM module. The hot row loop contains no
|
|
522
|
+
* opcode dispatch or operand-stack interpreter; browser engines can compile the
|
|
523
|
+
* exact fused arithmetic like an ordinary native function.
|
|
524
|
+
*/
|
|
525
|
+
function emitDirectResidentWasmModule(region, fields, captures) {
|
|
526
|
+
const maxOutputs = Math.max(0, ...region.kernels.map(kernel => kernel.kind === "map" ? kernel.outputs.length : 0));
|
|
527
|
+
const rowLocal = 4;
|
|
528
|
+
const rangeLocal = 5;
|
|
529
|
+
const endLocal = 6;
|
|
530
|
+
const pointerBase = 7;
|
|
531
|
+
const fieldPointers = new Map();
|
|
532
|
+
for (const [name, index] of fields)
|
|
533
|
+
fieldPointers.set(name, pointerBase + index);
|
|
534
|
+
const tempBase = pointerBase + fields.size;
|
|
535
|
+
const context = { fieldPointers, captures, rowLocal };
|
|
536
|
+
const instructions = [
|
|
537
|
+
// Hoist SoA column base addresses out of the row/range loops. This is one
|
|
538
|
+
// of the major advantages of region-specific code over the generic bytecode
|
|
539
|
+
// interpreter, which has to resolve columns dynamically.
|
|
540
|
+
...[...fields.entries()].flatMap(([, column], index) => [
|
|
541
|
+
WASM.localGet, ...encodeU32(0),
|
|
542
|
+
WASM.i32Const, ...encodeU32(column * 4),
|
|
543
|
+
WASM.i32Add,
|
|
544
|
+
WASM.i32Load, 0x02, 0x00,
|
|
545
|
+
WASM.localSet, ...encodeU32(pointerBase + index),
|
|
546
|
+
]),
|
|
547
|
+
WASM.i32Const, ...encodeU32(0),
|
|
548
|
+
WASM.localSet, ...encodeU32(rangeLocal),
|
|
549
|
+
WASM.block, WASM.emptyBlock,
|
|
550
|
+
WASM.loop, WASM.emptyBlock,
|
|
551
|
+
WASM.localGet, ...encodeU32(rangeLocal),
|
|
552
|
+
WASM.localGet, ...encodeU32(2),
|
|
553
|
+
WASM.i32GeU,
|
|
554
|
+
WASM.brIf, ...encodeU32(1),
|
|
555
|
+
// row = ranges[rangeIndex * 2]
|
|
556
|
+
WASM.localGet, ...encodeU32(1),
|
|
557
|
+
WASM.localGet, ...encodeU32(rangeLocal),
|
|
558
|
+
WASM.i32Const, ...encodeU32(8),
|
|
559
|
+
WASM.i32Mul,
|
|
560
|
+
WASM.i32Add,
|
|
561
|
+
WASM.i32Load, 0x02, 0x00,
|
|
562
|
+
WASM.localSet, ...encodeU32(rowLocal),
|
|
563
|
+
// end = ranges[rangeIndex * 2 + 1]
|
|
564
|
+
WASM.localGet, ...encodeU32(1),
|
|
565
|
+
WASM.localGet, ...encodeU32(rangeLocal),
|
|
566
|
+
WASM.i32Const, ...encodeU32(8),
|
|
567
|
+
WASM.i32Mul,
|
|
568
|
+
WASM.i32Add,
|
|
569
|
+
WASM.i32Const, ...encodeU32(4),
|
|
570
|
+
WASM.i32Add,
|
|
571
|
+
WASM.i32Load, 0x02, 0x00,
|
|
572
|
+
WASM.localSet, ...encodeU32(endLocal),
|
|
573
|
+
WASM.block, WASM.emptyBlock,
|
|
574
|
+
WASM.loop, WASM.emptyBlock,
|
|
575
|
+
WASM.localGet, ...encodeU32(rowLocal),
|
|
576
|
+
WASM.localGet, ...encodeU32(endLocal),
|
|
577
|
+
WASM.i32GeU,
|
|
578
|
+
WASM.brIf, ...encodeU32(1),
|
|
579
|
+
];
|
|
580
|
+
for (const kernel of region.kernels) {
|
|
581
|
+
if (kernel.kind !== "map")
|
|
582
|
+
throw new CompileFailure("non-map-kernel", "direct resident WASM currently supports fused map kernels");
|
|
583
|
+
const computed = new Map();
|
|
584
|
+
kernel.outputs.forEach((output, index) => {
|
|
585
|
+
const key = directExpressionKey(output.value);
|
|
586
|
+
const existing = computed.get(key);
|
|
587
|
+
instructions.push(...(existing === undefined
|
|
588
|
+
? emitDirectExpression(output.value, context)
|
|
589
|
+
: [WASM.localGet, ...encodeU32(tempBase + existing)]), WASM.localSet, ...encodeU32(tempBase + index));
|
|
590
|
+
if (existing === undefined)
|
|
591
|
+
computed.set(key, index);
|
|
592
|
+
});
|
|
593
|
+
kernel.outputs.forEach((output, index) => {
|
|
594
|
+
const pointerLocal = fieldPointers.get(output.name);
|
|
595
|
+
if (pointerLocal === undefined)
|
|
596
|
+
throw new CompileFailure("unknown-column", `resident WASM writes unknown column: ${output.name}`);
|
|
597
|
+
instructions.push(...directColumnAddress(pointerLocal, rowLocal), WASM.localGet, ...encodeU32(tempBase + index), WASM.f32Store, 0x02, 0x00);
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
instructions.push(WASM.localGet, ...encodeU32(rowLocal), WASM.i32Const, ...encodeU32(1), WASM.i32Add, WASM.localSet, ...encodeU32(rowLocal), WASM.br, ...encodeU32(0), WASM.end, WASM.end, WASM.localGet, ...encodeU32(rangeLocal), WASM.i32Const, ...encodeU32(1), WASM.i32Add, WASM.localSet, ...encodeU32(rangeLocal), WASM.br, ...encodeU32(0), WASM.end, WASM.end, WASM.i32Const, ...encodeU32(0), WASM.end);
|
|
601
|
+
const localGroups = [[...encodeU32(3 + fields.size), WASM.i32]];
|
|
602
|
+
if (maxOutputs > 0)
|
|
603
|
+
localGroups.push([...encodeU32(maxOutputs), WASM.f32]);
|
|
604
|
+
const body = [
|
|
605
|
+
...encodeU32(localGroups.length),
|
|
606
|
+
...localGroups.flat(),
|
|
607
|
+
...instructions,
|
|
608
|
+
];
|
|
609
|
+
const functionType = [
|
|
610
|
+
0x60,
|
|
611
|
+
...encodeU32(4), WASM.i32, WASM.i32, WASM.i32, WASM.i32,
|
|
612
|
+
...encodeU32(1), WASM.i32,
|
|
613
|
+
];
|
|
614
|
+
const importMemory = [
|
|
615
|
+
...wasmName("env"),
|
|
616
|
+
...wasmName("memory"),
|
|
617
|
+
0x02,
|
|
618
|
+
0x00, ...encodeU32(0),
|
|
619
|
+
];
|
|
620
|
+
const exportedFunction = [
|
|
621
|
+
...wasmName("resident_execute_direct"),
|
|
622
|
+
0x00, ...encodeU32(0),
|
|
623
|
+
];
|
|
624
|
+
return new Uint8Array([
|
|
625
|
+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
|
|
626
|
+
...wasmSection(1, wasmVector([functionType])),
|
|
627
|
+
...wasmSection(2, wasmVector([importMemory])),
|
|
628
|
+
...wasmSection(3, wasmVector([[...encodeU32(0)]])),
|
|
629
|
+
...wasmSection(7, wasmVector([exportedFunction])),
|
|
630
|
+
...wasmSection(10, wasmVector([[...encodeU32(body.length), ...body]])),
|
|
631
|
+
]);
|
|
632
|
+
}
|
|
633
|
+
function sameLayout(left, right) {
|
|
634
|
+
return left.length === right.length
|
|
635
|
+
&& left.fields.length === right.fields.length
|
|
636
|
+
&& left.fields.every((field, index) => field.name === right.fields[index]?.name && field.type === right.fields[index]?.type);
|
|
637
|
+
}
|
|
638
|
+
function f32Bits(value) {
|
|
639
|
+
const buffer = new ArrayBuffer(4);
|
|
640
|
+
new Float32Array(buffer)[0] = typeof value === "boolean" ? (value ? 1 : 0) : value;
|
|
641
|
+
return new Uint32Array(buffer)[0];
|
|
642
|
+
}
|
|
643
|
+
class CompileFailure extends Error {
|
|
644
|
+
reason;
|
|
645
|
+
constructor(reason, message) {
|
|
646
|
+
super(message);
|
|
647
|
+
this.reason = reason;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
function emitExpression(expression, context) {
|
|
651
|
+
context.operationCount += 1;
|
|
652
|
+
if (expression.op === "const") {
|
|
653
|
+
context.scalarValueOps += 1;
|
|
654
|
+
context.words.push(OP.ConstF32, f32Bits(expression.value));
|
|
655
|
+
return 1;
|
|
656
|
+
}
|
|
657
|
+
if (expression.op === "index") {
|
|
658
|
+
context.scalarValueOps += 1;
|
|
659
|
+
context.words.push(OP.Index);
|
|
660
|
+
return 1;
|
|
661
|
+
}
|
|
662
|
+
if (expression.op === "capture") {
|
|
663
|
+
context.scalarValueOps += 1;
|
|
664
|
+
const capture = context.captures.get(expression.name);
|
|
665
|
+
if (capture === undefined)
|
|
666
|
+
throw new CompileFailure("undeclared-capture", `resident capture is undeclared: ${expression.name}`);
|
|
667
|
+
context.words.push(OP.Capture, capture);
|
|
668
|
+
return 1;
|
|
669
|
+
}
|
|
670
|
+
if (expression.op === "load") {
|
|
671
|
+
context.loadOps += 1;
|
|
672
|
+
if (expression.path.length !== 1 || typeof expression.path[0] !== "string") {
|
|
673
|
+
throw new CompileFailure("unsupported-expression", "resident WASM requires a statically proven column load");
|
|
674
|
+
}
|
|
675
|
+
const column = context.fields.get(expression.path[0]);
|
|
676
|
+
if (column === undefined)
|
|
677
|
+
throw new CompileFailure("unknown-column", `resident WASM reads unknown column: ${expression.path[0]}`);
|
|
678
|
+
context.words.push(OP.LoadColumn, column);
|
|
679
|
+
return 1;
|
|
680
|
+
}
|
|
681
|
+
if (expression.op === "unary") {
|
|
682
|
+
context.arithmeticOps += 1;
|
|
683
|
+
const depth = emitExpression(expression.value, context);
|
|
684
|
+
if (expression.operator === "+")
|
|
685
|
+
context.words.push(OP.Positive);
|
|
686
|
+
else if (expression.operator === "-")
|
|
687
|
+
context.words.push(OP.Negative);
|
|
688
|
+
else if (expression.operator === "!")
|
|
689
|
+
context.words.push(OP.Not);
|
|
690
|
+
else
|
|
691
|
+
throw new CompileFailure("unsupported-expression", `resident WASM does not support unary ${expression.operator}`);
|
|
692
|
+
return depth;
|
|
693
|
+
}
|
|
694
|
+
if (expression.op === "binary") {
|
|
695
|
+
const opcode = BINARY_OPCODE[expression.operator];
|
|
696
|
+
if (opcode === undefined)
|
|
697
|
+
throw new CompileFailure("unsupported-expression", `resident WASM does not support binary ${expression.operator}`);
|
|
698
|
+
if (expression.operator === "+" || expression.operator === "-" || expression.operator === "*" || expression.operator === "/") {
|
|
699
|
+
context.arithmeticOps += 1;
|
|
700
|
+
if (expression.operator === "/")
|
|
701
|
+
context.divisionOps += 1;
|
|
702
|
+
}
|
|
703
|
+
else {
|
|
704
|
+
context.comparisonOps += 1;
|
|
705
|
+
}
|
|
706
|
+
const leftDepth = emitExpression(expression.left, context);
|
|
707
|
+
const rightDepth = emitExpression(expression.right, context);
|
|
708
|
+
context.words.push(opcode);
|
|
709
|
+
return Math.max(leftDepth, 1 + rightDepth);
|
|
710
|
+
}
|
|
711
|
+
context.selectOps += 1;
|
|
712
|
+
const conditionDepth = emitExpression(expression.condition, context);
|
|
713
|
+
const trueDepth = emitExpression(expression.whenTrue, context);
|
|
714
|
+
const falseDepth = emitExpression(expression.whenFalse, context);
|
|
715
|
+
context.words.push(OP.Select);
|
|
716
|
+
return Math.max(conditionDepth, 1 + trueDepth, 2 + falseDepth);
|
|
717
|
+
}
|
|
718
|
+
function staticRejections(region) {
|
|
719
|
+
const reasons = new Set();
|
|
720
|
+
if (region.lifetime === "single-use")
|
|
721
|
+
reasons.add("single-use-region");
|
|
722
|
+
if (region.inputResidency !== "packed" || region.outputResidency !== "packed")
|
|
723
|
+
reasons.add("non-packed-boundary");
|
|
724
|
+
if (region.estimatedTransferBytes !== 0)
|
|
725
|
+
reasons.add("transfer-required");
|
|
726
|
+
if (!sameLayout(region.source.layout, region.sink.layout))
|
|
727
|
+
reasons.add("layout-mismatch");
|
|
728
|
+
if (region.source.layout.fields.some(field => field.type !== "f32"))
|
|
729
|
+
reasons.add("layout-not-dense-f32");
|
|
730
|
+
if (region.source.layout.fields.length > 64)
|
|
731
|
+
reasons.add("too-many-columns");
|
|
732
|
+
if (region.kernels.some(kernel => kernel.kind !== "map"))
|
|
733
|
+
reasons.add("non-map-kernel");
|
|
734
|
+
if (region.kernels.some(kernel => kernel.kind === "map" && kernel.outputs.length > 64))
|
|
735
|
+
reasons.add("too-many-kernel-outputs");
|
|
736
|
+
return [...reasons];
|
|
737
|
+
}
|
|
738
|
+
export function analyzeResidentWasmRegion(region) {
|
|
739
|
+
const reasons = staticRejections(region);
|
|
740
|
+
if (reasons.length > 0)
|
|
741
|
+
return Object.freeze({ eligible: false, program: null, reasons: Object.freeze(reasons) });
|
|
742
|
+
const fieldNames = Object.freeze(region.source.layout.fields.map(field => field.name));
|
|
743
|
+
const fields = new Map(fieldNames.map((name, index) => [name, index]));
|
|
744
|
+
const captureNames = Object.freeze([...new Set(region.kernels.flatMap(kernel => [...kernel.captures]))].sort());
|
|
745
|
+
const captures = new Map(captureNames.map((name, index) => [name, index]));
|
|
746
|
+
const context = {
|
|
747
|
+
fields,
|
|
748
|
+
captures,
|
|
749
|
+
words: [],
|
|
750
|
+
operationCount: 0,
|
|
751
|
+
loadOps: 0,
|
|
752
|
+
storeOps: 0,
|
|
753
|
+
scalarValueOps: 0,
|
|
754
|
+
arithmeticOps: 0,
|
|
755
|
+
divisionOps: 0,
|
|
756
|
+
comparisonOps: 0,
|
|
757
|
+
selectOps: 0,
|
|
758
|
+
};
|
|
759
|
+
let maxStackDepth = 0;
|
|
760
|
+
try {
|
|
761
|
+
for (const kernel of region.kernels) {
|
|
762
|
+
if (kernel.kind !== "map")
|
|
763
|
+
throw new CompileFailure("non-map-kernel", "resident WASM currently supports fused map kernels");
|
|
764
|
+
context.words.push(OP.Kernel, kernel.outputs.length);
|
|
765
|
+
for (const output of kernel.outputs) {
|
|
766
|
+
const column = fields.get(output.name);
|
|
767
|
+
if (column === undefined)
|
|
768
|
+
throw new CompileFailure("unknown-column", `resident WASM writes unknown column: ${output.name}`);
|
|
769
|
+
maxStackDepth = Math.max(maxStackDepth, emitExpression(output.value, context));
|
|
770
|
+
context.words.push(OP.StoreTemp, column);
|
|
771
|
+
context.storeOps += 1;
|
|
772
|
+
}
|
|
773
|
+
context.words.push(OP.Commit);
|
|
774
|
+
}
|
|
775
|
+
if (maxStackDepth > 128)
|
|
776
|
+
throw new CompileFailure("operand-stack-too-deep", "resident WASM expression stack exceeds 128 values");
|
|
777
|
+
}
|
|
778
|
+
catch (error) {
|
|
779
|
+
if (!(error instanceof CompileFailure))
|
|
780
|
+
throw error;
|
|
781
|
+
return Object.freeze({ eligible: false, program: null, reasons: Object.freeze([error.reason]) });
|
|
782
|
+
}
|
|
783
|
+
context.words.push(OP.End);
|
|
784
|
+
let directModuleBytes;
|
|
785
|
+
let directSimdModuleBytes;
|
|
786
|
+
let directSharedSimdModuleBytes;
|
|
787
|
+
try {
|
|
788
|
+
directModuleBytes = emitDirectResidentWasmModule(region, fields, captures);
|
|
789
|
+
}
|
|
790
|
+
catch (error) {
|
|
791
|
+
// The bytecode program is still the required compatibility representation.
|
|
792
|
+
// A direct-module emitter rejection must never make an otherwise supported
|
|
793
|
+
// Resident Compute region incorrect or unavailable.
|
|
794
|
+
if (!(error instanceof CompileFailure))
|
|
795
|
+
throw error;
|
|
796
|
+
}
|
|
797
|
+
try {
|
|
798
|
+
directSimdModuleBytes = emitDirectResidentWasmSimdModule(region, fields, captures);
|
|
799
|
+
}
|
|
800
|
+
catch (error) {
|
|
801
|
+
// SIMD specialization is strictly optional. Scalar direct WASM and the
|
|
802
|
+
// generic resident kernels remain available when a vector expression is
|
|
803
|
+
// outside the current lowering subset.
|
|
804
|
+
if (!(error instanceof CompileFailure))
|
|
805
|
+
throw error;
|
|
806
|
+
}
|
|
807
|
+
if (directSimdModuleBytes) {
|
|
808
|
+
try {
|
|
809
|
+
directSharedSimdModuleBytes = emitDirectResidentWasmSimdModule(region, fields, captures, true);
|
|
810
|
+
}
|
|
811
|
+
catch (error) {
|
|
812
|
+
if (!(error instanceof CompileFailure))
|
|
813
|
+
throw error;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
const branchBase = Math.max(1, context.arithmeticOps + context.comparisonOps + context.selectOps);
|
|
817
|
+
const branchPressure = context.selectOps / branchBase;
|
|
818
|
+
const weightedOpsPerItem = context.operationCount
|
|
819
|
+
+ context.storeOps * 0.5
|
|
820
|
+
+ context.selectOps * 1.5
|
|
821
|
+
+ context.divisionOps * 1.5;
|
|
822
|
+
const simdSuitability = directSimdModuleBytes
|
|
823
|
+
? Math.max(0.75, 1 - branchPressure * 0.25)
|
|
824
|
+
: 0;
|
|
825
|
+
const costProfile = Object.freeze({
|
|
826
|
+
loadOpsPerItem: context.loadOps,
|
|
827
|
+
storeOpsPerItem: context.storeOps,
|
|
828
|
+
scalarValueOpsPerItem: context.scalarValueOps,
|
|
829
|
+
arithmeticOpsPerItem: context.arithmeticOps,
|
|
830
|
+
divisionOpsPerItem: context.divisionOps,
|
|
831
|
+
comparisonOpsPerItem: context.comparisonOps,
|
|
832
|
+
selectOpsPerItem: context.selectOps,
|
|
833
|
+
weightedOpsPerItem,
|
|
834
|
+
simdSuitability,
|
|
835
|
+
branchPressure,
|
|
836
|
+
});
|
|
837
|
+
const program = Object.freeze({
|
|
838
|
+
version: 1,
|
|
839
|
+
regionId: region.id,
|
|
840
|
+
words: new Uint32Array(context.words),
|
|
841
|
+
fieldNames,
|
|
842
|
+
captureNames,
|
|
843
|
+
operationCount: context.operationCount,
|
|
844
|
+
maxStackDepth,
|
|
845
|
+
costProfile,
|
|
846
|
+
...(directModuleBytes ? {
|
|
847
|
+
directModuleBytes,
|
|
848
|
+
directEntrypoint: "resident_execute_direct",
|
|
849
|
+
} : {}),
|
|
850
|
+
...(directSimdModuleBytes ? {
|
|
851
|
+
directSimdModuleBytes,
|
|
852
|
+
directSimdEntrypoint: "resident_execute_direct_simd",
|
|
853
|
+
} : {}),
|
|
854
|
+
...(directSharedSimdModuleBytes ? {
|
|
855
|
+
directSharedSimdModuleBytes,
|
|
856
|
+
directSharedSimdEntrypoint: "resident_execute_direct_simd",
|
|
857
|
+
} : {}),
|
|
858
|
+
});
|
|
859
|
+
return Object.freeze({ eligible: true, program, reasons: Object.freeze([]) });
|
|
860
|
+
}
|
|
861
|
+
export function compileResidentWasmRegion(region) {
|
|
862
|
+
const analysis = analyzeResidentWasmRegion(region);
|
|
863
|
+
if (!analysis.eligible)
|
|
864
|
+
throw new TypeError(`resident WASM promotion rejected: ${analysis.reasons.join(", ")}`);
|
|
865
|
+
return analysis.program;
|
|
866
|
+
}
|
|
867
|
+
//# sourceMappingURL=resident-wasm.js.map
|