@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/inline.js
ADDED
|
@@ -0,0 +1,766 @@
|
|
|
1
|
+
import binaryen from 'binaryen';
|
|
2
|
+
import { stackRoute } from "./extract.js";
|
|
3
|
+
import { importedMemory, shareMemory } from "./iteration-owner.js";
|
|
4
|
+
const info = (expression) => {
|
|
5
|
+
const id = binaryen.getExpressionId(expression);
|
|
6
|
+
if (id === binaryen.UnreachableId || id === binaryen.NopId) {
|
|
7
|
+
return { id, type: binaryen.getExpressionType(expression) };
|
|
8
|
+
}
|
|
9
|
+
return binaryen.getExpressionInfo(expression);
|
|
10
|
+
};
|
|
11
|
+
// #region inline tracking lowering
|
|
12
|
+
const capi = binaryen;
|
|
13
|
+
/** Names of the exported mutable i32 globals the inline lowering adds, plus the stack export. */
|
|
14
|
+
export const inlineGlobals = {
|
|
15
|
+
readBitmap: '__folio_rb',
|
|
16
|
+
writeBitmap: '__folio_wb',
|
|
17
|
+
logPointer: '__folio_lp',
|
|
18
|
+
logLimit: '__folio_ll',
|
|
19
|
+
stampAddress: '__folio_sa',
|
|
20
|
+
stackPointer: '__folio_sp',
|
|
21
|
+
frameLow: '__folio_fl',
|
|
22
|
+
frameSize: '__folio_fn',
|
|
23
|
+
frameShadow: '__folio_fs',
|
|
24
|
+
exemptLow: '__folio_xl',
|
|
25
|
+
exemptEnd: '__folio_xe',
|
|
26
|
+
slotLow: '__folio_sl',
|
|
27
|
+
slotEnd: '__folio_se',
|
|
28
|
+
arenaLow: '__folio_al',
|
|
29
|
+
arenaEnd: '__folio_ae',
|
|
30
|
+
violation: '__folio_xv',
|
|
31
|
+
windowLow: '__folio_wl',
|
|
32
|
+
windowEnd: '__folio_we',
|
|
33
|
+
windowDelta: '__folio_wd',
|
|
34
|
+
readWindow0Low: '__folio_r0l',
|
|
35
|
+
readWindow0Span: '__folio_r0m',
|
|
36
|
+
readWindow1Low: '__folio_r1l',
|
|
37
|
+
readWindow1Span: '__folio_r1m',
|
|
38
|
+
readSummary: '__folio_rs',
|
|
39
|
+
readHits: '__folio_ch',
|
|
40
|
+
readMisses: '__folio_cm',
|
|
41
|
+
readExempt: '__folio_ce'
|
|
42
|
+
};
|
|
43
|
+
/** Bytes of one undo entry: the granule address, the stamp, then the prior bytes of the granule. */
|
|
44
|
+
export const inlineEntryBytes = (writeGranule = 32) => 8 + writeGranule;
|
|
45
|
+
const inlineHelpers = [
|
|
46
|
+
'__folio_x_trap',
|
|
47
|
+
'__folio_x_r_range',
|
|
48
|
+
'__folio_x_w_range',
|
|
49
|
+
'__folio_r_mark',
|
|
50
|
+
'__folio_w_mark',
|
|
51
|
+
'__folio_w_set',
|
|
52
|
+
'__folio_undo',
|
|
53
|
+
'__folio_r_range',
|
|
54
|
+
'__folio_w_range',
|
|
55
|
+
'__folio_f_r',
|
|
56
|
+
'__folio_f_w',
|
|
57
|
+
'__folio_f_rr',
|
|
58
|
+
'__folio_f_rw',
|
|
59
|
+
'__folio_w_rebase'
|
|
60
|
+
];
|
|
61
|
+
const inlineLeaves = new Set([
|
|
62
|
+
binaryen.ConstId,
|
|
63
|
+
binaryen.LocalGetId,
|
|
64
|
+
binaryen.GlobalGetId,
|
|
65
|
+
binaryen.NopId,
|
|
66
|
+
binaryen.UnreachableId,
|
|
67
|
+
binaryen.MemorySizeId,
|
|
68
|
+
binaryen.DataDropId,
|
|
69
|
+
binaryen.AtomicFenceId
|
|
70
|
+
]);
|
|
71
|
+
const simdLoadBytes = new Map([
|
|
72
|
+
[binaryen.Operations.Load8SplatVec128, 1],
|
|
73
|
+
[binaryen.Operations.Load16SplatVec128, 2],
|
|
74
|
+
[binaryen.Operations.Load32SplatVec128, 4],
|
|
75
|
+
[binaryen.Operations.Load64SplatVec128, 8],
|
|
76
|
+
[binaryen.Operations.Load8x8SVec128, 8],
|
|
77
|
+
[binaryen.Operations.Load8x8UVec128, 8],
|
|
78
|
+
[binaryen.Operations.Load16x4SVec128, 8],
|
|
79
|
+
[binaryen.Operations.Load16x4UVec128, 8],
|
|
80
|
+
[binaryen.Operations.Load32x2SVec128, 8],
|
|
81
|
+
[binaryen.Operations.Load32x2UVec128, 8],
|
|
82
|
+
[binaryen.Operations.Load32ZeroVec128, 4],
|
|
83
|
+
[binaryen.Operations.Load64ZeroVec128, 8]
|
|
84
|
+
]);
|
|
85
|
+
const simdLaneBytes = new Map([
|
|
86
|
+
[binaryen.Operations.Load8LaneVec128, 1],
|
|
87
|
+
[binaryen.Operations.Load16LaneVec128, 2],
|
|
88
|
+
[binaryen.Operations.Load32LaneVec128, 4],
|
|
89
|
+
[binaryen.Operations.Load64LaneVec128, 8],
|
|
90
|
+
[binaryen.Operations.Store8LaneVec128, 1],
|
|
91
|
+
[binaryen.Operations.Store16LaneVec128, 2],
|
|
92
|
+
[binaryen.Operations.Store32LaneVec128, 4],
|
|
93
|
+
[binaryen.Operations.Store64LaneVec128, 8]
|
|
94
|
+
]);
|
|
95
|
+
// operand expressions of every node the lowering may meet; anything else is refused by name
|
|
96
|
+
function inlineChildren(v) {
|
|
97
|
+
switch (v.id) {
|
|
98
|
+
case binaryen.BlockId:
|
|
99
|
+
return v.children;
|
|
100
|
+
case binaryen.IfId:
|
|
101
|
+
return [v.condition, v.ifTrue, v.ifFalse].filter(Boolean);
|
|
102
|
+
case binaryen.LoopId:
|
|
103
|
+
return [v.body];
|
|
104
|
+
case binaryen.BreakId:
|
|
105
|
+
case binaryen.SwitchId:
|
|
106
|
+
return [v.value, v.condition].filter(Boolean);
|
|
107
|
+
case binaryen.CallId:
|
|
108
|
+
return v.operands;
|
|
109
|
+
case binaryen.CallIndirectId:
|
|
110
|
+
return [...v.operands, v.target];
|
|
111
|
+
case binaryen.LocalSetId:
|
|
112
|
+
case binaryen.GlobalSetId:
|
|
113
|
+
case binaryen.UnaryId:
|
|
114
|
+
case binaryen.DropId:
|
|
115
|
+
return [v.value];
|
|
116
|
+
case binaryen.ReturnId:
|
|
117
|
+
return v.value ? [v.value] : [];
|
|
118
|
+
case binaryen.LoadId:
|
|
119
|
+
case binaryen.SIMDLoadId:
|
|
120
|
+
return [v.ptr];
|
|
121
|
+
case binaryen.StoreId:
|
|
122
|
+
case binaryen.AtomicRMWId:
|
|
123
|
+
return [v.ptr, v.value];
|
|
124
|
+
case binaryen.BinaryId:
|
|
125
|
+
case binaryen.SIMDShuffleId:
|
|
126
|
+
return [v.left, v.right];
|
|
127
|
+
case binaryen.SelectId:
|
|
128
|
+
return [v.ifTrue, v.ifFalse, v.condition];
|
|
129
|
+
case binaryen.MemoryGrowId:
|
|
130
|
+
return [v.delta];
|
|
131
|
+
case binaryen.MemoryCopyId:
|
|
132
|
+
return [v.dest, v.source, v.size];
|
|
133
|
+
case binaryen.MemoryFillId:
|
|
134
|
+
return [v.dest, v.value, v.size];
|
|
135
|
+
case binaryen.MemoryInitId:
|
|
136
|
+
return [v.dest, v.offset, v.size];
|
|
137
|
+
case binaryen.AtomicCmpxchgId:
|
|
138
|
+
return [v.ptr, v.expected, v.replacement];
|
|
139
|
+
case binaryen.AtomicWaitId:
|
|
140
|
+
return [v.ptr, v.expected, v.timeout];
|
|
141
|
+
case binaryen.AtomicNotifyId:
|
|
142
|
+
return [v.ptr, v.notifyCount];
|
|
143
|
+
case binaryen.SIMDExtractId:
|
|
144
|
+
return [v.vec];
|
|
145
|
+
case binaryen.SIMDReplaceId:
|
|
146
|
+
return [v.vec, v.value];
|
|
147
|
+
case binaryen.SIMDTernaryId:
|
|
148
|
+
return [v.a, v.b, v.c];
|
|
149
|
+
case binaryen.SIMDShiftId:
|
|
150
|
+
return [v.vec, v.shift];
|
|
151
|
+
case binaryen.SIMDLoadStoreLaneId:
|
|
152
|
+
return [v.ptr, v.vec];
|
|
153
|
+
default:
|
|
154
|
+
if (inlineLeaves.has(v.id))
|
|
155
|
+
return [];
|
|
156
|
+
throw new Error(`unsupported expression ${v.id}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function granuleShift(value, fallback, low, high) {
|
|
160
|
+
const size = value ?? fallback;
|
|
161
|
+
if (!Number.isInteger(size) || size < low || size > high || size & (size - 1))
|
|
162
|
+
throw new RangeError(`granule must be a power of two from ${low} to ${high}`);
|
|
163
|
+
return Math.log2(size);
|
|
164
|
+
}
|
|
165
|
+
function memoryCount(bytes, native) {
|
|
166
|
+
let cursor = 8, defined = 0;
|
|
167
|
+
const unsigned = () => {
|
|
168
|
+
let value = 0, shift = 0, byte;
|
|
169
|
+
do {
|
|
170
|
+
byte = bytes[cursor++];
|
|
171
|
+
value += (byte & 127) * 2 ** shift;
|
|
172
|
+
shift += 7;
|
|
173
|
+
} while (byte & 128);
|
|
174
|
+
return value;
|
|
175
|
+
};
|
|
176
|
+
while (cursor < bytes.length) {
|
|
177
|
+
const section = bytes[cursor++];
|
|
178
|
+
const end = unsigned() + cursor;
|
|
179
|
+
if (section === 5)
|
|
180
|
+
defined = unsigned();
|
|
181
|
+
cursor = end;
|
|
182
|
+
}
|
|
183
|
+
return defined + WebAssembly.Module.imports(native).filter((i) => i.kind === 'memory').length;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* A second lowering, next to the host-call one in `prepare`: it marks memory accesses inline, with
|
|
187
|
+
* no host call and no added import, so a region can be validated after it ran. Nothing in
|
|
188
|
+
* `prepare` calls it yet and its output is not a `PreparedProgram` field.
|
|
189
|
+
*
|
|
190
|
+
* State lives in mutable i32 globals the owner exports and sets before each region; all start at
|
|
191
|
+
* 0, which traps on the first access. `__folio_rb` and `__folio_wb` are the bases of the read and
|
|
192
|
+
* write bitmaps, `__folio_lp` and `__folio_ll` the undo log pointer and limit, `__folio_sa` the
|
|
193
|
+
* address of a shared i32 stamp counter (4-byte aligned). The stack pointer global is exported as
|
|
194
|
+
* `__folio_sp` when the module has one. A bitmap has one bit per granule: granule `g`, with
|
|
195
|
+
* `g = address >> log2(granule)`, is bit `g & 7` of byte `base + (g >> 3)`. The bitmaps are
|
|
196
|
+
* private to a region and the owner clears them between regions.
|
|
197
|
+
*
|
|
198
|
+
* Every load, store, atomic access and SIMD load marks its read granule, and an access that
|
|
199
|
+
* crosses a boundary marks both. A store, a SIMD lane store and the destination of `memory.copy`,
|
|
200
|
+
* `memory.fill` and `memory.init` mark write granules; atomic accesses mark both sets; the source
|
|
201
|
+
* of `memory.copy` marks read granules. The first write to a write granule appends an undo entry
|
|
202
|
+
* at `__folio_lp`: the granule address (u32), a stamp from an atomic add on `__folio_sa` taken
|
|
203
|
+
* after the prior bytes were copied and before the write, then the prior bytes of the granule.
|
|
204
|
+
* `__folio_lp` then advances by `inlineEntryBytes(writeGranule)`; an entry that would pass
|
|
205
|
+
* `__folio_ll` traps with `unreachable`. The start function is not lowered.
|
|
206
|
+
*
|
|
207
|
+
* With `frame: true` the module also tracks one frame region, for a stack frame whose bytes an
|
|
208
|
+
* iteration may read after an earlier iteration wrote them. The owner sets `__folio_fl` (low
|
|
209
|
+
* address), `__folio_fn` (size in bytes) and `__folio_fs` (shadow base) before a region; with
|
|
210
|
+
* `__folio_fn` at 0 nothing is tracked. An access that overlaps the region is checked against a
|
|
211
|
+
* shadow of one written bit per frame byte, `4 * ceil(size / 32)` bytes with the bits of frame
|
|
212
|
+
* granule `g` (32 bytes) in the i32 at `__folio_fs + 4 * g`, then 4 pad bytes, then the exposed
|
|
213
|
+
* bits laid out the same way, then 4 more pad bytes. A store sets written bits after its value is
|
|
214
|
+
* computed; a load sets the exposed bit of each byte it reads whose written bit is clear, so a
|
|
215
|
+
* region reports exactly the frame bytes it read before it wrote them.
|
|
216
|
+
* `memory.copy`, `memory.fill` and `memory.init` set or check the exact byte range, an atomic
|
|
217
|
+
* read-modify-write checks then sets, and any other atomic access or SIMD load checks. The owner
|
|
218
|
+
* zeroes the shadow before each region. Accesses outside the region pay one compare. Stack below
|
|
219
|
+
* the region is not tracked.
|
|
220
|
+
*
|
|
221
|
+
* With `slot: true` and a stack global, the owner also sets three address windows before a region:
|
|
222
|
+
* the exempt window `[__folio_xl, __folio_xe)`, the whole private slot `[__folio_sl, __folio_se)`
|
|
223
|
+
* and the arena of all slots `[__folio_al, __folio_ae)`. An access inside the exempt window sets no
|
|
224
|
+
* mark and logs no undo entry. An access that overlaps the arena but does not lie inside the own
|
|
225
|
+
* slot stores its address in `__folio_xv` and traps with `unreachable`, because another iteration
|
|
226
|
+
* owns those bytes. Every other access is tracked as before, so the exempt window must not contain
|
|
227
|
+
* the tracked frame; an access that crosses the window edge is tracked. `memory.copy`,
|
|
228
|
+
* `memory.fill` and `memory.init` apply the same tests to the whole range, and a range counts as
|
|
229
|
+
* inside only when every byte is. The exempt bytes are not restored by a rollback, so the owner
|
|
230
|
+
* must treat the window as dead after the region.
|
|
231
|
+
*
|
|
232
|
+
* With `readWindows: n` the owner also sets, per window `i` below `n`, `__folio_r<i>l` (first byte)
|
|
233
|
+
* and `__folio_r<i>m` (window size minus 7, or 0 for no window) before a region, and marks every
|
|
234
|
+
* read granule that the window touches in the read bitmap itself. A plain, non-atomic load or SIMD
|
|
235
|
+
* load of at most 8 bytes whose address `a` satisfies `a - low < size - 7` lies wholly inside the
|
|
236
|
+
* window, so its read mark is already set: it sets no mark and skips the slot, arena and frame
|
|
237
|
+
* checks. Every other access, a store, an atomic, a wider load, a load that crosses a window edge
|
|
238
|
+
* and every bulk operation, is tracked as before. The window must lie outside the arena of all
|
|
239
|
+
* slots and the owner frame region, which is what those checks would have found; a false conflict
|
|
240
|
+
* is possible only for a granule of the window that another iteration writes and this one does
|
|
241
|
+
* not read. The exempt window is tested first, so an exempt load executes what it does without
|
|
242
|
+
* windows; the guards of the windows run only for a load outside it. With `readCount` the lowering
|
|
243
|
+
* also counts those loads by where they land (`__folio_ch`, `__folio_cm`, `__folio_ce`).
|
|
244
|
+
*
|
|
245
|
+
* With `readSummary: n` every read mark also sets one byte of the summary at `__folio_rs`, before
|
|
246
|
+
* the bit it summarizes. The byte is `(a >> (log2(readGranule) + 11)) & (n - 1)` for address `a`,
|
|
247
|
+
* one byte per 64 words of the read bitmap. A bitmap word is never non-zero while its summary byte
|
|
248
|
+
* is zero, so a scan may skip the blocks whose byte is zero. The owner sets the bytes of any
|
|
249
|
+
* granule it marks itself, a read window for one, and clears a byte only when it clears the words
|
|
250
|
+
* of its block. An address past the memory wraps to a byte that covers other words, which only
|
|
251
|
+
* adds blocks to scan. The owner must also set `n` large enough that every address of the memory
|
|
252
|
+
* has its own byte, or the scan visits blocks it need not.
|
|
253
|
+
*
|
|
254
|
+
* With `window: true` (which implies `frame`, and `slot` when there is a stack global) the owner also sets `__folio_wl` and
|
|
255
|
+
* `__folio_we`, a window of the address space, and `__folio_wd`, the distance from it to a private
|
|
256
|
+
* copy that lies inside the tracked frame region. A load, store, atomic or SIMD access that lies in
|
|
257
|
+
* the window runs on the copy (`address + __folio_wd`), a bulk operation does the same when its
|
|
258
|
+
* whole range is inside, and an access or range that crosses the window edge traps with
|
|
259
|
+
* `unreachable`. Accesses in the tracked frame region, the copy included, set no bitmap mark and
|
|
260
|
+
* log no undo entry: their only record is the frame shadow, whose exposed bits the owner
|
|
261
|
+
* validates, and the copy is discarded or committed byte by byte from the written bits. With the
|
|
262
|
+
* window empty (`__folio_we` at 0) nothing is rebased.
|
|
263
|
+
*
|
|
264
|
+
* Throws for a module without exactly one memory, a 64-bit memory, an unshared memory when
|
|
265
|
+
* `requireShared` is set, a start function that calls, a reserved name or an expression it does
|
|
266
|
+
* not know. An imported host function that touches memory is not seen.
|
|
267
|
+
*/
|
|
268
|
+
export function lowerInline(bytes, options = {}) {
|
|
269
|
+
const rg = granuleShift(options.readGranule, 64, 16, 4096);
|
|
270
|
+
const wg = granuleShift(options.writeGranule, 32, 16, 256);
|
|
271
|
+
const entry = inlineEntryBytes(1 << wg);
|
|
272
|
+
const readWindows = options.readWindows ?? 0;
|
|
273
|
+
if (!Number.isInteger(readWindows) || readWindows < 0 || readWindows > 2)
|
|
274
|
+
throw new RangeError('read windows must be 0, 1 or 2');
|
|
275
|
+
const summary = options.readSummary ?? 0;
|
|
276
|
+
if (!Number.isInteger(summary) || summary < 0 || summary > 32768 || summary & (summary - 1))
|
|
277
|
+
throw new RangeError('read summary must be 0 or a power of two up to 32768');
|
|
278
|
+
const original = Uint8Array.from(bytes);
|
|
279
|
+
const native = new WebAssembly.Module(original);
|
|
280
|
+
if (memoryCount(original, native) !== 1)
|
|
281
|
+
throw new Error('exactly one memory required');
|
|
282
|
+
const stack = options.stack ?? stackRoute(original).stack?.name;
|
|
283
|
+
const m = binaryen.readBinary(original);
|
|
284
|
+
try {
|
|
285
|
+
const compactImports = binaryen.Features
|
|
286
|
+
.CompactImports;
|
|
287
|
+
m.setFeatures(binaryen.Features.All & ~compactImports);
|
|
288
|
+
const memory = m.getMemoryInfo();
|
|
289
|
+
if (memory.is64)
|
|
290
|
+
throw new Error('memory64 is not supported');
|
|
291
|
+
if ((options.requireShared ?? true) && !memory.shared)
|
|
292
|
+
throw new Error('shared memory required; pass requireShared: false for one thread');
|
|
293
|
+
const fuel = Boolean(options.fuel);
|
|
294
|
+
const reserved = [
|
|
295
|
+
...Object.values(inlineGlobals),
|
|
296
|
+
...inlineHelpers,
|
|
297
|
+
...(fuel ? ['__folio_fuel', '__folio_exhausted', '__folio_set_fuel'] : [])
|
|
298
|
+
];
|
|
299
|
+
if (reserved.some((name) => m.getGlobal(name) || m.getFunction(name) || m.getExport(name)))
|
|
300
|
+
throw new Error('reserved function name');
|
|
301
|
+
if (stack !== undefined) {
|
|
302
|
+
const global = m.getGlobal(stack);
|
|
303
|
+
const shape = global ? binaryen.getGlobalInfo(global) : undefined;
|
|
304
|
+
if (!shape || !shape.mutable || shape.type !== binaryen.i32 || shape.module)
|
|
305
|
+
throw new Error(`stack global ${stack} is not a defined mutable i32`);
|
|
306
|
+
}
|
|
307
|
+
const window = Boolean(options.window);
|
|
308
|
+
const frame = Boolean(options.frame) || window;
|
|
309
|
+
const slot = ((Boolean(options.slot) || window) && stack !== undefined) || Boolean(options.guard);
|
|
310
|
+
const { exemptLow: xl, exemptEnd: xe, slotLow: sl, slotEnd: se } = inlineGlobals;
|
|
311
|
+
const { arenaLow: al, arenaEnd: ae, violation: xv } = inlineGlobals;
|
|
312
|
+
const { windowLow: wl, windowEnd: we, windowDelta: wd } = inlineGlobals;
|
|
313
|
+
const { readWindow0Low: r0l, readWindow0Span: r0m } = inlineGlobals;
|
|
314
|
+
const { readWindow1Low: r1l, readWindow1Span: r1m } = inlineGlobals;
|
|
315
|
+
const rs = inlineGlobals.readSummary;
|
|
316
|
+
const { readHits: ch, readMisses: cm, readExempt: ce } = inlineGlobals;
|
|
317
|
+
const counting = Boolean(options.readCount) && readWindows > 0;
|
|
318
|
+
const rb = inlineGlobals.readBitmap, wb = inlineGlobals.writeBitmap, lp = inlineGlobals.logPointer, ll = inlineGlobals.logLimit, sa = inlineGlobals.stampAddress, fl = inlineGlobals.frameLow, fn = inlineGlobals.frameSize, fs = inlineGlobals.frameShadow;
|
|
319
|
+
const i32 = binaryen.i32, none = binaryen.none;
|
|
320
|
+
const c = (value) => m.i32.const(value | 0);
|
|
321
|
+
const g = (name) => m.global.get(name, i32);
|
|
322
|
+
const l = (index) => m.local.get(index, i32);
|
|
323
|
+
const call = (name, ...args) => m.call(name, args, none);
|
|
324
|
+
const bitOf = (address, shift) => m.i32.shl(c(1), m.i32.and(m.i32.shr_u(address(), c(shift)), c(7)));
|
|
325
|
+
const byteOf = (base, address, shift) => m.i32.add(g(base), m.i32.shr_u(address(), c(shift + 3)));
|
|
326
|
+
const setBit = (base, address, shift) => m.i32.store8(0, 1, byteOf(base, address, shift), m.i32.or(m.i32.load8_u(0, 1, byteOf(base, address, shift)), bitOf(address, shift)));
|
|
327
|
+
// the summary byte goes first, so the bit is never set without it
|
|
328
|
+
const markRead = (address) => summary
|
|
329
|
+
? m.block(null, [
|
|
330
|
+
m.i32.store8(0, 1, m.i32.add(g(rs), m.i32.and(m.i32.shr_u(address(), c(rg + 11)), c(summary - 1))), c(1)),
|
|
331
|
+
setBit(rb, address, rg)
|
|
332
|
+
], none)
|
|
333
|
+
: setBit(rb, address, rg);
|
|
334
|
+
const unmarked = (address) => m.i32.eqz(m.i32.and(m.i32.load8_u(0, 1, byteOf(wb, address, wg)), bitOf(address, wg)));
|
|
335
|
+
// does an access of `width` bytes at the address overlap the frame region
|
|
336
|
+
const inFrame = (address, width) => width === 1
|
|
337
|
+
? m.i32.lt_u(m.i32.sub(address(), g(fl)), g(fn))
|
|
338
|
+
: m.i32.lt_u(m.i32.add(m.i32.sub(address(), g(fl)), c(width - 1)), m.i32.add(g(fn), c(width - 1)));
|
|
339
|
+
const frameCall = (name, address, width) => m.if(inFrame(address, width), call(name, address(), c(width)));
|
|
340
|
+
// does [address, address + width) lie inside the window named by two globals
|
|
341
|
+
const within = (low, end, address, width) => m.i32.and(m.i32.ge_u(address(), g(low)), m.i32.le_u(m.i32.add(address(), width), g(end)));
|
|
342
|
+
// does [address, address + width) overlap the arena of all slots
|
|
343
|
+
const overlapsArena = (address, width) => m.i32.and(m.i32.lt_u(address(), g(ae)), m.i32.gt_u(m.i32.add(address(), width), g(al)));
|
|
344
|
+
// does [address, address + width) lie inside the tracked frame region
|
|
345
|
+
const inRegion = (address, width) => m.i32.and(m.i32.ge_u(address(), g(fl)), m.i32.le_u(m.i32.add(address(), typeof width === 'number' ? c(width) : width()), m.i32.add(g(fl), g(fn))));
|
|
346
|
+
const hold = (local, value) => m.block(null, [m.local.set(local, value), l(local)], i32);
|
|
347
|
+
const tick = () => m.block(null, [
|
|
348
|
+
m.global.set('__folio_fuel', m.i32.sub(g('__folio_fuel'), c(1))),
|
|
349
|
+
m.if(m.i32.le_s(g('__folio_fuel'), c(0)), m.call('__folio_exhausted', [], none))
|
|
350
|
+
], none);
|
|
351
|
+
const stop = Boolean(options.stop);
|
|
352
|
+
const poll = () => m.if(m.i32.atomic.load(4, g(sa)), m.unreachable());
|
|
353
|
+
const startRef = m.getStart();
|
|
354
|
+
const start = startRef ? binaryen.getFunctionInfo(startRef).name : undefined;
|
|
355
|
+
const callsOut = (expression) => {
|
|
356
|
+
const v = info(expression);
|
|
357
|
+
return v.id === binaryen.CallId || inlineChildren(v).some(callsOut);
|
|
358
|
+
};
|
|
359
|
+
for (let index = 0, count = m.getNumFunctions(); index < count; index++) {
|
|
360
|
+
const f = m.getFunctionByIndex(index);
|
|
361
|
+
const { name, body } = binaryen.getFunctionInfo(f);
|
|
362
|
+
if (!body)
|
|
363
|
+
continue;
|
|
364
|
+
if (name === start) {
|
|
365
|
+
if (callsOut(body))
|
|
366
|
+
throw new Error('start function calls another function');
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
const vars = (n) => Array.from({ length: n }, () => capi._BinaryenFunctionAddVar(f, i32));
|
|
370
|
+
let pointer = -1, effective = -1;
|
|
371
|
+
const temps = new Map();
|
|
372
|
+
// a frame store marks its written bits after its value is computed, from a saved address
|
|
373
|
+
const afterValue = (value, saved, width, reads) => {
|
|
374
|
+
const type = binaryen.getExpressionType(value);
|
|
375
|
+
if (type === binaryen.unreachable)
|
|
376
|
+
return value;
|
|
377
|
+
if (!temps.has(type))
|
|
378
|
+
temps.set(type, capi._BinaryenFunctionAddVar(f, type));
|
|
379
|
+
const t = temps.get(type);
|
|
380
|
+
return m.block(null, [
|
|
381
|
+
m.local.set(t, value),
|
|
382
|
+
...(reads ? [frameCall('__folio_f_r', () => l(saved), width)] : []),
|
|
383
|
+
frameCall('__folio_f_w', () => l(saved), width),
|
|
384
|
+
m.local.get(t, type)
|
|
385
|
+
], type);
|
|
386
|
+
};
|
|
387
|
+
const saver = () => (frame ? vars(1)[0] : undefined);
|
|
388
|
+
// the pointer operand becomes a block that marks the granules and yields the pointer
|
|
389
|
+
const access = (ptr, offset, width, read, write, checks = false, saved) => {
|
|
390
|
+
if (pointer < 0)
|
|
391
|
+
[pointer, effective] = vars(2);
|
|
392
|
+
const t = pointer, u = effective;
|
|
393
|
+
const steps = [
|
|
394
|
+
m.local.set(t, ptr),
|
|
395
|
+
m.local.set(u, offset ? m.i32.add(l(t), c(offset)) : l(t))
|
|
396
|
+
];
|
|
397
|
+
const tracked = [];
|
|
398
|
+
const crosses = (shift, mark) => m.if(m.i32.gt_u(m.i32.and(l(u), c((1 << shift) - 1)), c((1 << shift) - width)), call(mark, m.i32.add(l(u), c(width - 1))));
|
|
399
|
+
if (read) {
|
|
400
|
+
tracked.push(markRead(() => l(u)));
|
|
401
|
+
if (width > 1)
|
|
402
|
+
tracked.push(crosses(rg, '__folio_r_mark'));
|
|
403
|
+
}
|
|
404
|
+
if (write) {
|
|
405
|
+
tracked.push(m.if(unmarked(() => l(u)), call('__folio_w_set', l(u))));
|
|
406
|
+
if (width > 1)
|
|
407
|
+
tracked.push(crosses(wg, '__folio_w_mark'));
|
|
408
|
+
}
|
|
409
|
+
const marks = tracked.splice(0);
|
|
410
|
+
if (window)
|
|
411
|
+
tracked.push(m.if(m.i32.eqz(inRegion(() => l(u), width)), m.block(null, marks, none)));
|
|
412
|
+
else
|
|
413
|
+
tracked.push(...marks);
|
|
414
|
+
if (frame && checks)
|
|
415
|
+
tracked.push(frameCall('__folio_f_r', () => l(u), width));
|
|
416
|
+
if (window)
|
|
417
|
+
steps.push(m.if(m.i32.and(m.i32.lt_u(l(u), g(we)), m.i32.gt_u(m.i32.add(l(u), c(width)), g(wl))), m.if(within(wl, we, () => l(u), c(width)), m.block(null, [
|
|
418
|
+
m.local.set(t, m.i32.add(l(t), g(wd))),
|
|
419
|
+
m.local.set(u, m.i32.add(l(u), g(wd)))
|
|
420
|
+
], none), m.unreachable())));
|
|
421
|
+
const windowed = readWindows > 0 && read && !write && width <= 8;
|
|
422
|
+
const bump = (name) => m.global.set(name, m.i32.add(g(name), c(1)));
|
|
423
|
+
// a load that misses every window is tracked, and counted as a miss when counting
|
|
424
|
+
const miss = (inner) => m.block(null, counting ? [bump(cm), ...inner] : inner, none);
|
|
425
|
+
// a hit inside a window the owner marked in full skips every other step
|
|
426
|
+
const chained = (tail) => {
|
|
427
|
+
let chain = tail;
|
|
428
|
+
for (let w = readWindows - 1; w >= 0; w--) {
|
|
429
|
+
const [low, span] = w === 0 ? [r0l, r0m] : [r1l, r1m];
|
|
430
|
+
chain = m.if(m.i32.lt_u(m.i32.sub(l(u), g(low)), g(span)), counting ? bump(ch) : m.nop(), chain);
|
|
431
|
+
}
|
|
432
|
+
return chain;
|
|
433
|
+
};
|
|
434
|
+
if (slot) {
|
|
435
|
+
const inner = [
|
|
436
|
+
m.if(overlapsArena(() => l(u), c(width)), m.if(m.i32.eqz(within(sl, se, () => l(u), c(width))), call('__folio_x_trap', l(u)))),
|
|
437
|
+
...tracked
|
|
438
|
+
];
|
|
439
|
+
const exempt = (tail) => m.if(within(xl, xe, () => l(u), c(width)), windowed && counting ? bump(ce) : m.nop(), tail);
|
|
440
|
+
// the exempt window first, or the windows first: a hit then skips the exempt check
|
|
441
|
+
// and an exempt load pays the guards
|
|
442
|
+
if (!windowed)
|
|
443
|
+
steps.push(exempt(m.block(null, inner, none)));
|
|
444
|
+
else if (options.exemptFirst)
|
|
445
|
+
steps.push(exempt(chained(miss(inner))));
|
|
446
|
+
else
|
|
447
|
+
steps.push(chained(exempt(miss(inner))));
|
|
448
|
+
}
|
|
449
|
+
else
|
|
450
|
+
steps.push(...(windowed ? [chained(miss(tracked))] : tracked));
|
|
451
|
+
if (saved !== undefined)
|
|
452
|
+
steps.push(m.local.set(saved, l(u)));
|
|
453
|
+
steps.push(l(t));
|
|
454
|
+
return m.block(null, steps, i32);
|
|
455
|
+
};
|
|
456
|
+
const visit = (e) => {
|
|
457
|
+
const v = info(e);
|
|
458
|
+
for (const child of inlineChildren(v))
|
|
459
|
+
visit(child);
|
|
460
|
+
if (window &&
|
|
461
|
+
(v.id === binaryen.MemoryCopyId ||
|
|
462
|
+
v.id === binaryen.MemoryFillId ||
|
|
463
|
+
v.id === binaryen.MemoryInitId)) {
|
|
464
|
+
// the destination operand evaluates all three, rebases the ranges and marks them;
|
|
465
|
+
// the other operands then read the locals back
|
|
466
|
+
const [td, tm, tn] = vars(3);
|
|
467
|
+
const copy = v.id === binaryen.MemoryCopyId;
|
|
468
|
+
const kind = copy ? 'Copy' : v.id === binaryen.MemoryFillId ? 'Fill' : 'Init';
|
|
469
|
+
const middle = copy ? v.source : kind === 'Fill' ? v.value : v.offset;
|
|
470
|
+
const rebase = (local) => m.local.set(local, m.call('__folio_w_rebase', [l(local), l(tn)], i32));
|
|
471
|
+
const api = capi;
|
|
472
|
+
api[`_BinaryenMemory${kind}SetDest`](e, m.block(null, [
|
|
473
|
+
m.local.set(td, v.dest),
|
|
474
|
+
m.local.set(tm, middle),
|
|
475
|
+
m.local.set(tn, v.size),
|
|
476
|
+
rebase(td),
|
|
477
|
+
...(copy
|
|
478
|
+
? [
|
|
479
|
+
rebase(tm),
|
|
480
|
+
call('__folio_x_r_range', l(tm), l(tn)),
|
|
481
|
+
call('__folio_f_rr', l(tm), l(tn))
|
|
482
|
+
]
|
|
483
|
+
: []),
|
|
484
|
+
call('__folio_x_w_range', l(td), l(tn)),
|
|
485
|
+
call('__folio_f_rw', l(td), l(tn)),
|
|
486
|
+
l(td)
|
|
487
|
+
], i32));
|
|
488
|
+
api[`_BinaryenMemory${kind}Set${copy ? 'Source' : kind === 'Fill' ? 'Value' : 'Offset'}`](e, l(tm));
|
|
489
|
+
api[`_BinaryenMemory${kind}SetSize`](e, l(tn));
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
switch (v.id) {
|
|
493
|
+
case binaryen.LoopId:
|
|
494
|
+
if (fuel || stop)
|
|
495
|
+
capi._BinaryenLoopSetBody(e, m.block(null, [...(fuel ? [tick()] : []), ...(stop ? [poll()] : []), v.body], binaryen.getExpressionType(v.body)));
|
|
496
|
+
break;
|
|
497
|
+
case binaryen.LoadId:
|
|
498
|
+
capi._BinaryenLoadSetPtr(e, access(v.ptr, v.offset, v.bytes, true, Boolean(v.isAtomic), true));
|
|
499
|
+
break;
|
|
500
|
+
case binaryen.StoreId: {
|
|
501
|
+
const saved = saver();
|
|
502
|
+
capi._BinaryenStoreSetPtr(e, access(v.ptr, v.offset, v.bytes, Boolean(v.isAtomic), true, false, saved));
|
|
503
|
+
if (saved !== undefined)
|
|
504
|
+
capi._BinaryenStoreSetValue(e, afterValue(v.value, saved, v.bytes, false));
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
case binaryen.AtomicRMWId: {
|
|
508
|
+
const saved = saver();
|
|
509
|
+
capi._BinaryenAtomicRMWSetPtr(e, access(v.ptr, v.offset, v.bytes, true, true, false, saved));
|
|
510
|
+
if (saved !== undefined)
|
|
511
|
+
capi._BinaryenAtomicRMWSetValue(e, afterValue(v.value, saved, v.bytes, true));
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
case binaryen.AtomicCmpxchgId: {
|
|
515
|
+
const saved = saver();
|
|
516
|
+
capi._BinaryenAtomicCmpxchgSetPtr(e, access(v.ptr, v.offset, v.bytes, true, true, false, saved));
|
|
517
|
+
if (saved !== undefined)
|
|
518
|
+
capi._BinaryenAtomicCmpxchgSetReplacement(e, afterValue(v.replacement, saved, v.bytes, true));
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
case binaryen.AtomicWaitId:
|
|
522
|
+
capi._BinaryenAtomicWaitSetPtr(e, access(v.ptr, 0, v.expectedType === binaryen.i64 ? 8 : 4, true, true, true));
|
|
523
|
+
break;
|
|
524
|
+
case binaryen.AtomicNotifyId:
|
|
525
|
+
capi._BinaryenAtomicNotifySetPtr(e, access(v.ptr, 0, 4, true, true, true));
|
|
526
|
+
break;
|
|
527
|
+
case binaryen.SIMDLoadId:
|
|
528
|
+
capi._BinaryenSIMDLoadSetPtr(e, access(v.ptr, v.offset, simdLoadBytes.get(v.op), true, false, true));
|
|
529
|
+
break;
|
|
530
|
+
case binaryen.SIMDLoadStoreLaneId: {
|
|
531
|
+
const bytes = simdLaneBytes.get(v.op);
|
|
532
|
+
const saved = v.isStore ? saver() : undefined;
|
|
533
|
+
capi._BinaryenSIMDLoadStoreLaneSetPtr(e, access(v.ptr, v.offset, bytes, !v.isStore, Boolean(v.isStore), !v.isStore, saved));
|
|
534
|
+
if (saved !== undefined)
|
|
535
|
+
capi._BinaryenSIMDLoadStoreLaneSetVec(e, afterValue(v.vec, saved, bytes, false));
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
case binaryen.MemoryCopyId: {
|
|
539
|
+
const [td, ts, tn] = vars(3);
|
|
540
|
+
capi._BinaryenMemoryCopySetDest(e, hold(td, v.dest));
|
|
541
|
+
capi._BinaryenMemoryCopySetSource(e, hold(ts, v.source));
|
|
542
|
+
capi._BinaryenMemoryCopySetSize(e, m.block(null, [
|
|
543
|
+
m.local.set(tn, v.size),
|
|
544
|
+
call(slot ? '__folio_x_r_range' : '__folio_r_range', l(ts), l(tn)),
|
|
545
|
+
call(slot ? '__folio_x_w_range' : '__folio_w_range', l(td), l(tn)),
|
|
546
|
+
...(frame
|
|
547
|
+
? [
|
|
548
|
+
call('__folio_f_rr', l(ts), l(tn)),
|
|
549
|
+
call('__folio_f_rw', l(td), l(tn))
|
|
550
|
+
]
|
|
551
|
+
: []),
|
|
552
|
+
l(tn)
|
|
553
|
+
], i32));
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
case binaryen.MemoryFillId:
|
|
557
|
+
case binaryen.MemoryInitId: {
|
|
558
|
+
const [td, tn] = vars(2);
|
|
559
|
+
const fill = v.id === binaryen.MemoryFillId;
|
|
560
|
+
capi[fill ? '_BinaryenMemoryFillSetDest' : '_BinaryenMemoryInitSetDest'](e, hold(td, v.dest));
|
|
561
|
+
capi[fill ? '_BinaryenMemoryFillSetSize' : '_BinaryenMemoryInitSetSize'](e, m.block(null, [
|
|
562
|
+
m.local.set(tn, v.size),
|
|
563
|
+
call(slot ? '__folio_x_w_range' : '__folio_w_range', l(td), l(tn)),
|
|
564
|
+
...(frame ? [call('__folio_f_rw', l(td), l(tn))] : []),
|
|
565
|
+
l(tn)
|
|
566
|
+
], i32));
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
visit(body);
|
|
572
|
+
if (fuel)
|
|
573
|
+
capi._BinaryenFunctionSetBody(f, m.block(null, [tick(), body], binaryen.getExpressionType(body)));
|
|
574
|
+
}
|
|
575
|
+
if (fuel) {
|
|
576
|
+
m.addGlobal('__folio_fuel', i32, true, c(0));
|
|
577
|
+
m.addFunctionImport('__folio_exhausted', 'folio', 'exhausted', none, none);
|
|
578
|
+
m.addFunction('__folio_set_fuel', i32, none, [], m.global.set('__folio_fuel', l(0)));
|
|
579
|
+
m.addFunctionExport('__folio_set_fuel', '__folio_set_fuel');
|
|
580
|
+
}
|
|
581
|
+
for (const name of [rb, wb, lp, ll, sa]) {
|
|
582
|
+
m.addGlobal(name, i32, true, c(0));
|
|
583
|
+
m.addGlobalExport(name, name);
|
|
584
|
+
}
|
|
585
|
+
if (frame)
|
|
586
|
+
for (const name of [fl, fn, fs]) {
|
|
587
|
+
m.addGlobal(name, i32, true, c(0));
|
|
588
|
+
m.addGlobalExport(name, name);
|
|
589
|
+
}
|
|
590
|
+
if (slot || window)
|
|
591
|
+
for (const name of [xl, xe, sl, se, al, ae, xv]) {
|
|
592
|
+
m.addGlobal(name, i32, true, c(0));
|
|
593
|
+
m.addGlobalExport(name, name);
|
|
594
|
+
}
|
|
595
|
+
if (window)
|
|
596
|
+
for (const name of [wl, we, wd]) {
|
|
597
|
+
m.addGlobal(name, i32, true, c(0));
|
|
598
|
+
m.addGlobalExport(name, name);
|
|
599
|
+
}
|
|
600
|
+
for (const [index, pair] of [
|
|
601
|
+
[r0l, r0m],
|
|
602
|
+
[r1l, r1m]
|
|
603
|
+
].entries())
|
|
604
|
+
if (index < readWindows)
|
|
605
|
+
for (const name of pair) {
|
|
606
|
+
m.addGlobal(name, i32, true, c(0));
|
|
607
|
+
m.addGlobalExport(name, name);
|
|
608
|
+
}
|
|
609
|
+
if (summary) {
|
|
610
|
+
m.addGlobal(rs, i32, true, c(0));
|
|
611
|
+
m.addGlobalExport(rs, rs);
|
|
612
|
+
}
|
|
613
|
+
if (counting)
|
|
614
|
+
for (const name of [ch, cm, ce]) {
|
|
615
|
+
m.addGlobal(name, i32, true, c(0));
|
|
616
|
+
m.addGlobalExport(name, name);
|
|
617
|
+
}
|
|
618
|
+
if (stack !== undefined)
|
|
619
|
+
m.addGlobalExport(stack, inlineGlobals.stackPointer);
|
|
620
|
+
const pair = binaryen.createType([i32, i32]);
|
|
621
|
+
const addFrameHelpers = () => {
|
|
622
|
+
const v = l;
|
|
623
|
+
const max = (a, b) => m.select(m.i32.gt_u(a(), b()), a(), b());
|
|
624
|
+
const exposedBase = () => m.i32.add(g(fs), m.i32.add(m.i32.shl(m.i32.shr_u(m.i32.add(g(fn), c(31)), c(5)), c(2)), c(4)));
|
|
625
|
+
// scalar access: params a, w; vars 2 lo, 3 hi, 4 end, 5 i, 6 n, 7 p, 8 miss, 9 q
|
|
626
|
+
const scalar = (name, read) => {
|
|
627
|
+
const mask = () => m.i32.shl(m.i32.sub(m.i32.shl(c(1), v(6)), c(1)), m.i32.and(v(5), c(7)));
|
|
628
|
+
const body = [
|
|
629
|
+
m.local.set(2, max(() => v(0), () => g(fl))),
|
|
630
|
+
m.local.set(3, m.i32.add(v(0), v(1))),
|
|
631
|
+
m.local.set(4, m.i32.add(g(fl), g(fn))),
|
|
632
|
+
m.if(m.i32.gt_u(v(3), v(4)), m.local.set(3, v(4))),
|
|
633
|
+
m.if(m.i32.le_u(v(3), v(2)), m.return()),
|
|
634
|
+
m.local.set(5, m.i32.sub(v(2), g(fl))),
|
|
635
|
+
m.local.set(6, m.i32.sub(v(3), v(2))),
|
|
636
|
+
m.local.set(7, m.i32.add(g(fs), m.i32.shr_u(v(5), c(3)))),
|
|
637
|
+
...(read
|
|
638
|
+
? [
|
|
639
|
+
m.local.set(8, m.i32.and(mask(), m.i32.xor(m.i32.load(0, 1, v(7)), c(-1)))),
|
|
640
|
+
m.if(v(8), m.block(null, [
|
|
641
|
+
m.local.set(9, m.i32.add(exposedBase(), m.i32.shr_u(v(5), c(3)))),
|
|
642
|
+
m.i32.store(0, 1, v(9), m.i32.or(m.i32.load(0, 1, v(9)), v(8)))
|
|
643
|
+
], none))
|
|
644
|
+
]
|
|
645
|
+
: [m.i32.store(0, 1, v(7), m.i32.or(m.i32.load(0, 1, v(7)), mask()))])
|
|
646
|
+
];
|
|
647
|
+
m.addFunction(name, pair, none, Array(8).fill(i32), m.block(null, body, none));
|
|
648
|
+
};
|
|
649
|
+
scalar('__folio_f_r', true);
|
|
650
|
+
scalar('__folio_f_w', false);
|
|
651
|
+
// range access: params a, n; vars 2 lo, 3 hi, 4 end, 5 i, 6 j, 7 g, 8 last, 9 ms, 10 len,
|
|
652
|
+
// 11 mask, 12 p, 13 miss
|
|
653
|
+
const bulk = (name, read) => {
|
|
654
|
+
const body = [
|
|
655
|
+
m.if(m.i32.eqz(v(1)), m.return()),
|
|
656
|
+
m.local.set(2, max(() => v(0), () => g(fl))),
|
|
657
|
+
m.local.set(3, m.i32.add(v(0), v(1))),
|
|
658
|
+
m.if(m.i32.lt_u(v(3), v(0)), m.local.set(3, c(-1))),
|
|
659
|
+
m.local.set(4, m.i32.add(g(fl), g(fn))),
|
|
660
|
+
m.if(m.i32.gt_u(v(3), v(4)), m.local.set(3, v(4))),
|
|
661
|
+
m.if(m.i32.le_u(v(3), v(2)), m.return()),
|
|
662
|
+
m.local.set(5, m.i32.sub(v(2), g(fl))),
|
|
663
|
+
m.local.set(6, m.i32.sub(v(3), g(fl))),
|
|
664
|
+
m.local.set(7, m.i32.shr_u(v(5), c(5))),
|
|
665
|
+
m.local.set(8, m.i32.shr_u(m.i32.sub(v(6), c(1)), c(5))),
|
|
666
|
+
m.loop('again', m.block(null, [
|
|
667
|
+
// ms: first byte of the range inside granule g; len: bytes of it inside
|
|
668
|
+
m.local.set(9, m.i32.sub(max(() => v(5), () => m.i32.shl(v(7), c(5))), m.i32.shl(v(7), c(5)))),
|
|
669
|
+
m.local.set(10, m.i32.sub(m.select(m.i32.lt_u(v(6), m.i32.add(m.i32.shl(v(7), c(5)), c(32))), v(6), m.i32.add(m.i32.shl(v(7), c(5)), c(32))), m.i32.add(m.i32.shl(v(7), c(5)), v(9)))),
|
|
670
|
+
m.local.set(11, m.select(m.i32.eq(v(10), c(32)), c(-1), m.i32.shl(m.i32.sub(m.i32.shl(c(1), v(10)), c(1)), v(9)))),
|
|
671
|
+
m.local.set(12, m.i32.add(g(fs), m.i32.shl(v(7), c(2)))),
|
|
672
|
+
...(read
|
|
673
|
+
? [
|
|
674
|
+
m.local.set(13, m.i32.and(v(11), m.i32.xor(m.i32.load(0, 4, v(12)), c(-1)))),
|
|
675
|
+
m.if(v(13), m.i32.store(0, 4, m.i32.add(v(12), m.i32.sub(exposedBase(), g(fs))), m.i32.or(m.i32.load(0, 4, m.i32.add(v(12), m.i32.sub(exposedBase(), g(fs)))), v(13))))
|
|
676
|
+
]
|
|
677
|
+
: [
|
|
678
|
+
m.i32.store(0, 4, v(12), m.i32.or(m.i32.load(0, 4, v(12)), v(11)))
|
|
679
|
+
]),
|
|
680
|
+
m.local.set(7, m.i32.add(v(7), c(1))),
|
|
681
|
+
m.br('again', m.i32.le_u(v(7), v(8)))
|
|
682
|
+
], none))
|
|
683
|
+
];
|
|
684
|
+
m.addFunction(name, pair, none, Array(12).fill(i32), m.block(null, body, none));
|
|
685
|
+
};
|
|
686
|
+
bulk('__folio_f_rr', true);
|
|
687
|
+
bulk('__folio_f_rw', false);
|
|
688
|
+
};
|
|
689
|
+
m.addFunction('__folio_r_mark', i32, none, [], markRead(() => l(0)));
|
|
690
|
+
m.addFunction('__folio_undo', i32, none, [i32], m.block(null, [
|
|
691
|
+
m.if(m.i32.gt_u(m.i32.add(g(lp), c(entry)), g(ll)), m.unreachable()),
|
|
692
|
+
m.local.set(1, g(lp)),
|
|
693
|
+
m.i32.store(0, 4, l(1), l(0)),
|
|
694
|
+
...Array.from({ length: (1 << wg) / 8 }, (_, k) => m.i64.store(8 + 8 * k, 8, l(1), m.i64.load(8 * k, 8, l(0)))),
|
|
695
|
+
m.i32.store(4, 4, l(1), m.i32.atomic.rmw.add(0, g(sa), c(1))),
|
|
696
|
+
m.global.set(lp, m.i32.add(l(1), c(entry)))
|
|
697
|
+
], none));
|
|
698
|
+
m.addFunction('__folio_w_set', i32, none, [], m.block(null, [setBit(wb, () => l(0), wg), call('__folio_undo', m.i32.and(l(0), c(-(1 << wg))))], none));
|
|
699
|
+
m.addFunction('__folio_w_mark', i32, none, [], m.if(unmarked(() => l(0)), call('__folio_w_set', l(0))));
|
|
700
|
+
// every granule from a to a + n - 1
|
|
701
|
+
const range = (name, shift, mark) => m.addFunction(name, pair, none, [i32, i32], m.if(l(1), m.block(null, [
|
|
702
|
+
m.local.set(2, m.i32.shr_u(l(0), c(shift))),
|
|
703
|
+
m.local.set(3, m.i32.shr_u(m.i32.add(l(0), m.i32.sub(l(1), c(1))), c(shift))),
|
|
704
|
+
m.loop('again', m.block(null, [
|
|
705
|
+
call(mark, m.i32.shl(l(2), c(shift))),
|
|
706
|
+
m.local.set(2, m.i32.add(l(2), c(1))),
|
|
707
|
+
m.br('again', m.i32.le_u(l(2), l(3)))
|
|
708
|
+
], none))
|
|
709
|
+
], none)));
|
|
710
|
+
range('__folio_r_range', rg, '__folio_r_mark');
|
|
711
|
+
range('__folio_w_range', wg, '__folio_w_mark');
|
|
712
|
+
if (frame)
|
|
713
|
+
addFrameHelpers();
|
|
714
|
+
if (slot || window) {
|
|
715
|
+
m.addFunction('__folio_x_trap', i32, none, [], m.block(null, [m.global.set(xv, l(0)), m.unreachable()], none));
|
|
716
|
+
// params a, n; the range is tracked unless it lies in the exempt window
|
|
717
|
+
const exempt = (name, tracked) => m.addFunction(name, pair, none, [i32], m.if(l(1), m.block(null, [
|
|
718
|
+
m.local.set(2, m.i32.add(l(0), l(1))),
|
|
719
|
+
m.if(m.i32.eqz(m.i32.and(m.i32.ge_u(l(0), g(xl)), m.i32.le_u(l(2), g(xe)))), m.block(null, [
|
|
720
|
+
m.if(m.i32.and(m.i32.lt_u(l(0), g(ae)), m.i32.gt_u(l(2), g(al))), m.if(m.i32.eqz(m.i32.and(m.i32.ge_u(l(0), g(sl)), m.i32.le_u(l(2), g(se)))), call('__folio_x_trap', l(0)))),
|
|
721
|
+
window
|
|
722
|
+
? m.if(m.i32.eqz(inRegion(() => l(0), () => l(1))), call(tracked, l(0), l(1)))
|
|
723
|
+
: call(tracked, l(0), l(1))
|
|
724
|
+
], none))
|
|
725
|
+
], none)));
|
|
726
|
+
exempt('__folio_x_r_range', '__folio_r_range');
|
|
727
|
+
exempt('__folio_x_w_range', '__folio_w_range');
|
|
728
|
+
}
|
|
729
|
+
if (window) {
|
|
730
|
+
// params a, n, result the address a bulk range runs at: inside the window it moves to the
|
|
731
|
+
// copy, across its edge it traps
|
|
732
|
+
m.addFunction('__folio_w_rebase', pair, i32, [i32], m.block(null, [
|
|
733
|
+
m.if(m.i32.eqz(l(1)), m.return(l(0))),
|
|
734
|
+
m.local.set(2, m.i32.add(l(0), l(1))),
|
|
735
|
+
m.if(m.i32.lt_u(l(2), l(0)), m.return(l(0))),
|
|
736
|
+
m.if(m.i32.and(m.i32.lt_u(l(0), g(we)), m.i32.gt_u(l(2), g(wl))), m.block(null, [
|
|
737
|
+
m.if(m.i32.and(m.i32.ge_u(l(0), g(wl)), m.i32.le_u(l(2), g(we))), m.return(m.i32.add(l(0), g(wd)))),
|
|
738
|
+
m.unreachable()
|
|
739
|
+
], none)),
|
|
740
|
+
l(0)
|
|
741
|
+
], i32));
|
|
742
|
+
}
|
|
743
|
+
if (!m.validate())
|
|
744
|
+
throw new Error('invalid inline lowering');
|
|
745
|
+
return m.emitBinary();
|
|
746
|
+
}
|
|
747
|
+
finally {
|
|
748
|
+
m.dispose();
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
// #endregion
|
|
752
|
+
// #region lane module
|
|
753
|
+
/**
|
|
754
|
+
* The module a managed lane runs with inline tracking: `lowerInline` with fuel and the arena
|
|
755
|
+
* guard on, and the memory import widened to the largest maximum so a lane can place the marks
|
|
756
|
+
* and the undo log above the program memory. Build time only; the input is the control plan's
|
|
757
|
+
* `workerOriginal`.
|
|
758
|
+
*/
|
|
759
|
+
export function inlineLane(workerOriginal) {
|
|
760
|
+
const lowered = lowerInline(workerOriginal, { fuel: true, guard: true });
|
|
761
|
+
const memories = importedMemory(lowered);
|
|
762
|
+
if (memories.length !== 1)
|
|
763
|
+
throw new Error('exactly one imported memory required');
|
|
764
|
+
return shareMemory(lowered, memories[0]);
|
|
765
|
+
}
|
|
766
|
+
// #endregion
|