Skip to content

Mark additional commutative xarch and arm64 intrinsics and instructions - #130984

Merged
tannergooding merged 2 commits into
dotnet:mainfrom
tannergooding:tannergooding-ubiquitous-tribble
Jul 21, 2026
Merged

tannergooding merged 2 commits into
dotnet:mainfrom
tannergooding:tannergooding-ubiquitous-tribble

Conversation

@tannergooding

@tannergooding tannergooding commented Jul 17, 2026 •

Copy link
Copy Markdown
Member

Fixes commutativity-metadata gaps found while auditing the xarch and arm64 hwintrinsic/instruction tables.

xarch

AVX2 and AVX512 MultiplyAddAdjacent use the same instructions as the X86Base variant (pmaddubsw/pmaddwd) but were missing HW_Flag_MaybeCommutative. isCommutativeHWIntrinsic only special-cased NI_X86Base_MultiplyAddAdjacent, so the pmaddwd form of the AVX2/AVX512 intrinsics lost the operand-swap-for-containment opportunity the SSE path already gets. This adds the flag to both rows and the matching switch cases. The pmaddubsw form (base type short) correctly stays non-commutative since its operands are asymmetric (unsigned x signed); only the pmaddwd form (base type int) becomes commutative, matching the existing !varTypeIsShort(...) condition.


Several SSE4.1/SSSE3 integer instructions that are commutative were not marked INS_Flags_IsAvxCommutative, unlike their SSE2 siblings (pmaxsw, pminsw, pmuludq, etc.). This marks pcmpeqq, pmuldq, pmulld, pmaxsb, pmaxsd, pmaxud, pmaxuw, pminsb, pminsd, pminud, pminuw, and pmulhrsw. These are all SSE38/SSE3A-encoded, so the emitter always forces a 3-byte VEX prefix and the emitIns_SIMD_R_R_R operand swap can't drop to the 2-byte form -- i.e. it's a no-op for code size here. Kept anyway for consistency with the SSE2 siblings: the flag correctly describes the instruction and avoids these rows looking like they're missing it. If the swap heuristic is ever tightened to skip SSE38/SSE3A, that's a single localized change.

arm64

AdvSimd_Arm64.AbsoluteDifference (fabd, double) and AbsoluteDifferenceScalar (fabd, float/double scalar) were missing HW_Flag_Commutative, even though the float32 AdvSimd.AbsoluteDifference sibling -- the same fabd instruction -- already carries it. fabd computes |a-b|, which is commutative and NaN/signed-zero safe. This enables constant-to-op2 reordering in morph and CSE canonicalization, matching the sibling.

The SVE commutativity gap (no Sve/Sve2 op carries HW_Flag_Commutative) is left out of scope -- those forms are predicated/RMW/embedded-mask and need separate investigation rather than a metadata one-liner.

Validation

  • clr+libs -rc checked baseline build succeeded; JIT rebuilt with 0 warnings / 0 errors. The arm64 altjit (clrjit_universal_arm64_x64) also builds clean, compiling the arm64 table change.
  • Ran a correctness test exercising the affected xarch intrinsics with commuted operand orders (128- and 256-bit) under the checked JIT: all cases pass with no assertion failures. The arm64 change relies on parity with the already-commutative float32 sibling; runtime behavior is covered by CI on arm64.
  • jitformat.py -r . -o windows -a x64 reports no formatting changes.

Note

This PR was authored by GitHub Copilot.

AVX2 and AVX512 MultiplyAddAdjacent used the same instructions as the
X86Base variant (pmaddubsw/pmaddwd) but were missing HW_Flag_MaybeCommutative,
so the pmaddwd form lost the operand-swap-for-containment the SSE path gets.
Add the flag and the matching isCommutativeHWIntrinsic cases; pmaddubsw stays
non-commutative since its operands are asymmetric (unsigned x signed).

Several SSE4.1/SSSE3 integer instructions that are commutative were also not
marked INS_Flags_IsAvxCommutative, unlike their SSE2 siblings. Mark pcmpeqq,
pmuldq, pmulld, pmaxsb/sd/ud/uw, pminsb/sd/ud/uw, and pmulhrsw so the emitter
can swap op1/op2 to enable the shorter 2-byte VEX prefix when op2 is an
extended register.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 17, 2026 17:36
@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 Jul 17, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 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.

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.

Pull request overview

This PR updates xarch commutativity metadata in the JIT so operand swapping can be applied more consistently for containment / encoding decisions.

Changes:

  • Marks AVX2 and AVX512 MultiplyAddAdjacent as HW_Flag_MaybeCommutative in hwintrinsiclistxarch.h.
  • Extends GenTree::isCommutativeHWIntrinsic to treat NI_AVX2_MultiplyAddAdjacent and NI_AVX512_MultiplyAddAdjacent as conditionally commutative (non-short base types).
  • Marks additional xarch SIMD instructions as INS_Flags_IsAvxCommutative in instrsxarch.h.
