Repository navigation
[EVM] Tune the cost model in the EVM Constant Unfolder - #223
Draft
PavelKopyl wants to merge 1 commit into
Draft
PavelKopyl wants to merge 1 commit into
PavelKopyl wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟡 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
force-pushed
the
kpv-tune-cost-model-constant-unfolder
branch
3 times, most recently
from
November 2, 2026 15:10
96d0dcb to
a2769b7
Compare
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
force-pushed
the
kpv-tune-cost-model-constant-unfolder
branch
from
November 2, 2026 16:33
a2769b7 to
cc49997
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


No description provided.