@asmlift/core 0.6.0 → 0.7.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.
Files changed (74) hide show
  1. package/README.md +2 -2
  2. package/package.json +1 -1
  3. package/src/backend/cfamily.ts +39 -11
  4. package/src/contracts.ts +60 -11
  5. package/src/frontend/ssa.ts +1 -1
  6. package/src/frontend/thumb.ts +2 -2
  7. package/src/ir/alias.ts +24 -0
  8. package/src/ir/core.ts +8 -0
  9. package/src/ir/opcodes.ts +43 -7
  10. package/src/ir/simplify.ts +1 -1
  11. package/src/l3/address.ts +2 -2
  12. package/src/l3/advance.ts +373 -0
  13. package/src/l3/argbase.ts +4 -4
  14. package/src/l3/ast.ts +65 -21
  15. package/src/l3/basecse.ts +48 -28
  16. package/src/l3/coalesce.ts +9 -9
  17. package/src/l3/gates.ts +75 -1
  18. package/src/l3/hoist.ts +1 -1
  19. package/src/l3/homesplit.ts +13 -13
  20. package/src/l3/initfirst.ts +3 -3
  21. package/src/l3/inlinebase.ts +16 -16
  22. package/src/l3/mentions.ts +68 -5
  23. package/src/l3/mulfirst.ts +3 -3
  24. package/src/l3/nearbase.ts +4 -4
  25. package/src/l3/offmember.ts +5 -5
  26. package/src/l3/parkfirst.ts +6 -6
  27. package/src/l3/pollguard.ts +3 -3
  28. package/src/l3/ptrfield.ts +4 -4
  29. package/src/l3/regspell.ts +8 -8
  30. package/src/l3/reindex.ts +22 -17
  31. package/src/l3/scopebase.ts +28 -25
  32. package/src/l3/sinkinit.ts +7 -7
  33. package/src/l3/slotorder.ts +3 -3
  34. package/src/l3/storage.ts +1 -1
  35. package/src/l3/tailmerge.ts +2 -2
  36. package/src/l3/typing.ts +3 -3
  37. package/src/l3/unmerge.ts +483 -59
  38. package/src/l3/unreduce.ts +13 -13
  39. package/src/l3/volatileptr.ts +11 -11
  40. package/src/l3/volatileval.ts +11 -11
  41. package/src/l3/volstore.ts +16 -16
  42. package/src/l3/zerosub.ts +6 -6
  43. package/src/pattern/engine.ts +4 -4
  44. package/src/pipeline.ts +17 -5
  45. package/src/proto.ts +2 -2
  46. package/src/raise/const.ts +203 -3
  47. package/src/raise/divpow2.ts +2 -2
  48. package/src/raise/extscale.ts +342 -0
  49. package/src/raise/globalshape.ts +32 -12
  50. package/src/raise/gvn.ts +2 -2
  51. package/src/raise/magicdiv.ts +2 -2
  52. package/src/raise/memberarrays.ts +4 -4
  53. package/src/raise/narrowlocal.ts +18 -2
  54. package/src/raise/paramwidth.ts +24 -2
  55. package/src/raise/pre-recovery.ts +90 -25
  56. package/src/raise/retsink.ts +381 -15
  57. package/src/raise/shortcircuit.ts +595 -34
  58. package/src/raise/structs.ts +4 -4
  59. package/src/raise/tailsink.ts +126 -0
  60. package/src/rank-declare.ts +4 -4
  61. package/src/{rank-axes.ts → rank-variations.ts} +319 -189
  62. package/src/rank.ts +1148 -803
  63. package/src/structure/analysis.ts +87 -90
  64. package/src/structure/bitfields.ts +130 -30
  65. package/src/structure/globalaccess.ts +30 -4
  66. package/src/structure/namecoalesce.ts +32 -13
  67. package/src/structure/structure.ts +1415 -200
  68. package/src/structure/switch-recover.ts +100 -7
  69. package/src/symbols.ts +127 -6
  70. package/src/target.ts +155 -35
  71. package/src/trace.ts +1 -1
  72. package/src/variation-definitions.ts +1540 -0
  73. package/src/variation-gates.ts +89 -0
  74. package/src/variation-tokens.ts +355 -0
