@geastack/compiler 1.0.48 → 1.0.62
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 +57 -19
- package/dist/cli.js +2 -1
- package/dist/compiler.d.ts +1 -1
- package/dist/compiler.js +13 -6
- package/dist/conversion/derive.js +5 -0
- package/dist/conversion/native-field-view.d.ts +7 -0
- package/dist/conversion/native-field-view.js +50 -0
- package/dist/conversion/native-record-products.js +1 -0
- package/dist/conversion/native-view-origins.d.ts +1 -1
- package/dist/conversion/native-view-origins.js +5 -4
- package/dist/conversion/nodes.d.ts +9 -0
- package/dist/conversion/nodes.js +18 -0
- package/dist/conversion/record-view.d.ts +25 -1
- package/dist/conversion/record-view.js +61 -2
- package/dist/conversion/structural-plan.js +7 -6
- package/dist/ir/borrowed-field-reads.d.ts +30 -0
- package/dist/ir/borrowed-field-reads.js +218 -0
- package/dist/ir/borrowed-window-cells.d.ts +35 -0
- package/dist/ir/borrowed-window-cells.js +117 -0
- package/dist/ir/call-dispatch.js +21 -4
- package/dist/ir/callable-class-flow.js +269 -43
- package/dist/ir/captures.js +18 -1
- package/dist/ir/certify.js +14 -1
- package/dist/ir/construction-only-fields.d.ts +15 -1
- package/dist/ir/construction-only-fields.js +71 -3
- package/dist/ir/dead-logical-merge.d.ts +4 -0
- package/dist/ir/dead-logical-merge.js +89 -18
- package/dist/ir/dense-loops.d.ts +38 -4
- package/dist/ir/dense-loops.js +112 -17
- package/dist/ir/dense-read-windows.d.ts +42 -0
- package/dist/ir/dense-read-windows.js +137 -0
- package/dist/ir/facts.d.ts +12 -0
- package/dist/ir/facts.js +18 -2
- package/dist/ir/field-read-reuse.d.ts +71 -0
- package/dist/ir/field-read-reuse.js +430 -0
- package/dist/ir/hoist.d.ts +19 -2
- package/dist/ir/hoist.js +143 -24
- package/dist/ir/lower-narrow.js +19 -1
- package/dist/ir/model.d.ts +19 -0
- package/dist/ir/native-field-view-domains.d.ts +12 -2
- package/dist/ir/native-field-view-domains.js +51 -15
- package/dist/ir/native-field-view-facts.js +5 -2
- package/dist/ir/native-method-frames.js +15 -2
- package/dist/ir/plain-field-read.d.ts +31 -0
- package/dist/ir/plain-field-read.js +51 -0
- package/dist/ir/program-facts.d.ts +14 -0
- package/dist/ir/program-facts.js +85 -2
- package/dist/ir/proven-branches.js +8 -1
- package/dist/ir/publish-conversion-recipes.js +5 -1
- package/dist/ir/short-circuit-threading.d.ts +12 -4
- package/dist/ir/short-circuit-threading.js +124 -12
- package/dist/ir/stable-cell-reads.d.ts +24 -1
- package/dist/ir/stable-cell-reads.js +65 -42
- package/dist/ir/write-effects.d.ts +42 -0
- package/dist/ir/write-effects.js +208 -0
- package/dist/plugins/gea/contract.d.ts +3 -2
- package/dist/plugins/gea/contract.js +3 -1
- package/dist/plugins/gea/host.d.ts +7 -2
- package/dist/plugins/gea/host.js +25 -5
- package/dist/project-preparation.d.ts +11 -0
- package/dist/project-preparation.js +44 -0
- package/dist/projection/classes.d.ts +8 -0
- package/dist/projection/classes.js +112 -12
- package/dist/projection/dispatch.d.ts +24 -4
- package/dist/projection/dispatch.js +71 -2
- package/dist/projection/fields.js +2 -2
- package/dist/representation/model.d.ts +3 -0
- package/dist/representation/model.js +3 -0
- package/dist/representation/value-records.js +22 -0
- package/dist/semantics/frontend-timing.d.ts +9 -0
- package/dist/semantics/frontend-timing.js +43 -2
- package/dist/semantics/frontend.js +103 -7
- package/dist/semantics/model/operations.d.ts +7 -0
- package/dist/semantics/normalize/binding-fixpoint.js +7 -2
- package/dist/semantics/normalize/callable-data-write.js +6 -2
- package/dist/semantics/normalize/field-bindings.js +151 -111
- package/dist/semantics/normalize/flow/callable-reach.js +27 -0
- package/dist/semantics/normalize/flow/component-solver.js +23 -5
- package/dist/semantics/normalize/flow/source-record-data.js +6 -2
- package/dist/semantics/normalize/flow/source-value-session.d.ts +13 -0
- package/dist/semantics/normalize/flow/source-value-session.js +207 -65
- package/dist/semantics/normalize/global-host-mutations.js +162 -1
- package/dist/semantics/normalize/parameter-bindings.js +16 -1
- package/dist/semantics/normalize/producers/class-lifecycle.js +2 -1
- package/dist/semantics/normalize/producers/erasure.js +13 -8
- package/dist/semantics/subclass-member-overlay-transform.d.ts +3 -2
- package/dist/semantics/subclass-member-overlay-transform.js +416 -316
- package/dist/semantics/typescript-tsgo.d.ts +20 -0
- package/dist/semantics/typescript-tsgo.js +832 -0
- package/dist/semantics/typescript.d.ts +0 -9
- package/dist/semantics/typescript.js +4 -1
- package/dist/targets/cpp/borrowed-call-entry.d.ts +9 -0
- package/dist/targets/cpp/borrowed-call-entry.js +12 -2
- package/dist/targets/cpp/captures.js +4 -0
- package/dist/targets/cpp/class-properties/computed-method-value.d.ts +1 -1
- package/dist/targets/cpp/class-properties/computed-method-value.js +17 -7
- package/dist/targets/cpp/class-properties/emit-class-properties.d.ts +8 -0
- package/dist/targets/cpp/class-properties/emit-class-properties.js +62 -2
- package/dist/targets/cpp/class-properties/native-method-read.js +12 -1
- package/dist/targets/cpp/conversions.js +1 -0
- package/dist/targets/cpp/emit-arrays.d.ts +5 -0
- package/dist/targets/cpp/emit-arrays.js +60 -10
- package/dist/targets/cpp/emit-binding-reference.d.ts +19 -6
- package/dist/targets/cpp/emit-binding-reference.js +26 -8
- package/dist/targets/cpp/emit-bindings.js +22 -0
- package/dist/targets/cpp/emit-callable.js +133 -16
- package/dist/targets/cpp/emit-carrier-members.d.ts +6 -0
- package/dist/targets/cpp/emit-carrier-members.js +24 -2
- package/dist/targets/cpp/emit-context.d.ts +97 -1
- package/dist/targets/cpp/emit-context.js +52 -0
- package/dist/targets/cpp/emit-equality.js +3 -0
- package/dist/targets/cpp/emit-narrowing.js +9 -0
- package/dist/targets/cpp/emit-native-field-view.d.ts +24 -0
- package/dist/targets/cpp/emit-native-field-view.js +87 -8
- package/dist/targets/cpp/emit-properties.js +139 -16
- package/dist/targets/cpp/emit-record-view.js +49 -22
- package/dist/targets/cpp/emit-return.js +5 -1
- package/dist/targets/cpp/emit-tonumber.js +4 -1
- package/dist/targets/cpp/emit-union-properties.js +1 -1
- package/dist/targets/cpp/emit.d.ts +4 -1
- package/dist/targets/cpp/emit.js +148 -6
- package/dist/targets/cpp/reactive-dependencies.js +100 -28
- package/dist/targets/cpp/translation-unit.js +71 -17
- package/dist/targets/cpp/virtual-methods.d.ts +2 -2
- package/dist/targets/cpp/virtual-methods.js +11 -3
- package/package.json +1 -1
- package/src/targets/cpp/runtime/gea_runtime.h +400 -21
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import { irValueId } from '../identity/ids.js';
|
|
2
|
+
import { classFamilyOverridesOf } from '../projection/dispatch.js';
|
|
3
|
+
import { classMemberOf, recordAccessorsOfShape, recordFieldsOfShape } from '../projection/fields.js';
|
|
4
|
+
import { representationKey } from '../representation/model.js';
|
|
5
|
+
import { stringConstantsOf } from './dead-values.js';
|
|
6
|
+
import { controlFlowGraphOf } from './dominance.js';
|
|
7
|
+
import { arrayAllocationDrainsDynamicIterator, operandsOfIrOperation, resultOfIrOperation } from './queries.js';
|
|
8
|
+
import { verifyIrBody } from './verify.js';
|
|
9
|
+
import { inertArgument, nativeData, primitive } from './write-effects.js';
|
|
10
|
+
/**
|
|
11
|
+
* Reuse a field read that an earlier read of the same field already answered.
|
|
12
|
+
*
|
|
13
|
+
* A slab test over `boxes` reads `box.min` and `box.max` once per axis, six
|
|
14
|
+
* reads of two fields with nothing between them that can store either. V8
|
|
15
|
+
* eliminates the repeated loads; clang cannot, because each one is a call
|
|
16
|
+
* through a live-view check that hands back a retained handle, and the retain
|
|
17
|
+
* and release around it are calls too. Here the reads are the same IR
|
|
18
|
+
* operation on the same receiver, and this pass can see that nothing between
|
|
19
|
+
* them stores the key or runs code, so the later read takes the earlier
|
|
20
|
+
* read's value.
|
|
21
|
+
*
|
|
22
|
+
* Forward availability over the body's control flow, intersected at joins:
|
|
23
|
+
* a read is available at an operation when every path from the entry passes
|
|
24
|
+
* through it and nothing on the path after it may change what it read. Being
|
|
25
|
+
* available on every path is what makes the earlier read dominate the later
|
|
26
|
+
* one, so the rewrite never uses a value its definition does not reach.
|
|
27
|
+
*
|
|
28
|
+
* The receiver is named by the SSA value or, for a binding read, by the cell:
|
|
29
|
+
* `box.min.x` and `box.max.x` read `box` twice, and both reads see the cell's
|
|
30
|
+
* one value until something writes it. A read qualifies only when it loads a
|
|
31
|
+
* data field no getter can answer, on any class or record that carries the
|
|
32
|
+
* key, so reusing it runs nothing less than the original did. Anything that
|
|
33
|
+
* can run the program's code -- a call, a coercion, an accessor -- forgets
|
|
34
|
+
* every read; a store forgets the reads of its key, on every receiver, since
|
|
35
|
+
* two names can be one object. A body with a try region is left alone: its
|
|
36
|
+
* exceptional edges are not in the graph this walks.
|
|
37
|
+
*/
|
|
38
|
+
export const reuseFieldReads = (bodies, classes, deriver) => {
|
|
39
|
+
const codeFree = codeFreeFieldReadOf(bodies.values(), classes, deriver);
|
|
40
|
+
let output = null;
|
|
41
|
+
for (const [id, body] of bodies) {
|
|
42
|
+
const reused = reusedBody(body, codeFree);
|
|
43
|
+
if (reused === body)
|
|
44
|
+
continue;
|
|
45
|
+
output ??= new Map(bodies);
|
|
46
|
+
output.set(id, reused);
|
|
47
|
+
}
|
|
48
|
+
return output ?? bodies;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* The one answer to which field reads this pass, and the borrowed reads that
|
|
52
|
+
* lean on the same proof (`borrowed-field-reads.ts`), may treat as a load.
|
|
53
|
+
*/
|
|
54
|
+
export const codeFreeFieldReadOf = (bodies, classes, deriver) => {
|
|
55
|
+
const accessorKeys = accessorKeysOf(bodies, classes, deriver);
|
|
56
|
+
return (receiver, key) => {
|
|
57
|
+
// A class instance is read through its own family's members, never a
|
|
58
|
+
// view's origin, so only a record answers to another shape's getter:
|
|
59
|
+
// three's `Euler` declaring `get x()` says nothing about `Vector3.x`.
|
|
60
|
+
if (receiver.kind === 'class-ref') {
|
|
61
|
+
if (classes.get(receiver.declaration)?.nativeBase !== null)
|
|
62
|
+
return false;
|
|
63
|
+
return (classMemberOf(classes, receiver.declaration, key)?.kind === 'field' &&
|
|
64
|
+
classFamilyOverridesOf(classes, receiver.declaration, key).length === 0);
|
|
65
|
+
}
|
|
66
|
+
if (accessorKeys.has(key))
|
|
67
|
+
return false;
|
|
68
|
+
if (receiver.kind === 'record')
|
|
69
|
+
return receiver.fields.some((field) => field.key === key);
|
|
70
|
+
if (receiver.kind === 'native-record-ref' && receiver.native === null)
|
|
71
|
+
return recordFieldsOfShape(deriver, receiver.shapeId)?.some((field) => field.key === key) ?? false;
|
|
72
|
+
return false;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Every key some getter or setter answers: a class accessor, or an accessor
|
|
77
|
+
* of a record shape a value of the program is carried as. A record viewed as
|
|
78
|
+
* another shape reads through its origin, so a field of the viewed shape can
|
|
79
|
+
* still be answered by a getter of the shape it came from.
|
|
80
|
+
*/
|
|
81
|
+
const accessorKeysOf = (bodies, classes, deriver) => {
|
|
82
|
+
const keys = new Set();
|
|
83
|
+
for (const layout of classes.values())
|
|
84
|
+
for (const accessor of layout.accessors)
|
|
85
|
+
keys.add(accessor.key);
|
|
86
|
+
const seen = new Set();
|
|
87
|
+
const shapes = new Set();
|
|
88
|
+
const visit = (representation) => {
|
|
89
|
+
if (seen.has(representation))
|
|
90
|
+
return;
|
|
91
|
+
seen.add(representation);
|
|
92
|
+
if (representation.kind === 'optional')
|
|
93
|
+
return visit(representation.payload);
|
|
94
|
+
if (representation.kind === 'tagged-union') {
|
|
95
|
+
for (const arm of representation.arms)
|
|
96
|
+
visit(arm.value);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (representation.kind === 'record') {
|
|
100
|
+
for (const accessor of representation.accessors)
|
|
101
|
+
keys.add(accessor.key);
|
|
102
|
+
for (const field of representation.fields)
|
|
103
|
+
visit(field.value);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (representation.kind === 'native-record-ref' && representation.native === null && !shapes.has(representation.shapeId)) {
|
|
107
|
+
shapes.add(representation.shapeId);
|
|
108
|
+
for (const accessor of recordAccessorsOfShape(deriver, representation.shapeId) ?? [])
|
|
109
|
+
keys.add(accessor.key);
|
|
110
|
+
for (const field of recordFieldsOfShape(deriver, representation.shapeId) ?? [])
|
|
111
|
+
visit(field.value);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
for (const body of bodies)
|
|
115
|
+
for (const representation of body.values.values())
|
|
116
|
+
visit(representation);
|
|
117
|
+
return keys;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Everything a read is besides its own identity. Two reads with the same
|
|
121
|
+
* shape on the same receiver render the same load; any published fact that
|
|
122
|
+
* differs between them (a reactive marker, a recipe, a narrowing) keeps them
|
|
123
|
+
* apart.
|
|
124
|
+
*/
|
|
125
|
+
const readShapeOf = (operation) => {
|
|
126
|
+
const { result, lineage: _lineage, receiver: _receiver, key: _key, ...rest } = operation;
|
|
127
|
+
return `${representationKey(result.representation)}|${JSON.stringify(rest)}`;
|
|
128
|
+
};
|
|
129
|
+
export const fieldReadFactsOf = (body, codeFree) => {
|
|
130
|
+
const keys = stringConstantsOf(body);
|
|
131
|
+
const cells = new Map();
|
|
132
|
+
const producers = new Map();
|
|
133
|
+
// A standard-prototype absence fact is authenticated by tracing its
|
|
134
|
+
// receiver back to the source operand it was proved for; a reused read
|
|
135
|
+
// traces to the other read's source, so a read such a fact consumes stays.
|
|
136
|
+
//
|
|
137
|
+
// The trace does not stop at the consumer's own operand: it walks back
|
|
138
|
+
// through the transports between the read and the consumer (`a.b?.c` reads
|
|
139
|
+
// `c` off the unwrapped `a.b`, a convert away from the read), so every value
|
|
140
|
+
// on that walk is pinned, not only the first.
|
|
141
|
+
const definitions = new Map();
|
|
142
|
+
for (const block of body.blocks.values())
|
|
143
|
+
for (const operation of block.operations) {
|
|
144
|
+
const result = resultOfIrOperation(operation);
|
|
145
|
+
if (result)
|
|
146
|
+
definitions.set(result.id, operation);
|
|
147
|
+
}
|
|
148
|
+
const pinned = new Set();
|
|
149
|
+
const pin = (start) => {
|
|
150
|
+
let value = start;
|
|
151
|
+
while (value !== undefined && !pinned.has(value)) {
|
|
152
|
+
pinned.add(value);
|
|
153
|
+
const producer = definitions.get(value);
|
|
154
|
+
value =
|
|
155
|
+
producer?.kind === 'convert' || producer?.kind === 'merge-live-arm-rebuild'
|
|
156
|
+
? producer.source.value
|
|
157
|
+
: producer?.kind === 'proxy-part'
|
|
158
|
+
? producer.proxy.value
|
|
159
|
+
: undefined;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
for (const block of body.blocks.values())
|
|
163
|
+
for (const operation of block.operations)
|
|
164
|
+
if (((operation.kind === 'get' || operation.kind === 'has-property') && operation.ordinaryObjectPrototypeKeyAbsent === true) ||
|
|
165
|
+
(operation.kind === 'set' && operation.ordinaryObjectDataWriteAbsent === true))
|
|
166
|
+
for (const operand of operandsOfIrOperation(operation))
|
|
167
|
+
pin(operand.value);
|
|
168
|
+
const candidates = new Map();
|
|
169
|
+
for (const block of body.blocks.values())
|
|
170
|
+
for (const operation of block.operations) {
|
|
171
|
+
if (operation.kind === 'binding-read')
|
|
172
|
+
cells.set(operation.result.id, operation.declaration);
|
|
173
|
+
if (operation.kind === 'get')
|
|
174
|
+
producers.set(operation.result.id, operation);
|
|
175
|
+
if (operation.kind !== 'get' || operation.reactive === true || operation.hostMethod !== undefined)
|
|
176
|
+
continue;
|
|
177
|
+
const key = keys.get(operation.key.value);
|
|
178
|
+
if (key === undefined || !codeFree(operation.receiver.representation, key))
|
|
179
|
+
continue;
|
|
180
|
+
candidates.set(operation, { key, shape: readShapeOf(operation) });
|
|
181
|
+
}
|
|
182
|
+
const receiverOf = (value) => {
|
|
183
|
+
const cell = cells.get(value);
|
|
184
|
+
return cell === undefined ? `value:${value}` : `cell:${cell}`;
|
|
185
|
+
};
|
|
186
|
+
const all = { kind: 'all' };
|
|
187
|
+
const killOf = (operation) => {
|
|
188
|
+
switch (operation.kind) {
|
|
189
|
+
case 'constant':
|
|
190
|
+
case 'binding-read':
|
|
191
|
+
case 'parameter':
|
|
192
|
+
case 'receiver':
|
|
193
|
+
case 'phi':
|
|
194
|
+
case 'global-this':
|
|
195
|
+
case 'allocate-ordinary-object':
|
|
196
|
+
case 'allocate-record':
|
|
197
|
+
case 'allocate-callable':
|
|
198
|
+
case 'test':
|
|
199
|
+
return null;
|
|
200
|
+
case 'binding-write':
|
|
201
|
+
case 'binding-renew':
|
|
202
|
+
return { kind: 'cell', cell: operation.declaration };
|
|
203
|
+
case 'allocate-array-object':
|
|
204
|
+
return arrayAllocationDrainsDynamicIterator(operation) ? all : null;
|
|
205
|
+
case 'compute':
|
|
206
|
+
return operandsOfIrOperation(operation).every((operand) => primitive(operand.representation)) ? null : all;
|
|
207
|
+
case 'convert':
|
|
208
|
+
return primitive(operation.source.representation) || !primitive(operation.result.representation) ? null : all;
|
|
209
|
+
case 'get':
|
|
210
|
+
return candidates.has(operation) || operation.hostMethod !== undefined || nativeData(operation.receiver.representation) ? null : all;
|
|
211
|
+
case 'call': {
|
|
212
|
+
// A host method over data -- `Math.max`, an array's `indexOf` -- runs
|
|
213
|
+
// none of the program's code (`write-effects.ts`).
|
|
214
|
+
const callee = producers.get(operation.callee.value);
|
|
215
|
+
if (operation.closedCallee !== undefined || callee?.kind !== 'get')
|
|
216
|
+
return all;
|
|
217
|
+
const receiver = callee.receiver.representation.kind === 'optional' ? callee.receiver.representation.payload : callee.receiver.representation;
|
|
218
|
+
const host = (callee.hostMethod !== undefined || nativeData(receiver)) && (receiver.kind === 'native-handle' || inertArgument(receiver));
|
|
219
|
+
return host && operation.arguments.every((argument) => inertArgument(argument.representation)) ? null : all;
|
|
220
|
+
}
|
|
221
|
+
case 'set':
|
|
222
|
+
case 'define-own-property': {
|
|
223
|
+
const key = keys.get(operation.key.value);
|
|
224
|
+
if (key === undefined)
|
|
225
|
+
return all;
|
|
226
|
+
const receiver = operation.receiver.representation;
|
|
227
|
+
return codeFree(receiver.kind === 'optional' ? receiver.payload : receiver, key) ? { kind: 'key', key } : all;
|
|
228
|
+
}
|
|
229
|
+
default:
|
|
230
|
+
return all;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
const forgets = (kill, fact) => kill !== null && (kill.kind === 'all' || (kill.kind === 'key' ? fact.key === kill.key : fact.receiver === `cell:${kill.cell}`));
|
|
234
|
+
const factId = (receiver, key, shape) => `${receiver}\u0000${key}\u0000${shape}`;
|
|
235
|
+
return { candidates, pinned, receiverOf, killOf, forgets, factId };
|
|
236
|
+
};
|
|
237
|
+
const reusedBody = (body, codeFree) => {
|
|
238
|
+
if (body.tryRegions.length > 0)
|
|
239
|
+
return body;
|
|
240
|
+
const { candidates, pinned, receiverOf, killOf, forgets, factId } = fieldReadFactsOf(body, codeFree);
|
|
241
|
+
if (candidates.size < 2)
|
|
242
|
+
return body;
|
|
243
|
+
const apply = (facts, kill) => {
|
|
244
|
+
if (kill === null)
|
|
245
|
+
return;
|
|
246
|
+
if (kill.kind === 'all')
|
|
247
|
+
facts.clear();
|
|
248
|
+
else
|
|
249
|
+
for (const [id, fact] of facts)
|
|
250
|
+
if (forgets(kill, fact))
|
|
251
|
+
facts.delete(id);
|
|
252
|
+
};
|
|
253
|
+
const graph = controlFlowGraphOf(body);
|
|
254
|
+
const predecessorsOf = (block) => [...new Set(graph.predecessors.get(block) ?? [])];
|
|
255
|
+
const values = new Map(body.values);
|
|
256
|
+
let ordinal = 0;
|
|
257
|
+
// A join every predecessor reaches with its own read of the same field
|
|
258
|
+
// takes a phi of those reads: one name per join and field, kept across
|
|
259
|
+
// iterations so the walk can settle.
|
|
260
|
+
const phis = new Map();
|
|
261
|
+
const phiOf = (block, fact, available) => {
|
|
262
|
+
const name = `${block}\u0000${fact}`;
|
|
263
|
+
const existing = phis.get(name);
|
|
264
|
+
if (existing !== undefined)
|
|
265
|
+
return existing.id;
|
|
266
|
+
while (values.has(irValueId(body.owner, ordinal)))
|
|
267
|
+
ordinal++;
|
|
268
|
+
const id = irValueId(body.owner, ordinal++);
|
|
269
|
+
values.set(id, available.representation);
|
|
270
|
+
phis.set(name, { id, block, fact, lineage: available.lineage });
|
|
271
|
+
return id;
|
|
272
|
+
};
|
|
273
|
+
const exitFacts = new Map();
|
|
274
|
+
const entryFacts = new Map();
|
|
275
|
+
const merge = (block) => {
|
|
276
|
+
const predecessors = predecessorsOf(block);
|
|
277
|
+
if (block === body.entry || predecessors.length === 0)
|
|
278
|
+
return new Map();
|
|
279
|
+
// An unreached predecessor is the top of the lattice: a loop header starts
|
|
280
|
+
// from its entry edge and is narrowed once its back edge is reached.
|
|
281
|
+
const reached = predecessors.flatMap((predecessor) => {
|
|
282
|
+
const exit = exitFacts.get(predecessor);
|
|
283
|
+
return exit === undefined ? [] : [exit];
|
|
284
|
+
});
|
|
285
|
+
const [first, ...rest] = reached;
|
|
286
|
+
if (first === undefined)
|
|
287
|
+
return null;
|
|
288
|
+
const merged = new Map();
|
|
289
|
+
for (const [fact, available] of first) {
|
|
290
|
+
const others = rest.map((exit) => exit.get(fact));
|
|
291
|
+
if (others.some((other) => other === undefined))
|
|
292
|
+
continue;
|
|
293
|
+
const same = others.every((other) => other.value === available.value);
|
|
294
|
+
merged.set(fact, same ? available : { ...available, value: phiOf(block, fact, available) });
|
|
295
|
+
}
|
|
296
|
+
return merged;
|
|
297
|
+
};
|
|
298
|
+
const transfer = (block, facts, replaced) => {
|
|
299
|
+
for (const operation of block.operations) {
|
|
300
|
+
const candidate = operation.kind === 'get' ? candidates.get(operation) : undefined;
|
|
301
|
+
if (candidate !== undefined && operation.kind === 'get') {
|
|
302
|
+
const receiver = receiverOf(operation.receiver.value);
|
|
303
|
+
const id = factId(receiver, candidate.key, candidate.shape);
|
|
304
|
+
const available = facts.get(id);
|
|
305
|
+
if (available !== undefined) {
|
|
306
|
+
if (!pinned.has(operation.result.id))
|
|
307
|
+
replaced?.set(operation.result.id, available.value);
|
|
308
|
+
}
|
|
309
|
+
else
|
|
310
|
+
facts.set(id, {
|
|
311
|
+
receiver,
|
|
312
|
+
key: candidate.key,
|
|
313
|
+
shape: candidate.shape,
|
|
314
|
+
value: operation.result.id,
|
|
315
|
+
representation: operation.result.representation,
|
|
316
|
+
lineage: operation.lineage
|
|
317
|
+
});
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
apply(facts, killOf(operation));
|
|
321
|
+
}
|
|
322
|
+
return facts;
|
|
323
|
+
};
|
|
324
|
+
const sameFacts = (left, right) => left.size === right.size && [...left].every(([fact, available]) => right.get(fact)?.value === available.value);
|
|
325
|
+
let settled = false;
|
|
326
|
+
for (let round = 0; round < 64 && !settled; round++) {
|
|
327
|
+
settled = true;
|
|
328
|
+
for (const id of body.blockOrder) {
|
|
329
|
+
const block = body.blocks.get(id);
|
|
330
|
+
if (block === undefined)
|
|
331
|
+
continue;
|
|
332
|
+
const incoming = merge(id);
|
|
333
|
+
if (incoming === null)
|
|
334
|
+
continue;
|
|
335
|
+
entryFacts.set(id, new Map(incoming));
|
|
336
|
+
const exit = transfer(block, incoming, null);
|
|
337
|
+
const previous = exitFacts.get(id);
|
|
338
|
+
if (previous === undefined || !sameFacts(previous, exit)) {
|
|
339
|
+
exitFacts.set(id, exit);
|
|
340
|
+
settled = false;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (!settled)
|
|
345
|
+
return body;
|
|
346
|
+
const replaced = new Map();
|
|
347
|
+
for (const id of body.blockOrder) {
|
|
348
|
+
const block = body.blocks.get(id);
|
|
349
|
+
const entry = entryFacts.get(id);
|
|
350
|
+
if (block !== undefined && entry !== undefined)
|
|
351
|
+
transfer(block, new Map(entry), replaced);
|
|
352
|
+
}
|
|
353
|
+
if (replaced.size === 0)
|
|
354
|
+
return body;
|
|
355
|
+
// Only the phis some reuse reads, and the phis those read through a back
|
|
356
|
+
// edge or an earlier join, are built.
|
|
357
|
+
const phisById = new Map([...phis.values()].map((phi) => [phi.id, phi]));
|
|
358
|
+
const built = new Map();
|
|
359
|
+
const pending = [...new Set(replaced.values())].filter((value) => phisById.has(value));
|
|
360
|
+
const placed = new Set();
|
|
361
|
+
while (pending.length > 0) {
|
|
362
|
+
const value = pending.pop();
|
|
363
|
+
if (placed.has(value))
|
|
364
|
+
continue;
|
|
365
|
+
placed.add(value);
|
|
366
|
+
const phi = phisById.get(value);
|
|
367
|
+
const representation = values.get(value);
|
|
368
|
+
const incoming = [];
|
|
369
|
+
for (const predecessor of predecessorsOf(phi.block)) {
|
|
370
|
+
const available = exitFacts.get(predecessor)?.get(phi.fact);
|
|
371
|
+
if (available === undefined)
|
|
372
|
+
return body;
|
|
373
|
+
incoming.push({ block: predecessor, value: { value: available.value, representation } });
|
|
374
|
+
if (phisById.has(available.value))
|
|
375
|
+
pending.push(available.value);
|
|
376
|
+
}
|
|
377
|
+
const list = built.get(phi.block) ?? [];
|
|
378
|
+
list.push({ kind: 'phi', lineage: phi.lineage, incoming, result: { id: value, representation } });
|
|
379
|
+
built.set(phi.block, list);
|
|
380
|
+
}
|
|
381
|
+
for (const phi of phis.values())
|
|
382
|
+
if (!placed.has(phi.id))
|
|
383
|
+
values.delete(phi.id);
|
|
384
|
+
const blocks = new Map();
|
|
385
|
+
for (const [id, block] of body.blocks) {
|
|
386
|
+
const kept = block.operations
|
|
387
|
+
.filter((operation) => !(operation.kind === 'get' && replaced.has(operation.result.id)))
|
|
388
|
+
.map((operation) => renamed(operation, (value) => replaced.get(value) ?? value));
|
|
389
|
+
const leading = kept.findIndex((operation) => operation.kind !== 'phi');
|
|
390
|
+
const split = leading < 0 ? kept.length : leading;
|
|
391
|
+
const operations = [...kept.slice(0, split), ...(built.get(id) ?? []), ...kept.slice(split)];
|
|
392
|
+
blocks.set(id, { ...block, operations, terminator: renamed(block.terminator, (value) => replaced.get(value) ?? value) });
|
|
393
|
+
}
|
|
394
|
+
for (const value of replaced.keys())
|
|
395
|
+
values.delete(value);
|
|
396
|
+
const rewritten = { ...body, blocks, values };
|
|
397
|
+
for (const block of blocks.values())
|
|
398
|
+
for (const operation of [...block.operations, block.terminator])
|
|
399
|
+
if (operandsOfIrOperation(operation).some((operand) => replaced.has(operand.value)))
|
|
400
|
+
return body;
|
|
401
|
+
return verifyIrBody(rewritten).length === 0 ? rewritten : body;
|
|
402
|
+
};
|
|
403
|
+
/**
|
|
404
|
+
* The operation with every operand naming a replaced read renamed to the read
|
|
405
|
+
* it reuses. An operand is the `{ value, representation }` pair wherever an
|
|
406
|
+
* operation or one of its published facts holds it.
|
|
407
|
+
*/
|
|
408
|
+
const renamed = (node, resolve) => {
|
|
409
|
+
if (Array.isArray(node)) {
|
|
410
|
+
let copy = null;
|
|
411
|
+
node.forEach((item, index) => {
|
|
412
|
+
const next = renamed(item, resolve);
|
|
413
|
+
if (next !== item)
|
|
414
|
+
(copy ??= [...node])[index] = next;
|
|
415
|
+
});
|
|
416
|
+
return (copy ?? node);
|
|
417
|
+
}
|
|
418
|
+
if (node === null || typeof node !== 'object' || Object.getPrototypeOf(node) !== Object.prototype)
|
|
419
|
+
return node;
|
|
420
|
+
const record = node;
|
|
421
|
+
let copy = null;
|
|
422
|
+
for (const [field, value] of Object.entries(record)) {
|
|
423
|
+
let next = renamed(value, resolve);
|
|
424
|
+
if (field === 'value' && typeof value === 'string' && 'representation' in record)
|
|
425
|
+
next = resolve(value);
|
|
426
|
+
if (next !== value)
|
|
427
|
+
(copy ??= { ...record })[field] = next;
|
|
428
|
+
}
|
|
429
|
+
return (copy ?? node);
|
|
430
|
+
};
|
package/dist/ir/hoist.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DeclarationId, IrValueId } from '../identity/ids.js';
|
|
2
|
-
import type { IrBlockId, IrBody, IrNonTerminatorOperation } from './model.js';
|
|
2
|
+
import type { GetOperation, IrBlockId, IrBody, IrNonTerminatorOperation, SetOperation } from './model.js';
|
|
3
3
|
/**
|
|
4
4
|
* Loop-invariant operations, and the block each one moves to.
|
|
5
5
|
*
|
|
@@ -35,6 +35,23 @@ export interface HoistPlan {
|
|
|
35
35
|
readonly relocated: ReadonlySet<IrValueId>;
|
|
36
36
|
}
|
|
37
37
|
export declare const emptyHoistPlan: HoistPlan;
|
|
38
|
+
/**
|
|
39
|
+
* Whether anything in a set of blocks can change what an array answers: a
|
|
40
|
+
* store, a call, an allocation -- any operation outside the whitelist below.
|
|
41
|
+
*
|
|
42
|
+
* A store into a record's data field and a pure `Math` call over Numbers run
|
|
43
|
+
* none of the program's code and touch no array: a capsule mover clamps with
|
|
44
|
+
* `Math.max(box.min.x, Math.min(box.max.x, p.x))` and pushes `position.x` out
|
|
45
|
+
* of each box, and either one kept its box loop from checking `boxes` once.
|
|
46
|
+
* `codeFreeStore` is the target's answer to which stores those are.
|
|
47
|
+
*
|
|
48
|
+
* `codeFreeGet` says which property reads run none of the program's code
|
|
49
|
+
* (`codeFreeGetOf`). A getter is a read too, and it can push onto the array
|
|
50
|
+
* the loop walks: trusting every read hoisted a loop's `boxes.length` past a
|
|
51
|
+
* getter that grew `boxes`, so the loop stopped at the length it began with,
|
|
52
|
+
* and a push that moved the storage left its window reading freed cells.
|
|
53
|
+
*/
|
|
54
|
+
export declare const arrayStorageMutationOf: (body: IrBody, codeFreeStore: (operation: SetOperation) => boolean, codeFreeGet: (operation: GetOperation) => boolean) => ((blocks: ReadonlySet<IrBlockId>) => boolean);
|
|
38
55
|
/**
|
|
39
56
|
* @param programConstants cells written exactly once program-wide, with that
|
|
40
57
|
* integer literal (`ir/integers.ts`'s `programCellConstantsOf`). A module
|
|
@@ -44,7 +61,7 @@ export declare const emptyHoistPlan: HoistPlan;
|
|
|
44
61
|
* in software doubles on a core with no double FPU, and `dense-loops.ts`
|
|
45
62
|
* refused the window because its bound was defined inside the loop.
|
|
46
63
|
*/
|
|
47
|
-
export declare const loopInvariantHoistsOf: (body: IrBody, programConstants?: ReadonlyMap<DeclarationId, number>, copiedCapture?: (declaration: DeclarationId) => boolean) => HoistPlan;
|
|
64
|
+
export declare const loopInvariantHoistsOf: (body: IrBody, programConstants?: ReadonlyMap<DeclarationId, number>, copiedCapture?: (declaration: DeclarationId) => boolean, settledCapture?: (declaration: DeclarationId) => boolean, codeFreeStore?: (operation: SetOperation) => boolean, codeFreeGet?: (operation: GetOperation) => boolean) => HoistPlan;
|
|
48
65
|
/**
|
|
49
66
|
* The values a loop cannot change: what clang's licm would lift, stated here.
|
|
50
67
|
*
|