Repository navigation
JIT: enable Wasm select for if-conversion - #137771
Conversation
Co-authored-by: Copilot <[email protected]>
|
Azure Pipelines: Successfully started running 6 pipeline(s). 9 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
|
@adamperlin PTAL Small code size savings (uncompressed). Didn't get a good read on perf here, but other Wasm emitters use this, so... |
There was a problem hiding this comment.
🔵 Needs a closer look
The new Wasm backend operator changes operand ordering with stackification and exception-preservation implications, requiring JIT maintainer confirmation because backend execution and reported SuperPMI results could not be independently validated.
No open issues 🎉
What changed in this PR
Enables the CoreCLR JIT to convert eligible WebAssembly conditionals into native select instructions, including integer, long, and floating-point values.
Changes:
- Adds
GT_WASM_SELECTwith operand traversal and code generation in Wasm stack order. - Enables Wasm if-conversion and moves condition evaluation after value operands only when safety checks permit.
- Adds directed tests for numeric results, nested selects, bit preservation, and exception behavior.
| File | Description |
|---|---|
src/tests/JIT/Directed/WasmSelect/WasmSelect.csproj |
Configures the optimized, priority-1 test project. |
src/tests/JIT/Directed/WasmSelect/WasmSelect.cs |
Tests select results, exact bits, and exception semantics. |
src/coreclr/jit/lowerwasm.cpp |
Adds guarded condition reordering for Wasm stack order. |
src/coreclr/jit/lower.h |
Declares the Wasm condition-reordering helper. |
src/coreclr/jit/lower.cpp |
Lowers selects to the Wasm-specific node. |
src/coreclr/jit/instrswasm.h |
Defines the Wasm select opcode. |
src/coreclr/jit/ifconversion.cpp |
Enables Wasm if-conversion, including floating-point and long selects. |
src/coreclr/jit/gtstructs.h |
Maps the new node to GenTreeConditional. |
src/coreclr/jit/gtlist.h |
Registers GT_WASM_SELECT and documents operand order. |
src/coreclr/jit/gentree.h |
Declares Wasm-select operand iteration. |
src/coreclr/jit/gentree.cpp |
Adds operand lookup, iteration, dispatch, and diagnostic display support. |
src/coreclr/jit/compiler.hpp |
Visits Wasm-select operands in stack order. |
src/coreclr/jit/codegenwasm.cpp |
Consumes the operands and emits select. |
Review effort: Balanced
adamperlin
left a comment
There was a problem hiding this comment.
I think this looks good! The compressed size regression is pretty small.
|
Looks like there may be a related failure, digging in. |
Clear the jump-use flag when if-conversion replaces a select with a value expression. On Wasm, leaving the flag set can cause a compare against zero to return a raw nonzero operand instead of 1. Add a regression for the masked comparison. Co-authored-by: Copilot <[email protected]>
|
@adamperlin Could you re-review the follow-up in d8b520b? It fixes the Wasm CI miscompile by clearing the jump-use flag when if-conversion turns the comparison into a value. The new regression returns 2 instead of 1 with the unfixed cross-JIT and passes with the fix. I also confirmed that the generated Wasm for Note This comment was generated with GitHub Copilot. |
|
/ba-g osx host stopped respoinding during build |
JIT: enable Wasm select for if-conversion
Description
If-conversion (
optIfConversion) previously skipped the WebAssembly target almost entirely: it only ran forTARGET_ARM64/TARGET_XARCH/TARGET_RISCV64, and even whereGT_SELECTcould be formed, it bailed out for floating-point selects and (via the 32-bit-operand restriction) forlongselects, since Wasm had no lowering/codegen forGT_SELECT.This change enables if-conversion for Wasm and gives it a native lowering path using the Wasm
selectinstruction:GT_WASM_SELECTnode (aGenTreeConditional) is introduced with operands in Wasm stack order: true value, false value, condition (the order theselectopcon expects on the value stack), as opposed toGT_SELECT's condition-first order.ifconversion.cppnow allows floating-point and (on!TARGET_64BIT)long-typed selects to proceed underTARGET_WASM, since Wasm'sselectinstruction natively handles bothi32/i64/f32/f64operands.Lowering::LowerSelectrenames the node toGT_WASM_SELECTfor Wasm instead of trying to split it intoGT_SELECTCC.Lowering::TryMoveWasmSelectConditionreorders the condition tree to sit immediately before the select node (so it naturally ends up last on the Wasm value stack) only when it's safe to do so: the range between the condition and the select must be a closed/contiguous LIR range, none of its nodes may be multiply-used, and the range must passIsRangeInvariantInRangeso moving it later can't change observed side effects or exception ordering. When any of these don't hold, the tree is left in its original order.codegenwasm.cpp/compiler.hpp/gentree.cppadd the corresponding codegen (genConsumeRegsfor all three operands followed by the newselectinstruction encoding ininstrswasm.h),GenTreeoperand-iteration/dispatch support, and dump support for the new node shape.Testing
src/tests/JIT/Directed/WasmSelect/WasmSelect.cs, covering: int/long/float/double selects, selects driven by a comparison, nested selects, selects feeding a stack-allocated local, exact bit preservation for negative zero and NaN payloads, and ordering/short-circuit cases where the untaken arm or the condition itself throws (array index, divide-by-zero) to confirm the reordering logic inTryMoveWasmSelectConditiondoesn't change exception semantics.nodehost) against a Checkedbrowser-wasmbuild — passed (Expected: 100 / Actual: 100).wasmcross-target altjit against thelibrariesandcoreclr_testscrossgen2 Windows x64 Checked MCH files: 0FailedCompile/0Missingacross all 222,608 (libraries) + 208,578 (coreclr_tests) contexts. Size deltas: libraries -88,437 bytes (-0.12%), tests -383,298 bytes (-0.12%), with a small net PerfScore improvement on both.Notes
selectsupport in if-conversion); JIT: SIMD Min/Max and 64-bit multiplication can reread aliased operands #135432 is an unrelated SIMD aliasing issue and is not the motivation here, so this PR doesn't close an issue.Note
This pull request description and the underlying change were drafted with AI assistance (GitHub Copilot).