@@ -23,7 +23,7 @@
23
23
  // base @ aload(index) -> array (variable index — untouched)
24
24
  //
25
25
  // This recovery is USUALLY BYTE-NEUTRAL — `->field_N` and `[idx]` mostly compile identically, so it
26
- // is a representation upgrade driven by access evidence rather than a scored lever. TWO
26
+ // is a representation upgrade driven by access evidence rather than a scored variation. TWO
27
27
  // MEASUREMENTS SAY "USUALLY" IS THE RIGHT WORD, and both were made on agbcc against a real target
28
28
  // object, each pair differing in ONE token:
29
29
  // • THE SPELLING. `synthetic:dmanest`'s reference compiles from `((struct Elem0 *)K)[a1].field_4`
@@ -34,9 +34,9 @@
34
34
  // `void *` rather than `s32` changes agbcc's alias set and lets a loop-invariant load leave the
35
35
  // loop: `synthetic:dmaptrsrc` matches with the pointer declaration and diffs by 35 without it
36
36
  // (its own fan: `/vol-store/unreduce/ptr-field` 0, `/vol-store/unreduce` 35).
37
- // That is what `l3/ptrfield.ts` offers as a differ-ranked lever.
37
+ // That is what `l3/ptrfield.ts` offers as a differ-ranked variation.
38
38
  // So the neutrality claim is CONDITIONAL, and nothing here says on what. Until it does, read it as
39
- // "no candidate is enumerated for this axis", not as "the differ could not referee one" — the
39
+ // "no candidate is enumerated for this question", not as "the differ could not referee one" — the
40
40
  // second reading is the one both measurements above falsify. GAPS between accessed
41
41
  // offsets (unaccessed leading/interior fields) are filled with `u8[N]` PAD fields so the declared
42
42
  // struct reproduces the observed offsets byte-for-byte and is self-describing (the same
