Repository navigation
Conversation
Thumb `subs r0, #0; it ne; movne r0, #1` printed as a branch on a named flag with the flag writes kept inside it. Three owners in normalize: flag_recovery - ARM's SLEIGH computes the condition codes into tmpNG/tmpZR/tmpCY/tmpOV and copies them into the architectural flags only for flag-setting forms. The temporaries join the flag family, so substitution and dead-flag cleanup see them. - A recorded flag definition is a snapshot of an expression. It was never dropped when something the expression reads was written, so `of = __scarry(a, b); a += b; if (of)` became `a += b; if (__scarry(a, b))`, and `cmp; movne r0, #1; moveq r0, #0` would test the register the first predicate replaced. Definitions are now killed by a write to a variable they read, by a memory write if they load, by a redefinition of the flag on some path, and by a label that lets control in from elsewhere; loops and switch arms take their own writes as in effect at entry. A definition that calls is not recorded. The whole-function single-definition fallback takes only definitions that read nothing the function assigns. conditional_move - `x = d; if (c) x = o;` became `x = c ? o : d`, which evaluates `c` before the default is stored: `rax = rax ? rax + 1 : strrchr(s, '/')`. The merge now requires that `c` and `o` do not read `x`, that `d` has no effect to lose, and that `c` has none that could change what `d` reads. - `x = d; if (x != 0) x = 1;` folds to `x = (d != 0)`; `d` is still evaluated once. scratch_liveness - A flag is scratch: its value matters only where something observable reads it. A flag an instruction carries through itself (`of = keep ? of : new`) has a reader for every write and no test; the unsound substitution used to break such cycles by accident. Flag variables are now among the names the post-structuring reachability pass may retire, and the flag primitives (`__carry`, `__sborrow`, ...) count as computations there, not calls. Measured on the full DecBench `inlined` (-O2) config, the same 24,186 rows on both sides, caches off: total structure distance 621,358 -> 620,160, perfect 7,378 -> 7,452, Union 7,827 -> 7,894 (ARM projects 178,504 -> 177,328 and 4,623 -> 4,700; x86 projects 442,854 -> 442,832 and 2,755 -> 2,752). Sample set gate: perfect 67 -> 67, distance 7,603 -> 7,625. The `unoptimized` full config is not measured. Details and the rows that got worse are in docs/proposals/2026-11-14-flag-snapshots-and-predicated-set.md. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Contributor
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
…e flag-snapshot change Distance 394,148 -> 393,462, perfect 12,976 -> 13,007, Union 15,131 -> 15,166 on the same 31,987 rows. Lists the 36 rows that lose a perfect score by cause, and the two rules that now reach ARM (default/override merge, return-tail duplication). Co-Authored-By: Claude Fable 5.2 <[email protected]>
This was referenced Nov 15, 2026
This branch has not been deployed
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.
What
Thumb
subs r0, #0; it ne; movne r0, #1(how a Cortex-M compiler returns a boolean) printed as a branch on a named flag:It now prints
return (uint)(*(uint *)(tmp + 20) != 0);.On the full DecBench
inlined(-O2) config, 1,511 of 26,012 functions namedtmpng/tmpzr/tmpcy/tmpovonmain(nine ARM projects); 107 do after this change. An earlier-O0run has the same names in 9,027 of 33,868 functions, so the shape is not an optimisation artefact.Three owners, all in normalize
1.
flag_recovery: SLEIGH's temporary flags are flags. ARM's SLEIGH computes the condition codes intotmpNG/tmpZR/tmpCY/tmpOVand copies them intoNG/ZR/CY/OVonly for flag-setting forms. The flag family listed only the architectural names.2.
flag_recovery: a flag definition is a snapshot. Reading the output of (1) turned up conditions that tested a register the previous statement had just replaced. The pass recordsflag -> defining expressionand substitutes the expression at later uses, and nothing dropped a record when an operand was written. This was already wrong onmain, for x86 too:A definition is now killed by a write to a variable it reads, by a memory write if it loads, by a redefinition of the flag on some path, and by a label that lets control in from elsewhere. A definition that calls is not recorded. The whole-function single-definition fallback takes only definitions that read nothing the function assigns.
3.
conditional_move: the default/override merge keeps evaluation order.x = d; if (c) x = o;becamex = c ? o : d, which evaluatescbefore the default is stored:The merge now requires that
candodo not readx, thatdhas no effect to lose, and thatchas none that could change whatdreads. Separately,x = d; if (x != 0) x = 1;folds tox = (d != 0).Plus
scratch_liveness: a flag is scratch. With (2) in place, x86 kept 42% more flag writes. Half of them were never read: a flag an instruction carries through itself (of = keep ? of : new) has a reader for every write and no test, and the unsound substitution had been breaking those cycles by accident. The post-structuring reachability pass that already retires closed scratch cycles now counts flag variables among the names it may retire. x86 flag-write statements end at 16,356 -> 14,556.Measurement
Full
inlinedconfig, the same 24,186 measured rows on both sides, caches off, one run each with nothing else on the machine:Full
unoptimizedconfig, the same 31,987 measured rows on both sides, caches off, no decompile failure on either side:509 rows moved: 284 down (−2,454), 225 up (+1,768). The gain is betaflight (−440, +35 perfect) and cleanflight (−286, +14); crazyflie (+302, −10 perfect) and openssh-portable (+234, −9) got worse.
36 rows lose a perfect score there (17 distinct functions), all read:
main: a select that reads its own result before defining it (_rs_rekey, linked into ten binaries, and one more),return v ? 1 : r0for avthat is always 0, anifwhose whole body was two dead flag writes.maindropped and the source has.mainalready turnedx = a; if (c) x = b;intox = c ? b : aon x86; on ARM a flag assignment sat between the two statements and blocked it. The source wrote anif, so these score worse.tail_dupcopies a label's tail into each jump when the tail is at most six statements. In the function traced pass by pass (20 → 120),main's tail held four dead flag assignments and was nine statements; now it is four and is copied into six arms,ifincluded. This is the largest family among the rows that got worse (18 rows, +392). The pass is untouched here.Sample-set gate (233 rows, mixed optimisation levels): perfect 67 -> 67, total distance 7,603 -> 7,625 (+22), types 17/222 -> 17/222. The sample set did not improve. Four rows moved, all up, all read:
buffer_appendnow printsxsum()twice. The source does call it twice:xtimes(xsum(capacity, 1), 2)is a macro that evaluates its argument twice.mainprinted one call because the merge in (3) foldedrax = xsum(); if (rax >= 0) rax = xsum();into a select with two equal arms.is_ownerloses a deadzfwrite and the branch shape it happened to supply.||costs nodes).Twelve rows of the full config lose a perfect score. Eight had been perfect with wrong text (five read-before-definition selects, two returning
v ? 1 : r0for avthat is always 0, oneifwhose whole body was two dead flag writes), three now print a stack storemaindropped, one duplicates areturn 0into each arm.Why the output is better without the metric: the old text tested a value on the far side of the write that changed it, and named registers the machine never tests.
Not fixed
tail_dupduplicates a tail that contains a branch, and more ARM tails now fit under its limit (above). Restricting what a duplicated tail may hold is a separate change with its own measurement.while (i < 10)for anint icompared unsigned). The cast-restoring pass skips loop conditions and stops at the first statement it changes. Both are onmain; fixed in fix: unsigned casts restored in every statement and loop condition; truncation survives a widening cast #901.cleanup_after_constant_folding_initdrops a stack store that is read only through its neighbour's address (buf[1]of a buffer passed as&buf[0]). It runs only when constant folding changed something; with the temporary flags gone it runs less often, so some of those stores now survive. That is a side effect of this PR, not a fix.movne r0, #1; moveq r0, #0) keep the flag as a variable rather than becoming oneif/else.Tests
flag_recovery,conditional_moveandscratch_liveness. Each guard was disabled in turn and the matching test failed.cargo nextest run -p fission-midend-normalize -p fission-pcode: 1,804 passed in debug, 1,803 in release.cargo clippy --workspace --locked -- -D warnings -A clippy::style -A clippy::complexityandcargo fmt --all --checkpass.Proposal:
docs/proposals/2026-11-14-flag-snapshots-and-predicated-set.md.🤖 Generated with Claude Code