@onda-lang/binaryen-web 0.9.1 → 0.9.3
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/README.md +10 -10
- package/api.md +325 -310
- package/package.json +3 -3
- package/src/compiler/core.js +175 -289
- package/src/compiler/expression-tree.js +108 -0
- package/src/compiler/guarded-addresses.js +243 -0
- package/src/compiler/index.js +41 -30
- package/src/compiler/inlining.js +62 -0
- package/src/compiler/lowering.js +16 -7
- package/src/compiler/shared.js +15 -9
- package/src/compiler/simd-reductions.js +229 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
// Pack independent elements of fixed-size state arrays, keeping their final
|
|
2
|
+
// reduction in scalar source order. Unsupported statements use scalar lowering.
|
|
3
|
+
import binaryen from "binaryen";
|
|
4
|
+
|
|
5
|
+
const local = (value) => value?.kind === "local" ? value.data : null;
|
|
6
|
+
const dest = (place) => place?.projections.length === 0 ? local(place.base) : null;
|
|
7
|
+
const laneCount = 4;
|
|
8
|
+
const isFloat = (type) => type === "f32" || type === "f64";
|
|
9
|
+
const opcodes = new Map([["add", "add"], ["subtract", "sub"], ["multiply", "mul"], ["divide", "div"]]);
|
|
10
|
+
|
|
11
|
+
// Resolve transport ordinals once. Lowering may revisit a body when falling back
|
|
12
|
+
// to scalar code, so it looks up plans by statement identity.
|
|
13
|
+
export function indexOrderedReductions(mir) {
|
|
14
|
+
const plans = new WeakMap();
|
|
15
|
+
if (!mir.ordered_reductions?.length) return plans;
|
|
16
|
+
const byOrdinal = new Map(mir.ordered_reductions.map(plan => [plan.loop_ordinal, plan]));
|
|
17
|
+
let ordinal = 0;
|
|
18
|
+
function visit(block) {
|
|
19
|
+
for (const statement of block.statements) {
|
|
20
|
+
const { kind, data } = statement.kind;
|
|
21
|
+
if (kind === "loop") {
|
|
22
|
+
const plan = byOrdinal.get(ordinal++);
|
|
23
|
+
if (plan) plans.set(statement, plan);
|
|
24
|
+
visit(data.body);
|
|
25
|
+
} else if (kind === "if") {
|
|
26
|
+
visit(data.then_block);
|
|
27
|
+
visit(data.else_block);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
visit(mir.functions[mir.entry_points.process].body);
|
|
32
|
+
return plans;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function vectorizeBankReduction(compiler, statement, context, scalarLoop, competingLoop) {
|
|
36
|
+
const loop = compiler.orderedReductions.get(statement);
|
|
37
|
+
const { body } = statement.kind.data;
|
|
38
|
+
if (!compiler.options.simd || !compiler.options.optimize || !loop) return;
|
|
39
|
+
// Short banks often lose to the scalar schedule when the alternate arm
|
|
40
|
+
// executes another loop. Standalone banks can pay off with one vector.
|
|
41
|
+
const minimum = competingLoop ? 4 * laneCount : laneCount;
|
|
42
|
+
if (!(loop.last - loop.first + 1 >= minimum)) return;
|
|
43
|
+
// The proof identifies the reduction assignment by its unique result.
|
|
44
|
+
const startOfReduction = body.statements.findIndex(({ kind: { kind, data } }) =>
|
|
45
|
+
kind === "assign" && dest(data.destination) === loop.sum_result);
|
|
46
|
+
if (startOfReduction < 0) return;
|
|
47
|
+
const plan = planStateReduction(compiler, context, loop, body.statements.slice(0, startOfReduction));
|
|
48
|
+
if (plan) return emitStateReduction(compiler, context, loop, plan, scalarLoop);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Lower the operations supported by contiguous SIMD lanes. MIR has already
|
|
52
|
+
// proved independence. Forward same-lane writes in statement order.
|
|
53
|
+
function planStateReduction(compiler, context, loop, body) {
|
|
54
|
+
const assigned = new Set([loop.index, loop.sum, loop.condition, loop.sum_result]);
|
|
55
|
+
for (const { kind: { kind, data } } of body) {
|
|
56
|
+
const ids = kind === "assign" ? [dest(data.destination)] : kind === "call" ? data.results : [];
|
|
57
|
+
for (const id of ids) assigned.add(id);
|
|
58
|
+
}
|
|
59
|
+
const m = compiler.module;
|
|
60
|
+
const vectors = { f32: m.f32x4, f64: m.f64x2, i32: m.i32x4 };
|
|
61
|
+
const indexValue = () => compiler.loadLocal(loop.index, context);
|
|
62
|
+
const nodes = [];
|
|
63
|
+
const node = (type, emit, invariant = false) => {
|
|
64
|
+
const value = {
|
|
65
|
+
type, emit, invariant, slots: [],
|
|
66
|
+
get(half = 0) {
|
|
67
|
+
return m.local.get(this.slots[type === "f64" ? half : 0], binaryen.v128);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
nodes.push(value);
|
|
71
|
+
return value;
|
|
72
|
+
};
|
|
73
|
+
const induction = node("i32", () => m.i32x4.add(m.i32x4.splat(indexValue()),
|
|
74
|
+
m.v128.const([0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0])));
|
|
75
|
+
const bank = new Map();
|
|
76
|
+
const writes = new Map();
|
|
77
|
+
const root = { locals: new Map([[loop.index, induction]]), params: [] };
|
|
78
|
+
const value = (v, frame) => {
|
|
79
|
+
if (v.kind === "constant" && isFloat(v.data.type)) {
|
|
80
|
+
return node(v.data.type, () => vectors[v.data.type].splat(compiler.compileValue(v, context)));
|
|
81
|
+
}
|
|
82
|
+
const id = local(v);
|
|
83
|
+
if (id === null) return;
|
|
84
|
+
if (frame.locals.has(id)) return frame.locals.get(id);
|
|
85
|
+
if (frame === root && !assigned.has(id) && !compiler.localScalarRefLayout[context.functionId][id] && isFloat(context.localScalars[id])) {
|
|
86
|
+
const type = context.localScalars[id];
|
|
87
|
+
return node(type, () => vectors[type].splat(compiler.loadLocal(id, context)));
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const reference = (p, frame) => {
|
|
91
|
+
if (p.base.kind === "parameter" && !p.projections.length) return frame.params[p.base.data];
|
|
92
|
+
const projection = p.projections[0];
|
|
93
|
+
if (p.base.kind !== "state" || p.projections.length !== 1 || projection.kind !== "index" ||
|
|
94
|
+
frame.locals.get(local(projection.data.index)) !== induction) return;
|
|
95
|
+
const id = p.base.data;
|
|
96
|
+
const array = compiler.type(compiler.mir.state[id].ty);
|
|
97
|
+
if (array.kind !== "array") return;
|
|
98
|
+
const type = compiler.type(array.data.element).data;
|
|
99
|
+
if (isFloat(type)) return { state: id, type };
|
|
100
|
+
};
|
|
101
|
+
const address = (ref) => m.i32.add(compiler.stateAddress(ref.state, context),
|
|
102
|
+
m.i32.mul(indexValue(), m.i32.const(compiler.scalarSize(ref.type))));
|
|
103
|
+
const load = (ref) => {
|
|
104
|
+
if (!ref || ref.state === undefined) return ref;
|
|
105
|
+
if (!bank.has(ref.state)) bank.set(ref.state, node(ref.type, (half) => m.v128.load(16 * half, 1, address(ref))));
|
|
106
|
+
return bank.get(ref.state);
|
|
107
|
+
};
|
|
108
|
+
const evaluate = ({ kind, data }, frame) => {
|
|
109
|
+
if (kind === "use") return value(data, frame);
|
|
110
|
+
if (kind === "load") return dest(data) === null ? load(reference(data, frame)) : value(data.base, frame);
|
|
111
|
+
if (kind === "cast") {
|
|
112
|
+
const a = value(data.value, frame);
|
|
113
|
+
if (!a) return;
|
|
114
|
+
if (a.type === data.to) return a;
|
|
115
|
+
if (data.to === "f32" && a.type === "i32") return node("f32", () => m.f32x4.convert_i32x4_s(a.get()));
|
|
116
|
+
if (data.to === "f64" && (a.type === "i32" || a.type === "f32")) {
|
|
117
|
+
const cast = a.type === "i32" ? m.f64x2.convert_low_i32x4_s : m.f64x2.promote_low_f32x4;
|
|
118
|
+
return node("f64", (half) => cast(half === 0 ? a.get() :
|
|
119
|
+
m.i8x16.shuffle(a.get(), a.get(), [8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7])));
|
|
120
|
+
}
|
|
121
|
+
if (data.to === "f32" && a.type === "f64") return node("f32", () => {
|
|
122
|
+
const demote = (slot) => m.f32x4.demote_f64x2_zero(m.local.get(slot, binaryen.v128));
|
|
123
|
+
return m.i8x16.shuffle(demote(a.slots[0]), demote(a.slots[1]), [0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23]);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
if (kind === "binary") {
|
|
127
|
+
const a = value(data.lhs, frame);
|
|
128
|
+
const b = value(data.rhs, frame);
|
|
129
|
+
const op = opcodes.get(data.op);
|
|
130
|
+
if (a && b && isFloat(a.type) && a.type === b.type && op) return node(a.type, (half) => vectors[a.type][op](a.get(half), b.get(half)));
|
|
131
|
+
}
|
|
132
|
+
if (kind === "input_load" && frame === root && data.element === null) {
|
|
133
|
+
const port = compiler.inputLayout[data.input];
|
|
134
|
+
if (!isFloat(port.scalar)) return;
|
|
135
|
+
return node(port.scalar, () => vectors[port.scalar].splat(compiler.compileInputLoad(data, context)), true);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
// Inline straight-line calls whose references resolve to the current
|
|
139
|
+
// element, including several references to that same element.
|
|
140
|
+
// Bound code growth across the entire expanded call tree.
|
|
141
|
+
let budget = 128;
|
|
142
|
+
function analyze(list, frame) {
|
|
143
|
+
for (const { kind: { kind, data } } of list) {
|
|
144
|
+
if (--budget < 0) return;
|
|
145
|
+
if (kind === "assign") {
|
|
146
|
+
const rhs = evaluate(data.value, frame);
|
|
147
|
+
if (!rhs) return;
|
|
148
|
+
const id = dest(data.destination);
|
|
149
|
+
if (id !== null) frame.locals.set(id, rhs);
|
|
150
|
+
else {
|
|
151
|
+
const ref = reference(data.destination, frame);
|
|
152
|
+
if (ref?.state === undefined || rhs.type !== ref.type) return;
|
|
153
|
+
bank.set(ref.state, rhs);
|
|
154
|
+
writes.set(ref.state, ref);
|
|
155
|
+
}
|
|
156
|
+
} else if (kind === "call") {
|
|
157
|
+
const callee = compiler.mir.functions[data.function];
|
|
158
|
+
if (callee.attributes?.inline === "never" || callee.locals.some((l) => compiler.type(l.ty).kind !== "scalar")) return;
|
|
159
|
+
const params = data.args.map((arg) => {
|
|
160
|
+
if (arg.kind === "value") return value(arg.data, frame);
|
|
161
|
+
if (arg.kind === "place") return reference(arg.data, frame);
|
|
162
|
+
});
|
|
163
|
+
if (params.some((p) => !p)) return;
|
|
164
|
+
const results = analyze(callee.body.statements, { locals: new Map(), params });
|
|
165
|
+
if (!results || results.length !== data.results.length) return;
|
|
166
|
+
data.results.forEach((id, i) => frame.locals.set(id, results[i]));
|
|
167
|
+
} else if (kind === "return" && frame !== root) {
|
|
168
|
+
const results = data.values.map((v) => value(v, frame));
|
|
169
|
+
return results.every(Boolean) ? results : undefined;
|
|
170
|
+
} else return;
|
|
171
|
+
}
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
174
|
+
if (!analyze(body, root) || !writes.size) return;
|
|
175
|
+
const term = value(loop.term, root);
|
|
176
|
+
if (term?.type !== context.localScalars[loop.sum]) return;
|
|
177
|
+
|
|
178
|
+
return { nodes, writes, bank, root, term, address };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function emitStateReduction(compiler, context, loop, plan, scalarLoop) {
|
|
182
|
+
const { nodes, writes, bank, root, term, address } = plan;
|
|
183
|
+
const m = compiler.module;
|
|
184
|
+
const vectors = { f32: m.f32x4, f64: m.f64x2, i32: m.i32x4 };
|
|
185
|
+
const indexValue = () => compiler.loadLocal(loop.index, context);
|
|
186
|
+
const stride = laneCount;
|
|
187
|
+
const preheader = [];
|
|
188
|
+
const instructions = [];
|
|
189
|
+
for (const n of nodes) {
|
|
190
|
+
const dest = n.invariant ? preheader : instructions;
|
|
191
|
+
for (let half = 0; half < (n.type === "f64" ? 2 : 1); half++) {
|
|
192
|
+
const slot = compiler.allocateGeneratedLocal(context, "v128", "bank.vector");
|
|
193
|
+
n.slots.push(slot);
|
|
194
|
+
dest.push(m.local.set(slot, n.emit(half)));
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
for (const ref of writes.values()) {
|
|
198
|
+
const value = bank.get(ref.state);
|
|
199
|
+
for (let half = 0; half < value.slots.length; half++) {
|
|
200
|
+
instructions.push(m.v128.store(16 * half, 1, address(ref), value.get(half)));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
let reduced = compiler.loadLocal(loop.sum, context);
|
|
204
|
+
for (let lane = 0; lane < stride; lane++) {
|
|
205
|
+
const half = Math.floor(lane / 2);
|
|
206
|
+
const scalar = vectors[term.type].extract_lane(term.get(half), term.type === "f64" ? lane % 2 : lane);
|
|
207
|
+
reduced = m[term.type].add(reduced, scalar);
|
|
208
|
+
}
|
|
209
|
+
instructions.push(compiler.storeLocal(loop.sum, reduced, context));
|
|
210
|
+
instructions.push(compiler.storeLocal(loop.index, m.i32.add(indexValue(), m.i32.const(stride)), context));
|
|
211
|
+
const label = `$onda.bank.vector.${compiler.nextLabel++}`;
|
|
212
|
+
const enough = () => m.i32.lt_u(indexValue(), m.i32.const(loop.last - stride + 2));
|
|
213
|
+
instructions.push(m.br_if(label, enough()));
|
|
214
|
+
// A final complete vector must leave each externally visible local at the
|
|
215
|
+
// value of its last lane. A partial vector runs the original scalar tail.
|
|
216
|
+
const exit = `$onda.bank.done.${compiler.nextLabel++}`;
|
|
217
|
+
const restore = [];
|
|
218
|
+
for (const [id, v] of root.locals) {
|
|
219
|
+
if (id === loop.index) continue;
|
|
220
|
+
restore.push(compiler.storeLocal(id, vectors[v.type].extract_lane(v.get(1), v.type === "f64" ? 1 : 3), context));
|
|
221
|
+
}
|
|
222
|
+
restore.push(compiler.storeLocal(loop.index, m.i32.const(loop.last), context));
|
|
223
|
+
restore.push(compiler.storeLocal(loop.condition, m.i32.const(1), context));
|
|
224
|
+
restore.push(compiler.storeLocal(loop.sum_result, compiler.loadLocal(loop.sum, context), context));
|
|
225
|
+
restore.push(m.br(exit));
|
|
226
|
+
const vectorLoop = m.block(null, [...preheader, m.loop(label, m.block(null, instructions)),
|
|
227
|
+
m.if(m.i32.eq(indexValue(), m.i32.const(loop.last + 1)), m.block(null, restore))]);
|
|
228
|
+
return m.block(exit, [m.if(enough(), vectorLoop), scalarLoop]);
|
|
229
|
+
}
|