Skip to content

[EVM] Tune the cost model in the EVM Constant Unfolder - #223

Draft
PavelKopyl wants to merge 1 commit into
mainfrom
kpv-tune-cost-model-constant-unfolder
Draft

PavelKopyl wants to merge 1 commit into
mainfrom
kpv-tune-cost-model-constant-unfolder

Conversation

@PavelKopyl

Copy link
Copy Markdown
Contributor

No description provided.

@PavelKopyl
PavelKopyl marked this pull request as draft October 31, 2026 14:27
@PavelKopyl
PavelKopyl requested a balanced review from Copilot October 31, 2026 14:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The cost model can increase gas in loops and repeatedly called helpers, miss size reductions, and trigger an assertion on equal-size candidates.

5 open issues
What changed in this PR

This PR retunes the EVM constant unfolder’s trade-off between bytecode size and execution gas, using loop depth, block frequency, and recursion to guide transformation selection.

Changes:

  • Lowers the size-reduction threshold and introduces quantized gas weights for scoring and caching candidates.
  • Adds coverage for flat code, loops, cold paths, and recursion.
  • Updates code-generation expectations for the newly preferred constant sequences.
File Description
llvm/​test/​CodeGen/​EVM/​zext.ll Updates zero-extension mask expectations.
llvm/​test/​CodeGen/​EVM/​force-constant-unfolding.ll Updates the forced-unfolding sequence.
llvm/​test/​CodeGen/​EVM/​constant-unfolding.ll Updates shifted-mask expectations.
llvm/​test/​CodeGen/​EVM/​constant-unfolding-weight.ll Adds execution-weighting regression cases.
llvm/​test/​CodeGen/​EVM/​constant-unfolding-speed-and-Oz.ll Revises the speed-versus-size comparison.
llvm/​test/​CodeGen/​EVM/​clz-ctz.ll Updates masks used in leading/trailing-zero operations.
llvm/​test/​CodeGen/​EVM/​bitmanipulation-intrinsics.ll Updates constant sequences for bit-manipulation intrinsics.
llvm/​test/​CodeGen/​EVM/​add.ll Expects shift-based constant construction for addition.
llvm/​lib/​Target/​EVM/​EVMConstantUnfolding.cpp Implements execution-sensitive scoring and cache keys.

Review effort: Balanced

// discounts it, which is what tells a revert block apart from ordinary
// straight-line code at the same depth. The clamp keeps this a discount:
// a hot block is priced by its nesting, never by its frequency.
float Weight = gasWeightAt(MLI.getLoopDepth(&MBB) + RecursionBonus);
Comment on lines +637 to +639
Weight *= std::min(
1.0F,
static_cast<float>(MBFI.getBlockFreqRelativeToEntryBlock(&MBB)));
Comment on lines +295 to +296
ScoreA = (CostA.getByteSize() * SizeWeight) + CostA.getGas();
ScoreB = (CostB.getByteSize() * SizeWeight) + CostB.getGas();
ScoreA = (CostA.getByteSize() * SizeWeight) + CostA.getGas();
ScoreB = (CostB.getByteSize() * SizeWeight) + CostB.getGas();
} else {
const float Weight = weightOf(QuantizedWeight);
Comment on lines +490 to +494
auto *OptIt = std::min_element(
Transformations.begin(), Transformations.end(),
[OptForSize, QuantizedWeight](const auto &A, const auto &B) {
return isBetterCandidate(*A, *B, OptForSize, QuantizedWeight);
});
@PavelKopyl
PavelKopyl force-pushed the kpv-tune-cost-model-constant-unfolder branch 3 times, most recently from 96d0dcb to a2769b7 Compare November 2, 2026 15:10
EVMConstantUnfolding scored a candidate encoding as bytes + 2 * gas and then
required the unfolded form to be 2.5 times smaller. The weight was the same
for a dispatcher arm that runs once, a loop body that runs every iteration
and a revert block that almost never runs, and the threshold rejected
transformations that were a clear win.

Weight the gas by how often the constant runs:

  W = GasWeight * LoopGasFactor^depth * min(1, block frequency)

W is an integer in sixteenths of a byte per gas and the key of the
transformation cache. GasWeight 3 and LoopGasFactor 3 give 3/16, 9/16 and
27/16 at depths 0, 1 and 2. The values were chosen for the all-ones mask.
Its 64-bit form is 9 bytes as a push, or 5 bytes and 11 gas unfolded, and
both score the same at a weight of 0.5. So outside a loop every mask of 6
bytes or more unfolds, and in a loop the 64-bit mask and every narrower one
stay folded.

A function of a call graph cycle has the evm-recursive attribute and is
charged RecursionDepthBonus extra levels, because MachineLoopInfo does not
see recursion. The block frequency relative to the entry block only lowers
the weight, so the revert and panic blocks at depth 0 are priced below the
code around them. Raising the weight by frequency was measured and is worse.

The size reduction threshold falls to 1.05. A candidate over the instruction
limit is dropped before the ranking, equal scores go to the cheaper
candidate, and size mode ranks by bytes alone, so it never keeps a constant
that speed mode unfolds.
@PavelKopyl
PavelKopyl force-pushed the kpv-tune-cost-model-constant-unfolder branch from a2769b7 to cc49997 Compare November 2, 2026 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants