@gmitch215/folio 1.1.0 → 1.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/README.md +78 -810
- package/dist/boot.d.ts +1 -0
- package/dist/boot.js +1 -0
- package/dist/browser/index.d.ts +73 -0
- package/dist/browser/index.js +277 -0
- package/dist/browser.d.ts +1 -68
- package/dist/browser.js +1 -274
- package/dist/continuation.d.ts +1 -0
- package/dist/continuation.js +1 -0
- package/dist/{compiler.d.ts → execution/compiler.d.ts} +1 -1
- package/dist/{compiler.js → execution/compiler.js} +5 -12
- package/dist/{coordinator.d.ts → execution/coordinator.d.ts} +1 -1
- package/dist/{coordinator.js → execution/coordinator.js} +5 -5
- package/dist/{extract.d.ts → execution/extract.d.ts} +3 -13
- package/dist/{extract.js → execution/extract.js} +7 -8
- package/dist/{fold.d.ts → execution/fold.d.ts} +15 -3
- package/dist/{guards.d.ts → execution/guards.d.ts} +2 -1
- package/dist/execution/types.d.ts +221 -0
- package/dist/{affine-state.d.ts → folds/affine-state.d.ts} +18 -47
- package/dist/{affine-state.js → folds/affine-state.js} +18 -44
- package/dist/{affine.d.ts → folds/affine.d.ts} +12 -24
- package/dist/{affine.js → folds/affine.js} +8 -17
- package/dist/{boundaries.d.ts → folds/boundaries.d.ts} +7 -18
- package/dist/{boundaries.js → folds/boundaries.js} +5 -13
- package/dist/{certified.d.ts → folds/certified.d.ts} +13 -24
- package/dist/{certified.js → folds/certified.js} +12 -26
- package/dist/{certify.d.ts → folds/certify.d.ts} +11 -22
- package/dist/{certify.js → folds/certify.js} +24 -41
- package/dist/{memo.d.ts → footprints/memo.d.ts} +8 -33
- package/dist/{memo.js → footprints/memo.js} +8 -2
- package/dist/{ranges.js → footprints/ranges.js} +1 -1
- package/dist/{read-scan.js → footprints/read-scan.js} +1 -1
- package/dist/{read-windows.d.ts → footprints/read-windows.d.ts} +6 -32
- package/dist/{read-windows.js → footprints/read-windows.js} +8 -7
- package/dist/index.d.ts +16 -4
- package/dist/index.js +14 -2
- package/dist/{iteration-admission.d.ts → iteration/admission.d.ts} +2 -38
- package/dist/{iteration.d.ts → iteration/index.d.ts} +8 -98
- package/dist/{iteration.js → iteration/index.js} +28 -28
- package/dist/{inline.js → iteration/inline.js} +2 -9
- package/dist/{iteration-owner.d.ts → iteration/owner.d.ts} +3 -3
- package/dist/{iteration-owner.js → iteration/owner.js} +4 -3
- package/dist/{iteration-worker.js → iteration/worker.js} +3 -3
- package/dist/{managed.d.ts → managed/index.d.ts} +3 -3
- package/dist/{managed.js → managed/index.js} +7 -9
- package/dist/managed/outbox.d.ts +128 -0
- package/dist/managed/outbox.js +156 -0
- package/dist/managed/pool.d.ts +221 -0
- package/dist/managed/pool.js +310 -0
- package/dist/managed/tasks.d.ts +154 -0
- package/dist/managed/tasks.js +183 -0
- package/dist/{bake.d.ts → memory/bake.d.ts} +12 -6
- package/dist/{bake.js → memory/bake.js} +9 -7
- package/dist/memory/continuation.d.ts +141 -0
- package/dist/memory/continuation.js +495 -0
- package/dist/memory/elastic-peer.d.ts +20 -0
- package/dist/memory/elastic-peer.js +39 -0
- package/dist/memory/elastic-sql.d.ts +11 -0
- package/dist/memory/elastic-sql.js +35 -0
- package/dist/memory/elastic.d.ts +130 -0
- package/dist/memory/elastic.js +475 -0
- package/dist/memory/growth-governor.d.ts +16 -0
- package/dist/memory/growth-governor.js +42 -0
- package/dist/memory/imports.d.ts +10 -0
- package/dist/memory/imports.js +89 -0
- package/dist/{iteration-layout.d.ts → memory/iteration-layout.d.ts} +1 -1
- package/dist/{pages.d.ts → memory/pages.d.ts} +3 -3
- package/dist/memory.d.ts +5 -10
- package/dist/memory.js +5 -89
- package/dist/{context.d.ts → node/context.d.ts} +1 -1
- package/dist/{host.d.ts → node/host.d.ts} +1 -1
- package/dist/{host.js → node/host.js} +2 -2
- package/dist/{node.d.ts → node/index.d.ts} +4 -49
- package/dist/{node.js → node/index.js} +16 -17
- package/dist/{tasks.d.ts → node/tasks.d.ts} +2 -13
- package/dist/{worker.js → node/worker.js} +2 -2
- package/dist/{ownership.js → ownership/index.js} +7 -10
- package/dist/{ownership-redirect.d.ts → ownership/redirect.d.ts} +2 -2
- package/dist/{ownership-redirect.js → ownership/redirect.js} +2 -2
- package/dist/{ownership-slices.d.ts → ownership/slices.d.ts} +3 -3
- package/dist/{ownership-slices.js → ownership/slices.js} +3 -3
- package/dist/{frames.d.ts → remote/frames.d.ts} +1 -1
- package/dist/{frames.js → remote/frames.js} +2 -2
- package/dist/{remote.d.ts → remote/index.d.ts} +7 -2
- package/dist/{remote.js → remote/index.js} +7 -2
- package/dist/{router.d.ts → routing/index.d.ts} +3 -3
- package/dist/{router.js → routing/index.js} +19 -15
- package/dist/{router-worker.js → routing/worker.js} +1 -1
- package/dist/{slices.d.ts → slices/index.d.ts} +2 -2
- package/dist/{slices.js → slices/index.js} +5 -5
- package/dist/slices/worker.d.ts +1 -0
- package/dist/{slices-worker.js → slices/worker.js} +2 -2
- package/dist/workers.d.ts +5 -0
- package/dist/workers.js +5 -0
- package/package.json +30 -80
- /package/dist/{browser-worker.d.ts → browser/worker.d.ts} +0 -0
- /package/dist/{browser-worker.js → browser/worker.js} +0 -0
- /package/dist/{fold.js → execution/fold.js} +0 -0
- /package/dist/{guards.js → execution/guards.js} +0 -0
- /package/dist/{import-order.d.ts → execution/import-order.d.ts} +0 -0
- /package/dist/{import-order.js → execution/import-order.js} +0 -0
- /package/dist/{runs.d.ts → execution/runs.d.ts} +0 -0
- /package/dist/{runs.js → execution/runs.js} +0 -0
- /package/dist/{transaction.d.ts → execution/transaction.d.ts} +0 -0
- /package/dist/{transaction.js → execution/transaction.js} +0 -0
- /package/dist/{iteration-worker.d.ts → execution/types.js} +0 -0
- /package/dist/{ranges.d.ts → footprints/ranges.d.ts} +0 -0
- /package/dist/{read-scan.d.ts → footprints/read-scan.d.ts} +0 -0
- /package/dist/{iteration-admission.js → iteration/admission.js} +0 -0
- /package/dist/{inline.d.ts → iteration/inline.d.ts} +0 -0
- /package/dist/{worker.d.ts → iteration/worker.d.ts} +0 -0
- /package/dist/{iteration-layout.js → memory/iteration-layout.js} +0 -0
- /package/dist/{lease.d.ts → memory/lease.d.ts} +0 -0
- /package/dist/{lease.js → memory/lease.js} +0 -0
- /package/dist/{pages.js → memory/pages.js} +0 -0
- /package/dist/{context.js → node/context.js} +0 -0
- /package/dist/{tasks.js → node/tasks.js} +0 -0
- /package/dist/{router-worker.d.ts → node/worker.d.ts} +0 -0
- /package/dist/{ownership.d.ts → ownership/index.d.ts} +0 -0
- /package/dist/{values.d.ts → remote/values.d.ts} +0 -0
- /package/dist/{values.js → remote/values.js} +0 -0
- /package/dist/{slices-worker.d.ts → routing/worker.d.ts} +0 -0
package/dist/browser.js
CHANGED
|
@@ -1,274 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export class BrowserFolio {
|
|
3
|
-
exports;
|
|
4
|
-
memory;
|
|
5
|
-
diagnostics;
|
|
6
|
-
#program;
|
|
7
|
-
#workers = [];
|
|
8
|
-
#listeners = [];
|
|
9
|
-
#minimumWork;
|
|
10
|
-
#sequence = 0;
|
|
11
|
-
#closed = false;
|
|
12
|
-
#busy = false;
|
|
13
|
-
#failure;
|
|
14
|
-
#active;
|
|
15
|
-
#closing;
|
|
16
|
-
constructor(program, options) {
|
|
17
|
-
this.#program = program;
|
|
18
|
-
this.#minimumWork = options.minimumParallelWork ?? 100_000;
|
|
19
|
-
if (!Number.isFinite(this.#minimumWork) || this.#minimumWork < 0)
|
|
20
|
-
throw new RangeError('minimumParallelWork must be finite and nonnegative');
|
|
21
|
-
this.memory = options.memory;
|
|
22
|
-
const descriptor = program.memories?.length === 1 ? program.memories[0] : undefined;
|
|
23
|
-
const original = options.imports ?? {};
|
|
24
|
-
const imports = descriptor && this.memory
|
|
25
|
-
? new Proxy(original, {
|
|
26
|
-
get: (target, module) => {
|
|
27
|
-
const namespace = Reflect.get(target, module, target);
|
|
28
|
-
if (module !== descriptor.module)
|
|
29
|
-
return namespace;
|
|
30
|
-
return new Proxy(namespace ?? {}, {
|
|
31
|
-
get: (bindings, name) => name === descriptor.name
|
|
32
|
-
? this.memory
|
|
33
|
-
: Reflect.get(bindings, name, bindings)
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
})
|
|
37
|
-
: original;
|
|
38
|
-
this.exports = new WebAssembly.Instance(program.module, imports).exports;
|
|
39
|
-
this.diagnostics = Object.freeze([...program.reasons]);
|
|
40
|
-
}
|
|
41
|
-
/** Opens the original instance and attaches only compiler-admitted, initialization-free workers. */
|
|
42
|
-
static async open(program, options = {}) {
|
|
43
|
-
const width = options.workers ?? 4;
|
|
44
|
-
if (!Number.isInteger(width) || width < 1 || width > 32)
|
|
45
|
-
throw new RangeError('workers must be an integer from 1 through 32');
|
|
46
|
-
const pool = new BrowserFolio(program, options);
|
|
47
|
-
const descriptor = program.memories?.length === 1 ? program.memories[0] : undefined;
|
|
48
|
-
const bindings = WebAssembly.Module.imports(program.module);
|
|
49
|
-
const eligible = width > 1 &&
|
|
50
|
-
program.reasons.length === 0 &&
|
|
51
|
-
Object.keys(program.native ?? {}).some((name) => Object.hasOwn(program.loops, name)) &&
|
|
52
|
-
descriptor?.shared &&
|
|
53
|
-
!descriptor.memory64 &&
|
|
54
|
-
bindings.length === 1 &&
|
|
55
|
-
bindings[0].kind === 'memory' &&
|
|
56
|
-
bindings[0].module === descriptor.module &&
|
|
57
|
-
bindings[0].name === descriptor.name &&
|
|
58
|
-
pool.memory &&
|
|
59
|
-
typeof SharedArrayBuffer !== 'undefined' &&
|
|
60
|
-
pool.memory.buffer.byteLength >= 4 &&
|
|
61
|
-
pool.memory.buffer instanceof SharedArrayBuffer;
|
|
62
|
-
if (!eligible)
|
|
63
|
-
return pool;
|
|
64
|
-
const memory = pool.memory;
|
|
65
|
-
const words = new Int32Array(memory.buffer);
|
|
66
|
-
const saved = Atomics.load(words, 0);
|
|
67
|
-
try {
|
|
68
|
-
for (let index = 0; index < width; index++) {
|
|
69
|
-
const worker = options.workerFactory
|
|
70
|
-
? options.workerFactory(index)
|
|
71
|
-
: new Worker(options.workerURL ?? new URL('./browser-worker.js', import.meta.url), {
|
|
72
|
-
type: 'module'
|
|
73
|
-
});
|
|
74
|
-
pool.#workers.push(worker);
|
|
75
|
-
const fail = () => {
|
|
76
|
-
pool.#failure ??= new Error('browser worker failed; shared arena requires checkpoint recovery');
|
|
77
|
-
};
|
|
78
|
-
worker.addEventListener('error', fail);
|
|
79
|
-
worker.addEventListener('messageerror', fail);
|
|
80
|
-
pool.#listeners.push(() => {
|
|
81
|
-
worker.removeEventListener('error', fail);
|
|
82
|
-
worker.removeEventListener('messageerror', fail);
|
|
83
|
-
});
|
|
84
|
-
const before = (0x46100000 + index) | 0;
|
|
85
|
-
const after = (before ^ 0x00ffffff) | 0;
|
|
86
|
-
Atomics.store(words, 0, before);
|
|
87
|
-
const result = await pool.#request(worker, {
|
|
88
|
-
action: 'attach',
|
|
89
|
-
module: program.module,
|
|
90
|
-
memory,
|
|
91
|
-
importModule: descriptor.module,
|
|
92
|
-
importName: descriptor.name,
|
|
93
|
-
before,
|
|
94
|
-
after
|
|
95
|
-
});
|
|
96
|
-
if (result !== before || Atomics.load(words, 0) !== after)
|
|
97
|
-
throw new Error('browser worker shared-memory alias handshake failed');
|
|
98
|
-
}
|
|
99
|
-
return pool;
|
|
100
|
-
}
|
|
101
|
-
catch (error) {
|
|
102
|
-
pool.#terminate();
|
|
103
|
-
throw error;
|
|
104
|
-
}
|
|
105
|
-
finally {
|
|
106
|
-
Atomics.store(words, 0, saved);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
/** Describes counted-loop coverage; generalized control-flow extraction remains at the owner. */
|
|
110
|
-
inspect(entry) {
|
|
111
|
-
return {
|
|
112
|
-
mechanism: 'counted-loop',
|
|
113
|
-
loop: Object.hasOwn(this.#program.loops, entry),
|
|
114
|
-
nativeGuard: Object.hasOwn(this.#program.native ?? {}, entry),
|
|
115
|
-
workers: this.#workers.length
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
#request(worker, message) {
|
|
119
|
-
const sequence = ++this.#sequence;
|
|
120
|
-
return new Promise((resolve, reject) => {
|
|
121
|
-
const cleanup = () => {
|
|
122
|
-
worker.removeEventListener('message', done);
|
|
123
|
-
worker.removeEventListener('error', fail);
|
|
124
|
-
worker.removeEventListener('messageerror', fail);
|
|
125
|
-
};
|
|
126
|
-
const fail = (event) => {
|
|
127
|
-
cleanup();
|
|
128
|
-
reject(new Error(event.message || 'browser worker failed'));
|
|
129
|
-
};
|
|
130
|
-
const done = (event) => {
|
|
131
|
-
if (event.data?.sequence !== sequence)
|
|
132
|
-
return;
|
|
133
|
-
cleanup();
|
|
134
|
-
if (event.data.error)
|
|
135
|
-
reject(new Error(event.data.error));
|
|
136
|
-
else
|
|
137
|
-
resolve(event.data.value);
|
|
138
|
-
};
|
|
139
|
-
worker.addEventListener('message', done);
|
|
140
|
-
worker.addEventListener('error', fail);
|
|
141
|
-
worker.addEventListener('messageerror', fail);
|
|
142
|
-
try {
|
|
143
|
-
worker.postMessage({ ...message, sequence });
|
|
144
|
-
}
|
|
145
|
-
catch (error) {
|
|
146
|
-
cleanup();
|
|
147
|
-
reject(error);
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
#disjoint(calls, proved, plan) {
|
|
152
|
-
const footprints = calls.map((call) => proved.accesses.map((access) => {
|
|
153
|
-
const first = Number(call.args[access.parameter]) >>> 0;
|
|
154
|
-
const count = Number(call.args[plan.countParameter]) >>> 0;
|
|
155
|
-
const step = plan.steps.find((item) => item.parameter === access.parameter)?.step ?? 0;
|
|
156
|
-
const last = first + (count - 1) * step;
|
|
157
|
-
return {
|
|
158
|
-
start: Math.min(first, last) + access.offset,
|
|
159
|
-
end: Math.max(first, last) + access.offset + access.bytes,
|
|
160
|
-
write: access.write,
|
|
161
|
-
wrap: last < 0 || last > 0xffffffff
|
|
162
|
-
};
|
|
163
|
-
}));
|
|
164
|
-
for (let index = 0; index < footprints.length; index++) {
|
|
165
|
-
for (const range of footprints[index]) {
|
|
166
|
-
if (range.wrap || range.start < 0 || range.end > this.memory.buffer.byteLength)
|
|
167
|
-
return false;
|
|
168
|
-
for (const other of footprints.slice(0, index).flat())
|
|
169
|
-
if ((range.write || other.write) &&
|
|
170
|
-
range.start < other.end &&
|
|
171
|
-
other.start < range.end)
|
|
172
|
-
return false;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return true;
|
|
176
|
-
}
|
|
177
|
-
/** Derives guarded regions from one ordinary call, then joins every lane before returning. */
|
|
178
|
-
run(entry, args = [], options = {}) {
|
|
179
|
-
if (this.#closed)
|
|
180
|
-
return Promise.reject(new Error('folio is closed'));
|
|
181
|
-
if (this.#failure)
|
|
182
|
-
return Promise.reject(this.#failure);
|
|
183
|
-
if (this.#busy)
|
|
184
|
-
return Promise.reject(new Error('memory already has an active run'));
|
|
185
|
-
this.#busy = true;
|
|
186
|
-
const running = this.#run(entry, args, options);
|
|
187
|
-
this.#active = running;
|
|
188
|
-
running
|
|
189
|
-
.finally(() => {
|
|
190
|
-
this.#busy = false;
|
|
191
|
-
if (this.#active === running)
|
|
192
|
-
this.#active = undefined;
|
|
193
|
-
})
|
|
194
|
-
.catch(() => { });
|
|
195
|
-
return running;
|
|
196
|
-
}
|
|
197
|
-
async #run(entry, args, options) {
|
|
198
|
-
const plan = Object.hasOwn(this.#program.loops, entry)
|
|
199
|
-
? this.#program.loops[entry]
|
|
200
|
-
: undefined;
|
|
201
|
-
const proved = Object.hasOwn(this.#program.native ?? {}, entry)
|
|
202
|
-
? this.#program.native[entry]
|
|
203
|
-
: undefined;
|
|
204
|
-
const numeric = args.every((arg) => typeof arg === 'number');
|
|
205
|
-
const count = plan && numeric ? Number(args[plan.countParameter]) >>> 0 : 0;
|
|
206
|
-
const work = numeric
|
|
207
|
-
? proved?.workParameters.reduce((value, parameter) => value * (Number(args[parameter]) >>> 0), count)
|
|
208
|
-
: undefined;
|
|
209
|
-
let reason = options.parallel === 'off'
|
|
210
|
-
? 'requested'
|
|
211
|
-
: !numeric
|
|
212
|
-
? 'opaque-input'
|
|
213
|
-
: !plan || !proved || this.#workers.length < 2
|
|
214
|
-
? 'no-parallel-plan'
|
|
215
|
-
: count < 2
|
|
216
|
-
? 'single-region'
|
|
217
|
-
: work !== undefined && work < this.#minimumWork
|
|
218
|
-
? 'small-workload'
|
|
219
|
-
: undefined;
|
|
220
|
-
const calls = [];
|
|
221
|
-
if (!reason && plan && proved) {
|
|
222
|
-
const regions = Math.min(count, this.#workers.length);
|
|
223
|
-
let begin = 0;
|
|
224
|
-
for (let index = 0; index < regions; index++) {
|
|
225
|
-
const end = Math.floor(((index + 1) * count) / regions);
|
|
226
|
-
const parameters = [...args];
|
|
227
|
-
parameters[plan.countParameter] = end - begin;
|
|
228
|
-
for (const { parameter, step } of plan.steps)
|
|
229
|
-
parameters[parameter] =
|
|
230
|
-
((Number(args[parameter]) | 0) + Math.imul(begin, step)) | 0;
|
|
231
|
-
calls.push({ export: entry, args: parameters });
|
|
232
|
-
begin = end;
|
|
233
|
-
}
|
|
234
|
-
if (!this.#disjoint(calls, proved, plan))
|
|
235
|
-
reason = 'guard-failed';
|
|
236
|
-
}
|
|
237
|
-
if (reason) {
|
|
238
|
-
const value = this.exports[entry](...args);
|
|
239
|
-
return { values: [value], mode: 'serial', workers: 0, regions: 1, reason };
|
|
240
|
-
}
|
|
241
|
-
const outcomes = await Promise.allSettled(calls.map((call, index) => this.#request(this.#workers[index], { action: 'run', call })));
|
|
242
|
-
if (outcomes.some((outcome) => outcome.status === 'rejected')) {
|
|
243
|
-
this.#failure = new Error('native region failed; shared arena requires checkpoint recovery');
|
|
244
|
-
this.#terminate();
|
|
245
|
-
throw this.#failure;
|
|
246
|
-
}
|
|
247
|
-
return {
|
|
248
|
-
values: [undefined],
|
|
249
|
-
mode: 'native-shared',
|
|
250
|
-
workers: calls.length,
|
|
251
|
-
regions: calls.length
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
#terminate() {
|
|
255
|
-
for (const detach of this.#listeners)
|
|
256
|
-
detach();
|
|
257
|
-
this.#listeners.length = 0;
|
|
258
|
-
for (const worker of this.#workers)
|
|
259
|
-
worker.terminate();
|
|
260
|
-
this.#workers.length = 0;
|
|
261
|
-
}
|
|
262
|
-
/** Joins an active finite call before terminating dedicated workers. */
|
|
263
|
-
close() {
|
|
264
|
-
this.#closed = true;
|
|
265
|
-
return (this.#closing ??= (async () => {
|
|
266
|
-
await this.#active?.catch(() => { });
|
|
267
|
-
this.#terminate();
|
|
268
|
-
})());
|
|
269
|
-
}
|
|
270
|
-
/** Joins execution and releases workers for `await using`. */
|
|
271
|
-
[Symbol.asyncDispose]() {
|
|
272
|
-
return this.close();
|
|
273
|
-
}
|
|
274
|
-
}
|
|
1
|
+
export * from "./browser/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './memory/continuation.ts';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./memory/continuation.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AdmissionOptions, ControlPlan, IterationRoute, StackRoute } from './extract.ts';
|
|
2
2
|
import type { NativePlan } from './guards.ts';
|
|
3
|
-
/**
|
|
3
|
+
/** Exports added so a caller can checkpoint an instance. */
|
|
4
4
|
export interface PreparedContext {
|
|
5
5
|
/** The module as passed in. */
|
|
6
6
|
original: Uint8Array;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import binaryen from 'binaryen';
|
|
2
|
-
import { admission, extract, iterationRoute, stackRoute } from "./extract.js";
|
|
2
|
+
import { admission, extract, info, iterationRoute, stackRoute } from "./extract.js";
|
|
3
3
|
import { nativePlan } from "./guards.js";
|
|
4
4
|
/**
|
|
5
5
|
* Exposes Wasm memories, mutable globals and tables for private-context reset. This adds exports
|
|
@@ -114,13 +114,6 @@ export function prepareContext(input) {
|
|
|
114
114
|
}
|
|
115
115
|
return result;
|
|
116
116
|
}
|
|
117
|
-
const info = (expression) => {
|
|
118
|
-
const id = binaryen.getExpressionId(expression);
|
|
119
|
-
if (id === binaryen.UnreachableId || id === binaryen.NopId) {
|
|
120
|
-
return { id, type: binaryen.getExpressionType(expression) };
|
|
121
|
-
}
|
|
122
|
-
return binaryen.getExpressionInfo(expression);
|
|
123
|
-
};
|
|
124
117
|
function walk(expression, visit) {
|
|
125
118
|
const value = info(expression);
|
|
126
119
|
visit(value);
|
|
@@ -147,7 +140,7 @@ function walk(expression, visit) {
|
|
|
147
140
|
walk(value[key], visit);
|
|
148
141
|
}
|
|
149
142
|
}
|
|
150
|
-
function loopPlan(
|
|
143
|
+
function loopPlan(fn, diagnostic) {
|
|
151
144
|
const f = binaryen.getFunctionInfo(fn);
|
|
152
145
|
if (f.results !== binaryen.none) {
|
|
153
146
|
diagnostic.reason = 'non-void entry requires a result merge';
|
|
@@ -343,8 +336,8 @@ function prepareTracked(bytes, options = {}) {
|
|
|
343
336
|
const nativePlans = {};
|
|
344
337
|
try {
|
|
345
338
|
const module = (analysisModule = binaryen.readBinary(original));
|
|
346
|
-
//
|
|
347
|
-
// does not infer every proposal flag from a binary's instruction bodies
|
|
339
|
+
// native validation already fixed the source instruction set; binaryen
|
|
340
|
+
// does not infer every proposal flag from a binary's instruction bodies
|
|
348
341
|
const compactImports = binaryen.Features
|
|
349
342
|
.CompactImports;
|
|
350
343
|
module.setFeatures(binaryen.Features.All & ~compactImports);
|
|
@@ -416,7 +409,7 @@ function prepareTracked(bytes, options = {}) {
|
|
|
416
409
|
}
|
|
417
410
|
});
|
|
418
411
|
const diagnostic = { reason: 'loop shape not recognized' };
|
|
419
|
-
const plan = loopPlan(
|
|
412
|
+
const plan = loopPlan(fn, diagnostic);
|
|
420
413
|
const names = exportedFunctions.get(f.name) ?? [];
|
|
421
414
|
const proved = names.length && plan ? nativePlan(module, fn) : undefined;
|
|
422
415
|
for (const name of names) {
|
|
@@ -63,7 +63,7 @@ export class Coordinator {
|
|
|
63
63
|
}
|
|
64
64
|
this.#queue.push(call);
|
|
65
65
|
this.report.regions++;
|
|
66
|
-
//
|
|
66
|
+
// amortize transport and join cost without changing per-region validation order
|
|
67
67
|
return this.#queue.length >= this.#width * 16 ? this.flush() : undefined;
|
|
68
68
|
}
|
|
69
69
|
/** Bounded polling also joins an earlier trapping region before an infinite loop. */
|
|
@@ -97,8 +97,8 @@ export class Coordinator {
|
|
|
97
97
|
}
|
|
98
98
|
async #execute(batch) {
|
|
99
99
|
if (batch.length === 1) {
|
|
100
|
-
//
|
|
101
|
-
// at its original owner
|
|
100
|
+
// an observation boundary left no independent companion; keep the work
|
|
101
|
+
// at its original owner, since IPC and a full memory snapshot buy nothing
|
|
102
102
|
if (this.report.mode === 'serial')
|
|
103
103
|
this.report.reason = 'single-region';
|
|
104
104
|
this.#native(batch[0]);
|
|
@@ -118,7 +118,7 @@ export class Coordinator {
|
|
|
118
118
|
catch (error) {
|
|
119
119
|
if (!(error instanceof RangeError))
|
|
120
120
|
throw error;
|
|
121
|
-
//
|
|
121
|
+
// a failed optimization allocation must not introduce a program trap
|
|
122
122
|
this.report.fallbacks++;
|
|
123
123
|
for (const call of batch)
|
|
124
124
|
this.#native(call);
|
|
@@ -212,7 +212,7 @@ export class Coordinator {
|
|
|
212
212
|
this.#profile.replayMs += performance.now() - replayStart;
|
|
213
213
|
}
|
|
214
214
|
if (result.exhausted) {
|
|
215
|
-
//
|
|
215
|
+
// unknown native write footprints invalidate every later speculative result
|
|
216
216
|
for (let rest = i; rest < batch.length; rest++)
|
|
217
217
|
this.#native(batch[rest]);
|
|
218
218
|
return;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import binaryen from 'binaryen';
|
|
1
2
|
/** Region admission. Both values are estimates in instruction units, not guarantees. */
|
|
2
3
|
export interface AdmissionOptions {
|
|
3
4
|
/**
|
|
@@ -10,7 +11,6 @@ export interface AdmissionOptions {
|
|
|
10
11
|
}
|
|
11
12
|
/** Calibrated on Apple M2 Pro; see `results/execution/admission/admission-calibration.summary.json`. */
|
|
12
13
|
export declare const DEFAULT_MINIMUM_REGION_WORK = 200000;
|
|
13
|
-
export declare const DEFAULT_ASSUMED_TRIP_COUNT = 1000;
|
|
14
14
|
export interface ControlPlan {
|
|
15
15
|
coordinator: Uint8Array;
|
|
16
16
|
workerOriginal: Uint8Array;
|
|
@@ -30,6 +30,8 @@ export interface ControlPlan {
|
|
|
30
30
|
}[];
|
|
31
31
|
diagnostics: string[];
|
|
32
32
|
}
|
|
33
|
+
export type Info = ReturnType<typeof binaryen.getExpressionInfo> & Record<string, any>;
|
|
34
|
+
export declare function info(expression: number): Info;
|
|
33
35
|
export interface Work {
|
|
34
36
|
compute: number;
|
|
35
37
|
accesses: number;
|
|
@@ -38,18 +40,6 @@ export interface Work {
|
|
|
38
40
|
export interface Term extends Work {
|
|
39
41
|
params: number[];
|
|
40
42
|
}
|
|
41
|
-
/** Which locals feed a loop bound untouched, and which are written and so can count. */
|
|
42
|
-
export interface LoopInputs {
|
|
43
|
-
input(index: number): boolean;
|
|
44
|
-
counter(index: number): boolean;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Trip-weighted instruction and tracked-access counts of one body, calls included. A loop bounded
|
|
48
|
-
* by an untouched input makes a term in that local; other unknown bounds use `assumedTrips`.
|
|
49
|
-
*/
|
|
50
|
-
export declare function estimateTerms(body: number, callee: (name: string) => Work | undefined, assumedTrips: number, inputs?: LoopInputs): Term[];
|
|
51
|
-
/** Numeric work with every parameter-bounded loop assumed to run `assumedTrips` times. */
|
|
52
|
-
export declare function settleWork(terms: readonly Term[], assumedTrips: number): Work;
|
|
53
43
|
export declare function estimateWork(body: number, callee: (name: string) => Work | undefined, assumedTrips: number): Work;
|
|
54
44
|
export declare const netWork: (work: Work) => number;
|
|
55
45
|
/** Resolves defaults and rejects values the estimate cannot use. */
|
|
@@ -2,7 +2,7 @@ import binaryen from 'binaryen';
|
|
|
2
2
|
import { restoreImportOrder } from "./import-order.js";
|
|
3
3
|
/** Calibrated on Apple M2 Pro; see `results/execution/admission/admission-calibration.summary.json`. */
|
|
4
4
|
export const DEFAULT_MINIMUM_REGION_WORK = 200_000;
|
|
5
|
-
|
|
5
|
+
const DEFAULT_ASSUMED_TRIP_COUNT = 1000;
|
|
6
6
|
// a tracked load or store costs about this many plain instructions in a speculative lane
|
|
7
7
|
const ACCESS_COST = 3600;
|
|
8
8
|
const CALL_COST = 2;
|
|
@@ -61,7 +61,7 @@ const pureNodes = new Set([
|
|
|
61
61
|
]);
|
|
62
62
|
const raw = binaryen;
|
|
63
63
|
const numeric = new Set([binaryen.i32, binaryen.i64, binaryen.f32, binaryen.f64]);
|
|
64
|
-
function info(expression) {
|
|
64
|
+
export function info(expression) {
|
|
65
65
|
const id = binaryen.getExpressionId(expression);
|
|
66
66
|
if (id === binaryen.NopId || id === binaryen.UnreachableId)
|
|
67
67
|
return { id, type: binaryen.getExpressionType(expression) };
|
|
@@ -220,7 +220,7 @@ function countdownLocal(loop) {
|
|
|
220
220
|
* Trip-weighted instruction and tracked-access counts of one body, calls included. A loop bounded
|
|
221
221
|
* by an untouched input makes a term in that local; other unknown bounds use `assumedTrips`.
|
|
222
222
|
*/
|
|
223
|
-
|
|
223
|
+
function estimateTerms(body, callee, assumedTrips, inputs) {
|
|
224
224
|
const terms = new Map();
|
|
225
225
|
const add = (factor, params, compute, accesses) => {
|
|
226
226
|
const key = params.join(',');
|
|
@@ -304,7 +304,7 @@ function mergeTerms(lists) {
|
|
|
304
304
|
return [...merged.values()];
|
|
305
305
|
}
|
|
306
306
|
/** Numeric work with every parameter-bounded loop assumed to run `assumedTrips` times. */
|
|
307
|
-
|
|
307
|
+
function settleWork(terms, assumedTrips) {
|
|
308
308
|
const total = { compute: 0, accesses: 0 };
|
|
309
309
|
for (const term of terms) {
|
|
310
310
|
const factor = Math.min(assumedTrips ** term.params.length, SCALE_LIMIT);
|
|
@@ -987,7 +987,7 @@ export function extract(original, prepareTracked, options = {}) {
|
|
|
987
987
|
const types = operands.map((e) => info(e).type);
|
|
988
988
|
if (types.some((type) => !numeric.has(type)))
|
|
989
989
|
throw new Error('non-scalar helper operand');
|
|
990
|
-
//
|
|
990
|
+
// one memory is admitted; keep every opcode attribute and turn only operands into parameters
|
|
991
991
|
const memoryKey = v.id === binaryen.LoadId || v.id === binaryen.StoreId
|
|
992
992
|
? JSON.stringify([
|
|
993
993
|
types,
|
|
@@ -1200,9 +1200,8 @@ export function extract(original, prepareTracked, options = {}) {
|
|
|
1200
1200
|
module?.dispose();
|
|
1201
1201
|
}
|
|
1202
1202
|
}
|
|
1203
|
-
// a table is
|
|
1204
|
-
//
|
|
1205
|
-
// `written` reads the whole module as text, so it runs only when a call needs the answer
|
|
1203
|
+
// a table is shared by every worker only when nothing can change it: no instruction writes it
|
|
1204
|
+
// and every segment is a constant-offset list; `written` scans the module text, so call it last
|
|
1206
1205
|
function tableShapes(m) {
|
|
1207
1206
|
const shapes = new Map();
|
|
1208
1207
|
let text;
|
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { TaskRecord } from './types.ts';
|
|
2
|
+
interface FoldPart {
|
|
3
|
+
readonly bytes: Uint8Array;
|
|
4
|
+
readonly record: TaskRecord;
|
|
5
|
+
text(): string;
|
|
6
|
+
json<T = unknown>(): T;
|
|
7
|
+
}
|
|
8
|
+
interface FoldPool {
|
|
9
|
+
readonly width: number;
|
|
10
|
+
submit(name: string, input: Uint8Array): string;
|
|
11
|
+
join(handle: string): FoldPart;
|
|
12
|
+
}
|
|
2
13
|
/** A byte range of the input, one per task. */
|
|
3
14
|
export interface FoldChunk {
|
|
4
15
|
start: number;
|
|
@@ -15,7 +26,7 @@ export interface FoldOptions<State> {
|
|
|
15
26
|
/** State before the first chunk. */
|
|
16
27
|
initial: State;
|
|
17
28
|
/** Folds one chunk's joined result into the state. Chunks arrive in order. */
|
|
18
|
-
combine(state: State, part:
|
|
29
|
+
combine(state: State, part: FoldPart, chunk: FoldChunk): State;
|
|
19
30
|
}
|
|
20
31
|
/** CRC-32 of A||B from the CRCs of A and B and the length of B. */
|
|
21
32
|
export declare function crc32Combine(crcA: number, crcB: number, lengthB: number): number;
|
|
@@ -29,4 +40,5 @@ export declare function chunkRanges(length: number, count: number, align?: numbe
|
|
|
29
40
|
* Runs one task per chunk on `pool` and folds the results in order. The input is copied into each
|
|
30
41
|
* task (see `TaskPool.submit`), so the state is built from copies, not from the input buffer.
|
|
31
42
|
*/
|
|
32
|
-
export declare function foldChunks<State>(pool:
|
|
43
|
+
export declare function foldChunks<State>(pool: FoldPool, input: Uint8Array, options: FoldOptions<State>): State;
|
|
44
|
+
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import binaryen from 'binaryen';
|
|
2
|
-
|
|
2
|
+
interface NativeAccess {
|
|
3
3
|
parameter: number;
|
|
4
4
|
offset: number;
|
|
5
5
|
bytes: number;
|
|
@@ -10,3 +10,4 @@ export interface NativePlan {
|
|
|
10
10
|
workParameters: number[];
|
|
11
11
|
}
|
|
12
12
|
export declare function nativePlan(module: binaryen.Module, fn: binaryen.FunctionRef): NativePlan | undefined;
|
|
13
|
+
export {};
|