Repository navigation
Mark additional commutative xarch and arm64 intrinsics and instructions - #130984
Conversation
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]>
|
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. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
There was a problem hiding this comment.
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
AVX2andAVX512MultiplyAddAdjacentasHW_Flag_MaybeCommutativeinhwintrinsiclistxarch.h. - Extends
GenTree::isCommutativeHWIntrinsicto treatNI_AVX2_MultiplyAddAdjacentandNI_AVX512_MultiplyAddAdjacentas conditionally commutative (non-shortbase types). - Marks additional xarch SIMD instructions as
INS_Flags_IsAvxCommutativeininstrsxarch.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
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]>
|
@dotnet/jit-contrib This adds the missing commutative tags. Low priority, but also a simple review. |
|
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
}
]
} |
There was a problem hiding this comment.
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
JulieLeeMSFT
left a comment
There was a problem hiding this comment.
LGTM.
Verified that instructions are flagged correctly with either HW_Flag_Commutative or HW_Flag_MaybeCommutative.
Fixes commutativity-metadata gaps found while auditing the xarch and arm64 hwintrinsic/instruction tables.
xarch
AVX2andAVX512MultiplyAddAdjacentuse the same instructions as theX86Basevariant (pmaddubsw/pmaddwd) but were missingHW_Flag_MaybeCommutative.isCommutativeHWIntrinsiconly special-casedNI_X86Base_MultiplyAddAdjacent, so thepmaddwdform 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. Thepmaddubswform (base typeshort) correctly stays non-commutative since its operands are asymmetric (unsigned x signed); only thepmaddwdform (base typeint) becomes commutative, matching the existing!varTypeIsShort(...)condition.Several
SSE4.1/SSSE3integer instructions that are commutative were not markedINS_Flags_IsAvxCommutative, unlike theirSSE2siblings (pmaxsw,pminsw,pmuludq, etc.). This markspcmpeqq,pmuldq,pmulld,pmaxsb,pmaxsd,pmaxud,pmaxuw,pminsb,pminsd,pminud,pminuw, andpmulhrsw. These are allSSE38/SSE3A-encoded, so the emitter always forces a 3-byte VEX prefix and theemitIns_SIMD_R_R_Roperand 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 theSSE2siblings: the flag correctly describes the instruction and avoids these rows looking like they're missing it. If the swap heuristic is ever tightened to skipSSE38/SSE3A, that's a single localized change.arm64
AdvSimd_Arm64.AbsoluteDifference(fabd,double) andAbsoluteDifferenceScalar(fabd,float/doublescalar) were missingHW_Flag_Commutative, even though the float32AdvSimd.AbsoluteDifferencesibling -- the samefabdinstruction -- already carries it.fabdcomputes|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/Sve2op carriesHW_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 checkedbaseline build succeeded; JIT rebuilt with 0 warnings / 0 errors. The arm64 altjit (clrjit_universal_arm64_x64) also builds clean, compiling the arm64 table change.jitformat.py -r . -o windows -a x64reports no formatting changes.Note
This PR was authored by GitHub Copilot.