← Back to Pull requests

test: enforce in-place updates of IRContext #1974

Closed
unknown closed this pull request without merging on Nov 10, 2026 20:57 UTC
View on GitHub
unknown unknown commented on Nov 03, 2026 08:49 UTC
Author

A pass that modifies an IRContext while another reference to it is still alive copies the whole context, because Lean updates the context's maps in place only when they are referenced once. When this happens for every op or block, the pass becomes quadratic. #1879, #1973 and #1951 each fixed one such case. In each, an old ctx stayed alive, as the fallback of a | return ctx or for the next iteration of a loop, while the code modified it.

Nothing reports these copies. The output is the same and the tests are small, so they only show up as time on large inputs. On #1951 John asked for a way to detect them automatically.

Lean has a runtime check for exactly this. Array.markLinear returns its argument unchanged, but sets a flag on the array object, and HashMap.markLinear does the same for the map's bucket array. Before Lean updates an array that is referenced more than once, it copies it, and with the environment variable LEAN_ABORT_ON_NONLINEAR set, copying a marked array panics with "array marked by Array.markLinear was used non-linearly". In-place updates keep the flag, and HashMap moves it to the new bucket array when it resizes. So once the maps of a context are marked, any copy of them is reported, whatever caused it.

This PR adds WfIRContext.markLinear, which marks the three maps of a context, and WfIRContext.create returns a marked context, so every context is checked. markLinear is the identity logically, so the proof that the context stays well-formed is simpa. lit.cfg sets LEAN_ABORT_ON_NONLINEAR, so a test whose pipeline copies the context fails. The check is on the copy path only, so a pass that does not copy runs as before.

With #1951 or #1973 reverted, lit fails 13 or 4 tests. The check covers only the code the tests run.

The check is new (leanprover/lean4#15052), and Lean does not print a backtrace for this panic yet, even with LEAN_BACKTRACE set, so to find the code behind it, rerun the failing command under gdb with a breakpoint on lean_internal_panic:

LEAN_ABORT_ON_NONLINEAR=1 gdb -batch -ex 'break lean_internal_panic' -ex run -ex bt \
  --args .lake/build/bin/veir-opt <test>.mlir <options>

The innermost frames are the map insert that copies. Further out is the function that passed on a context it still held, such as combineArtifactStep with #1951 reverted.

Claude drafted this PR; I've reviewed it and understand the change.

Diff: 303 lines (+227 / -76) across 3 files Reviewer: math-fehr Review duration: 10808m
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 303 lines diff across 3 files
0.40
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.342
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1974"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 40.1% (MEDIUM) · Higher = larger blast radius
40.1%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 15.8% · Safe to merge
15.8%
Repository