Skip to content

JIT: enable Wasm select for if-conversion - #137771

Merged
AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:wasm-select
Nov 20, 2026
Merged

AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:wasm-select

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

JIT: enable Wasm select for if-conversion

Description

If-conversion (optIfConversion) previously skipped the WebAssembly target almost entirely: it only ran for TARGET_ARM64/TARGET_XARCH/TARGET_RISCV64, and even where GT_SELECT could be formed, it bailed out for floating-point selects and (via the 32-bit-operand restriction) for long selects, since Wasm had no lowering/codegen for GT_SELECT.

This change enables if-conversion for Wasm and gives it a native lowering path using the Wasm select instruction:

  • A new GT_WASM_SELECT node (a GenTreeConditional) is introduced with operands in Wasm stack order: true value, false value, condition (the order the select opcon expects on the value stack), as opposed to GT_SELECT's condition-first order.
  • ifconversion.cpp now allows floating-point and (on !TARGET_64BIT) long-typed selects to proceed under TARGET_WASM, since Wasm's select instruction natively handles both i32/i64/f32/f64 operands.
  • Lowering::LowerSelect renames the node to GT_WASM_SELECT for Wasm instead of trying to split it into GT_SELECTCC.
  • A new Lowering::TryMoveWasmSelectCondition reorders 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 pass IsRangeInvariantInRange so 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.cpp add the corresponding codegen (genConsumeRegs for all three operands followed by the new select instruction encoding in instrswasm.h), GenTree operand-iteration/dispatch support, and dump support for the new node shape.

Testing

  • Added 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 in TryMoveWasmSelectCondition doesn't change exception semantics.
  • Ran this test under the Wasm JIT (node host) against a Checked browser-wasm build — passed (Expected: 100 / Actual: 100).
  • Ran SuperPMI asm diffs for the Checked wasm cross-target altjit against the libraries and coreclr_tests crossgen2 Windows x64 Checked MCH files: 0 FailedCompile/0 Missing across 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.
  • I did not obtain a reliable end-to-end measurement of this change against a real Blazor/WasmAot app: one local comparison build (Havit sample, Release/AOT) showed a +0.1% larger cold-download size, which is inconsistent with the SPMI code-size deltas above, so I'm not reporting an end-to-end size or speed claim here — only the SPMI-level JIT evidence, which I was able to reproduce directly from the logs.

Notes

Note

This pull request description and the underlying change were drafted with AI assistance (GitHub Copilot).

@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 17, 2026
@azure-pipelines

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

@adamperlin PTAL
fyi @dotnet/wasm-contrib

Small code size savings (uncompressed). Didn't get a good read on perf here, but other Wasm emitters use this, so...

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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_SELECT with 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 adamperlin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good! The compressed size regression is pretty small.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

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]>
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@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 HandleSuspended now normalizes the callback flags, though the original async test wasn't verified end to end locally.

Note

This comment was generated with GitHub Copilot.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

/ba-g osx host stopped respoinding during build

@AndyAyersMS
AndyAyersMS merged commit fc2dfbe into dotnet:main Nov 20, 2026
127 of 130 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants