@onda-lang/binaryen-web 0.8.38 → 0.8.40
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 +2 -3
- package/api.md +9 -2
- package/package.json +2 -2
- package/src/compiler/lowering.js +3 -1
- package/src/compiler/simd.js +154 -0
package/README.md
CHANGED
|
@@ -44,8 +44,7 @@ execution. These are the complete wasm32-module profile of the generic
|
|
|
44
44
|
owns allocation in linear memory. Metadata contains resolved target/integration facts,
|
|
45
45
|
state/parameter layouts, state-backed control-output offsets, flattened audio-port channels, packed
|
|
46
46
|
event/delegate payload layouts, print log sites, source tables, and all exported ABI names. See
|
|
47
|
-
the
|
|
48
|
-
[print host integration](../../docs/printing.md) references for batch sizing, decoding, formatting,
|
|
47
|
+
the [host output guide](../../docs/host-output.md) for batch sizing, decoding, formatting,
|
|
49
48
|
and overflow handling.
|
|
50
49
|
|
|
51
50
|
Each `onda_event_N` receives a pointer to the ABI's 16-byte wasm32 `EventInput` descriptor before
|
|
@@ -174,7 +173,7 @@ lowers it with Binaryen, and validates the generated Wasm. It also
|
|
|
174
173
|
requires the native build. `npm test` does not.
|
|
175
174
|
|
|
176
175
|
Run `npm run bench` for the reproducible development comparison documented in
|
|
177
|
-
[
|
|
176
|
+
[the benchmark guide](../../scripts/bench/README.md#compare-backend-compilation-and-runtime). Those measurements validate output
|
|
178
177
|
and compare native LLVM JIT/processing with Binaryen compilation, Wasm instantiation, and Wasm
|
|
179
178
|
processing; they require the native Rust/LLVM Onda build and are not universal browser-performance
|
|
180
179
|
claims. The benchmark fails by default if Binaryen/Wasm beats LLVM in any checked scenario; the
|
package/api.md
CHANGED
|
@@ -70,7 +70,7 @@ The returned instance provides:
|
|
|
70
70
|
- `compileSource(source, options?)` and `inspectSourceConstants(source, options?)`.
|
|
71
71
|
- `compileWorkspace(workspace, options?)` and `inspectWorkspaceConstants(workspace, options?)`.
|
|
72
72
|
- `compileProjectImage(bytes, options?)` and `inspectProjectImageConstants(bytes, options?)`.
|
|
73
|
-
- `createProjectImage(sourceGraph, buffers?)`, `inspectProjectImage(bytes)`,
|
|
73
|
+
- `createProjectImage(sourceGraph, buffers?, constants?)`, `inspectProjectImage(bytes)`,
|
|
74
74
|
`loadProjectFiles(files, projectFilePath?)`, and `materializeProjectImage(bytes, names?)`.
|
|
75
75
|
- `encodeBufferAsset(binding)`, `decodeBufferAsset(bytes)`, and `decodeBufferFile(bytes, path?)`.
|
|
76
76
|
- `projectCapabilities()` for supported image, buffer, and standard-library versions.
|
|
@@ -78,6 +78,9 @@ The returned instance provides:
|
|
|
78
78
|
- `dispose()`, an idempotent terminal release of compiler and worker resources.
|
|
79
79
|
|
|
80
80
|
Compile options accept `sampleRate`, `blockSize`, typed compile-constant overrides, and `codegen`.
|
|
81
|
+
Workspaces may carry `constants` in project-manifest form (numbers, booleans, decimal strings for
|
|
82
|
+
`i64`, and primitive arrays). These are project defaults; explicit `options.constants` take
|
|
83
|
+
precedence. Pass the same defaults to `createProjectImage` when exporting an edited workspace.
|
|
81
84
|
The sample rate defaults to 48000 Hz and the block size to 512 frames.
|
|
82
85
|
Code generation can select optimization level `0..4`, shrink level `0..2`, strict or fast math,
|
|
83
86
|
SIMD, loop-containing inlining, and optional WAT emission. A successful compilation returns an
|
|
@@ -136,6 +139,9 @@ endpoints, apply linear, logarithmic, or curved scale, and clamp and snap to the
|
|
|
136
139
|
|
|
137
140
|
### Delegates and print output
|
|
138
141
|
|
|
142
|
+
See the [host output guide](https://onda-lang.org/docs/host-output/#javascript-and-web-audio)
|
|
143
|
+
for batch sizing, decoding, formatting, and Web Audio listener examples.
|
|
144
|
+
|
|
139
145
|
Complete wasm32 processor exports receive an optional execution-output descriptor containing
|
|
140
146
|
independently nullable delegate and print batches. Allocate all descriptors and storage before
|
|
141
147
|
real-time execution. Their linear-memory regions must be mutually disjoint and must not overlap any
|
|
@@ -156,7 +162,7 @@ chronological output stream.
|
|
|
156
162
|
|
|
157
163
|
Batch capacity is host policy. `overflowCount` reports whole records that did not fit; it is not a
|
|
158
164
|
byte count. Consume the returned storage before reusing it for another init, process, or event call.
|
|
159
|
-
See the [language guide](https://onda-lang.org/docs/language/#delegates) for authored semantics and
|
|
165
|
+
See the [language guide](https://onda-lang.org/docs/language/#delegates-and-when) for authored semantics and
|
|
160
166
|
the [processor API](https://onda-lang.org/docs/processor-api/#call-scoped-execution-output) for
|
|
161
167
|
physical record layout.
|
|
162
168
|
|
|
@@ -425,6 +431,7 @@ OndaProcessorInitMode
|
|
|
425
431
|
OndaProcessorMetadata
|
|
426
432
|
OndaProjectBufferInfo
|
|
427
433
|
OndaProjectCapabilities
|
|
434
|
+
OndaProjectConstant
|
|
428
435
|
OndaProjectImageInfo
|
|
429
436
|
OndaProjectMaterialization
|
|
430
437
|
OndaResolvedCompileConstValue
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onda-lang/binaryen-web",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.40",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Binaryen backend for versioned Onda MIR in browsers and Node.js",
|
|
6
6
|
"license": "MIT",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"verify-mir": "node ./scripts/verify-mir.mjs"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@onda-lang/processor-abi": "0.8.
|
|
49
|
+
"@onda-lang/processor-abi": "0.8.40",
|
|
50
50
|
"binaryen": "130.0.0"
|
|
51
51
|
},
|
|
52
52
|
"publishConfig": {
|
package/src/compiler/lowering.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import binaryen from "binaryen";
|
|
2
|
+
import { vectorizeSampleLoop } from "./simd.js";
|
|
2
3
|
import { MirCompilerCore } from "./core.js";
|
|
3
4
|
import {
|
|
4
5
|
DELEGATE_RECORD_HEADER_SIZE,
|
|
@@ -94,12 +95,13 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
94
95
|
const body = this.compileBlock(data.body, context);
|
|
95
96
|
context.breakLabels.pop();
|
|
96
97
|
context.continueLabels.pop();
|
|
97
|
-
|
|
98
|
+
const scalarLoop = this.module.block(breakLabel, [
|
|
98
99
|
this.module.loop(
|
|
99
100
|
continueLabel,
|
|
100
101
|
this.module.block(null, [body, this.module.br(continueLabel)]),
|
|
101
102
|
),
|
|
102
103
|
]);
|
|
104
|
+
return vectorizeSampleLoop(this, statement, context, scalarLoop) ?? scalarLoop;
|
|
103
105
|
}
|
|
104
106
|
case "break":
|
|
105
107
|
return this.module.br(this.currentLabel(context.breakLabels, "break"));
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Vectorize independent sample iterations. The proof admits only straight-line
|
|
2
|
+
// float arithmetic, invariant reads, and contiguous audio I/O. Stateful loops,
|
|
3
|
+
// calls, gathers, and loop-carried values retain scalar lowering.
|
|
4
|
+
import binaryen from "binaryen";
|
|
5
|
+
import { POINTER_GLOBALS } from "./shared.js";
|
|
6
|
+
|
|
7
|
+
const local = (value) => value?.kind === "local" ? value.data : null;
|
|
8
|
+
const destination = (data) => data?.destination?.projections?.length === 0
|
|
9
|
+
? local(data.destination.base) : null;
|
|
10
|
+
const isOne = (value) => value?.kind === "constant"
|
|
11
|
+
&& value.data.type === "i32" && value.data.value === 1;
|
|
12
|
+
const same = (a, b) => local(a) !== null && local(a) === local(b);
|
|
13
|
+
|
|
14
|
+
export function vectorizeSampleLoop(compiler, statement, context, scalarLoop) {
|
|
15
|
+
if (!compiler.options.simd || context.function.kind.kind !== "process") return null;
|
|
16
|
+
const loop = statement.kind.data.body.statements;
|
|
17
|
+
if (loop.length !== 2 || loop[0].kind.kind !== "assign" || loop[1].kind.kind !== "if") return null;
|
|
18
|
+
const test = loop[0].kind.data;
|
|
19
|
+
const branch = loop[1].kind.data;
|
|
20
|
+
const comparison = test.value;
|
|
21
|
+
if (comparison.kind !== "compare" || comparison.data.op !== "less"
|
|
22
|
+
|| local(comparison.data.lhs) === null || destination(test) !== local(branch.condition)
|
|
23
|
+
|| branch.else_block.statements.length !== 1
|
|
24
|
+
|| branch.else_block.statements[0].kind.kind !== "break") return null;
|
|
25
|
+
const index = local(comparison.data.lhs);
|
|
26
|
+
const limit = comparison.data.rhs;
|
|
27
|
+
const body = branch.then_block.statements;
|
|
28
|
+
const step = body.at(-1)?.kind;
|
|
29
|
+
if (step?.kind !== "assign" || destination(step.data) !== index
|
|
30
|
+
|| step.data.value.kind !== "binary" || step.data.value.data.op !== "add"
|
|
31
|
+
|| local(step.data.value.data.lhs) !== index || !isOne(step.data.value.data.rhs)) return null;
|
|
32
|
+
// Require the actual segment length, not an arbitrary integer loop bound.
|
|
33
|
+
const limitDefinitions = context.function.body.statements.filter((item) =>
|
|
34
|
+
item.kind.kind === "assign" && destination(item.kind.data) === local(limit));
|
|
35
|
+
if (limitDefinitions.length !== 1) return null;
|
|
36
|
+
const limitLoad = limitDefinitions[0].kind.data.value;
|
|
37
|
+
if (limitLoad.kind !== "load" || limitLoad.data.base.kind !== "parameter"
|
|
38
|
+
|| limitLoad.data.base.data !== 1 || limitLoad.data.projections.length !== 0) return null;
|
|
39
|
+
const frameStatement = body[0]?.kind;
|
|
40
|
+
if (frameStatement?.kind !== "assign" || frameStatement.data.value.kind !== "process_frame"
|
|
41
|
+
|| local(frameStatement.data.value.data.offset) !== index) return null;
|
|
42
|
+
const frame = destination(frameStatement.data);
|
|
43
|
+
const operations = body.slice(1, -1);
|
|
44
|
+
const written = new Set();
|
|
45
|
+
let scalar;
|
|
46
|
+
const ports = new Map();
|
|
47
|
+
const addPort = (kind, data) => {
|
|
48
|
+
const layout = kind === "input" ? compiler.inputLayout[data.input] : compiler.outputLayout[data.output];
|
|
49
|
+
if (!layout || layout.scalar !== scalar || data.element !== null || local(data.frame) !== frame) return false;
|
|
50
|
+
ports.set(`${kind}:${kind === "input" ? data.input : data.output}`, { kind, data, layout });
|
|
51
|
+
return true;
|
|
52
|
+
};
|
|
53
|
+
for (const operation of operations) {
|
|
54
|
+
if (operation.kind.kind === "assign") {
|
|
55
|
+
const id = destination(operation.kind.data);
|
|
56
|
+
if (id === null || id === index || id === frame || compiler.localScalarRefLayout[context.functionId][id]) return null;
|
|
57
|
+
const type = context.localScalars[id];
|
|
58
|
+
if (!['f32', 'f64'].includes(type) || (scalar && scalar !== type)) return null;
|
|
59
|
+
scalar = type;
|
|
60
|
+
written.add(id);
|
|
61
|
+
} else if (operation.kind.kind !== "output_store") return null;
|
|
62
|
+
}
|
|
63
|
+
if (!scalar) return null;
|
|
64
|
+
const defined = new Set();
|
|
65
|
+
const validValue = (value) => value?.kind === "constant" ? value.data.type === scalar
|
|
66
|
+
: local(value) !== null && context.localScalars[value.data] === scalar
|
|
67
|
+
&& (!written.has(value.data) || defined.has(value.data));
|
|
68
|
+
let stores = 0;
|
|
69
|
+
for (const operation of operations) {
|
|
70
|
+
const { kind, data } = operation.kind;
|
|
71
|
+
if (kind === "output_store") {
|
|
72
|
+
if (!validValue(data.value) || !addPort("output", data)) return null;
|
|
73
|
+
stores += 1;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const value = data.value;
|
|
77
|
+
switch (value.kind) {
|
|
78
|
+
case "use": if (!validValue(value.data)) return null; break;
|
|
79
|
+
case "binary":
|
|
80
|
+
if (!['add', 'subtract', 'multiply', 'divide'].includes(value.data.op)
|
|
81
|
+
|| !validValue(value.data.lhs) || !validValue(value.data.rhs)) return null;
|
|
82
|
+
break;
|
|
83
|
+
case "input_load": if (!addPort("input", value.data)) return null; break;
|
|
84
|
+
// Parameters and state cannot change in the admitted loop. A projected
|
|
85
|
+
// load could depend on the induction variable, so leave it scalar.
|
|
86
|
+
case "load":
|
|
87
|
+
if (!['state', 'param', 'parameter'].includes(value.data.base.kind) || value.data.projections.length) return null;
|
|
88
|
+
break;
|
|
89
|
+
default: return null;
|
|
90
|
+
}
|
|
91
|
+
defined.add(destination(data));
|
|
92
|
+
}
|
|
93
|
+
if (!stores) return null;
|
|
94
|
+
|
|
95
|
+
const m = compiler.module;
|
|
96
|
+
const lanes = scalar === "f32" ? 4 : 2;
|
|
97
|
+
const vector = scalar === "f32" ? m.f32x4 : m.f64x2;
|
|
98
|
+
const locals = new Map([...written].map((id) => [id, compiler.allocateGeneratedLocal(context, "v128", "sample.vector")]));
|
|
99
|
+
const getVector = (value) => locals.has(local(value))
|
|
100
|
+
? m.local.get(locals.get(value.data), binaryen.v128)
|
|
101
|
+
: vector.splat(compiler.compileValue(value, context));
|
|
102
|
+
const pointers = new Map([...ports].map(([key, { kind, data, layout }]) => [key, () => compiler.audioChannelPointer(
|
|
103
|
+
kind === "input" ? POINTER_GLOBALS.inputs : POINTER_GLOBALS.outputs,
|
|
104
|
+
layout, data.element, data.bounds, context,
|
|
105
|
+
)]));
|
|
106
|
+
const address = (kind, data) => m.i32.add(
|
|
107
|
+
pointers.get(`${kind}:${kind === "input" ? data.input : data.output}`)(),
|
|
108
|
+
m.i32.mul(compiler.compileProcessFrame({ offset: comparison.data.lhs }, context), m.i32.const(compiler.scalarSize(scalar))),
|
|
109
|
+
);
|
|
110
|
+
const vectorBody = [];
|
|
111
|
+
const binary = { add: 'add', subtract: 'sub', multiply: 'mul', divide: 'div' };
|
|
112
|
+
for (const operation of operations) {
|
|
113
|
+
const { kind, data } = operation.kind;
|
|
114
|
+
if (kind === "output_store") {
|
|
115
|
+
vectorBody.push(m.v128.store(0, 1, address("output", data), getVector(data.value)));
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const value = data.value;
|
|
119
|
+
let expression;
|
|
120
|
+
if (value.kind === "use") expression = getVector(value.data);
|
|
121
|
+
else if (value.kind === "binary") expression = vector[binary[value.data.op]](getVector(value.data.lhs), getVector(value.data.rhs));
|
|
122
|
+
else if (value.kind === "input_load") expression = m.v128.load(0, 1, address("input", value.data));
|
|
123
|
+
else expression = vector.splat(compiler.compileRvalue(value, scalar, context));
|
|
124
|
+
vectorBody.push(m.local.set(locals.get(destination(data)), expression));
|
|
125
|
+
}
|
|
126
|
+
// Preserve locals observed by block-post code, including the last frame.
|
|
127
|
+
for (const [id, vectorLocal] of locals) {
|
|
128
|
+
vectorBody.push(compiler.storePlace({ base: { kind: "local", data: id }, projections: [] },
|
|
129
|
+
vector.extract_lane(m.local.get(vectorLocal, binaryen.v128), lanes - 1), scalar, context));
|
|
130
|
+
}
|
|
131
|
+
vectorBody.push(compiler.storePlace(frameStatement.data.destination,
|
|
132
|
+
m.i32.add(compiler.compileProcessFrame({ offset: comparison.data.lhs }, context), m.i32.const(lanes - 1)), "i32", context));
|
|
133
|
+
vectorBody.push(compiler.storePlace(step.data.destination,
|
|
134
|
+
m.i32.add(compiler.compileValue(comparison.data.lhs, context), m.i32.const(lanes)), "i32", context));
|
|
135
|
+
const label = `$onda.sample.vector.${compiler.nextLabel++}`;
|
|
136
|
+
const vectorLoop = m.loop(label, m.if(
|
|
137
|
+
m.i32.le_s(compiler.compileValue(comparison.data.lhs, context), m.i32.sub(compiler.compileValue(limit, context), m.i32.const(lanes))),
|
|
138
|
+
m.block(null, [...vectorBody, m.br(label)]),
|
|
139
|
+
));
|
|
140
|
+
// Exact in-place buffers preserve per-lane order. Partial overlap can carry
|
|
141
|
+
// data between iterations, so version the loop with a disjointness check.
|
|
142
|
+
const accessed = [...ports.keys()];
|
|
143
|
+
let safe = m.i32.const(1);
|
|
144
|
+
for (let i = 0; i < accessed.length; i += 1) {
|
|
145
|
+
for (let j = i + 1; j < accessed.length; j += 1) {
|
|
146
|
+
if (accessed[i].startsWith("input:") && accessed[j].startsWith("input:")) continue;
|
|
147
|
+
const a = pointers.get(accessed[i]);
|
|
148
|
+
const b = pointers.get(accessed[j]);
|
|
149
|
+
const distance = m.select(m.i32.lt_u(a(), b()), m.i32.sub(b(), a()), m.i32.sub(a(), b()), binaryen.i32);
|
|
150
|
+
safe = m.i32.and(safe, m.i32.or(m.i32.eq(a(), b()), m.i32.ge_u(distance, m.i32.const(compiler.mir.config.block_size * compiler.scalarSize(scalar)))));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return m.block(null, [m.if(safe, vectorLoop), scalarLoop]);
|
|
154
|
+
}
|