JonSnow1807 opened PR #15220 from JonSnow1807:x64-branch-on-arith-flags-11125 to bytecodealliance:main:
Closes #11125.
On x64,
brif,select,trapz/trapnzandicmp eq/neagainst zero on the result of aniadd,isub,bororbxornow use that instruction’s flags instead of a separatetest:orl %esi, %edi orl %esi, %edi testl %edi, %edi => jnz ... jnz ...The op uses its exact width, so for i8/i16 ZF only sees the low bits. Only zero/nonzero conditions are folded, since OF/CF differ from
test. The result is an opportunistic def (#14228), so a value that is used again later is still computed once.The rules only fire when the op is in the same block and its other uses are already lowered. Otherwise the op gets emitted twice, which made
crit-edge.clifworse. For that check,compute_use_statesnow also returns exact use counts. I’m happy to drop the check if you’d rather not touchlower.rs.On 25 Sightglass modules, code size goes down 0.14%. I also tried
cmpfor single-useisub/bxor, as the issue suggests, but it wasn’t smaller. There are new precise-output and run tests, and all filetests pass.
JonSnow1807 removed the review request for wasmtime-compiler-reviewers from PR #15220.
JonSnow1807 requested wasmtime-compiler-reviewers for a review on PR #15220.
JonSnow1807 requested alexcrichton for a review on PR #15220.
cfallin requested cfallin for a review on PR #15220.
cfallin removed the review request for alexcrichton from PR #15220.
cfallin commented on PR #15220:
(stealing the review here -- happy to take a look)
cfallin commented on PR #15220:
Thanks for this change, @JonSnow1807 ! Can you detail why you had to modify the lowering logic to have a separate "foldable" predicate? I am pretty concerned about the complexity and duplication/partial overlap with our other lowering logic: I don't want yet another definition of a "foldable" op with its own subtle conditions.
I had at least naively hoped that the opportunistic-def mechanism would be enough here. Basically we want to match on
is_nonzero-of-op and emit the op itself as an opportunistic def as you've done; what happens if we elide the "foldable" predicate there? We emit the ALU op instead of thetest? I guess that slightly increases register pressure (extending liverange of the two inputs rather than using the one output) but I'd want to see Sightglass data on this simpler alternative before discarding it.The complexity around 8- and 16-bit exact values is also IMHO not worth it -- testing nonzero on those ops is such a niche thing that I don't want to introduce the complexity risk of having special lowerings here.
JonSnow1807 commented on PR #15220:
Thanks for taking this! The predicate was there to avoid computing the op twice: if the op is defined in another block, or its value is used before the branch, the opportunistic def isn't taken, so the op is emitted at its definition and again at the branch. In crit-edge.clif that showed up as a mov + add instead of a test. But it doesn't buy much: on the same 25 Sightglass modules, the version without it reduces code size by 0.126% vs 0.139% with it, and the instruction counts are almost the same. So I'm happy to drop it, along with the 8/16-bit handling. I'll push that and post Sightglass runtime numbers for main vs the simpler version.
JonSnow1807 updated PR #15220.
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
Again, let's not make changes that are specific to 8/16-bit cases: this extra complexity plus the
_rawvariants everywhere make for hard-to-follow code. Ideally I want to see the new lowering rules but no changes to the underlying common case here.
:memo: JonSnow1807 submitted PR review.
:speech_balloon: JonSnow1807 created PR review comment:
Makes sense. I’ll revert the x64_xor split so the common path is untouched. That leaves no flags-producing xor, so for bxor I can either add a small 32/64-bit-only x64_xor_with_flags_paired next to the existing or one, without changing x64_xor, or drop bxor from this PR. Which would you prefer?
JonSnow1807 commented on PR #15220:
Pushed e2a7623: the predicate and the 8/16-bit cases are gone, so it relies only on the opportunistic def. The only existing filetest that changes is crit-edge.clif, where the iadd is now recomputed in the other block instead of a testl. Sightglass (default suite, cycles, pinned cores): the execution geomean is unchanged vs main, with individual benchmarks between -4.9% (libsodium-siphashx24) and +1.8% (rust-protobuf). Code size is -0.12%. With the predicate it was -0.14% and about 0.2-0.3% faster on the geomean. Happy to go with whichever you prefer.
github-actions[bot] added the label cranelift on PR #15220.
github-actions[bot] added the label cranelift:area:x64 on PR #15220.
cfallin commented on PR #15220:
I still see the
_rawvariants here that I asked to remove from the diff earlier, and I still see changes to the 8/16-bit cases in the diff? Can you take a look at the diff and ensure that it is minimal, ideally adding only the new lowering rules (and any new instruction constructors, not changing existing ones)?
JonSnow1807 updated PR #15220.
JonSnow1807 commented on PR #15220:
Sorry, I should have pushed that right away. c34b3f2 leaves x64_xor and the other existing constructors unchanged. inst.isle now only adds the new rules and two new constructors.
:thumbs_up: cfallin submitted PR review:
Thanks! Just some nits below then good to merge.
:speech_balloon: cfallin created PR review comment:
No need for this narrative comment in the runtest -- the runtest shouldn't have any association or statements about what assembly we expect.
:speech_balloon: cfallin created PR review comment:
Can we add all architectures here? There's no reason not to run it everywhere if it tests some generic behavior (that we happen to have an optimized lowering for on x64)...
JonSnow1807 updated PR #15220.
JonSnow1807 edited PR #15220:
Closes #11125.
On x64,
brif,select,trapz/trapnzandicmp eq/neagainst zero on the result of a 32- or 64-bitiadd,isub,bororbxornow use that instruction's flags instead of a separatetest:orl %esi, %edi orl %esi, %edi testl %edi, %edi => jnz ... jnz ...Only zero/nonzero conditions are folded, since OF/CF differ from
test. The result is an opportunistic def (#14228), so a value used again later is usually still computed once; when it isn't, the op is emitted again in place of thetest(as incrit-edge.clif).On 25 Sightglass modules, code size goes down 0.12%, and the execution geomean is unchanged.
JonSnow1807 commented on PR #15220:
Done in aa65a11: the runtest runs on all targets and has no comment. I dropped its one load case, which needed a 64-bit stack address; the precise-output test still covers it.
JonSnow1807 commented on PR #15220:
Thanks for the review! CI is green on aa65a11. Could you add it to the merge queue when you get a chance?
cfallin commented on PR #15220:
Sorry, I'm away for the rest of this week but I'll review the final commits when I'm back on Monday!
:thumbs_up: cfallin submitted PR review.
cfallin added PR #15220 Cranelift: x64: branch on the flags of iadd, isub, bor and bxor to the merge queue
:check: cfallin merged PR #15220.
cfallin removed PR #15220 Cranelift: x64: branch on the flags of iadd, isub, bor and bxor from the merge queue
Last updated: Nov 19 2026 at 20:05 UTC