Skip to content

Commit 5640bfd

Browse files
committed
bugc: emit the coalesce transform on read-write merging
Mark the SHL/OR field-packing sequence produced by read-write merging with `transform: ["coalesce"]`, so a packed-storage write reads as compiler-synthesized rather than source the user wrote. Composes with a folded value into `["fold", "coalesce"]`.
1 parent 4fc242c commit 5640bfd

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎packages/bugc/src/evmgen/transform-contexts.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,29 @@ code { r = (2 + 3) * (4 * 5); }`;
120120
});
121121
}
122122
});
123+
124+
describe("optimizer emits coalesce transform contexts", () => {
125+
// Two adjacent packed writes of a runtime value to one storage
126+
// slot; read/write merging (level 3) packs them with SHL/OR into
127+
// a single word write.
128+
const source = `name Coalesce;
129+
130+
define { struct S { a: uint128; b: uint128; }; }
131+
storage { [0] s: S; [1] src: uint256; }
132+
create {}
133+
code { let v = src; s.a = v; s.b = v; }`;
134+
135+
for (const level of [0, 1, 2] as const) {
136+
it(`emits no coalesce transform at level ${level}`, async () => {
137+
const bc = await compileBytecode(source, level);
138+
expect(countTransform(bc.runtimeInstructions, "coalesce")).toBe(0);
139+
});
140+
}
141+
142+
it("emits coalesce transform at level 3", async () => {
143+
const bc = await compileBytecode(source, 3);
144+
expect(countTransform(bc.runtimeInstructions, "coalesce")).toBeGreaterThan(
145+
0,
146+
);
147+
});
148+
});

‎packages/bugc/src/optimizer/steps/read-write-merging.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,18 @@ export class ReadWriteMergingStep extends BaseOptimizationStep {
368368
reason: `Merged ${writes.length} writes to same location`,
369369
});
370370

371+
// Mark every instruction produced by the merge with
372+
// transform:["coalesce"] so debuggers can show the SHL/OR
373+
// field-packing sequence as compiler-synthesized rather than
374+
// source the user wrote. Appends to any existing transform
375+
// array (e.g. a folded packed value → ["fold","coalesce"]).
376+
for (const inst of instructions) {
377+
inst.operationDebug = Ir.Utils.addTransform(
378+
inst.operationDebug,
379+
"coalesce",
380+
);
381+
}
382+
371383
return instructions;
372384
}
373385

0 commit comments

Comments
 (0)