Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions packages/bugc/src/evmgen/optimizer-contexts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,65 @@ code { r = count(0, 5); }`;
}
});
});

/**
* Count invoke/return contexts that carry a `declaration`
* (source id + range for the called/returning function's
* declaration). Walks gather wrappers and flat
* multi-discriminator contexts.
*/
function countDeclarations(program: Format.Program): {
invoke: number;
return: number;
} {
let invoke = 0;
let ret = 0;
for (const instr of program.instructions) {
if (!instr.context) continue;
for (const leaf of unwrapLeaves(instr.context)) {
if (Context.isInvoke(leaf) && leaf.invoke.declaration) invoke += 1;
if (Context.isReturn(leaf) && leaf.return.declaration) ret += 1;
}
}
return { invoke, return: ret };
}

/**
* Regression: the optimizer must preserve each function's
* `loc`/`sourceId` so evmgen can emit `declaration` source
* ranges on invoke/return contexts. cloneFunction used to
* drop these fields, so every declaration vanished from
* optimization level 1 upward.
*/
describe("optimizer preserves function declaration info", () => {
const source = `name Recur;

define {
function count(n: uint256, target: uint256) -> uint256 {
if (n < target) { return count(n + 1, target); }
else { return n; }
};
}

storage { [0] r: uint256; }
create { r = 0; }
code { r = count(0, 5); }`;

it("carries declarations at level 0 (baseline)", async () => {
const program = await compileAt(source, 0);
const decls = countDeclarations(program);
expect(decls.invoke).toBeGreaterThan(0);
expect(decls.return).toBeGreaterThan(0);
});

for (const level of [1, 2, 3] as const) {
it(`preserves declarations through optimization at level ${level}`, async () => {
const program = await compileAt(source, level);
const decls = countDeclarations(program);
// The optimizer must not strip function loc/sourceId:
// invoke/return contexts still carry declaration ranges.
expect(decls.invoke).toBeGreaterThan(0);
expect(decls.return).toBeGreaterThan(0);
});
}
});
6 changes: 6 additions & 0 deletions packages/bugc/src/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ export abstract class BaseOptimizationStep implements OptimizationStep {
parameters: [...func.parameters],
entry: func.entry,
blocks: clonedBlocks,
// Preserve declaration source info so evmgen can emit
// `declaration` ranges on invoke/return contexts.
// Dropping these here erased all declarations from
// optimization level 1 upward.
...(func.loc ? { loc: func.loc } : {}),
...(func.sourceId !== undefined ? { sourceId: func.sourceId } : {}),
};
}

Expand Down
Loading