Show a summary per file
File Description
src/coreclr/jit/instrsxarch.h Adds INS_Flags_IsAvxCommutative to more VEX/EVEX instructions.
src/coreclr/jit/hwintrinsiclistxarch.h Marks AVX2/AVX512 MultiplyAddAdjacent as maybe-commutative.
src/coreclr/jit/gentree.cpp Adds commutativity switch-cases for AVX2/AVX512 MultiplyAddAdjacent.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 1

Comment thread src/coreclr/jit/instrsxarch.h Resolved
The double and scalar fabd forms compute |a-b|, which is commutative and
NaN/signed-zero safe, matching the float32 AdvSimd.AbsoluteDifference sibling
that already carries the flag.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 17, 2026 22:35
@tannergooding tannergooding changed the title Mark additional commutative xarch intrinsics and instructions Mark additional commutative xarch and arm64 intrinsics and instructions Jul 17, 2026

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.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 1

Comment thread src/coreclr/jit/instrsxarch.h Resolved
@tannergooding

Copy link
Copy Markdown
Member Author

@dotnet/jit-contrib This adds the missing commutative tags. Low priority, but also a simple review.

@github-actions

github-actions Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow state for the Holistic Review Orchestrator.

{
  "version": 5,
  "last_dispatched_commit": "7dd1768b538a6eec8182c592c214072c6d384749",
  "last_dispatched_base_ref": "main",
  "last_dispatched_base_sha": "ce12d869155b7fba9b8701db6e0e4254a74275b2",
  "last_reviewed_commit": "7dd1768b538a6eec8182c592c214072c6d384749",
  "last_reviewed_base_ref": "main",
  "last_reviewed_base_sha": "ce12d869155b7fba9b8701db6e0e4254a74275b2",
  "last_recorded_worker_run_id": "29688138974",
  "review_attempt_commit": "",
  "review_attempt_base_ref": "",
  "review_attempt_count": 0,
  "max_review_attempts": 5,
  "review_history_format": "holistic-review-disclosure-v1",
  "review_history": [
    {
      "commit": "7dd1768b538a6eec8182c592c214072c6d384749",
      "review_id": 4730810688
    }
  ]
}

@github-actions github-actions Bot 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.

Holistic Review

Motivation: The PR closes commutativity-metadata gaps found while auditing the xarch and arm64 hwintrinsic/instruction tables. Missing HW_Flag_MaybeCommutative on the AVX2/AVX512 MultiplyAddAdjacent rows, missing INS_Flags_IsAvxCommutative on several SSE4.1/SSSE3 integer instructions, and missing HW_Flag_Commutative on the arm64 fabd-backed AbsoluteDifference/AbsoluteDifferenceScalar all diverged from siblings that use the identical underlying instruction, denying the JIT operand-swap-for-containment and constant-reordering opportunities.

Approach: Small, targeted metadata edits. For xarch MultiplyAddAdjacent, the flag is added to both AVX2 and AVX512 table rows and the NI_*_MultiplyAddAdjacent cases are added to the isCommutativeHWIntrinsic switch, reusing the existing !varTypeIsShort(...) guard so only the pmaddwd (int) form is commutative while the asymmetric pmaddubsw (short) form stays non-commutative. The SSE4.1/SSSE3 INST3 rows gain INS_Flags_IsAvxCommutative matching their SSE2 siblings. The arm64 rows gain HW_Flag_Commutative, matching the already-commutative float32 AdvSimd.AbsoluteDifference sibling.

Summary: I verified the correctness claims against the tree: the AdvSimd fabd sibling already carries HW_Flag_Commutative; the SSE2 siblings (pmaxsw, pminsw, pmuludq, pmulhw, pmullw) and pcmpeqb/w/d already carry INS_Flags_IsAvxCommutative; and the MaybeCommutative switch correctly resolves the new AVX2/AVX512 cases through the shared !varTypeIsShort guard (avoiding the default: unreached() path). The changes are consistent, minimal, and semantically sound — fabd (|a-b|) is commutative and NaN/signed-zero safe, and pmaddwd is commutative for its int form. The author notes the swap is a no-op for code size on the SSE38/SSE3A-encoded rows (always 3-byte VEX), which is accurate; keeping the flag for descriptive consistency is reasonable. Leaving the SVE predicated/RMW forms out of scope is appropriate. No correctness or performance concerns. LGTM.

Note

This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.

Generated by Holistic Review · 62.1 AIC · ⌖ 10.5 AIC · ⊞ 10K

@tannergooding
tannergooding enabled auto-merge (squash) July 21, 2026 13:52
@JulieLeeMSFT JulieLeeMSFT added the Priority:2 Work that is important, but not critical for the release label Jul 21, 2026

@JulieLeeMSFT JulieLeeMSFT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.
Verified that instructions are flagged correctly with either HW_Flag_Commutative or HW_Flag_MaybeCommutative.

@tannergooding
tannergooding merged commit ce44c23 into dotnet:main Jul 21, 2026
151 checks passed
@tannergooding
tannergooding deleted the tannergooding-ubiquitous-tribble branch July 21, 2026 20:03
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI Priority:2 Work that is important, but not critical for the release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants