Keep InstructionInfo metadata consistent during constant propagation#60
Open
Keep InstructionInfo metadata consistent during constant propagation#60
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #60 +/- ##
==========================================
+ Coverage 41.00% 41.09% +0.09%
==========================================
Files 123 124 +1
Lines 3790 3937 +147
==========================================
+ Hits 1554 1618 +64
- Misses 2134 2214 +80
- Partials 102 105 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
cb3008f to
8695e9a
Compare
Implements a DFS-based constant propagation pass (opt.ConstantPropagation) that substitutes known-constant registers with immediates and folds constant expressions (e.g. add #2 #3 → #5). Key design points: - Uses a DFS forest over the CFG so that unreachable blocks are handled correctly and never corrupt reaching-constant stacks. - Tracks a per-register stack of reaching constants; only propagates when all definitions of a register reachable in the current DFS component are in the current block (safe for non-SSA code at join points). - Leverages the SubstituteArgument API (PR 1) to keep register usage metadata consistent during substitution. - Leverages FunctionControlFlowInfo (PR 2) for CFG traversal without pulling in SSA construction. ISA support: add, sub, mul, and, or, xor, move, phi all implement PropagateConstants; branches and calls embed PropagatesNoConstants. Binary folding lives in usm/isa/binary_calculation.go. Wired into the CLI as 'cp' / 'constant-propagation'. Bumps alon.kr/x/graph to a version that provides DfsForest(). https://claude.ai/code/session_0165cPFCURfazKMvWB6yPZBa
8695e9a to
a9efccc
Compare
Removes collectRegisters and the cpBlockSeparator sentinel. registerStacks is now a map[*RegisterInfo]*Stack created on demand in define(), and registerPushes tracks *RegisterInfo pointers directly using nil as the block boundary marker. This eliminates the upfront O(instructions) scan and the parallel registersToIndex map. https://claude.ai/code/session_0165cPFCURfazKMvWB6yPZBa
Removes collectRegisters and populates registersToIndex on demand in define(): the first time a register is seen, it gets the next available index and an empty stack is appended to registerStacks. This keeps the slice-of-stacks layout (better cache behaviour than a map of pointers) while eliminating the upfront O(instructions) scan. https://claude.ai/code/session_0165cPFCURfazKMvWB6yPZBa
…onent Tests the case where a register is defined in one unreachable DFS component (.second_isolated) and used in a different unreachable DFS component (.first_isolated), with a jump from the defining block to the using block that becomes a cross-edge in the DFS forest. A third isolated self-loop (.noise) sits between them to ensure the two blocks are assigned to separate DFS trees. CP must not propagate the constant to the cross-component use. https://claude.ai/code/session_0165cPFCURfazKMvWB6yPZBa
Replaces DfsForest with Dfs(0) so only code reachable from the entry block is transformed. Removes all connected-component tracking; define() now checks only reachable definitions when deciding whether to propagate a constant. Unreachable blocks are left unchanged. https://claude.ai/code/session_0165cPFCURfazKMvWB6yPZBa
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.
InstructionInfo and update Usages on both old and new registers,
removing the TODO; update call site in usm/ssa
while keeping RegisterInfo.Usages consistent (mirrors SwitchTarget)
each constant register's Usages list directly instead of re-scanning
all blocks
exactly one definition: without SSA form a register may be assigned in
multiple locations, so only a single-definition register is guaranteed
to carry the same constant at every use (safe without reaching-defs
analysis)
https://claude.ai/code/session_0165cPFCURfazKMvWB6yPZBa