@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
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/** Granule sizes of the inline lowering the iteration executor runs. */
|
|
2
|
+
export const iterationGranules = { read: 64, write: 32 };
|
|
3
|
+
/**
|
|
4
|
+
* Undo entries per iteration of the grown log, 64 times the default capacity: 4 MiB of tracked
|
|
5
|
+
* writes. A batch whose log is exhausted is retried once at this size when the memory can grow.
|
|
6
|
+
*/
|
|
7
|
+
export const iterationLogCeiling = 131072;
|
|
8
|
+
/** Bytes of one undo entry: granule address, stamp, then the prior bytes of the granule. */
|
|
9
|
+
export const iterationEntryBytes = 8 + iterationGranules.write;
|
|
10
|
+
/** Words of a read bitmap one summary byte covers. */
|
|
11
|
+
export const iterationSummaryWords = 64;
|
|
12
|
+
/** Log2 of the bytes of address space one summary byte covers: 64 words of 32 granules. */
|
|
13
|
+
export const iterationSummaryShift = Math.log2(iterationGranules.read) + 11;
|
|
14
|
+
/** Bytes of slot-private windows a Folio reserves room for in its arena. */
|
|
15
|
+
export const iterationWindowBytes = 24576;
|
|
16
|
+
const align = (value, to) => Math.ceil(value / to) * to;
|
|
17
|
+
/**
|
|
18
|
+
* Shadow of one tracked frame: `4 * ceil(size / 32)` bytes of written bits, one per frame byte
|
|
19
|
+
* (the bits of frame granule `g`, 32 bytes, are the i32 at `4 * g`), 4 pad bytes, the exposed bits
|
|
20
|
+
* in the same layout, and 4 pad bytes.
|
|
21
|
+
*/
|
|
22
|
+
export function iterationFrameShadow(frameBytes) {
|
|
23
|
+
if (!frameBytes)
|
|
24
|
+
return { exposed: 0, bytes: 0 };
|
|
25
|
+
const granules = Math.ceil(frameBytes / 32);
|
|
26
|
+
const exposed = 4 * granules + 4;
|
|
27
|
+
return { exposed, bytes: align(exposed + 4 * granules + 4, 64) };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Lays out slots, the stamp word, undo logs and bitmaps from `base`. The bitmaps cover the whole
|
|
31
|
+
* memory, arena included, so `bytes` depends on `memoryBytes` and not on `base`.
|
|
32
|
+
*/
|
|
33
|
+
export function iterationArena(base, memoryBytes, options = {}) {
|
|
34
|
+
const batch = options.batch ?? 32;
|
|
35
|
+
const logCapacity = options.logCapacity ?? 2048;
|
|
36
|
+
const slotBytes = options.slotBytes ?? 32768;
|
|
37
|
+
const frameBytes = options.frameBytes ?? 0;
|
|
38
|
+
const windowBytes = options.windowBytes ?? 0;
|
|
39
|
+
if (!Number.isSafeInteger(frameBytes) || frameBytes < 0)
|
|
40
|
+
throw new RangeError('invalid frame');
|
|
41
|
+
if (!Number.isSafeInteger(windowBytes) || windowBytes < 0 || windowBytes % 64)
|
|
42
|
+
throw new RangeError('invalid window size');
|
|
43
|
+
if (!Number.isSafeInteger(base) || base < 0 || base % 64)
|
|
44
|
+
throw new RangeError('invalid base');
|
|
45
|
+
if (!Number.isSafeInteger(memoryBytes) || memoryBytes <= 0)
|
|
46
|
+
throw new RangeError('invalid memory size');
|
|
47
|
+
if (!Number.isSafeInteger(batch) || batch < 1 || batch > 1024)
|
|
48
|
+
throw new RangeError('invalid batch');
|
|
49
|
+
if (!Number.isSafeInteger(logCapacity) || logCapacity < 1)
|
|
50
|
+
throw new RangeError('invalid log capacity');
|
|
51
|
+
if (!Number.isSafeInteger(slotBytes) || slotBytes < 64 || slotBytes % 64)
|
|
52
|
+
throw new RangeError('invalid slot size');
|
|
53
|
+
const slots = base + slotBytes;
|
|
54
|
+
const stamp = slots + batch * slotBytes;
|
|
55
|
+
const logs = stamp + 64;
|
|
56
|
+
const logBytes = align(logCapacity * iterationEntryBytes, 64);
|
|
57
|
+
const rmaps = logs + batch * logBytes;
|
|
58
|
+
const rsize = align(Math.ceil(memoryBytes / (iterationGranules.read * 8)), 64);
|
|
59
|
+
const wmaps = rmaps + batch * rsize;
|
|
60
|
+
const wsize = align(Math.ceil(memoryBytes / (iterationGranules.write * 8)), 64);
|
|
61
|
+
const frameShadow = wmaps + batch * wsize;
|
|
62
|
+
// a window copy sits below the frame, up to 63 bytes of padding apart, and both are shadowed
|
|
63
|
+
const frameSlotBytes = iterationFrameShadow(frameBytes + (windowBytes ? windowBytes + 64 : 0)).bytes;
|
|
64
|
+
const frameEnd = frameShadow + batch * frameSlotBytes;
|
|
65
|
+
let ssize = 0;
|
|
66
|
+
if (options.readSummary) {
|
|
67
|
+
const blocks = Math.ceil(rsize / 4 / iterationSummaryWords);
|
|
68
|
+
ssize = Math.max(64, 2 ** Math.ceil(Math.log2(blocks)));
|
|
69
|
+
}
|
|
70
|
+
const end = frameEnd + batch * ssize;
|
|
71
|
+
return {
|
|
72
|
+
base,
|
|
73
|
+
bytes: end - base,
|
|
74
|
+
memoryBytes,
|
|
75
|
+
batch,
|
|
76
|
+
logCapacity,
|
|
77
|
+
slotBytes,
|
|
78
|
+
slots,
|
|
79
|
+
stamp,
|
|
80
|
+
logs,
|
|
81
|
+
logBytes,
|
|
82
|
+
rmaps,
|
|
83
|
+
rsize,
|
|
84
|
+
wmaps,
|
|
85
|
+
wsize,
|
|
86
|
+
frameShadow,
|
|
87
|
+
frameSlotBytes,
|
|
88
|
+
frameBytes,
|
|
89
|
+
windowBytes,
|
|
90
|
+
smaps: ssize ? frameEnd : 0,
|
|
91
|
+
ssize
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** Bytes the caller must keep free for a pool over a memory of `memoryBytes`. */
|
|
95
|
+
export const iterationArenaBytes = (memoryBytes, options = {}) => iterationArena(0, memoryBytes, options).bytes;
|
|
96
|
+
const PAGE = 65536;
|
|
97
|
+
/**
|
|
98
|
+
* Smallest memory of whole pages that holds the arena above `startBytes`. The bitmaps cover the
|
|
99
|
+
* whole memory, so the size is a fixed point: it grows until the arena it needs fits. Undefined
|
|
100
|
+
* when the result would pass 65,536 pages.
|
|
101
|
+
*/
|
|
102
|
+
export function iterationFit(startBytes, options = {}) {
|
|
103
|
+
const base = align(startBytes, 64);
|
|
104
|
+
let pages = Math.ceil((base + PAGE) / PAGE);
|
|
105
|
+
for (;;) {
|
|
106
|
+
if (pages > PAGE)
|
|
107
|
+
return;
|
|
108
|
+
const bytes = iterationArenaBytes(pages * PAGE, options);
|
|
109
|
+
if (base + bytes <= pages * PAGE)
|
|
110
|
+
return { base, pages, bytes, memoryBytes: pages * PAGE };
|
|
111
|
+
pages = Math.ceil((base + bytes) / PAGE);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Arguments of `iterate` for iteration `k`. A strided local is `entry + k * stride` modulo 2^32, or
|
|
116
|
+
* `entry + k * stride * entry[strideLocal]` when its step is held in a local, an invariant local
|
|
117
|
+
* keeps its entry value, and the frame local takes `frame`.
|
|
118
|
+
*/
|
|
119
|
+
export function iterationArguments(locals, entry, k, frame) {
|
|
120
|
+
const values = [];
|
|
121
|
+
for (const item of locals) {
|
|
122
|
+
if (item.argument === undefined)
|
|
123
|
+
continue;
|
|
124
|
+
if (item.kind === 'frame') {
|
|
125
|
+
values[item.argument] = frame;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const from = entry[item.local];
|
|
129
|
+
if (from === undefined)
|
|
130
|
+
throw new RangeError(`no loop-entry value for local ${item.local}`);
|
|
131
|
+
if (item.strideLocal !== undefined) {
|
|
132
|
+
const step = entry[item.strideLocal];
|
|
133
|
+
if (step === undefined)
|
|
134
|
+
throw new RangeError(`no loop-entry value for local ${item.strideLocal}`);
|
|
135
|
+
const advance = Math.imul(k, Number(step));
|
|
136
|
+
values[item.argument] = (Number(from) + (item.stride < 0 ? -advance : advance)) | 0;
|
|
137
|
+
}
|
|
138
|
+
else
|
|
139
|
+
values[item.argument] = item.stride ? (Number(from) + k * item.stride) | 0 : from;
|
|
140
|
+
}
|
|
141
|
+
return values;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Where one slot keeps the private copies of certified windows: each below the one before it, the
|
|
145
|
+
* first below the frame, each at its window's offset modulo 64. `low` is the lowest address used,
|
|
146
|
+
* where the slot's stack may start.
|
|
147
|
+
*/
|
|
148
|
+
export function iterationShadowCopies(top, frameSize, windows) {
|
|
149
|
+
let at = top - frameSize;
|
|
150
|
+
const copies = windows.map((w) => {
|
|
151
|
+
const base = at - (w.end - w.low);
|
|
152
|
+
const copy = base - ((((base - w.low) % 64) + 64) % 64);
|
|
153
|
+
at = copy;
|
|
154
|
+
return { copy, delta: copy - w.low };
|
|
155
|
+
});
|
|
156
|
+
return { copies, low: at };
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Where one slot keeps its copy of a window: below the frame, so that the copy and the frame form
|
|
160
|
+
* one shadowed region from `copy` to the slot's top. The copy sits at the same offset modulo 64 as
|
|
161
|
+
* the window, so accesses keep their alignment. `frameOffset` is the frame's offset in the region.
|
|
162
|
+
*/
|
|
163
|
+
export function iterationWindowCopy(top, frameSize, window) {
|
|
164
|
+
const bytes = window.end - window.low;
|
|
165
|
+
const frame = top - frameSize;
|
|
166
|
+
const base = frame - bytes;
|
|
167
|
+
const copy = base - ((((base - window.low) % 64) + 64) % 64);
|
|
168
|
+
return { copy, delta: copy - window.low, region: top - copy, frameOffset: frame - copy };
|
|
169
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { IterationRoute } from './extract.ts';
|
|
2
|
+
/** Why the iteration route is not used, in `ExecutionReport.reason`. */
|
|
3
|
+
export type IterationReason = 'no-arena' | 'memory-size-observed' | 'import-order' | 'workers-unavailable' | 'trip-zero' | 'no-admission' | 'log-exhausted' | 'frame-carried dependence' | 'stalled';
|
|
4
|
+
/** The owner module with its loop handed to `iterate`, and what the host needs to bind it. */
|
|
5
|
+
export interface IterationOwner {
|
|
6
|
+
module: Uint8Array;
|
|
7
|
+
/** import module of the host function; the function is `iterate` */
|
|
8
|
+
namespace: string;
|
|
9
|
+
/** the owner local passed as each argument of `iterate`, in order */
|
|
10
|
+
locals: number[];
|
|
11
|
+
/** export of the owner's stack global, when the module has one */
|
|
12
|
+
stackExport?: string;
|
|
13
|
+
/** the memory import, as declared by the original module */
|
|
14
|
+
memory: {
|
|
15
|
+
module: string;
|
|
16
|
+
name: string;
|
|
17
|
+
initial: number;
|
|
18
|
+
maximum?: number;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** Reads the one imported memory's limits from the import section. */
|
|
22
|
+
export declare function importedMemory(bytes: Uint8Array): {
|
|
23
|
+
module: string;
|
|
24
|
+
name: string;
|
|
25
|
+
flags: number;
|
|
26
|
+
initial: number;
|
|
27
|
+
maximum?: number;
|
|
28
|
+
limits: [number, number];
|
|
29
|
+
header: number;
|
|
30
|
+
body: [number, number];
|
|
31
|
+
}[];
|
|
32
|
+
/** Rewrites the memory import limits to shared with the largest maximum. */
|
|
33
|
+
export declare function shareMemory(bytes: Uint8Array, memory: ReturnType<typeof importedMemory>[number]): Uint8Array<ArrayBuffer>;
|
|
34
|
+
/**
|
|
35
|
+
* Builds the owner module of an iteration route: the memory import is shared, and the one loop
|
|
36
|
+
* of the owner function becomes `if (iterate(entry locals...)) { final values } else { the loop }`.
|
|
37
|
+
* The host function returns 1 after it ran every iteration and 0 to leave the original loop to
|
|
38
|
+
* run in the owner. A refusal names its reason and never throws.
|
|
39
|
+
*/
|
|
40
|
+
export declare function iterationOwner(original: Uint8Array, route: IterationRoute, stack?: string): {
|
|
41
|
+
owner: IterationOwner;
|
|
42
|
+
} | {
|
|
43
|
+
reason: IterationReason;
|
|
44
|
+
detail: string;
|
|
45
|
+
};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import binaryen from 'binaryen';
|
|
2
|
+
import { restoreImportOrder } from "./import-order.js";
|
|
3
|
+
const PAGE_LIMIT = 65536;
|
|
4
|
+
function uleb(value) {
|
|
5
|
+
const out = [];
|
|
6
|
+
do {
|
|
7
|
+
let byte = value % 128;
|
|
8
|
+
value = Math.floor(value / 128);
|
|
9
|
+
if (value)
|
|
10
|
+
byte |= 128;
|
|
11
|
+
out.push(byte);
|
|
12
|
+
} while (value);
|
|
13
|
+
return out;
|
|
14
|
+
}
|
|
15
|
+
/** Reads the one imported memory's limits from the import section. */
|
|
16
|
+
export function importedMemory(bytes) {
|
|
17
|
+
let at = 8;
|
|
18
|
+
const uint = () => {
|
|
19
|
+
let value = 0, shift = 0, byte;
|
|
20
|
+
do {
|
|
21
|
+
byte = bytes[at++];
|
|
22
|
+
value += (byte & 127) * 2 ** shift;
|
|
23
|
+
shift += 7;
|
|
24
|
+
} while (byte & 128);
|
|
25
|
+
return value;
|
|
26
|
+
};
|
|
27
|
+
const name = () => {
|
|
28
|
+
const length = uint();
|
|
29
|
+
const text = new TextDecoder().decode(bytes.subarray(at, at + length));
|
|
30
|
+
at += length;
|
|
31
|
+
return text;
|
|
32
|
+
};
|
|
33
|
+
const reference = () => {
|
|
34
|
+
const type = bytes[at++];
|
|
35
|
+
if (type === 0x63 || type === 0x64)
|
|
36
|
+
uint();
|
|
37
|
+
};
|
|
38
|
+
while (at < bytes.length) {
|
|
39
|
+
const header = at;
|
|
40
|
+
const id = bytes[at++];
|
|
41
|
+
const length = uint();
|
|
42
|
+
const sectionEnd = at + length;
|
|
43
|
+
const sectionStart = at;
|
|
44
|
+
if (id !== 2) {
|
|
45
|
+
at = sectionEnd;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const count = uint();
|
|
49
|
+
const found = [];
|
|
50
|
+
for (let i = 0; i < count; i++) {
|
|
51
|
+
const module = name();
|
|
52
|
+
const base = name();
|
|
53
|
+
const kind = bytes[at++];
|
|
54
|
+
if (kind === 0)
|
|
55
|
+
uint();
|
|
56
|
+
else if (kind === 1) {
|
|
57
|
+
reference();
|
|
58
|
+
const flags = uint();
|
|
59
|
+
uint();
|
|
60
|
+
if (flags & 1)
|
|
61
|
+
uint();
|
|
62
|
+
}
|
|
63
|
+
else if (kind === 2) {
|
|
64
|
+
const from = at;
|
|
65
|
+
const flags = uint();
|
|
66
|
+
const initial = uint();
|
|
67
|
+
const maximum = flags & 1 ? uint() : undefined;
|
|
68
|
+
found.push({
|
|
69
|
+
module,
|
|
70
|
+
name: base,
|
|
71
|
+
flags,
|
|
72
|
+
initial,
|
|
73
|
+
maximum,
|
|
74
|
+
limits: [from, at],
|
|
75
|
+
header,
|
|
76
|
+
body: [sectionStart, sectionEnd]
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
else if (kind === 3) {
|
|
80
|
+
reference();
|
|
81
|
+
at++;
|
|
82
|
+
}
|
|
83
|
+
else if (kind === 4) {
|
|
84
|
+
at++;
|
|
85
|
+
uint();
|
|
86
|
+
}
|
|
87
|
+
else
|
|
88
|
+
throw new TypeError('unsupported import descriptor');
|
|
89
|
+
}
|
|
90
|
+
return found;
|
|
91
|
+
}
|
|
92
|
+
return [];
|
|
93
|
+
}
|
|
94
|
+
/** Rewrites the memory import limits to shared with the largest maximum. */
|
|
95
|
+
export function shareMemory(bytes, memory) {
|
|
96
|
+
const { limits, header, body } = memory;
|
|
97
|
+
const content = [
|
|
98
|
+
...bytes.subarray(body[0], limits[0]),
|
|
99
|
+
3,
|
|
100
|
+
...uleb(memory.initial),
|
|
101
|
+
...uleb(PAGE_LIMIT),
|
|
102
|
+
...bytes.subarray(limits[1], body[1])
|
|
103
|
+
];
|
|
104
|
+
return Uint8Array.from([
|
|
105
|
+
...bytes.subarray(0, header),
|
|
106
|
+
2,
|
|
107
|
+
...uleb(content.length),
|
|
108
|
+
...content,
|
|
109
|
+
...bytes.subarray(body[1])
|
|
110
|
+
]);
|
|
111
|
+
}
|
|
112
|
+
const refusal = (reason, detail) => ({ reason, detail });
|
|
113
|
+
/**
|
|
114
|
+
* Builds the owner module of an iteration route: the memory import is shared, and the one loop
|
|
115
|
+
* of the owner function becomes `if (iterate(entry locals...)) { final values } else { the loop }`.
|
|
116
|
+
* The host function returns 1 after it ran every iteration and 0 to leave the original loop to
|
|
117
|
+
* run in the owner. A refusal names its reason and never throws.
|
|
118
|
+
*/
|
|
119
|
+
export function iterationOwner(original, route, stack) {
|
|
120
|
+
let module;
|
|
121
|
+
try {
|
|
122
|
+
if (!route.eligible || !route.owner || !route.trip)
|
|
123
|
+
return refusal('no-arena', 'route is not eligible');
|
|
124
|
+
const memories = importedMemory(original);
|
|
125
|
+
if (memories.length !== 1)
|
|
126
|
+
return refusal('no-arena', 'iteration needs exactly one imported memory');
|
|
127
|
+
const declared = memories[0];
|
|
128
|
+
if (declared.flags & ~3)
|
|
129
|
+
return refusal('no-arena', 'wasm64 or unsupported memory limits');
|
|
130
|
+
const shared = shareMemory(original, declared);
|
|
131
|
+
module = binaryen.readBinary(shared);
|
|
132
|
+
const compactImports = binaryen.Features
|
|
133
|
+
.CompactImports;
|
|
134
|
+
module.setFeatures(binaryen.Features.All & ~compactImports);
|
|
135
|
+
const m = module;
|
|
136
|
+
if (m.getMemoryInfo().is64)
|
|
137
|
+
return refusal('no-arena', 'wasm64 memory');
|
|
138
|
+
if (/\bmemory\.(size|grow)\b/.test(m.emitText()))
|
|
139
|
+
return refusal('memory-size-observed', 'the module executes memory.size or memory.grow, which would observe the arena');
|
|
140
|
+
const owner = m.getFunction(route.owner);
|
|
141
|
+
if (!owner)
|
|
142
|
+
return refusal('no-arena', `owner ${route.owner} not found`);
|
|
143
|
+
const f = binaryen.getFunctionInfo(owner);
|
|
144
|
+
const types = [...binaryen.expandType(f.params), ...f.vars];
|
|
145
|
+
const id = (e) => binaryen.getExpressionId(e);
|
|
146
|
+
const locate = (e) => {
|
|
147
|
+
if (id(e) === binaryen.LoopId)
|
|
148
|
+
return { index: -1, loop: e };
|
|
149
|
+
if (id(e) !== binaryen.BlockId)
|
|
150
|
+
return;
|
|
151
|
+
const kids = binaryen.getExpressionInfo(e).children;
|
|
152
|
+
for (let i = 0; i < kids.length; i++) {
|
|
153
|
+
if (id(kids[i]) === binaryen.LoopId)
|
|
154
|
+
return { parent: e, index: i, loop: kids[i] };
|
|
155
|
+
const inner = locate(kids[i]);
|
|
156
|
+
if (inner)
|
|
157
|
+
return inner;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
const place = locate(f.body);
|
|
161
|
+
if (!place)
|
|
162
|
+
return refusal('no-arena', 'loop not found in the owner');
|
|
163
|
+
const imports = WebAssembly.Module.imports(new WebAssembly.Module(Uint8Array.from(original)));
|
|
164
|
+
let namespace = '__folio_iteration';
|
|
165
|
+
while (imports.some((item) => item.module === namespace))
|
|
166
|
+
namespace += '_';
|
|
167
|
+
let call = '__folio_iterate';
|
|
168
|
+
while (m.getFunction(call))
|
|
169
|
+
call += '_';
|
|
170
|
+
const locals = route.locals.map((item) => item.local);
|
|
171
|
+
m.addFunctionImport(call, namespace, 'iterate', binaryen.createType(locals.map((local) => types[local])), binaryen.i32);
|
|
172
|
+
const get = (local) => m.local.get(local, types[local]);
|
|
173
|
+
const counter = route.trip.local;
|
|
174
|
+
const done = [];
|
|
175
|
+
for (const item of route.locals)
|
|
176
|
+
if (item.kind === 'stride')
|
|
177
|
+
done.push(m.local.set(item.local, m.i32.add(get(item.local), m.i32.mul(get(counter), m.i32.const(item.stride | 0)))));
|
|
178
|
+
done.push(m.local.set(counter, m.i32.const(0)));
|
|
179
|
+
const replacement = m.if(m.call(call, locals.map((local) => get(local)), binaryen.i32), m.block(null, done), place.loop);
|
|
180
|
+
const raw = binaryen;
|
|
181
|
+
if (place.parent)
|
|
182
|
+
raw._BinaryenBlockSetChildAt(place.parent, place.index, replacement);
|
|
183
|
+
else
|
|
184
|
+
raw._BinaryenFunctionSetBody(owner, replacement);
|
|
185
|
+
let stackExport;
|
|
186
|
+
if (stack !== undefined && m.getGlobal(stack)) {
|
|
187
|
+
stackExport = '__folio_owner_stack';
|
|
188
|
+
while (m.getExport(stackExport))
|
|
189
|
+
stackExport += '_';
|
|
190
|
+
m.addGlobalExport(stack, stackExport);
|
|
191
|
+
}
|
|
192
|
+
if (!m.validate())
|
|
193
|
+
throw new Error('invalid iteration owner');
|
|
194
|
+
let bytes;
|
|
195
|
+
try {
|
|
196
|
+
bytes = restoreImportOrder(m.emitBinary(), imports);
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
return refusal('import-order', `import order cannot be kept: ${error instanceof Error ? error.message : String(error)}`);
|
|
200
|
+
}
|
|
201
|
+
new WebAssembly.Module(Uint8Array.from(bytes));
|
|
202
|
+
return {
|
|
203
|
+
owner: {
|
|
204
|
+
module: bytes,
|
|
205
|
+
namespace,
|
|
206
|
+
locals,
|
|
207
|
+
stackExport,
|
|
208
|
+
memory: {
|
|
209
|
+
module: declared.module,
|
|
210
|
+
name: declared.name,
|
|
211
|
+
initial: declared.initial,
|
|
212
|
+
maximum: declared.maximum
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
return refusal('no-arena', `iteration owner unavailable: ${error instanceof Error ? error.message : String(error)}`);
|
|
219
|
+
}
|
|
220
|
+
finally {
|
|
221
|
+
module?.dispose();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { parentPort, workerData } from 'node:worker_threads';
|
|
2
|
+
import { iterationArguments, iterationEntryBytes, iterationShadowCopies, iterationWindowCopy } from "./iteration-layout.js";
|
|
3
|
+
import { markReadSummary, scanReads } from "./read-scan.js";
|
|
4
|
+
import { markReadWindow, readWindowsAt } from "./read-windows.js";
|
|
5
|
+
const { module, memory, arena, globals, iterate, locals, frameSize, port, signal, control, ownership } = workerData;
|
|
6
|
+
const L = arena;
|
|
7
|
+
const ENTRY = iterationEntryBytes;
|
|
8
|
+
const plain = new WebAssembly.Instance(module, { env: { memory } }).exports;
|
|
9
|
+
// the other lowerings, instantiated when the pool sends them, by key
|
|
10
|
+
const variants = new Map();
|
|
11
|
+
let u8 = new Uint8Array(memory.buffer);
|
|
12
|
+
let u32 = new Uint32Array(memory.buffer);
|
|
13
|
+
const counter = new Int32Array(control);
|
|
14
|
+
const done = new Int32Array(signal);
|
|
15
|
+
const now = () => performance.now();
|
|
16
|
+
const stats = { run: 0, scan: 0, words: 0, set: 0, mark: 0 };
|
|
17
|
+
// status 0 ran, 1 trapped, 2 undo log exhausted, 3 stopped by the owner's stall word
|
|
18
|
+
function runOne(job, s) {
|
|
19
|
+
const index = job.first + s;
|
|
20
|
+
const top = L.slots + (s + 1) * L.slotBytes;
|
|
21
|
+
const frame = top - frameSize;
|
|
22
|
+
const win = job.window ? iterationWindowCopy(top, frameSize, job.window) : undefined;
|
|
23
|
+
const exportsOf = job.variant ? variants.get(job.variant) : plain;
|
|
24
|
+
// the tracked region is the frame, or the window copy and the frame below which it sits
|
|
25
|
+
const region = win ? win.region : frameSize;
|
|
26
|
+
const regionLow = win ? win.copy : frame;
|
|
27
|
+
if (frameSize)
|
|
28
|
+
u8.copyWithin(frame, job.frameBase, job.frameBase + frameSize);
|
|
29
|
+
if (win)
|
|
30
|
+
u8.copyWithin(win.copy, job.window.low, job.window.end);
|
|
31
|
+
// a certified window is private and untracked: the slot starts from the owner's bytes
|
|
32
|
+
const own = ownership ? iterationShadowCopies(top, frameSize, ownership.windows) : undefined;
|
|
33
|
+
if (own)
|
|
34
|
+
ownership.windows.forEach((w, i) => {
|
|
35
|
+
u8.copyWithin(own.copies[i].copy, w.low, w.end);
|
|
36
|
+
exportsOf[w.delta].value = own.copies[i].delta;
|
|
37
|
+
});
|
|
38
|
+
const shadow = L.frameShadow + s * L.frameSlotBytes;
|
|
39
|
+
if (region) {
|
|
40
|
+
u8.fill(0, shadow, shadow + L.frameSlotBytes);
|
|
41
|
+
exportsOf[globals.frameLow].value = regionLow;
|
|
42
|
+
exportsOf[globals.frameSize].value = region;
|
|
43
|
+
exportsOf[globals.frameShadow].value = shadow;
|
|
44
|
+
}
|
|
45
|
+
if (win) {
|
|
46
|
+
exportsOf[globals.windowLow].value = job.window.low;
|
|
47
|
+
exportsOf[globals.windowEnd].value = job.window.end;
|
|
48
|
+
exportsOf[globals.windowDelta].value = win.delta;
|
|
49
|
+
}
|
|
50
|
+
const rbase = L.rmaps + s * L.rsize;
|
|
51
|
+
const sbase = L.ssize ? L.smaps + s * L.ssize : 0;
|
|
52
|
+
const wbase = L.wmaps + s * L.wsize;
|
|
53
|
+
const lbase = job.logs.base + s * job.logs.bytes;
|
|
54
|
+
const limit = lbase + job.logs.capacity * ENTRY;
|
|
55
|
+
exportsOf[globals.readBitmap].value = rbase;
|
|
56
|
+
if (L.ssize)
|
|
57
|
+
exportsOf[globals.readSummary].value = sbase;
|
|
58
|
+
exportsOf[globals.writeBitmap].value = wbase;
|
|
59
|
+
exportsOf[globals.logPointer].value = lbase;
|
|
60
|
+
exportsOf[globals.logLimit].value = limit;
|
|
61
|
+
exportsOf[globals.stampAddress].value = L.stamp;
|
|
62
|
+
const stack = exportsOf[globals.stackPointer];
|
|
63
|
+
if (stack)
|
|
64
|
+
stack.value = own ? own.low : win ? win.copy : frameSize ? frame : top;
|
|
65
|
+
const slotted = exportsOf[globals.slotLow] !== undefined;
|
|
66
|
+
if (slotted) {
|
|
67
|
+
exportsOf[globals.exemptLow].value = top - L.slotBytes;
|
|
68
|
+
exportsOf[globals.exemptEnd].value = win ? win.copy : frame;
|
|
69
|
+
exportsOf[globals.slotLow].value = top - L.slotBytes;
|
|
70
|
+
exportsOf[globals.slotEnd].value = top;
|
|
71
|
+
exportsOf[globals.arenaLow].value = L.slots;
|
|
72
|
+
exportsOf[globals.arenaEnd].value = L.stamp;
|
|
73
|
+
exportsOf[globals.violation].value = 0;
|
|
74
|
+
}
|
|
75
|
+
const tm = now();
|
|
76
|
+
if (exportsOf[globals.readWindow0Low]) {
|
|
77
|
+
// the guard of a window with no bytes is false, so a stale window never carries over
|
|
78
|
+
const found = job.readWindows ? readWindowsAt(job.readWindows, index, job.memoryBytes) : [];
|
|
79
|
+
for (const [i, names] of [
|
|
80
|
+
[globals.readWindow0Low, globals.readWindow0Span],
|
|
81
|
+
[globals.readWindow1Low, globals.readWindow1Span]
|
|
82
|
+
].entries()) {
|
|
83
|
+
const w = found[i];
|
|
84
|
+
if (exportsOf[names[0]] === undefined)
|
|
85
|
+
continue;
|
|
86
|
+
exportsOf[names[0]].value = w ? w.low : 0;
|
|
87
|
+
exportsOf[names[1]].value = w ? w.bytes - 7 : 0;
|
|
88
|
+
if (w) {
|
|
89
|
+
markReadWindow(u8, rbase, w.low, w.bytes);
|
|
90
|
+
if (L.ssize)
|
|
91
|
+
markReadSummary(u8, sbase, L.ssize - 1, w.low, w.bytes);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const args = iterationArguments(locals, job.entry, index, frame);
|
|
96
|
+
const counted = exportsOf[globals.readHits] !== undefined;
|
|
97
|
+
if (counted) {
|
|
98
|
+
exportsOf[globals.readHits].value = 0;
|
|
99
|
+
exportsOf[globals.readMisses].value = 0;
|
|
100
|
+
exportsOf[globals.readExempt].value = 0;
|
|
101
|
+
}
|
|
102
|
+
const t1 = now();
|
|
103
|
+
stats.mark += t1 - tm;
|
|
104
|
+
let status = 0;
|
|
105
|
+
let touched = -1;
|
|
106
|
+
try {
|
|
107
|
+
exportsOf[iterate](...args);
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
const reached = slotted ? exportsOf[globals.violation].value >>> 0 : 0;
|
|
111
|
+
if (reached) {
|
|
112
|
+
touched = Math.floor((Math.max(reached, L.slots) - L.slots) / L.slotBytes);
|
|
113
|
+
touched = Math.min(touched, L.batch);
|
|
114
|
+
}
|
|
115
|
+
const left = limit - (exportsOf[globals.logPointer].value >>> 0);
|
|
116
|
+
const unreachable = error instanceof WebAssembly.RuntimeError && /unreachable/.test(error.message);
|
|
117
|
+
status =
|
|
118
|
+
!reached && unreachable && Atomics.load(u32, (L.stamp + 4) / 4) !== 0
|
|
119
|
+
? 3
|
|
120
|
+
: !reached && unreachable && left < ENTRY
|
|
121
|
+
? 2
|
|
122
|
+
: 1;
|
|
123
|
+
}
|
|
124
|
+
const t2 = now();
|
|
125
|
+
stats.run += t2 - t1;
|
|
126
|
+
const runMs = t2 - t1;
|
|
127
|
+
const entries = ((exportsOf[globals.logPointer].value >>> 0) - lbase) / ENTRY;
|
|
128
|
+
const written = new Int32Array(entries);
|
|
129
|
+
for (let e = 0; e < entries; e++)
|
|
130
|
+
written[e] = u32[(lbase + e * ENTRY) / 4] >>> 5;
|
|
131
|
+
written.sort();
|
|
132
|
+
const rwords = new Uint32Array(memory.buffer, rbase, L.rsize / 4);
|
|
133
|
+
const summary = L.ssize ? new Uint8Array(memory.buffer, sbase, L.ssize) : undefined;
|
|
134
|
+
const reads = scanReads(rwords, summary, stats);
|
|
135
|
+
let frameWritten = new Uint32Array(0);
|
|
136
|
+
let frameExposed = new Uint32Array(0);
|
|
137
|
+
if (region) {
|
|
138
|
+
const granules = Math.ceil(region / 32);
|
|
139
|
+
const words = (at) => u32.slice(at / 4, at / 4 + granules);
|
|
140
|
+
const copy = (at) => {
|
|
141
|
+
const found = words(at);
|
|
142
|
+
return found.some(Boolean) ? found : new Uint32Array(0);
|
|
143
|
+
};
|
|
144
|
+
frameWritten = copy(shadow);
|
|
145
|
+
frameExposed = copy(shadow + 4 * granules + 4);
|
|
146
|
+
}
|
|
147
|
+
if (status)
|
|
148
|
+
u8.fill(0, wbase, wbase + L.wsize);
|
|
149
|
+
else
|
|
150
|
+
for (let e = 0; e < entries; e++)
|
|
151
|
+
u8[wbase + (written[e] >>> 3)] = 0;
|
|
152
|
+
stats.scan += now() - t2;
|
|
153
|
+
return {
|
|
154
|
+
index,
|
|
155
|
+
status,
|
|
156
|
+
entries,
|
|
157
|
+
runMs,
|
|
158
|
+
written,
|
|
159
|
+
reads,
|
|
160
|
+
frameWritten,
|
|
161
|
+
frameExposed,
|
|
162
|
+
touched,
|
|
163
|
+
counts: counted
|
|
164
|
+
? {
|
|
165
|
+
hits: exportsOf[globals.readHits].value >>> 0,
|
|
166
|
+
misses: exportsOf[globals.readMisses].value >>> 0,
|
|
167
|
+
exempt: exportsOf[globals.readExempt].value >>> 0
|
|
168
|
+
}
|
|
169
|
+
: undefined
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
port.on('message', (job) => {
|
|
173
|
+
if (job.install) {
|
|
174
|
+
variants.set(job.key, new WebAssembly.Instance(job.install, { env: { memory } }).exports);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
// the pool may have grown the memory for a larger log; the old views end at the old length
|
|
178
|
+
if (job.memoryBytes > u8.length) {
|
|
179
|
+
u8 = new Uint8Array(memory.buffer);
|
|
180
|
+
u32 = new Uint32Array(memory.buffer);
|
|
181
|
+
}
|
|
182
|
+
stats.run = 0;
|
|
183
|
+
stats.scan = 0;
|
|
184
|
+
stats.words = 0;
|
|
185
|
+
stats.set = 0;
|
|
186
|
+
stats.mark = 0;
|
|
187
|
+
let reply;
|
|
188
|
+
try {
|
|
189
|
+
const results = [];
|
|
190
|
+
for (;;) {
|
|
191
|
+
const k = Atomics.add(counter, 0, 1);
|
|
192
|
+
if (k >= job.count)
|
|
193
|
+
break;
|
|
194
|
+
const outcome = runOne(job, job.reverse ? job.count - 1 - k : k);
|
|
195
|
+
results.push(outcome);
|
|
196
|
+
// every later iteration is rolled back and replayed anyway, so leave them unstarted
|
|
197
|
+
if (outcome.status && !job.reverse)
|
|
198
|
+
Atomics.store(counter, 0, 1 << 30);
|
|
199
|
+
}
|
|
200
|
+
reply = {
|
|
201
|
+
done: results,
|
|
202
|
+
runMs: stats.run,
|
|
203
|
+
scanMs: stats.scan,
|
|
204
|
+
scanWords: stats.words,
|
|
205
|
+
scanSet: stats.set,
|
|
206
|
+
markMs: stats.mark
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
reply = { error: String(error?.stack ?? error) };
|
|
211
|
+
}
|
|
212
|
+
port.postMessage(reply);
|
|
213
|
+
Atomics.add(done, 0, 1);
|
|
214
|
+
Atomics.notify(done, 0);
|
|
215
|
+
});
|
|
216
|
+
parentPort.postMessage({ ready: true });
|