Skip to content

Do not apply the flags of a conditional instruction in TransformConstDst - #516

Merged
totalspectrum merged 1 commit into
totalspectrum:masterfrom
j-modernc-org:fix-conditional-flags-fold
Oct 11, 2026
Merged

totalspectrum merged 1 commit into
totalspectrum:masterfrom
j-modernc-org:fix-conditional-flags-fold

Conversation

@j-modernc-org

Copy link
Copy Markdown

This is the fix suggested in item 1 of totalspectrum/flexprop#118, with a test.

TransformConstDst folds an instruction whose operands are known, and hands the
flags it would set to ApplyConditionAfter without looking at ir->cond. A
conditional instruction sets its flags only where its condition holds, so after it
they are not known. In the #118 reproducer, if_e cmp f, #0 wz was folded as an
unconditional compare, the if_e jmp after it became a jmp, and
(12 != g) || f came out 0 for a g of 0.

The change returns early for a conditional instruction, as OptimizeCompares and the
constant moves in OptimizeMoves already do.

Test/cexec06.c is the reproducer as a hardware test. It prints 0 1 without the
change and 1 1 with it.

Checked, with flexspin built from this branch (master eb26396 plus this commit):

  • make test_offline: the same tests pass as on master.
  • Test/runtests_p2.sh on a P2-EDGE: 27 of 27 pass, cexec06 included.
  • About 1550 C programs (the test corpus and fuzzer output of our compiler): six
    compile differently. All six run correctly on the board with the change, and one
    of them fails without it.

Item 2 of #118 (the hang at nine nested calls) and item 3 (the question) are not
touched here.

A conditional instruction sets its flags only where its condition holds,
so after it they are not known. TransformConstDst handed them to
ApplyConditionAfter anyway: `if_e cmp f, #0 wz` folded as an
unconditional compare turned the `if_e jmp` after it into a jmp, and
`(12 != g) || f` came out 0 for a g of 0 (flexprop issue 118).

Test/cexec06.c is the reproducer as a hardware test.
@totalspectrum
totalspectrum merged commit 63ef451 into totalspectrum:master Oct 11, 2026
@totalspectrum

Copy link
Copy Markdown
Owner

Thanks for your contribution! This was very nice, and having it as a pull request for spin2cpp made it easy to merge.

I'll address your other questions on the flexprop bug.

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