@gmitch215/folio 1.0.0 → 1.1.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 +455 -26
- package/dist/affine-state.d.ts +171 -0
- package/dist/affine-state.js +763 -0
- package/dist/affine.d.ts +160 -0
- package/dist/affine.js +571 -0
- package/dist/bake.d.ts +8 -3
- package/dist/bake.js +5 -2
- package/dist/boundaries.d.ts +59 -0
- package/dist/boundaries.js +127 -0
- package/dist/certified.d.ts +66 -0
- package/dist/certified.js +192 -0
- package/dist/certify.d.ts +98 -0
- package/dist/certify.js +2628 -0
- package/dist/compiler.d.ts +37 -1
- package/dist/compiler.js +116 -1
- package/dist/context.d.ts +47 -0
- package/dist/context.js +139 -0
- package/dist/coordinator.d.ts +1 -1
- package/dist/coordinator.js +54 -5
- package/dist/extract.d.ts +114 -1
- package/dist/extract.js +1011 -242
- package/dist/fold.d.ts +32 -0
- package/dist/fold.js +105 -0
- package/dist/frames.d.ts +20 -2
- package/dist/frames.js +205 -10
- package/dist/inline.d.ts +182 -0
- package/dist/inline.js +766 -0
- package/dist/iteration-admission.d.ts +185 -0
- package/dist/iteration-admission.js +201 -0
- package/dist/iteration-layout.d.ts +135 -0
- package/dist/iteration-layout.js +169 -0
- package/dist/iteration-owner.d.ts +45 -0
- package/dist/iteration-owner.js +223 -0
- package/dist/iteration-worker.d.ts +1 -0
- package/dist/iteration-worker.js +216 -0
- package/dist/iteration.d.ts +329 -0
- package/dist/iteration.js +1210 -0
- package/dist/lease.d.ts +45 -0
- package/dist/lease.js +274 -0
- package/dist/managed.d.ts +78 -0
- package/dist/managed.js +448 -27
- package/dist/memo.d.ts +99 -0
- package/dist/memo.js +250 -0
- package/dist/node.d.ts +61 -5
- package/dist/node.js +421 -23
- package/dist/ownership-redirect.d.ts +30 -0
- package/dist/ownership-redirect.js +179 -0
- package/dist/ownership-slices.d.ts +44 -0
- package/dist/ownership-slices.js +72 -0
- package/dist/ownership.d.ts +161 -0
- package/dist/ownership.js +6568 -0
- package/dist/ranges.d.ts +78 -0
- package/dist/ranges.js +263 -0
- package/dist/read-scan.d.ts +18 -0
- package/dist/read-scan.js +94 -0
- package/dist/read-windows.d.ts +103 -0
- package/dist/read-windows.js +244 -0
- package/dist/router-worker.d.ts +1 -0
- package/dist/router-worker.js +4 -0
- package/dist/router.d.ts +203 -0
- package/dist/router.js +667 -0
- package/dist/runs.d.ts +44 -0
- package/dist/runs.js +182 -0
- package/dist/slices-worker.d.ts +1 -0
- package/dist/slices-worker.js +38 -0
- package/dist/slices.d.ts +126 -0
- package/dist/slices.js +441 -0
- package/dist/tasks.d.ts +14 -4
- package/dist/tasks.js +23 -9
- package/dist/transaction.d.ts +10 -0
- package/package.json +44 -4
package/dist/extract.js
CHANGED
|
@@ -1,12 +1,64 @@
|
|
|
1
1
|
import binaryen from 'binaryen';
|
|
2
2
|
import { restoreImportOrder } from "./import-order.js";
|
|
3
|
-
/** Calibrated on Apple M2 Pro; see `results/
|
|
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
|
export 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;
|
|
9
9
|
const SCALE_LIMIT = 2 ** 50;
|
|
10
|
+
const supported = new Set([
|
|
11
|
+
binaryen.BlockId,
|
|
12
|
+
binaryen.IfId,
|
|
13
|
+
binaryen.LoopId,
|
|
14
|
+
binaryen.BreakId,
|
|
15
|
+
binaryen.SwitchId,
|
|
16
|
+
binaryen.CallId,
|
|
17
|
+
binaryen.CallIndirectId,
|
|
18
|
+
binaryen.LocalGetId,
|
|
19
|
+
binaryen.LocalSetId,
|
|
20
|
+
binaryen.GlobalGetId,
|
|
21
|
+
binaryen.GlobalSetId,
|
|
22
|
+
binaryen.LoadId,
|
|
23
|
+
binaryen.StoreId,
|
|
24
|
+
binaryen.MemoryCopyId,
|
|
25
|
+
binaryen.MemoryFillId,
|
|
26
|
+
binaryen.MemoryInitId,
|
|
27
|
+
binaryen.DataDropId,
|
|
28
|
+
binaryen.MemorySizeId,
|
|
29
|
+
binaryen.MemoryGrowId,
|
|
30
|
+
binaryen.TableSizeId,
|
|
31
|
+
binaryen.ConstId,
|
|
32
|
+
binaryen.UnaryId,
|
|
33
|
+
binaryen.BinaryId,
|
|
34
|
+
binaryen.SelectId,
|
|
35
|
+
binaryen.DropId,
|
|
36
|
+
binaryen.ReturnId,
|
|
37
|
+
binaryen.NopId,
|
|
38
|
+
binaryen.UnreachableId
|
|
39
|
+
]);
|
|
40
|
+
const pureNodes = new Set([
|
|
41
|
+
binaryen.BlockId,
|
|
42
|
+
binaryen.IfId,
|
|
43
|
+
binaryen.LoopId,
|
|
44
|
+
binaryen.BreakId,
|
|
45
|
+
binaryen.SwitchId,
|
|
46
|
+
binaryen.CallId,
|
|
47
|
+
binaryen.LocalGetId,
|
|
48
|
+
binaryen.LocalSetId,
|
|
49
|
+
binaryen.LoadId,
|
|
50
|
+
binaryen.StoreId,
|
|
51
|
+
binaryen.MemoryCopyId,
|
|
52
|
+
binaryen.MemoryFillId,
|
|
53
|
+
binaryen.ConstId,
|
|
54
|
+
binaryen.UnaryId,
|
|
55
|
+
binaryen.BinaryId,
|
|
56
|
+
binaryen.SelectId,
|
|
57
|
+
binaryen.DropId,
|
|
58
|
+
binaryen.ReturnId,
|
|
59
|
+
binaryen.NopId,
|
|
60
|
+
binaryen.UnreachableId
|
|
61
|
+
]);
|
|
10
62
|
const raw = binaryen;
|
|
11
63
|
const numeric = new Set([binaryen.i32, binaryen.i64, binaryen.f32, binaryen.f64]);
|
|
12
64
|
function info(expression) {
|
|
@@ -287,6 +339,215 @@ export function admission(options) {
|
|
|
287
339
|
throw new RangeError('invalid assumedTripCount');
|
|
288
340
|
return { minimumWork, assumedTrips };
|
|
289
341
|
}
|
|
342
|
+
// every function on a call cycle, in discovery order
|
|
343
|
+
function recursiveFunctions(calls) {
|
|
344
|
+
const indexes = new Map();
|
|
345
|
+
const lowlinks = new Map();
|
|
346
|
+
const stack = [];
|
|
347
|
+
const active = new Set();
|
|
348
|
+
const recursive = new Set();
|
|
349
|
+
const components = (name) => {
|
|
350
|
+
indexes.set(name, indexes.size);
|
|
351
|
+
lowlinks.set(name, indexes.get(name));
|
|
352
|
+
stack.push(name);
|
|
353
|
+
active.add(name);
|
|
354
|
+
for (const target of calls.get(name) ?? []) {
|
|
355
|
+
if (!calls.has(target))
|
|
356
|
+
continue;
|
|
357
|
+
if (!indexes.has(target)) {
|
|
358
|
+
components(target);
|
|
359
|
+
lowlinks.set(name, Math.min(lowlinks.get(name), lowlinks.get(target)));
|
|
360
|
+
}
|
|
361
|
+
else if (active.has(target))
|
|
362
|
+
lowlinks.set(name, Math.min(lowlinks.get(name), indexes.get(target)));
|
|
363
|
+
}
|
|
364
|
+
if (lowlinks.get(name) !== indexes.get(name))
|
|
365
|
+
return;
|
|
366
|
+
const component = [];
|
|
367
|
+
let member;
|
|
368
|
+
do {
|
|
369
|
+
member = stack.pop();
|
|
370
|
+
active.delete(member);
|
|
371
|
+
component.push(member);
|
|
372
|
+
} while (member !== name);
|
|
373
|
+
if (component.length > 1 || calls.get(name)?.has(name))
|
|
374
|
+
for (const member of component)
|
|
375
|
+
recursive.add(member);
|
|
376
|
+
};
|
|
377
|
+
for (const name of calls.keys())
|
|
378
|
+
if (!indexes.has(name))
|
|
379
|
+
components(name);
|
|
380
|
+
return recursive;
|
|
381
|
+
}
|
|
382
|
+
// memoized per-function work; a call cycle is cut at its back edge, so the estimate of a recursive
|
|
383
|
+
// function counts one level of its own calls and always terminates
|
|
384
|
+
function workModel(functions, included, assumedTrips) {
|
|
385
|
+
const known = new Map();
|
|
386
|
+
const termsOf = (name) => {
|
|
387
|
+
let terms = known.get(name);
|
|
388
|
+
if (!terms) {
|
|
389
|
+
known.set(name, []);
|
|
390
|
+
const f = functions.get(name);
|
|
391
|
+
const written = new Set();
|
|
392
|
+
const scan = (e) => {
|
|
393
|
+
const v = info(e);
|
|
394
|
+
if (v.id === binaryen.LocalSetId)
|
|
395
|
+
written.add(v.index);
|
|
396
|
+
for (const child of children(v))
|
|
397
|
+
scan(child);
|
|
398
|
+
};
|
|
399
|
+
scan(f.body);
|
|
400
|
+
const count = binaryen.expandType(f.params).length;
|
|
401
|
+
terms = estimateTerms(f.body, workOf, assumedTrips, {
|
|
402
|
+
input: (index) => index < count && !written.has(index),
|
|
403
|
+
counter: (index) => written.has(index)
|
|
404
|
+
});
|
|
405
|
+
known.set(name, terms);
|
|
406
|
+
}
|
|
407
|
+
return terms;
|
|
408
|
+
};
|
|
409
|
+
const workOf = (name) => included(name) ? settleWork(termsOf(name), assumedTrips) : undefined;
|
|
410
|
+
return { termsOf, workOf };
|
|
411
|
+
}
|
|
412
|
+
// a span with every reason it cannot move into another function; none means it can
|
|
413
|
+
function summarizeSpan(expression, allow = {}) {
|
|
414
|
+
const span = {
|
|
415
|
+
expression,
|
|
416
|
+
reads: new Map(),
|
|
417
|
+
writes: new Set(),
|
|
418
|
+
locals: new Set(),
|
|
419
|
+
memoryWrite: false,
|
|
420
|
+
compute: false
|
|
421
|
+
};
|
|
422
|
+
const refusals = new Set();
|
|
423
|
+
const labels = new Set();
|
|
424
|
+
const targets = [];
|
|
425
|
+
if (info(expression).type !== binaryen.none)
|
|
426
|
+
refusals.add('statement has a value');
|
|
427
|
+
const visit = (e) => {
|
|
428
|
+
const v = info(e);
|
|
429
|
+
if (v.id === binaryen.CallId) {
|
|
430
|
+
if (!allow.call?.(v.target))
|
|
431
|
+
refusals.add('call outside the worker set');
|
|
432
|
+
}
|
|
433
|
+
else if (v.id === binaryen.GlobalGetId) {
|
|
434
|
+
if (!allow.global?.(v.name))
|
|
435
|
+
refusals.add('global access');
|
|
436
|
+
}
|
|
437
|
+
else if (v.id === binaryen.CallIndirectId)
|
|
438
|
+
refusals.add('indirect call');
|
|
439
|
+
else if (v.id === binaryen.ReturnId)
|
|
440
|
+
refusals.add('return in the body');
|
|
441
|
+
else if (!pureNodes.has(v.id))
|
|
442
|
+
refusals.add('impure expression');
|
|
443
|
+
if (v.isAtomic)
|
|
444
|
+
refusals.add('atomic access');
|
|
445
|
+
if (![binaryen.none, binaryen.unreachable].includes(v.type) && !numeric.has(v.type))
|
|
446
|
+
refusals.add('non-numeric value');
|
|
447
|
+
if ((v.id === binaryen.BlockId || v.id === binaryen.LoopId) && v.name)
|
|
448
|
+
labels.add(v.name);
|
|
449
|
+
if (v.id === binaryen.BreakId)
|
|
450
|
+
targets.push(v.name);
|
|
451
|
+
if (v.id === binaryen.SwitchId)
|
|
452
|
+
targets.push(...v.names, v.defaultName);
|
|
453
|
+
if (v.id === binaryen.LocalGetId) {
|
|
454
|
+
span.locals.add(v.index);
|
|
455
|
+
span.reads.set(v.index, (span.reads.get(v.index) ?? 0) + 1);
|
|
456
|
+
}
|
|
457
|
+
if (v.id === binaryen.LocalSetId) {
|
|
458
|
+
span.locals.add(v.index);
|
|
459
|
+
span.writes.add(v.index);
|
|
460
|
+
}
|
|
461
|
+
if ([binaryen.StoreId, binaryen.MemoryCopyId, binaryen.MemoryFillId].includes(v.id))
|
|
462
|
+
span.memoryWrite = true;
|
|
463
|
+
if ([binaryen.LoopId, binaryen.IfId, binaryen.BinaryId, binaryen.UnaryId].includes(v.id))
|
|
464
|
+
span.compute = true;
|
|
465
|
+
for (const child of children(v))
|
|
466
|
+
visit(child);
|
|
467
|
+
};
|
|
468
|
+
visit(expression);
|
|
469
|
+
if (!targets.every((target) => labels.has(target)))
|
|
470
|
+
refusals.add('branch escapes the body');
|
|
471
|
+
return { span, refusals };
|
|
472
|
+
}
|
|
473
|
+
// locals the spans write that something else reads, or that they read before assigning
|
|
474
|
+
function carriedLocals(spans, totalReads) {
|
|
475
|
+
const reads = new Map();
|
|
476
|
+
const writes = new Set();
|
|
477
|
+
for (const span of spans) {
|
|
478
|
+
for (const [index, amount] of span.reads)
|
|
479
|
+
reads.set(index, (reads.get(index) ?? 0) + amount);
|
|
480
|
+
for (const index of span.writes)
|
|
481
|
+
writes.add(index);
|
|
482
|
+
}
|
|
483
|
+
const carried = new Set([...writes].filter((index) => (totalReads.get(index) ?? 0) !== (reads.get(index) ?? 0)));
|
|
484
|
+
const exits = new Map();
|
|
485
|
+
const intersect = (paths) => paths.length
|
|
486
|
+
? new Set([...paths[0]].filter((index) => paths.every((path) => path.has(index))))
|
|
487
|
+
: undefined;
|
|
488
|
+
const flow = (expression, incoming) => {
|
|
489
|
+
const v = info(expression);
|
|
490
|
+
let assigned = new Set(incoming);
|
|
491
|
+
if (v.id === binaryen.LocalGetId && writes.has(v.index) && !assigned.has(v.index))
|
|
492
|
+
carried.add(v.index);
|
|
493
|
+
if (v.id === binaryen.IfId) {
|
|
494
|
+
assigned = flow(v.condition, assigned);
|
|
495
|
+
if (!assigned)
|
|
496
|
+
return;
|
|
497
|
+
return intersect([flow(v.ifTrue, assigned), v.ifFalse ? flow(v.ifFalse, assigned) : assigned].filter((path) => path !== undefined));
|
|
498
|
+
}
|
|
499
|
+
if (v.id === binaryen.BlockId || v.id === binaryen.LoopId) {
|
|
500
|
+
if (v.name)
|
|
501
|
+
exits.set(v.name, []);
|
|
502
|
+
for (const child of children(v)) {
|
|
503
|
+
if (!assigned)
|
|
504
|
+
break;
|
|
505
|
+
assigned = flow(child, assigned);
|
|
506
|
+
}
|
|
507
|
+
const branches = v.name ? exits.get(v.name) : [];
|
|
508
|
+
if (v.name)
|
|
509
|
+
exits.delete(v.name);
|
|
510
|
+
return intersect([
|
|
511
|
+
...(assigned ? [assigned] : []),
|
|
512
|
+
...(v.id === binaryen.BlockId ? branches : [])
|
|
513
|
+
]);
|
|
514
|
+
}
|
|
515
|
+
for (const child of children(v)) {
|
|
516
|
+
if (!assigned)
|
|
517
|
+
return;
|
|
518
|
+
assigned = flow(child, assigned);
|
|
519
|
+
}
|
|
520
|
+
if (!assigned)
|
|
521
|
+
return;
|
|
522
|
+
if (v.id === binaryen.LocalSetId)
|
|
523
|
+
assigned.add(v.index);
|
|
524
|
+
if (v.id === binaryen.BreakId || v.id === binaryen.SwitchId) {
|
|
525
|
+
const targets = v.id === binaryen.SwitchId ? [...v.names, v.defaultName] : [v.name];
|
|
526
|
+
for (const target of targets)
|
|
527
|
+
exits.get(target)?.push(new Set(assigned));
|
|
528
|
+
if (v.id === binaryen.SwitchId || !v.condition)
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
return v.id === binaryen.UnreachableId ? undefined : assigned;
|
|
532
|
+
};
|
|
533
|
+
let assigned = new Set();
|
|
534
|
+
for (const span of spans) {
|
|
535
|
+
if (!assigned)
|
|
536
|
+
break;
|
|
537
|
+
assigned = flow(span.expression, assigned);
|
|
538
|
+
}
|
|
539
|
+
return carried;
|
|
540
|
+
}
|
|
541
|
+
// renumbers the locals of an expression in place
|
|
542
|
+
function remapLocals(expression, positions) {
|
|
543
|
+
const v = info(expression);
|
|
544
|
+
if (v.id === binaryen.LocalGetId)
|
|
545
|
+
raw._BinaryenLocalGetSetIndex(expression, positions.get(v.index));
|
|
546
|
+
if (v.id === binaryen.LocalSetId)
|
|
547
|
+
raw._BinaryenLocalSetSetIndex(expression, positions.get(v.index));
|
|
548
|
+
for (const child of children(v))
|
|
549
|
+
remapLocals(child, positions);
|
|
550
|
+
}
|
|
290
551
|
export function extract(original, prepareTracked, options = {}) {
|
|
291
552
|
const { minimumWork, assumedTrips } = admission(options);
|
|
292
553
|
let module;
|
|
@@ -304,58 +565,6 @@ export function extract(original, prepareTracked, options = {}) {
|
|
|
304
565
|
m.setFeatures(binaryen.Features.All & ~compactImports);
|
|
305
566
|
const internalMemory = memoryName(m);
|
|
306
567
|
const functions = new Map();
|
|
307
|
-
const supported = new Set([
|
|
308
|
-
binaryen.BlockId,
|
|
309
|
-
binaryen.IfId,
|
|
310
|
-
binaryen.LoopId,
|
|
311
|
-
binaryen.BreakId,
|
|
312
|
-
binaryen.SwitchId,
|
|
313
|
-
binaryen.CallId,
|
|
314
|
-
binaryen.CallIndirectId,
|
|
315
|
-
binaryen.LocalGetId,
|
|
316
|
-
binaryen.LocalSetId,
|
|
317
|
-
binaryen.GlobalGetId,
|
|
318
|
-
binaryen.GlobalSetId,
|
|
319
|
-
binaryen.LoadId,
|
|
320
|
-
binaryen.StoreId,
|
|
321
|
-
binaryen.MemoryCopyId,
|
|
322
|
-
binaryen.MemoryFillId,
|
|
323
|
-
binaryen.MemoryInitId,
|
|
324
|
-
binaryen.DataDropId,
|
|
325
|
-
binaryen.MemorySizeId,
|
|
326
|
-
binaryen.MemoryGrowId,
|
|
327
|
-
binaryen.TableSizeId,
|
|
328
|
-
binaryen.ConstId,
|
|
329
|
-
binaryen.UnaryId,
|
|
330
|
-
binaryen.BinaryId,
|
|
331
|
-
binaryen.SelectId,
|
|
332
|
-
binaryen.DropId,
|
|
333
|
-
binaryen.ReturnId,
|
|
334
|
-
binaryen.NopId,
|
|
335
|
-
binaryen.UnreachableId
|
|
336
|
-
]);
|
|
337
|
-
const pureNodes = new Set([
|
|
338
|
-
binaryen.BlockId,
|
|
339
|
-
binaryen.IfId,
|
|
340
|
-
binaryen.LoopId,
|
|
341
|
-
binaryen.BreakId,
|
|
342
|
-
binaryen.SwitchId,
|
|
343
|
-
binaryen.CallId,
|
|
344
|
-
binaryen.LocalGetId,
|
|
345
|
-
binaryen.LocalSetId,
|
|
346
|
-
binaryen.LoadId,
|
|
347
|
-
binaryen.StoreId,
|
|
348
|
-
binaryen.MemoryCopyId,
|
|
349
|
-
binaryen.MemoryFillId,
|
|
350
|
-
binaryen.ConstId,
|
|
351
|
-
binaryen.UnaryId,
|
|
352
|
-
binaryen.BinaryId,
|
|
353
|
-
binaryen.SelectId,
|
|
354
|
-
binaryen.DropId,
|
|
355
|
-
binaryen.ReturnId,
|
|
356
|
-
binaryen.NopId,
|
|
357
|
-
binaryen.UnreachableId
|
|
358
|
-
]);
|
|
359
568
|
let outlineIndex = 0;
|
|
360
569
|
const originals = Array.from({ length: m.getNumFunctions() }, (_, index) => binaryen.getFunctionInfo(m.getFunctionByIndex(index)));
|
|
361
570
|
const originalBodies = new Map(originals.filter((f) => f.body).map((f) => [f.name, m.copyExpression(f.body)]));
|
|
@@ -385,126 +594,11 @@ export function extract(original, prepareTracked, options = {}) {
|
|
|
385
594
|
if (!traversable)
|
|
386
595
|
continue;
|
|
387
596
|
const summarize = (expression) => {
|
|
388
|
-
const
|
|
389
|
-
|
|
390
|
-
reads: new Map(),
|
|
391
|
-
writes: new Set(),
|
|
392
|
-
locals: new Set(),
|
|
393
|
-
memoryWrite: false,
|
|
394
|
-
compute: false
|
|
395
|
-
};
|
|
396
|
-
const labels = new Set();
|
|
397
|
-
const targets = [];
|
|
398
|
-
let eligible = info(expression).type === binaryen.none;
|
|
399
|
-
const visit = (e) => {
|
|
400
|
-
const v = info(e);
|
|
401
|
-
if (!pureNodes.has(v.id) ||
|
|
402
|
-
v.isAtomic ||
|
|
403
|
-
v.id === binaryen.CallId ||
|
|
404
|
-
v.id === binaryen.ReturnId)
|
|
405
|
-
eligible = false;
|
|
406
|
-
if (![binaryen.none, binaryen.unreachable].includes(v.type) &&
|
|
407
|
-
!numeric.has(v.type))
|
|
408
|
-
eligible = false;
|
|
409
|
-
if ((v.id === binaryen.BlockId || v.id === binaryen.LoopId) && v.name)
|
|
410
|
-
labels.add(v.name);
|
|
411
|
-
if (v.id === binaryen.BreakId)
|
|
412
|
-
targets.push(v.name);
|
|
413
|
-
if (v.id === binaryen.SwitchId)
|
|
414
|
-
targets.push(...v.names, v.defaultName);
|
|
415
|
-
if (v.id === binaryen.LocalGetId) {
|
|
416
|
-
span.locals.add(v.index);
|
|
417
|
-
span.reads.set(v.index, (span.reads.get(v.index) ?? 0) + 1);
|
|
418
|
-
}
|
|
419
|
-
if (v.id === binaryen.LocalSetId) {
|
|
420
|
-
span.locals.add(v.index);
|
|
421
|
-
span.writes.add(v.index);
|
|
422
|
-
}
|
|
423
|
-
if ([binaryen.StoreId, binaryen.MemoryCopyId, binaryen.MemoryFillId].includes(v.id))
|
|
424
|
-
span.memoryWrite = true;
|
|
425
|
-
if ([
|
|
426
|
-
binaryen.LoopId,
|
|
427
|
-
binaryen.IfId,
|
|
428
|
-
binaryen.BinaryId,
|
|
429
|
-
binaryen.UnaryId
|
|
430
|
-
].includes(v.id))
|
|
431
|
-
span.compute = true;
|
|
432
|
-
for (const child of children(v))
|
|
433
|
-
visit(child);
|
|
434
|
-
};
|
|
435
|
-
visit(expression);
|
|
436
|
-
return eligible && targets.every((target) => labels.has(target)) ? span : undefined;
|
|
597
|
+
const summary = summarizeSpan(expression);
|
|
598
|
+
return summary.refusals.size ? undefined : summary.span;
|
|
437
599
|
};
|
|
438
600
|
const outline = (spans) => {
|
|
439
|
-
const
|
|
440
|
-
const writes = new Set();
|
|
441
|
-
for (const span of spans) {
|
|
442
|
-
for (const [index, amount] of span.reads)
|
|
443
|
-
reads.set(index, (reads.get(index) ?? 0) + amount);
|
|
444
|
-
for (const index of span.writes)
|
|
445
|
-
writes.add(index);
|
|
446
|
-
}
|
|
447
|
-
const carried = new Set([...writes].filter((index) => (totalReads.get(index) ?? 0) !== (reads.get(index) ?? 0)));
|
|
448
|
-
const exits = new Map();
|
|
449
|
-
const intersect = (paths) => paths.length
|
|
450
|
-
? new Set([...paths[0]].filter((index) => paths.every((path) => path.has(index))))
|
|
451
|
-
: undefined;
|
|
452
|
-
const flow = (expression, incoming) => {
|
|
453
|
-
const v = info(expression);
|
|
454
|
-
let assigned = new Set(incoming);
|
|
455
|
-
if (v.id === binaryen.LocalGetId &&
|
|
456
|
-
writes.has(v.index) &&
|
|
457
|
-
!assigned.has(v.index))
|
|
458
|
-
carried.add(v.index);
|
|
459
|
-
if (v.id === binaryen.IfId) {
|
|
460
|
-
assigned = flow(v.condition, assigned);
|
|
461
|
-
if (!assigned)
|
|
462
|
-
return;
|
|
463
|
-
return intersect([
|
|
464
|
-
flow(v.ifTrue, assigned),
|
|
465
|
-
v.ifFalse ? flow(v.ifFalse, assigned) : assigned
|
|
466
|
-
].filter((path) => path !== undefined));
|
|
467
|
-
}
|
|
468
|
-
if (v.id === binaryen.BlockId || v.id === binaryen.LoopId) {
|
|
469
|
-
if (v.name)
|
|
470
|
-
exits.set(v.name, []);
|
|
471
|
-
for (const child of children(v)) {
|
|
472
|
-
if (!assigned)
|
|
473
|
-
break;
|
|
474
|
-
assigned = flow(child, assigned);
|
|
475
|
-
}
|
|
476
|
-
const branches = v.name ? exits.get(v.name) : [];
|
|
477
|
-
if (v.name)
|
|
478
|
-
exits.delete(v.name);
|
|
479
|
-
return intersect([
|
|
480
|
-
...(assigned ? [assigned] : []),
|
|
481
|
-
...(v.id === binaryen.BlockId ? branches : [])
|
|
482
|
-
]);
|
|
483
|
-
}
|
|
484
|
-
for (const child of children(v)) {
|
|
485
|
-
if (!assigned)
|
|
486
|
-
return;
|
|
487
|
-
assigned = flow(child, assigned);
|
|
488
|
-
}
|
|
489
|
-
if (!assigned)
|
|
490
|
-
return;
|
|
491
|
-
if (v.id === binaryen.LocalSetId)
|
|
492
|
-
assigned.add(v.index);
|
|
493
|
-
if (v.id === binaryen.BreakId || v.id === binaryen.SwitchId) {
|
|
494
|
-
const targets = v.id === binaryen.SwitchId ? [...v.names, v.defaultName] : [v.name];
|
|
495
|
-
for (const target of targets)
|
|
496
|
-
exits.get(target)?.push(new Set(assigned));
|
|
497
|
-
if (v.id === binaryen.SwitchId || !v.condition)
|
|
498
|
-
return;
|
|
499
|
-
}
|
|
500
|
-
return v.id === binaryen.UnreachableId ? undefined : assigned;
|
|
501
|
-
};
|
|
502
|
-
let assigned = new Set();
|
|
503
|
-
for (const span of spans) {
|
|
504
|
-
if (!assigned)
|
|
505
|
-
break;
|
|
506
|
-
assigned = flow(span.expression, assigned);
|
|
507
|
-
}
|
|
601
|
+
const carried = carriedLocals(spans, totalReads);
|
|
508
602
|
if (carried.size) {
|
|
509
603
|
const result = [];
|
|
510
604
|
let pending = [];
|
|
@@ -539,16 +633,7 @@ export function extract(original, prepareTracked, options = {}) {
|
|
|
539
633
|
const captured = [...new Set(spans.flatMap((span) => [...span.locals]))].sort((a, b) => a - b);
|
|
540
634
|
const positions = new Map(captured.map((index, position) => [index, position]));
|
|
541
635
|
const body = m.block(null, spans.map((span) => m.copyExpression(span.expression)));
|
|
542
|
-
|
|
543
|
-
const v = info(expression);
|
|
544
|
-
if (v.id === binaryen.LocalGetId)
|
|
545
|
-
raw._BinaryenLocalGetSetIndex(expression, positions.get(v.index));
|
|
546
|
-
if (v.id === binaryen.LocalSetId)
|
|
547
|
-
raw._BinaryenLocalSetSetIndex(expression, positions.get(v.index));
|
|
548
|
-
for (const child of children(v))
|
|
549
|
-
remap(child);
|
|
550
|
-
};
|
|
551
|
-
remap(body);
|
|
636
|
+
remapLocals(body, positions);
|
|
552
637
|
let name;
|
|
553
638
|
do {
|
|
554
639
|
name = `__folio_inline_${outlineIndex++}`;
|
|
@@ -638,43 +723,7 @@ export function extract(original, prepareTracked, options = {}) {
|
|
|
638
723
|
if (candidate && control)
|
|
639
724
|
pure.add(f.name);
|
|
640
725
|
}
|
|
641
|
-
const
|
|
642
|
-
const lowlinks = new Map();
|
|
643
|
-
const stack = [];
|
|
644
|
-
const active = new Set();
|
|
645
|
-
const recursive = new Set();
|
|
646
|
-
const components = (name) => {
|
|
647
|
-
indexes.set(name, indexes.size);
|
|
648
|
-
lowlinks.set(name, indexes.get(name));
|
|
649
|
-
stack.push(name);
|
|
650
|
-
active.add(name);
|
|
651
|
-
for (const target of calls.get(name) ?? []) {
|
|
652
|
-
if (!calls.has(target))
|
|
653
|
-
continue;
|
|
654
|
-
if (!indexes.has(target)) {
|
|
655
|
-
components(target);
|
|
656
|
-
lowlinks.set(name, Math.min(lowlinks.get(name), lowlinks.get(target)));
|
|
657
|
-
}
|
|
658
|
-
else if (active.has(target))
|
|
659
|
-
lowlinks.set(name, Math.min(lowlinks.get(name), indexes.get(target)));
|
|
660
|
-
}
|
|
661
|
-
if (lowlinks.get(name) !== indexes.get(name))
|
|
662
|
-
return;
|
|
663
|
-
const component = [];
|
|
664
|
-
let member;
|
|
665
|
-
do {
|
|
666
|
-
member = stack.pop();
|
|
667
|
-
active.delete(member);
|
|
668
|
-
component.push(member);
|
|
669
|
-
} while (member !== name);
|
|
670
|
-
if (component.length > 1 || calls.get(name)?.has(name))
|
|
671
|
-
for (const member of component)
|
|
672
|
-
recursive.add(member);
|
|
673
|
-
};
|
|
674
|
-
for (const name of calls.keys())
|
|
675
|
-
if (!indexes.has(name))
|
|
676
|
-
components(name);
|
|
677
|
-
for (const name of recursive) {
|
|
726
|
+
for (const name of recursiveFunctions(calls)) {
|
|
678
727
|
controllers.delete(name);
|
|
679
728
|
pure.delete(name);
|
|
680
729
|
diagnostics.push(`ordered native recursion: ${name}`);
|
|
@@ -692,30 +741,7 @@ export function extract(original, prepareTracked, options = {}) {
|
|
|
692
741
|
options.refused?.('no pure function to run as a region');
|
|
693
742
|
return;
|
|
694
743
|
}
|
|
695
|
-
const
|
|
696
|
-
const termsOf = (name) => {
|
|
697
|
-
let terms = known.get(name);
|
|
698
|
-
if (!terms) {
|
|
699
|
-
const f = functions.get(name);
|
|
700
|
-
const written = new Set();
|
|
701
|
-
const scan = (e) => {
|
|
702
|
-
const v = info(e);
|
|
703
|
-
if (v.id === binaryen.LocalSetId)
|
|
704
|
-
written.add(v.index);
|
|
705
|
-
for (const child of children(v))
|
|
706
|
-
scan(child);
|
|
707
|
-
};
|
|
708
|
-
scan(f.body);
|
|
709
|
-
const count = binaryen.expandType(f.params).length;
|
|
710
|
-
terms = estimateTerms(f.body, workOf, assumedTrips, {
|
|
711
|
-
input: (index) => index < count && !written.has(index),
|
|
712
|
-
counter: (index) => written.has(index)
|
|
713
|
-
});
|
|
714
|
-
known.set(name, terms);
|
|
715
|
-
}
|
|
716
|
-
return terms;
|
|
717
|
-
};
|
|
718
|
-
const workOf = (name) => pure.has(name) ? settleWork(termsOf(name), assumedTrips) : undefined;
|
|
744
|
+
const { termsOf } = workModel(functions, (name) => pure.has(name), assumedTrips);
|
|
719
745
|
const admitted = new Set();
|
|
720
746
|
const guarded = new Map();
|
|
721
747
|
for (const name of pure) {
|
|
@@ -1174,3 +1200,746 @@ export function extract(original, prepareTracked, options = {}) {
|
|
|
1174
1200
|
module?.dispose();
|
|
1175
1201
|
}
|
|
1176
1202
|
}
|
|
1203
|
+
// a table is the same in every worker when nothing can change it after instantiation: it is
|
|
1204
|
+
// defined here, not exported, no instruction writes it and every segment is a constant-offset list.
|
|
1205
|
+
// `written` reads the whole module as text, so it runs only when a call needs the answer
|
|
1206
|
+
function tableShapes(m) {
|
|
1207
|
+
const shapes = new Map();
|
|
1208
|
+
let text;
|
|
1209
|
+
const written = () => (text ??= /\btable\.(set|grow|fill|copy|init)\b/.test(m.emitText()));
|
|
1210
|
+
const count = m.getNumTables();
|
|
1211
|
+
if (!count)
|
|
1212
|
+
return { shapes, written };
|
|
1213
|
+
const exported = new Set();
|
|
1214
|
+
for (let i = 0; i < m.getNumExports(); i++) {
|
|
1215
|
+
const e = binaryen.getExportInfo(m.getExportByIndex(i));
|
|
1216
|
+
if (e.kind === binaryen.ExternalTable)
|
|
1217
|
+
exported.add(e.value);
|
|
1218
|
+
}
|
|
1219
|
+
for (let i = 0; i < count; i++) {
|
|
1220
|
+
const t = binaryen.getTableInfo(m.getTableByIndex(i));
|
|
1221
|
+
shapes.set(t.name, {
|
|
1222
|
+
refusal: t.module
|
|
1223
|
+
? 'imported table'
|
|
1224
|
+
: exported.has(t.name)
|
|
1225
|
+
? 'exported table'
|
|
1226
|
+
: undefined,
|
|
1227
|
+
runs: [],
|
|
1228
|
+
functions: new Set()
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
for (let i = 0; i < m.getNumElementSegments(); i++) {
|
|
1232
|
+
const s = binaryen.getElementSegmentInfo(m.getElementSegmentByIndex(i));
|
|
1233
|
+
const shape = shapes.get(s.table);
|
|
1234
|
+
if (!shape)
|
|
1235
|
+
continue;
|
|
1236
|
+
const offset = info(s.offset);
|
|
1237
|
+
if (offset.id !== binaryen.ConstId || typeof offset.value !== 'number') {
|
|
1238
|
+
shape.refusal ??= 'element segment offset is not a constant';
|
|
1239
|
+
continue;
|
|
1240
|
+
}
|
|
1241
|
+
let start = 0;
|
|
1242
|
+
for (let k = 0; k <= s.data.length; k++) {
|
|
1243
|
+
if (k < s.data.length && s.data[k])
|
|
1244
|
+
continue;
|
|
1245
|
+
if (k > start)
|
|
1246
|
+
shape.runs.push({
|
|
1247
|
+
name: `${s.name}_${shape.runs.length}`,
|
|
1248
|
+
offset: (offset.value >>> 0) + start,
|
|
1249
|
+
names: s.data.slice(start, k)
|
|
1250
|
+
});
|
|
1251
|
+
start = k + 1;
|
|
1252
|
+
}
|
|
1253
|
+
for (const name of s.data)
|
|
1254
|
+
if (name)
|
|
1255
|
+
shape.functions.add(name);
|
|
1256
|
+
}
|
|
1257
|
+
return { shapes, written };
|
|
1258
|
+
}
|
|
1259
|
+
/** Name prefix of the mutable delta globals an ownership redirect adds; the stack route sets them aside. */
|
|
1260
|
+
export const redirectDelta = '__folio_od';
|
|
1261
|
+
/** Computes the stack route of a module. A failure is reported in `diagnostics`, never thrown. */
|
|
1262
|
+
export function stackRoute(original, options = {}) {
|
|
1263
|
+
const { assumedTrips } = admission(options);
|
|
1264
|
+
const route = {
|
|
1265
|
+
eligible: false,
|
|
1266
|
+
constants: [],
|
|
1267
|
+
functions: [],
|
|
1268
|
+
recursive: [],
|
|
1269
|
+
tables: [],
|
|
1270
|
+
work: Object.create(null),
|
|
1271
|
+
diagnostics: []
|
|
1272
|
+
};
|
|
1273
|
+
let module;
|
|
1274
|
+
try {
|
|
1275
|
+
if (memoryCount(original) !== 1) {
|
|
1276
|
+
route.diagnostics.push('exactly one memory required');
|
|
1277
|
+
return route;
|
|
1278
|
+
}
|
|
1279
|
+
module = binaryen.readBinary(original);
|
|
1280
|
+
if (module.getMemoryInfo().is64) {
|
|
1281
|
+
route.diagnostics.push('wasm64 memory');
|
|
1282
|
+
return route;
|
|
1283
|
+
}
|
|
1284
|
+
const compactImports = binaryen.Features
|
|
1285
|
+
.CompactImports;
|
|
1286
|
+
module.setFeatures(binaryen.Features.All & ~compactImports);
|
|
1287
|
+
const globals = new Map();
|
|
1288
|
+
for (let i = 0; i < module.getNumGlobals(); i++) {
|
|
1289
|
+
const g = binaryen.getGlobalInfo(module.getGlobalByIndex(i));
|
|
1290
|
+
const init = g.module ? undefined : info(g.init);
|
|
1291
|
+
const constant = init?.id === binaryen.ConstId;
|
|
1292
|
+
globals.set(g.name, {
|
|
1293
|
+
mutable: g.mutable,
|
|
1294
|
+
constant,
|
|
1295
|
+
initial: constant && g.type === binaryen.i32 ? Number(init.value) : undefined,
|
|
1296
|
+
refusal: g.module
|
|
1297
|
+
? 'imported global'
|
|
1298
|
+
: !numeric.has(g.type)
|
|
1299
|
+
? 'reference-typed global'
|
|
1300
|
+
: !constant
|
|
1301
|
+
? 'initializer is not a constant'
|
|
1302
|
+
: g.mutable && g.type !== binaryen.i32
|
|
1303
|
+
? 'mutable global is not i32'
|
|
1304
|
+
: undefined
|
|
1305
|
+
});
|
|
1306
|
+
}
|
|
1307
|
+
const { shapes: tables, written } = tableShapes(module);
|
|
1308
|
+
const refusedTables = new Set();
|
|
1309
|
+
const bodies = [];
|
|
1310
|
+
for (let i = 0; i < module.getNumFunctions(); i++) {
|
|
1311
|
+
const f = binaryen.getFunctionInfo(module.getFunctionByIndex(i));
|
|
1312
|
+
if (!f.body)
|
|
1313
|
+
continue;
|
|
1314
|
+
const body = { info: f, targets: new Set(), refs: new Set(), tables: new Set() };
|
|
1315
|
+
if (![...binaryen.expandType(f.params), ...f.vars].every((t) => numeric.has(t)) ||
|
|
1316
|
+
!(f.results === binaryen.none || numeric.has(f.results)))
|
|
1317
|
+
body.cause = 'non-numeric signature';
|
|
1318
|
+
const visit = (expression) => {
|
|
1319
|
+
const v = info(expression);
|
|
1320
|
+
if (!supported.has(v.id))
|
|
1321
|
+
body.cause ??= 'unsupported expression';
|
|
1322
|
+
else if (v.id === binaryen.GlobalGetId || v.id === binaryen.GlobalSetId)
|
|
1323
|
+
body.refs.add(v.name);
|
|
1324
|
+
else if (v.id === binaryen.CallIndirectId) {
|
|
1325
|
+
const shape = tables.get(v.table);
|
|
1326
|
+
const refusal = shape
|
|
1327
|
+
? (shape.refusal ?? (written() ? 'the module writes a table' : undefined))
|
|
1328
|
+
: 'no such table';
|
|
1329
|
+
if (!shape || refusal) {
|
|
1330
|
+
body.cause ??= 'indirect call';
|
|
1331
|
+
refusedTables.add(`${v.table}: ${refusal}`);
|
|
1332
|
+
}
|
|
1333
|
+
else {
|
|
1334
|
+
body.tables.add(v.table);
|
|
1335
|
+
// any function of the call's type in the table can run, so each must be a member
|
|
1336
|
+
for (const name of shape.functions) {
|
|
1337
|
+
const g = binaryen.getFunctionInfo(module.getFunction(name));
|
|
1338
|
+
if (g.params === v.params && g.results === v.results)
|
|
1339
|
+
body.targets.add(name);
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
else if (!pureNodes.has(v.id))
|
|
1344
|
+
body.cause ??= 'impure expression';
|
|
1345
|
+
if (v.isAtomic)
|
|
1346
|
+
body.cause ??= 'atomic access';
|
|
1347
|
+
if (![binaryen.none, binaryen.unreachable].includes(v.type) && !numeric.has(v.type))
|
|
1348
|
+
body.cause ??= 'non-numeric value';
|
|
1349
|
+
if (v.id === binaryen.CallId)
|
|
1350
|
+
body.targets.add(v.target);
|
|
1351
|
+
for (const child of children(v))
|
|
1352
|
+
visit(child);
|
|
1353
|
+
};
|
|
1354
|
+
try {
|
|
1355
|
+
visit(f.body);
|
|
1356
|
+
}
|
|
1357
|
+
catch {
|
|
1358
|
+
body.cause ??= 'unreadable expression';
|
|
1359
|
+
}
|
|
1360
|
+
bodies.push(body);
|
|
1361
|
+
}
|
|
1362
|
+
const referenced = new Set(bodies.flatMap((body) => [...body.refs]));
|
|
1363
|
+
const deltas = [...globals.keys()].filter((name) => name.startsWith(redirectDelta) &&
|
|
1364
|
+
referenced.has(name) &&
|
|
1365
|
+
globals.get(name).mutable &&
|
|
1366
|
+
!globals.get(name).refusal);
|
|
1367
|
+
if (deltas.length)
|
|
1368
|
+
route.redirect = deltas;
|
|
1369
|
+
const named = [...globals.keys()].filter((name) => referenced.has(name) && !deltas.includes(name));
|
|
1370
|
+
const mutable = named.filter((name) => globals.get(name).mutable);
|
|
1371
|
+
if (mutable.length > 1) {
|
|
1372
|
+
for (const name of mutable.slice(1))
|
|
1373
|
+
route.diagnostics.push(`refused global ${name}: second mutable global (stack candidate ${mutable[0]})`);
|
|
1374
|
+
return route;
|
|
1375
|
+
}
|
|
1376
|
+
const refused = new Set();
|
|
1377
|
+
for (const name of named) {
|
|
1378
|
+
const g = globals.get(name);
|
|
1379
|
+
if (g.refusal) {
|
|
1380
|
+
refused.add(name);
|
|
1381
|
+
route.diagnostics.push(`refused global ${name}: ${g.refusal}`);
|
|
1382
|
+
}
|
|
1383
|
+
else if (g.mutable)
|
|
1384
|
+
route.stack = { name, initial: g.initial };
|
|
1385
|
+
else
|
|
1386
|
+
route.constants.push(name);
|
|
1387
|
+
}
|
|
1388
|
+
if (route.stack)
|
|
1389
|
+
route.diagnostics.push(`stack global ${route.stack.name}`);
|
|
1390
|
+
for (const body of bodies)
|
|
1391
|
+
if (!body.cause && [...body.refs].some((name) => refused.has(name)))
|
|
1392
|
+
body.cause = 'refused global';
|
|
1393
|
+
const members = new Set(bodies.filter((body) => !body.cause).map((b) => b.info.name));
|
|
1394
|
+
for (let changed = true; changed;) {
|
|
1395
|
+
changed = false;
|
|
1396
|
+
for (const body of bodies) {
|
|
1397
|
+
if (body.cause || ![...body.targets].some((name) => !members.has(name)))
|
|
1398
|
+
continue;
|
|
1399
|
+
body.cause = 'call outside the set';
|
|
1400
|
+
members.delete(body.info.name);
|
|
1401
|
+
changed = true;
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
const inSet = bodies.filter((body) => members.has(body.info.name));
|
|
1405
|
+
route.functions = inSet.map((body) => body.info.name);
|
|
1406
|
+
route.tables = [...new Set(inSet.flatMap((body) => [...body.tables]))];
|
|
1407
|
+
for (const refusal of [...refusedTables].sort())
|
|
1408
|
+
route.diagnostics.push(`refused table ${refusal}`);
|
|
1409
|
+
const cyclic = recursiveFunctions(new Map(inSet.map((b) => [b.info.name, b.targets])));
|
|
1410
|
+
route.recursive = route.functions.filter((name) => cyclic.has(name));
|
|
1411
|
+
const model = workModel(new Map(inSet.map((body) => [body.info.name, body.info])), (name) => members.has(name), assumedTrips);
|
|
1412
|
+
for (const name of route.functions)
|
|
1413
|
+
route.work[name] = model.workOf(name);
|
|
1414
|
+
route.diagnostics.push(`worker set: ${route.functions.length} of ${bodies.length} functions, ${route.recursive.length} recursive`);
|
|
1415
|
+
const causes = new Map();
|
|
1416
|
+
for (const body of bodies)
|
|
1417
|
+
if (body.cause)
|
|
1418
|
+
causes.set(body.cause, (causes.get(body.cause) ?? 0) + 1);
|
|
1419
|
+
if (causes.size)
|
|
1420
|
+
route.diagnostics.push(`excluded: ${[...causes]
|
|
1421
|
+
.sort(([a], [b]) => (a < b ? -1 : 1))
|
|
1422
|
+
.map(([cause, count]) => `${count} ${cause}`)
|
|
1423
|
+
.join(', ')}`);
|
|
1424
|
+
route.eligible = route.functions.length > 0;
|
|
1425
|
+
if (!route.eligible)
|
|
1426
|
+
route.diagnostics.push('no function can run on a worker');
|
|
1427
|
+
return route;
|
|
1428
|
+
}
|
|
1429
|
+
catch (error) {
|
|
1430
|
+
return {
|
|
1431
|
+
eligible: false,
|
|
1432
|
+
constants: [],
|
|
1433
|
+
functions: [],
|
|
1434
|
+
recursive: [],
|
|
1435
|
+
tables: [],
|
|
1436
|
+
work: Object.create(null),
|
|
1437
|
+
diagnostics: [
|
|
1438
|
+
`stack route unavailable: ${error instanceof Error ? error.message : String(error)}`
|
|
1439
|
+
]
|
|
1440
|
+
};
|
|
1441
|
+
}
|
|
1442
|
+
finally {
|
|
1443
|
+
module?.dispose();
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
const emptyIteration = (diagnostics) => ({
|
|
1447
|
+
eligible: false,
|
|
1448
|
+
locals: [],
|
|
1449
|
+
diagnostics
|
|
1450
|
+
});
|
|
1451
|
+
/**
|
|
1452
|
+
* Finds an owner whose single loop is a countdown over independent calls into the stack route's
|
|
1453
|
+
* worker set and outlines one iteration of it. A refusal is a named diagnostic, never an exception.
|
|
1454
|
+
*/
|
|
1455
|
+
export function iterationRoute(original, route) {
|
|
1456
|
+
if (!route.eligible)
|
|
1457
|
+
return emptyIteration([
|
|
1458
|
+
`stack route refused: ${route.diagnostics.join('; ') || 'empty set'}`
|
|
1459
|
+
]);
|
|
1460
|
+
let module;
|
|
1461
|
+
try {
|
|
1462
|
+
module = binaryen.readBinary(original);
|
|
1463
|
+
const m = module;
|
|
1464
|
+
const compactImports = binaryen.Features
|
|
1465
|
+
.CompactImports;
|
|
1466
|
+
m.setFeatures(binaryen.Features.All & ~compactImports);
|
|
1467
|
+
const members = new Set(route.functions);
|
|
1468
|
+
const stack = route.stack?.name;
|
|
1469
|
+
const constants = new Set(route.constants);
|
|
1470
|
+
const refusals = [];
|
|
1471
|
+
let omitted = 0;
|
|
1472
|
+
for (let i = 0; i < m.getNumFunctions(); i++) {
|
|
1473
|
+
const f = binaryen.getFunctionInfo(m.getFunctionByIndex(i));
|
|
1474
|
+
if (!f.body || !callsInLoop(f.body, members))
|
|
1475
|
+
continue;
|
|
1476
|
+
const plan = outlineOwner(m, f, { members, stack, constants, route });
|
|
1477
|
+
if (plan.route) {
|
|
1478
|
+
plan.route.diagnostics.unshift(...refusals);
|
|
1479
|
+
return plan.route;
|
|
1480
|
+
}
|
|
1481
|
+
for (const reason of plan.refusals) {
|
|
1482
|
+
if (refusals.length < 64)
|
|
1483
|
+
refusals.push(`refused loop in ${f.name}: ${reason}`);
|
|
1484
|
+
else
|
|
1485
|
+
omitted++;
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
if (omitted)
|
|
1489
|
+
refusals.push(`${omitted} more refused loops not listed`);
|
|
1490
|
+
refusals.push('no loop calls into the worker set');
|
|
1491
|
+
return emptyIteration(refusals);
|
|
1492
|
+
}
|
|
1493
|
+
catch (error) {
|
|
1494
|
+
return emptyIteration([
|
|
1495
|
+
`iteration route unavailable: ${error instanceof Error ? error.message : String(error)}`
|
|
1496
|
+
]);
|
|
1497
|
+
}
|
|
1498
|
+
finally {
|
|
1499
|
+
module?.dispose();
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
// does any loop in the body contain a call to a member of the set
|
|
1503
|
+
function callsInLoop(body, members) {
|
|
1504
|
+
const visit = (expression, inLoop) => {
|
|
1505
|
+
const v = info(expression);
|
|
1506
|
+
if (inLoop && v.id === binaryen.CallId && members.has(v.target))
|
|
1507
|
+
return true;
|
|
1508
|
+
return children(v).some((child) => visit(child, inLoop || v.id === binaryen.LoopId));
|
|
1509
|
+
};
|
|
1510
|
+
try {
|
|
1511
|
+
return visit(body, false);
|
|
1512
|
+
}
|
|
1513
|
+
catch {
|
|
1514
|
+
return false;
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
// `local = local + c`, `local - c`, `local + other` or `local - other` as a statement
|
|
1518
|
+
function strideUpdate(statement) {
|
|
1519
|
+
const set = info(statement);
|
|
1520
|
+
if (set.id !== binaryen.LocalSetId || set.isTee)
|
|
1521
|
+
return;
|
|
1522
|
+
const next = info(set.value);
|
|
1523
|
+
if (next.id !== binaryen.BinaryId)
|
|
1524
|
+
return;
|
|
1525
|
+
const [left, right] = [info(next.left), info(next.right)];
|
|
1526
|
+
const isSelf = (v) => v.id === binaryen.LocalGetId && v.index === set.index;
|
|
1527
|
+
const constant = (v) => v.id === binaryen.ConstId && typeof v.value === 'number' ? v : undefined;
|
|
1528
|
+
const other = (v) => v.id === binaryen.LocalGetId && v.index !== set.index ? v.index : undefined;
|
|
1529
|
+
if (next.op === binaryen.AddInt32) {
|
|
1530
|
+
if (isSelf(left) && constant(right))
|
|
1531
|
+
return { local: set.index, stride: right.value | 0 };
|
|
1532
|
+
if (isSelf(right) && constant(left))
|
|
1533
|
+
return { local: set.index, stride: left.value | 0 };
|
|
1534
|
+
if (isSelf(left) && other(right) !== undefined)
|
|
1535
|
+
return { local: set.index, stride: 1, strideLocal: other(right) };
|
|
1536
|
+
if (isSelf(right) && other(left) !== undefined)
|
|
1537
|
+
return { local: set.index, stride: 1, strideLocal: other(left) };
|
|
1538
|
+
}
|
|
1539
|
+
if (next.op === binaryen.SubInt32 && isSelf(left)) {
|
|
1540
|
+
if (constant(right))
|
|
1541
|
+
return { local: set.index, stride: -right.value | 0 };
|
|
1542
|
+
if (other(right) !== undefined)
|
|
1543
|
+
return { local: set.index, stride: -1, strideLocal: other(right) };
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
function outlineOwner(m, f, context) {
|
|
1547
|
+
const { members, stack, constants } = context;
|
|
1548
|
+
const refuse = new Set();
|
|
1549
|
+
const localTypes = [...binaryen.expandType(f.params), ...f.vars];
|
|
1550
|
+
const parameterCount = binaryen.expandType(f.params).length;
|
|
1551
|
+
if (!localTypes.every((type) => numeric.has(type)))
|
|
1552
|
+
return { refusals: ['non-numeric signature'] };
|
|
1553
|
+
// one pass: loops, write and read counts, the loop's own writes
|
|
1554
|
+
const loops = [];
|
|
1555
|
+
const writes = new Map();
|
|
1556
|
+
const totalReads = new Map();
|
|
1557
|
+
const scan = (expression) => {
|
|
1558
|
+
const v = info(expression);
|
|
1559
|
+
if (v.id === binaryen.LoopId)
|
|
1560
|
+
loops.push(expression);
|
|
1561
|
+
if (v.id === binaryen.LocalSetId)
|
|
1562
|
+
writes.set(v.index, (writes.get(v.index) ?? 0) + 1);
|
|
1563
|
+
if (v.id === binaryen.LocalGetId)
|
|
1564
|
+
totalReads.set(v.index, (totalReads.get(v.index) ?? 0) + 1);
|
|
1565
|
+
for (const child of children(v))
|
|
1566
|
+
scan(child);
|
|
1567
|
+
};
|
|
1568
|
+
scan(f.body);
|
|
1569
|
+
if (loops.length !== 1)
|
|
1570
|
+
return { refusals: [`${loops.length} loops, one required`] };
|
|
1571
|
+
const loopExpression = loops[0];
|
|
1572
|
+
const loop = info(loopExpression);
|
|
1573
|
+
// the blocks that lead to the loop, outermost first
|
|
1574
|
+
const levels = [];
|
|
1575
|
+
const locate = (expression) => {
|
|
1576
|
+
if (expression === loopExpression)
|
|
1577
|
+
return true;
|
|
1578
|
+
const v = info(expression);
|
|
1579
|
+
if (v.id !== binaryen.BlockId)
|
|
1580
|
+
return false;
|
|
1581
|
+
for (let i = 0; i < v.children.length; i++) {
|
|
1582
|
+
if (!locate(v.children[i]))
|
|
1583
|
+
continue;
|
|
1584
|
+
levels.unshift({ children: v.children, index: i });
|
|
1585
|
+
return true;
|
|
1586
|
+
}
|
|
1587
|
+
return false;
|
|
1588
|
+
};
|
|
1589
|
+
if (!locate(f.body))
|
|
1590
|
+
return { refusals: ['the loop is not reached through blocks'] };
|
|
1591
|
+
const before = levels.flatMap((level) => level.children.slice(0, level.index));
|
|
1592
|
+
const after = levels.flatMap((level) => level.children.slice(level.index + 1));
|
|
1593
|
+
// the trip count: a countdown that ends on its own decrement
|
|
1594
|
+
const counter = countdownLocal(loop);
|
|
1595
|
+
if (counter === undefined || localTypes[counter] !== binaryen.i32)
|
|
1596
|
+
return { refusals: ['trip count is not computable before the loop starts'] };
|
|
1597
|
+
const loopWrites = new Map();
|
|
1598
|
+
const countLoopWrites = (expression) => {
|
|
1599
|
+
const v = info(expression);
|
|
1600
|
+
if (v.id === binaryen.LocalSetId)
|
|
1601
|
+
loopWrites.set(v.index, (loopWrites.get(v.index) ?? 0) + 1);
|
|
1602
|
+
for (const child of children(v))
|
|
1603
|
+
countLoopWrites(child);
|
|
1604
|
+
};
|
|
1605
|
+
countLoopWrites(loopExpression);
|
|
1606
|
+
if ((loopWrites.get(counter) ?? 0) !== 1)
|
|
1607
|
+
return { refusals: ['trip count is not computable before the loop starts'] };
|
|
1608
|
+
// the loop body, the trailing stride updates and the back edge; a step is a constant or an i32
|
|
1609
|
+
// local the loop never writes
|
|
1610
|
+
const body = info(loop.body);
|
|
1611
|
+
const statements = body.id === binaryen.BlockId ? body.children : [loop.body];
|
|
1612
|
+
const stepIsInvariant = (update) => update.strideLocal === undefined ||
|
|
1613
|
+
(localTypes[update.strideLocal] === binaryen.i32 && !loopWrites.get(update.strideLocal));
|
|
1614
|
+
const updates = new Map([[counter, { local: counter, stride: -1 }]]);
|
|
1615
|
+
let end = statements.length - 1;
|
|
1616
|
+
while (end > 0) {
|
|
1617
|
+
const update = strideUpdate(statements[end - 1]);
|
|
1618
|
+
if (!update ||
|
|
1619
|
+
updates.has(update.local) ||
|
|
1620
|
+
localTypes[update.local] !== binaryen.i32 ||
|
|
1621
|
+
loopWrites.get(update.local) !== 1 ||
|
|
1622
|
+
!stepIsInvariant(update))
|
|
1623
|
+
break;
|
|
1624
|
+
updates.set(update.local, update);
|
|
1625
|
+
end--;
|
|
1626
|
+
}
|
|
1627
|
+
// top-level updates that stay in the body: iterate runs them, so later statements see the new
|
|
1628
|
+
// value; a stride-shaped statement that cannot stay names why, used if its local is carried
|
|
1629
|
+
const midUpdates = new Map();
|
|
1630
|
+
const shaped = new Map();
|
|
1631
|
+
const scanUpdates = (expression, top, conditional) => {
|
|
1632
|
+
const v = info(expression);
|
|
1633
|
+
const update = v.id === binaryen.LocalSetId ? strideUpdate(expression) : undefined;
|
|
1634
|
+
if (update && update.local !== counter && localTypes[update.local] === binaryen.i32) {
|
|
1635
|
+
if (!top)
|
|
1636
|
+
shaped.set(update.local, conditional
|
|
1637
|
+
? 'stride update is conditional'
|
|
1638
|
+
: 'stride update is not at the top level of the body');
|
|
1639
|
+
else if (loopWrites.get(update.local) !== 1)
|
|
1640
|
+
shaped.set(update.local, 'stride update of a local the loop also writes elsewhere');
|
|
1641
|
+
else if (!stepIsInvariant(update))
|
|
1642
|
+
shaped.set(update.local, 'stride is written in the loop');
|
|
1643
|
+
else
|
|
1644
|
+
midUpdates.set(update.local, update);
|
|
1645
|
+
}
|
|
1646
|
+
for (const child of children(v))
|
|
1647
|
+
scanUpdates(child, false, conditional || v.id === binaryen.IfId);
|
|
1648
|
+
};
|
|
1649
|
+
for (const statement of statements.slice(0, end))
|
|
1650
|
+
scanUpdates(statement, true, false);
|
|
1651
|
+
for (const [local, update] of midUpdates)
|
|
1652
|
+
updates.set(local, update);
|
|
1653
|
+
const spans = [];
|
|
1654
|
+
for (const statement of statements.slice(0, end)) {
|
|
1655
|
+
const summary = summarizeSpan(statement, {
|
|
1656
|
+
call: (target) => members.has(target),
|
|
1657
|
+
global: (name) => name === stack || constants.has(name)
|
|
1658
|
+
});
|
|
1659
|
+
for (const reason of summary.refusals)
|
|
1660
|
+
refuse.add(reason);
|
|
1661
|
+
spans.push(summary.span);
|
|
1662
|
+
}
|
|
1663
|
+
if (!spans.length)
|
|
1664
|
+
refuse.add('empty loop body');
|
|
1665
|
+
// the frame: `sp = global - size` in the owner's prologue
|
|
1666
|
+
let frame;
|
|
1667
|
+
if (stack) {
|
|
1668
|
+
const frameOf = (expression) => {
|
|
1669
|
+
const sub = info(expression);
|
|
1670
|
+
if (sub.id !== binaryen.BinaryId || sub.op !== binaryen.SubInt32)
|
|
1671
|
+
return;
|
|
1672
|
+
const [from, size] = [info(sub.left), info(sub.right)];
|
|
1673
|
+
if (from.id === binaryen.GlobalGetId &&
|
|
1674
|
+
from.name === stack &&
|
|
1675
|
+
size.id === binaryen.ConstId)
|
|
1676
|
+
return size.value;
|
|
1677
|
+
};
|
|
1678
|
+
const top = levels[0] ? levels[0].children.slice(0, levels[0].index) : [];
|
|
1679
|
+
for (const [i, statement] of top.entries()) {
|
|
1680
|
+
const v = info(statement);
|
|
1681
|
+
if (v.id === binaryen.GlobalSetId && v.name === stack) {
|
|
1682
|
+
const tee = info(v.value);
|
|
1683
|
+
const size = tee.id === binaryen.LocalSetId && tee.isTee ? frameOf(tee.value) : undefined;
|
|
1684
|
+
if (size !== undefined)
|
|
1685
|
+
frame = { global: stack, size, local: tee.index, copy: true };
|
|
1686
|
+
}
|
|
1687
|
+
if (v.id === binaryen.LocalSetId && !v.isTee && frameOf(v.value) !== undefined) {
|
|
1688
|
+
const next = top[i + 1] ? info(top[i + 1]) : undefined;
|
|
1689
|
+
const value = next?.id === binaryen.GlobalSetId ? info(next.value) : undefined;
|
|
1690
|
+
if (next?.name === stack &&
|
|
1691
|
+
value?.id === binaryen.LocalGetId &&
|
|
1692
|
+
value.index === v.index)
|
|
1693
|
+
frame = { global: stack, size: frameOf(v.value), local: v.index, copy: true };
|
|
1694
|
+
}
|
|
1695
|
+
if (frame)
|
|
1696
|
+
break;
|
|
1697
|
+
}
|
|
1698
|
+
if (frame && (writes.get(frame.local) ?? 0) !== 1)
|
|
1699
|
+
frame = undefined;
|
|
1700
|
+
}
|
|
1701
|
+
// locals that hold or derive from a frame address
|
|
1702
|
+
const tainted = new Set(frame ? [frame.local] : []);
|
|
1703
|
+
const derives = (expression) => {
|
|
1704
|
+
const v = info(expression);
|
|
1705
|
+
if (v.id === binaryen.LocalGetId && tainted.has(v.index))
|
|
1706
|
+
return true;
|
|
1707
|
+
if (v.id === binaryen.GlobalGetId && v.name === stack)
|
|
1708
|
+
return true;
|
|
1709
|
+
return children(v).some(derives);
|
|
1710
|
+
};
|
|
1711
|
+
for (let changed = true; changed;) {
|
|
1712
|
+
changed = false;
|
|
1713
|
+
const grow = (expression) => {
|
|
1714
|
+
const v = info(expression);
|
|
1715
|
+
if (v.id === binaryen.LocalSetId && !tainted.has(v.index) && derives(v.value)) {
|
|
1716
|
+
tainted.add(v.index);
|
|
1717
|
+
changed = true;
|
|
1718
|
+
}
|
|
1719
|
+
for (const child of children(v))
|
|
1720
|
+
grow(child);
|
|
1721
|
+
};
|
|
1722
|
+
grow(f.body);
|
|
1723
|
+
}
|
|
1724
|
+
const callsMember = (expression) => {
|
|
1725
|
+
const v = info(expression);
|
|
1726
|
+
return (v.id === binaryen.CallId && members.has(v.target)) || children(v).some(callsMember);
|
|
1727
|
+
};
|
|
1728
|
+
if (spans.length && !spans.some((span) => callsMember(span.expression)))
|
|
1729
|
+
refuse.add('no call into the worker set in the loop body');
|
|
1730
|
+
for (const span of spans) {
|
|
1731
|
+
const check = (expression) => {
|
|
1732
|
+
const v = info(expression);
|
|
1733
|
+
if (v.id === binaryen.StoreId && derives(v.value))
|
|
1734
|
+
refuse.add('frame address stored to memory');
|
|
1735
|
+
if ((v.id === binaryen.CallIndirectId ||
|
|
1736
|
+
(v.id === binaryen.CallId && !members.has(v.target))) &&
|
|
1737
|
+
children(v).some(derives))
|
|
1738
|
+
refuse.add('frame address passed outside the worker set');
|
|
1739
|
+
if (v.id === binaryen.GlobalGetId && v.name === stack && !frame)
|
|
1740
|
+
refuse.add('stack global read in the body without a frame');
|
|
1741
|
+
for (const child of children(v))
|
|
1742
|
+
check(child);
|
|
1743
|
+
};
|
|
1744
|
+
check(span.expression);
|
|
1745
|
+
}
|
|
1746
|
+
// what runs after the loop must not read the frame; the epilogue restore is the one allowed use
|
|
1747
|
+
const afterReads = new Map();
|
|
1748
|
+
const inspect = (expression) => {
|
|
1749
|
+
const v = info(expression);
|
|
1750
|
+
if (frame && v.id === binaryen.GlobalSetId && v.name === stack) {
|
|
1751
|
+
const sum = info(v.value);
|
|
1752
|
+
const [a, b] = sum.id === binaryen.BinaryId ? [info(sum.left), info(sum.right)] : [];
|
|
1753
|
+
if (sum.op === binaryen.AddInt32 &&
|
|
1754
|
+
a?.id === binaryen.LocalGetId &&
|
|
1755
|
+
a.index === frame.local &&
|
|
1756
|
+
b?.id === binaryen.ConstId &&
|
|
1757
|
+
b.value === frame.size)
|
|
1758
|
+
return;
|
|
1759
|
+
}
|
|
1760
|
+
if (v.id === binaryen.LocalGetId) {
|
|
1761
|
+
afterReads.set(v.index, (afterReads.get(v.index) ?? 0) + 1);
|
|
1762
|
+
if (tainted.has(v.index))
|
|
1763
|
+
refuse.add('frame object read after the loop');
|
|
1764
|
+
}
|
|
1765
|
+
for (const child of children(v))
|
|
1766
|
+
inspect(child);
|
|
1767
|
+
};
|
|
1768
|
+
for (const statement of after)
|
|
1769
|
+
inspect(statement);
|
|
1770
|
+
const bodyWrites = new Set(spans.flatMap((span) => [...span.writes]));
|
|
1771
|
+
const carried = carriedLocals(spans, totalReads);
|
|
1772
|
+
for (const local of bodyWrites) {
|
|
1773
|
+
if (midUpdates.has(local))
|
|
1774
|
+
continue;
|
|
1775
|
+
if (afterReads.get(local))
|
|
1776
|
+
refuse.add('loop body writes a local the code after the loop reads');
|
|
1777
|
+
else if (carried.has(local)) {
|
|
1778
|
+
refuse.add('carried local is not a constant stride');
|
|
1779
|
+
const why = shaped.get(local);
|
|
1780
|
+
if (why)
|
|
1781
|
+
refuse.add(why);
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
// the owner sets a constant-stride local to its loop-exit value, and has no way to do that for a
|
|
1785
|
+
// step that is held in a local
|
|
1786
|
+
for (const [local, update] of updates)
|
|
1787
|
+
if (update.strideLocal !== undefined && afterReads.get(local))
|
|
1788
|
+
refuse.add('local advanced by a run-time stride is read after the loop');
|
|
1789
|
+
// classify what the body reads
|
|
1790
|
+
const bodyReads = new Set(spans.flatMap((span) => [...span.reads.keys()]));
|
|
1791
|
+
const entry = (local) => {
|
|
1792
|
+
const outside = (writes.get(local) ?? 0) - (loopWrites.get(local) ?? 0);
|
|
1793
|
+
if (local < parameterCount && outside === 0)
|
|
1794
|
+
return { parameter: local };
|
|
1795
|
+
if (outside !== 1)
|
|
1796
|
+
return;
|
|
1797
|
+
for (const statement of [...before].reverse()) {
|
|
1798
|
+
const v = info(statement);
|
|
1799
|
+
if (v.id !== binaryen.LocalSetId || v.isTee || v.index !== local)
|
|
1800
|
+
continue;
|
|
1801
|
+
const value = info(v.value);
|
|
1802
|
+
if (value.id === binaryen.ConstId && typeof value.value === 'number')
|
|
1803
|
+
return { constant: value.value >>> 0 };
|
|
1804
|
+
if (value.id === binaryen.LocalGetId &&
|
|
1805
|
+
value.index < parameterCount &&
|
|
1806
|
+
!writes.get(value.index))
|
|
1807
|
+
return { parameter: value.index };
|
|
1808
|
+
return;
|
|
1809
|
+
}
|
|
1810
|
+
};
|
|
1811
|
+
const strided = [];
|
|
1812
|
+
const invariant = [];
|
|
1813
|
+
for (const [local, update] of [...updates].sort(([a], [b]) => a - b))
|
|
1814
|
+
strided.push({
|
|
1815
|
+
local,
|
|
1816
|
+
kind: local === counter ? 'induction' : 'stride',
|
|
1817
|
+
start: entry(local),
|
|
1818
|
+
stride: update.stride,
|
|
1819
|
+
...(update.strideLocal === undefined ? {} : { strideLocal: update.strideLocal })
|
|
1820
|
+
});
|
|
1821
|
+
for (const local of [...bodyReads].sort((a, b) => a - b)) {
|
|
1822
|
+
if (bodyWrites.has(local) || updates.has(local) || local === frame?.local)
|
|
1823
|
+
continue;
|
|
1824
|
+
if ((loopWrites.get(local) ?? 0) > 0)
|
|
1825
|
+
refuse.add('carried local is not a constant stride');
|
|
1826
|
+
invariant.push({ local, kind: 'invariant', start: entry(local) });
|
|
1827
|
+
}
|
|
1828
|
+
// a step local the body does not read still needs its loop-entry value, so `iterate` takes it too
|
|
1829
|
+
const steps = new Set([...updates.values()].flatMap((update) => update.strideLocal === undefined ? [] : [update.strideLocal]));
|
|
1830
|
+
for (const local of [...steps].sort((a, b) => a - b))
|
|
1831
|
+
if (!invariant.some((item) => item.local === local))
|
|
1832
|
+
invariant.push({ local, kind: 'invariant', start: entry(local) });
|
|
1833
|
+
invariant.sort((a, b) => a.local - b.local);
|
|
1834
|
+
for (const item of [...strided, ...invariant]) {
|
|
1835
|
+
if (bodyReads.has(item.local) && tainted.has(item.local))
|
|
1836
|
+
refuse.add('frame address held in a local the body reads');
|
|
1837
|
+
if (steps.has(item.local) && tainted.has(item.local))
|
|
1838
|
+
refuse.add('frame address used as a run-time stride');
|
|
1839
|
+
}
|
|
1840
|
+
if (refuse.size)
|
|
1841
|
+
return { refusals: [...refuse] };
|
|
1842
|
+
// `iterate`: strided and invariant locals in order, then the frame base, then the body's temporaries
|
|
1843
|
+
const parameters = [...strided, ...invariant].filter((item) => bodyReads.has(item.local) || steps.has(item.local));
|
|
1844
|
+
const order = [...parameters.map((item) => item.local), ...(frame ? [frame.local] : [])];
|
|
1845
|
+
const temporaries = [...new Set(spans.flatMap((span) => [...span.locals]))]
|
|
1846
|
+
.filter((local) => !order.includes(local))
|
|
1847
|
+
.sort((a, b) => a - b);
|
|
1848
|
+
const positions = new Map([...order, ...temporaries].map((local, index) => [local, index]));
|
|
1849
|
+
parameters.forEach((item, index) => (item.argument = index));
|
|
1850
|
+
const memory = m.getMemoryInfo();
|
|
1851
|
+
const internalMemory = memoryName(m);
|
|
1852
|
+
const w = new binaryen.Module();
|
|
1853
|
+
try {
|
|
1854
|
+
w.setFeatures(m.getFeatures() | binaryen.Features.Atomics);
|
|
1855
|
+
w.setMemory(memory.initial, memory.max ?? 65536, null, [], true, false, internalMemory);
|
|
1856
|
+
w.addMemoryImport(internalMemory, 'env', 'memory', true);
|
|
1857
|
+
if (stack)
|
|
1858
|
+
w.addGlobal(stack, binaryen.i32, true, w.i32.const(context.route.stack.initial));
|
|
1859
|
+
for (const name of constants) {
|
|
1860
|
+
const g = binaryen.getGlobalInfo(m.getGlobal(name));
|
|
1861
|
+
w.addGlobal(name, g.type, false, w.copyExpression(g.init));
|
|
1862
|
+
}
|
|
1863
|
+
for (const name of context.route.redirect ?? []) {
|
|
1864
|
+
w.addGlobal(name, binaryen.i32, true, w.i32.const(0));
|
|
1865
|
+
w.addGlobalExport(name, name);
|
|
1866
|
+
}
|
|
1867
|
+
for (const name of context.route.functions) {
|
|
1868
|
+
const g = binaryen.getFunctionInfo(m.getFunction(name));
|
|
1869
|
+
w.addFunction(name, g.params, g.results, g.vars, w.copyExpression(g.body));
|
|
1870
|
+
}
|
|
1871
|
+
// same slots as the owner's table; a function outside the set gets a stub that no member
|
|
1872
|
+
// can reach, because the route took every function of a called type into the set
|
|
1873
|
+
const shapes = context.route.tables.length
|
|
1874
|
+
? tableShapes(m).shapes
|
|
1875
|
+
: new Map();
|
|
1876
|
+
for (const name of context.route.tables) {
|
|
1877
|
+
const table = binaryen.getTableInfo(m.getTable(name));
|
|
1878
|
+
w.addTable(name, table.initial, table.max ?? 0xffffffff);
|
|
1879
|
+
for (const run of shapes.get(name).runs) {
|
|
1880
|
+
for (const entry of run.names)
|
|
1881
|
+
if (!w.getFunction(entry)) {
|
|
1882
|
+
const g = binaryen.getFunctionInfo(m.getFunction(entry));
|
|
1883
|
+
w.addFunction(entry, g.params, g.results, [], w.unreachable());
|
|
1884
|
+
}
|
|
1885
|
+
w.addActiveElementSegment(name, run.name, run.names, w.i32.const(run.offset));
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
let iterate = '__folio_iterate';
|
|
1889
|
+
while (w.getFunction(iterate))
|
|
1890
|
+
iterate += '_';
|
|
1891
|
+
const block = w.block(null, spans.map((span) => w.copyExpression(span.expression)));
|
|
1892
|
+
remapLocals(block, positions);
|
|
1893
|
+
w.addFunction(iterate, binaryen.createType(order.map((local) => localTypes[local])), binaryen.none, temporaries.map((local) => localTypes[local]), block);
|
|
1894
|
+
w.addFunctionExport(iterate, iterate);
|
|
1895
|
+
const stackExport = '__folio_stack';
|
|
1896
|
+
if (stack)
|
|
1897
|
+
w.addGlobalExport(stack, stackExport);
|
|
1898
|
+
if (!w.validate())
|
|
1899
|
+
throw new Error('invalid iteration module');
|
|
1900
|
+
// a redirect keeps names so the stack route still sets its delta globals aside
|
|
1901
|
+
const named = Boolean(context.route.redirect?.length);
|
|
1902
|
+
const before = binaryen.getDebugInfo();
|
|
1903
|
+
if (named)
|
|
1904
|
+
binaryen.setDebugInfo(true);
|
|
1905
|
+
let bytes;
|
|
1906
|
+
try {
|
|
1907
|
+
bytes = w.emitBinary();
|
|
1908
|
+
}
|
|
1909
|
+
finally {
|
|
1910
|
+
binaryen.setDebugInfo(before);
|
|
1911
|
+
}
|
|
1912
|
+
new WebAssembly.Module(Uint8Array.from(bytes));
|
|
1913
|
+
const trip = entry(counter);
|
|
1914
|
+
const locals = [
|
|
1915
|
+
...strided,
|
|
1916
|
+
...invariant,
|
|
1917
|
+
...(frame
|
|
1918
|
+
? [{ local: frame.local, kind: 'frame', argument: order.length - 1 }]
|
|
1919
|
+
: [])
|
|
1920
|
+
];
|
|
1921
|
+
const diagnostics = [
|
|
1922
|
+
`owner ${f.name}: countdown loop on local ${counter}, ${spans.length} body statements, ${order.length} arguments`
|
|
1923
|
+
];
|
|
1924
|
+
if (frame)
|
|
1925
|
+
diagnostics.push(`frame ${frame.global} - ${frame.size} in local ${frame.local}`);
|
|
1926
|
+
return {
|
|
1927
|
+
refusals: [],
|
|
1928
|
+
route: {
|
|
1929
|
+
eligible: true,
|
|
1930
|
+
owner: f.name,
|
|
1931
|
+
loop: loop.name || undefined,
|
|
1932
|
+
trip: { local: counter, ...(trip ?? {}) },
|
|
1933
|
+
locals,
|
|
1934
|
+
frame,
|
|
1935
|
+
iterate,
|
|
1936
|
+
stackExport: stack ? stackExport : undefined,
|
|
1937
|
+
module: bytes,
|
|
1938
|
+
diagnostics
|
|
1939
|
+
}
|
|
1940
|
+
};
|
|
1941
|
+
}
|
|
1942
|
+
finally {
|
|
1943
|
+
w.dispose();
|
|
1944
|
+
}
|
|
1945
|
+
}
|