test: enforce in-place updates of IRContext #1974
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
math-fehr
A pass that modifies an
IRContextwhile 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 oldctxstayed alive, as the fallback of a| return ctxor 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.markLinearreturns its argument unchanged, but sets a flag on the array object, andHashMap.markLineardoes 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 variableLEAN_ABORT_ON_NONLINEARset, copying a marked array panics with "array marked byArray.markLinearwas used non-linearly". In-place updates keep the flag, andHashMapmoves 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, andWfIRContext.createreturns a marked context, so every context is checked.markLinearis the identity logically, so the proof that the context stays well-formed issimpa.lit.cfgsetsLEAN_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_BACKTRACEset, so to find the code behind it, rerun the failing command under gdb with a breakpoint onlean_internal_panic:The innermost frames are the map insert that copies. Further out is the function that passed on a context it still held, such as
combineArtifactStepwith #1951 reverted.Claude drafted this PR; I've reviewed it and understand the change.