@@ -64,7 +64,7 @@ interface Access {
64
64
  // is a DIFFERENT operation: this pass is ALIGNMENT-AWARE (no explicit pad when C's own inter-field
65
65
  // padding already lands the field) and carries NO trailing pad / struct `size` (a recovered struct
66
66
  // here is only ever a `struct S *` pointee accessed by named field — never an array element or a
67
- // by-value param, so sizeof is never taken). If the two are ever unified, PARAMETERIZE those axes
67
+ // by-value param, so sizeof is never taken). If the two are ever unified, PARAMETERIZE those dimensions
68
68
  // — a naive merge would break the natural-alignment golden or silently mislay a struct that later
69
69
  // becomes an element / ABI value.
70
70
  const sizeAlign = (width: number): number => width;
@@ -0,0 +1,126 @@
1
+ // asmlift — sink a shared STORE tail back into the paths that branch to it.
2
+ //
3
+ // agbcc's gcse.c PRE finds a trailing store's base partially redundant, INSERTS it at the end of
4
+ // every predecessor of the join, and the arms then cross-jump into one `store; ret`:
5
+ //
6
+ // if (c) { for (…) …; } else { if (x) { gQ.cur = fnA; return; } }
7
+ // gQ.cur = fnB;
8
+ //
9
+ // lifts as ONE tail block `^t(v): store gQ.cur, v; ret` that the `fnA` arm and the join the two
10
+ // sides reach both branch to. The structurer can spell that tail once, after the `if`, only if the
11
+ // `fnA` path returns before it — so this duplicates the tail into every path that branches to it.
12
+ // WHICH copy is the tail the source wrote once is not decided here: it is the `ret` reachable from
13
+ // both successors of the `if`, which `structure()`'s `followEarlyReturns` finds and spells after
14
+ // the `if`, with every other copy an early `return;` (`synthetic:gcsetail`). Under `-fno-gcse` the
15
+ // tail/duplicated pairs compile byte-identical, and there is no loop gate: a loop before the join
16
+ // is a sample, not a law.
17
+ //
18
+ // A block that supplies the tail's arguments is a SOURCE: a PURE FORWARDER (`^f(v): br ^t(v)`,
19
+ // reached only by `br`) is seen through to its own predecessors, so the arms that branch into the
20
+ // tail through a shared base reload are sources like any other (`synthetic:gcsefwd`).
21
+ //
22
+ // A LIFT VARIATION, NEVER A DEFAULT: `synthetic:gcsepre` and `synthetic:gcsepredup` compile to
23
+ // different objects (an r4/r5 swap) and lift to byte-identical IR, the first written with the
24
+ // shared tail and the second with the default duplicated into each arm — no IR rule tells them
25
+ // apart, so rank.ts enumerates this beside the unsunk lift and the differ referees.
26
+ //
27
+ // SOUND BY CONSTRUCTION: tail duplication. Each copy runs on exactly the paths that ran the tail,
28
+ // immediately before the same `ret`. A value the tail reads is one of two things. It is a block
29
+ // parameter of the tail OR OF A FORWARDER on the way, and the copy takes the argument that path's
30
+ // edge into that block carried. Or it is defined elsewhere, dominates the tail, and so dominates
31
+ // every source of it. The forwarder's own parameter is not a corner: a tail whose only predecessor
32
+ // is a forwarder loses its parameter to raise's `simplifyTrivialPhis` and reads the forwarder's
33
+ // directly — a jump pad `.L4: b .L6` in front of the store does exactly that — and the forwarder is
34
+ // swept once every source holds its copy. A copy replaces a `br`, the source's only edge. A
35
+ // CONDITIONAL edge cannot carry one — splicing over it would drop the branch's other successor — so
36
+ // the tail stays for the sources that reach it that way; that restriction is the rewrite's own
37
+ // precondition, not a gate.
38
+ //
39
+ // NO GATE: which copy stays shared is the follow's question, and whether a sunk function is worth
40
+ // a candidate is rank.ts's, asked with the follow's own predicate (`hasDivergentSharedRet`) rather
41
+ // than a copy of it here.
42
+ import { type Block, type Fn, type Value, mkOp, predecessors, reachableBlocks, terminator } from '../ir/core';
43
+ import { simplifyTrivialPhis } from '../ir/simplify';
44
+
45
+ /** One edge that supplies the tail its arguments. `resolve` sends a value the tail reads to the
46
+ * value it has at the end of `from`: a parameter of the tail or of any forwarder between becomes
47
+ * the argument this path's edge into that block carried, and anything else is left alone. */
48
+ interface Source {
49
+ readonly from: Block;
50
+ readonly resolve: (v: Value) => Value;
51
+ }
52
+
53
+ /** A block whose ops are one or more `store`s and then a void `ret`. */
54
+ function isStoreTail(b: Block): boolean {
55
+ const t = b.ops[b.ops.length - 1];
56
+ return (
57
+ b.ops.length >= 2 &&
58
+ t.opcode === 'ret' &&
59
+ t.operands.length === 0 &&
60
+ b.ops.slice(0, -1).every((o) => o.opcode === 'store')
61
+ );
62
+ }
63
+
64
+ /** Copy every store tail that two or more sources reach into each source that reaches it by a
65
+ * `br`; returns whether anything changed. */
66
+ export function sinkStoreTails(fn: Fn): boolean {
67
+ let changed = false;
68
+ for (const tail of [...fn.blocks]) {
69
+ if (tail === fn.blocks[0] || !fn.blocks.includes(tail) || !isStoreTail(tail)) {
70
+ continue;
71
+ }
72
+ const preds = predecessors(fn);
73
+ // The sources, seen through pure forwarders. `resolve` sends a value the tail reads to the value
74
+ // it has on entry to the block being walked; each edge out of a predecessor composes one more
75
+ // step onto it — `to`'s own parameters, not only the tail's, because the tail may read a
76
+ // forwarder's parameter directly.
77
+ const forwarders = new Set<Block>();
78
+ const sources: Source[] = [];
79
+ const conditional: Block[] = [];
80
+ const walk = (to: Block, resolve: (v: Value) => Value) => {
81
+ for (const p of preds.get(to) ?? []) {
82
+ const t = terminator(p)!;
83
+ if (t.opcode !== 'br') {
84
+ conditional.push(p);
85
+ continue;
86
+ }
87
+ const edge = t.successors[0].args;
88
+ const through = (v: Value): Value => {
89
+ const w = resolve(v);
90
+ const i = to.params.indexOf(w);
91
+ return i >= 0 ? edge[i] : w;
92
+ };
93
+ const isForwarder =
94
+ p !== fn.blocks[0] &&
95
+ p.ops.length === 1 &&
96
+ !forwarders.has(p) &&
97
+ (preds.get(p) ?? []).every((q) => terminator(q)?.opcode === 'br');
98
+ if (isForwarder) {
99
+ forwarders.add(p);
100
+ walk(p, through);
101
+ } else {
102
+ sources.push({ from: p, resolve: through });
103
+ }
104
+ }
105
+ };
106
+ walk(tail, (v) => v);
107
+ if (sources.length === 0 || sources.length + conditional.length < 2) {
108
+ continue;
109
+ }
110
+ const body = tail.ops.slice(0, -1);
111
+ for (const src of sources) {
112
+ const copies = body.map((o) => mkOp('store', { operands: o.operands.map(src.resolve), attrs: { ...o.attrs } }));
113
+ src.from.ops.splice(src.from.ops.length - 1, 1, ...copies, mkOp('ret'));
114
+ }
115
+ // By REACHABILITY, not predecessor count: the tail (unless a conditional edge keeps it) and
116
+ // every forwarder on the way are left unreachable, however long the chain, and a forwarder's
117
+ // in-edge from another orphan still counts as a predecessor.
118
+ const live = reachableBlocks(fn);
119
+ fn.blocks = fn.blocks.filter((b) => live.has(b));
120
+ changed = true;
121
+ }
122
+ if (changed) {
123
+ simplifyTrivialPhis(fn);
124
+ }
125
+ return changed;
126
+ }
@@ -5,9 +5,9 @@
5
5
  // (`bareGlobalAccessFacts`), which names exist at all (`bareGlobalSymbols`), and which of them a
6
6
  // declaration must REFUSE to claim (`makeRefCollector`, via `RefusedDeclarationReason`).
7
7
  //
8
- // It knows nothing about axes, levers or ranking: the enumeration driver hands it a dictionary and
8
+ // It knows nothing about variations or ranking: the enumeration driver hands it a dictionary and
9
9
  // asks each emitted tree for its references. A SIBLING MODULE, never a `rank/` directory — see the
10
- // same note on rank-axes.ts.
10
+ // same note on rank-variations.ts.
11
11
  import { type Fn, type Value, defOpMap } from './ir/core';
12
12
  import type { SFn } from './l3/ast';
13
13
  import { type SymbolRef, collectSymbolRefs } from './l3/symbol-refs';
@@ -134,7 +134,7 @@ const EMITTER_NAME = /^[avt]\d+$/;
134
134
 
135
135
  /** Why a name the candidate's tree references got NO declaration. Reported rather than silently
136
136
  * applied, because an undeclared name and a REFUSED one produce the same `'x' undeclared` from
137
- * the compiler and only the second one is asmlift's own decision. Same argument as `onLeverError`
137
+ * the compiler and only the second one is asmlift's own decision. Same argument as `onEnumerationError`
138
138
  * one screen down: a refusal nobody can see is indistinguishable from a capability that was
139
139
  * never there.
140
140
  *
@@ -223,7 +223,7 @@ export function makeRefCollector(ctx: {
223
223
  // `tree.locals`: every use of the emitter's local binds the extern instead, and the
224
224
  // loop pointer it was holding becomes a store to that global once per iteration.
225
225
  // Both compile, both are wrong, and a compiling wrong answer is the one outcome this
226
- // project trades nothing for — so the spelling dies here and `fanOut`'s catch reports it.
226
+ // project trades nothing for — so the spelling dies here and `respellTree`'s catch reports it.
227
227
  // If every spelling of every tree dies, the row declines LOUDLY naming the collision.
228
228
  if (bound.has(r.name) || EMITTER_NAME.test(r.name)) {
229
229
  refuse(r.name, 'emitter-name');