Skip to content

Keep InstructionInfo metadata consistent during constant propagation#60

Open
RealA10N wants to merge 7 commits intomainfrom
claude/constant-propagation-l9a1P
Open

Keep InstructionInfo metadata consistent during constant propagation#60
RealA10N wants to merge 7 commits intomainfrom
claude/constant-propagation-l9a1P

Conversation

@RealA10N
Copy link
Copy Markdown
Owner

  • Add RegisterInfo.RemoveUsage (mirrors RemoveDefinition)
  • Fix RegisterArgumentInfo.SwitchRegister to accept the owning
    InstructionInfo and update Usages on both old and new registers,
    removing the TODO; update call site in usm/ssa
  • Add InstructionInfo.SubstituteArgument: replaces an argument by index
    while keeping RegisterInfo.Usages consistent (mirrors SwitchTarget)
  • Rewrite substituteConstants to use SubstituteArgument and to walk
    each constant register's Usages list directly instead of re-scanning
    all blocks
  • Fix buildConstantMap to only propagate constants for registers with
    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

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 15, 2026

Codecov Report

❌ Patch coverage is 43.53741% with 83 lines in your changes missing coverage. Please review.
✅ Project coverage is 41.09%. Comparing base (661e744) to head (9448dd3).

Files with missing lines Patch % Lines
usm/isa/binary_calculation.go 0.00% 17 Missing ⚠️
usm/isa/phi.go 0.00% 14 Missing ⚠️
opt/constant_propagation.go 84.21% 9 Missing and 3 partials ⚠️
usm/isa/move.go 0.00% 10 Missing ⚠️
usm.go 0.00% 6 Missing ⚠️
usm/isa/add.go 0.00% 4 Missing ⚠️
usm/isa/and.go 0.00% 4 Missing ⚠️
usm/isa/mul.go 0.00% 4 Missing ⚠️
usm/isa/or.go 0.00% 4 Missing ⚠️
usm/isa/sub.go 0.00% 4 Missing ⚠️
... and 1 more
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@RealA10N RealA10N force-pushed the claude/constant-propagation-l9a1P branch from cb3008f to 8695e9a Compare March 16, 2026 10:33
claude added 2 commits March 16, 2026 15:46
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
@RealA10N RealA10N force-pushed the claude/constant-propagation-l9a1P branch from 8695e9a to a9efccc Compare March 16, 2026 15:47
claude added 5 commits March 16, 2026 17:20
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
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