[llvm] [X86] Expand vector atomicrmw through a cmpxchg loop (PR #230788)
Krishnan R via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 10 05:38:53 PDT 2026
https://github.com/Rmuk655 created https://github.com/llvm/llvm-project/pull/230788
Fixes #226002.
`shouldExpandAtomicRMWInIR` returned None for the operations xchg/add/sub when the operand
was a vector whose size did not exceed the native width of 64 bits (if size > native_size). A <2 x i32> is 64 bits, so it skipped that check and reached type legalization stage as an `ATOMIC_LOAD_ADD` node with a vector result, which aborts with "do not know how to widen the result of this operator".
x86 has no native atomic RMW instruction for vectors, so this patch always expands them to a `cmpxchg` loop. `AtomicExpandPass` already bitcasts the vector to an integer of the same width.
128-bit vectors use `cmpxchg16b` when `+cx16` is available and otherwise fall back to libcalls such as `__atomic_*_16` (typically implemented with a lock; correct but slower).
This is a target-specific fix. A generic fallback in `AtomicExpandPass` could be considered as a follow-up for other targets.
Tests: added llvm/test/CodeGen/X86/atomicrmw-int-vector.ll, run with and without +cx16. Also covers idempotent `or x, 0` on vectors.
>From 86e67ca083f1d11991ff56bce50356b0d861de37 Mon Sep 17 00:00:00 2001
From: Rmuk655 <cs24btech11036 at iith.ac.in>
Date: Sat, 10 Oct 2026 10:23:53 +0000
Subject: [PATCH] [X86] Expand vector atomicrmw through a cmpxchg loop
shouldExpandAtomicRMWInIR returned None for xchg/add/sub even when the
operand was a vector. A 64-bit vector such as <2 x i32> is not wider than
the native width, so it skipped the width check and reached type
legalization as an ATOMIC_LOAD_ADD node with a vector result, which
aborts with "do not know how to widen the result of this operator".
Vectors have no native atomic add/xchg on x86, so always expand them to a
cmpxchg loop; AtomicExpandPass already bitcasts the vector to an integer
of the same width for this.
Fixes #226002
---
llvm/lib/Target/X86/X86ISelLowering.cpp | 5 +
llvm/test/CodeGen/X86/atomicrmw-int-vector.ll | 265 ++++++++++++++++++
2 files changed, 270 insertions(+)
create mode 100644 llvm/test/CodeGen/X86/atomicrmw-int-vector.ll
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a265c163cf276..bd592a9482b98 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -33169,6 +33169,11 @@ X86TargetLowering::shouldExpandAtomicRMWInIR(const AtomicRMWInst *AI) const {
unsigned NativeWidth = Subtarget.is64Bit() ? 64 : 32;
Type *MemType = AI->getType();
+ // x86 has no native atomic RMW operations for vectors; expanded to a CmpXChg
+ // loop.
+ if (MemType->isVectorTy())
+ return AtomicExpansionKind::CmpXChg;
+
// If the operand is too big, we must see if cmpxchg8/16b is available
// and default to library calls otherwise.
if (MemType->getPrimitiveSizeInBits() > NativeWidth) {
diff --git a/llvm/test/CodeGen/X86/atomicrmw-int-vector.ll b/llvm/test/CodeGen/X86/atomicrmw-int-vector.ll
new file mode 100644
index 0000000000000..05fb8e1926ee4
--- /dev/null
+++ b/llvm/test/CodeGen/X86/atomicrmw-int-vector.ll
@@ -0,0 +1,265 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64-linux-gnu | FileCheck %s --check-prefixes=CHECK,NOCX16
+; RUN: llc < %s -mtriple=x86_64-linux-gnu -mattr=+cx16 | FileCheck %s --check-prefixes=CHECK,CX16
+
+define void @atomicrmw_add_v2i32(ptr %p, <2 x i32> %v) {
+; CHECK-LABEL: atomicrmw_add_v2i32:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movq {{.*#+}} xmm1 = mem[0],zero
+; CHECK-NEXT: .p2align 4
+; CHECK-NEXT: .LBB0_1: # %atomicrmw.start
+; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
+; CHECK-NEXT: movq %xmm1, %rax
+; CHECK-NEXT: paddd %xmm0, %xmm1
+; CHECK-NEXT: movq %xmm1, %rcx
+; CHECK-NEXT: lock cmpxchgq %rcx, (%rdi)
+; CHECK-NEXT: movq %rax, %xmm1
+; CHECK-NEXT: jne .LBB0_1
+; CHECK-NEXT: # %bb.2: # %atomicrmw.end
+; CHECK-NEXT: retq
+ %r = atomicrmw add ptr %p, <2 x i32> %v seq_cst
+ ret void
+}
+
+define void @atomicrmw_sub_v2i64(ptr %p, <2 x i64> %v) {
+; NOCX16-LABEL: atomicrmw_sub_v2i64:
+; NOCX16: # %bb.0:
+; NOCX16-NEXT: pushq %rax
+; NOCX16-NEXT: .cfi_def_cfa_offset 16
+; NOCX16-NEXT: movq %xmm0, %rsi
+; NOCX16-NEXT: pshufd {{.*#+}} xmm0 = xmm0[2,3,2,3]
+; NOCX16-NEXT: movq %xmm0, %rdx
+; NOCX16-NEXT: movl $5, %ecx
+; NOCX16-NEXT: callq __atomic_fetch_sub_16 at PLT
+; NOCX16-NEXT: popq %rax
+; NOCX16-NEXT: .cfi_def_cfa_offset 8
+; NOCX16-NEXT: retq
+;
+; CX16-LABEL: atomicrmw_sub_v2i64:
+; CX16: # %bb.0:
+; CX16-NEXT: pushq %rbx
+; CX16-NEXT: .cfi_def_cfa_offset 16
+; CX16-NEXT: .cfi_offset %rbx, -16
+; CX16-NEXT: movdqa (%rdi), %xmm1
+; CX16-NEXT: .p2align 4
+; CX16-NEXT: .LBB1_1: # %atomicrmw.start
+; CX16-NEXT: # =>This Inner Loop Header: Depth=1
+; CX16-NEXT: movq %xmm1, %rax
+; CX16-NEXT: pshufd {{.*#+}} xmm2 = xmm1[2,3,2,3]
+; CX16-NEXT: psubq %xmm0, %xmm1
+; CX16-NEXT: movq %xmm1, %rbx
+; CX16-NEXT: pshufd {{.*#+}} xmm1 = xmm1[2,3,2,3]
+; CX16-NEXT: movq %xmm1, %rcx
+; CX16-NEXT: movq %xmm2, %rdx
+; CX16-NEXT: lock cmpxchg16b (%rdi)
+; CX16-NEXT: movq %rdx, %xmm2
+; CX16-NEXT: movq %rax, %xmm1
+; CX16-NEXT: punpcklqdq {{.*#+}} xmm1 = xmm1[0],xmm2[0]
+; CX16-NEXT: jne .LBB1_1
+; CX16-NEXT: # %bb.2: # %atomicrmw.end
+; CX16-NEXT: popq %rbx
+; CX16-NEXT: .cfi_def_cfa_offset 8
+; CX16-NEXT: retq
+ %r = atomicrmw sub ptr %p, <2 x i64> %v seq_cst
+ ret void
+}
+
+define void @atomicrmw_xchg_v4i32(ptr %p, <4 x i32> %v) {
+; NOCX16-LABEL: atomicrmw_xchg_v4i32:
+; NOCX16: # %bb.0:
+; NOCX16-NEXT: pushq %rax
+; NOCX16-NEXT: .cfi_def_cfa_offset 16
+; NOCX16-NEXT: movq %xmm0, %rsi
+; NOCX16-NEXT: pshufd {{.*#+}} xmm0 = xmm0[2,3,2,3]
+; NOCX16-NEXT: movq %xmm0, %rdx
+; NOCX16-NEXT: movl $5, %ecx
+; NOCX16-NEXT: callq __atomic_exchange_16 at PLT
+; NOCX16-NEXT: popq %rax
+; NOCX16-NEXT: .cfi_def_cfa_offset 8
+; NOCX16-NEXT: retq
+;
+; CX16-LABEL: atomicrmw_xchg_v4i32:
+; CX16: # %bb.0:
+; CX16-NEXT: pushq %rbx
+; CX16-NEXT: .cfi_def_cfa_offset 16
+; CX16-NEXT: .cfi_offset %rbx, -16
+; CX16-NEXT: movq %xmm0, %rbx
+; CX16-NEXT: pshufd {{.*#+}} xmm0 = xmm0[2,3,2,3]
+; CX16-NEXT: movq %xmm0, %rcx
+; CX16-NEXT: movq (%rdi), %rax
+; CX16-NEXT: movq 8(%rdi), %rdx
+; CX16-NEXT: .p2align 4
+; CX16-NEXT: .LBB2_1: # %atomicrmw.start
+; CX16-NEXT: # =>This Inner Loop Header: Depth=1
+; CX16-NEXT: lock cmpxchg16b (%rdi)
+; CX16-NEXT: jne .LBB2_1
+; CX16-NEXT: # %bb.2: # %atomicrmw.end
+; CX16-NEXT: popq %rbx
+; CX16-NEXT: .cfi_def_cfa_offset 8
+; CX16-NEXT: retq
+ %r = atomicrmw xchg ptr %p, <4 x i32> %v seq_cst
+ ret void
+}
+
+define void @atomicrmw_and_v2i32(ptr %p, <2 x i32> %v) {
+; CHECK-LABEL: atomicrmw_and_v2i32:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movq {{.*#+}} xmm1 = mem[0],zero
+; CHECK-NEXT: .p2align 4
+; CHECK-NEXT: .LBB3_1: # %atomicrmw.start
+; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
+; CHECK-NEXT: movq %xmm1, %rax
+; CHECK-NEXT: pand %xmm0, %xmm1
+; CHECK-NEXT: movq %xmm1, %rcx
+; CHECK-NEXT: lock cmpxchgq %rcx, (%rdi)
+; CHECK-NEXT: movq %rax, %xmm1
+; CHECK-NEXT: jne .LBB3_1
+; CHECK-NEXT: # %bb.2: # %atomicrmw.end
+; CHECK-NEXT: retq
+ %r = atomicrmw and ptr %p, <2 x i32> %v seq_cst
+ ret void
+}
+
+define void @atomicrmw_or_v2i64(ptr %p, <2 x i64> %v) {
+; NOCX16-LABEL: atomicrmw_or_v2i64:
+; NOCX16: # %bb.0:
+; NOCX16-NEXT: pushq %rax
+; NOCX16-NEXT: .cfi_def_cfa_offset 16
+; NOCX16-NEXT: movq %xmm0, %rsi
+; NOCX16-NEXT: pshufd {{.*#+}} xmm0 = xmm0[2,3,2,3]
+; NOCX16-NEXT: movq %xmm0, %rdx
+; NOCX16-NEXT: movl $5, %ecx
+; NOCX16-NEXT: callq __atomic_fetch_or_16 at PLT
+; NOCX16-NEXT: popq %rax
+; NOCX16-NEXT: .cfi_def_cfa_offset 8
+; NOCX16-NEXT: retq
+;
+; CX16-LABEL: atomicrmw_or_v2i64:
+; CX16: # %bb.0:
+; CX16-NEXT: pushq %rbx
+; CX16-NEXT: .cfi_def_cfa_offset 16
+; CX16-NEXT: .cfi_offset %rbx, -16
+; CX16-NEXT: movdqa (%rdi), %xmm1
+; CX16-NEXT: .p2align 4
+; CX16-NEXT: .LBB4_1: # %atomicrmw.start
+; CX16-NEXT: # =>This Inner Loop Header: Depth=1
+; CX16-NEXT: movq %xmm1, %rax
+; CX16-NEXT: pshufd {{.*#+}} xmm2 = xmm1[2,3,2,3]
+; CX16-NEXT: por %xmm0, %xmm1
+; CX16-NEXT: movq %xmm1, %rbx
+; CX16-NEXT: pshufd {{.*#+}} xmm1 = xmm1[2,3,2,3]
+; CX16-NEXT: movq %xmm1, %rcx
+; CX16-NEXT: movq %xmm2, %rdx
+; CX16-NEXT: lock cmpxchg16b (%rdi)
+; CX16-NEXT: movq %rdx, %xmm2
+; CX16-NEXT: movq %rax, %xmm1
+; CX16-NEXT: punpcklqdq {{.*#+}} xmm1 = xmm1[0],xmm2[0]
+; CX16-NEXT: jne .LBB4_1
+; CX16-NEXT: # %bb.2: # %atomicrmw.end
+; CX16-NEXT: popq %rbx
+; CX16-NEXT: .cfi_def_cfa_offset 8
+; CX16-NEXT: retq
+ %r = atomicrmw or ptr %p, <2 x i64> %v seq_cst
+ ret void
+}
+
+define void @atomicrmw_xor_v4i32(ptr %p, <4 x i32> %v) {
+; NOCX16-LABEL: atomicrmw_xor_v4i32:
+; NOCX16: # %bb.0:
+; NOCX16-NEXT: pushq %rax
+; NOCX16-NEXT: .cfi_def_cfa_offset 16
+; NOCX16-NEXT: movq %xmm0, %rsi
+; NOCX16-NEXT: pshufd {{.*#+}} xmm0 = xmm0[2,3,2,3]
+; NOCX16-NEXT: movq %xmm0, %rdx
+; NOCX16-NEXT: movl $5, %ecx
+; NOCX16-NEXT: callq __atomic_fetch_xor_16 at PLT
+; NOCX16-NEXT: popq %rax
+; NOCX16-NEXT: .cfi_def_cfa_offset 8
+; NOCX16-NEXT: retq
+;
+; CX16-LABEL: atomicrmw_xor_v4i32:
+; CX16: # %bb.0:
+; CX16-NEXT: pushq %rbx
+; CX16-NEXT: .cfi_def_cfa_offset 16
+; CX16-NEXT: .cfi_offset %rbx, -16
+; CX16-NEXT: movdqa (%rdi), %xmm1
+; CX16-NEXT: .p2align 4
+; CX16-NEXT: .LBB5_1: # %atomicrmw.start
+; CX16-NEXT: # =>This Inner Loop Header: Depth=1
+; CX16-NEXT: movq %xmm1, %rax
+; CX16-NEXT: pshufd {{.*#+}} xmm2 = xmm1[2,3,2,3]
+; CX16-NEXT: pxor %xmm0, %xmm1
+; CX16-NEXT: movq %xmm1, %rbx
+; CX16-NEXT: pshufd {{.*#+}} xmm1 = xmm1[2,3,2,3]
+; CX16-NEXT: movq %xmm1, %rcx
+; CX16-NEXT: movq %xmm2, %rdx
+; CX16-NEXT: lock cmpxchg16b (%rdi)
+; CX16-NEXT: movq %rdx, %xmm2
+; CX16-NEXT: movq %rax, %xmm1
+; CX16-NEXT: punpcklqdq {{.*#+}} xmm1 = xmm1[0],xmm2[0]
+; CX16-NEXT: jne .LBB5_1
+; CX16-NEXT: # %bb.2: # %atomicrmw.end
+; CX16-NEXT: popq %rbx
+; CX16-NEXT: .cfi_def_cfa_offset 8
+; CX16-NEXT: retq
+ %r = atomicrmw xor ptr %p, <4 x i32> %v seq_cst
+ ret void
+}
+
+; The result is used, so the value returned by the cmpxchg loop is needed.
+define <2 x i32> @atomicrmw_add_v2i32_result(ptr %p, <2 x i32> %v) {
+; CHECK-LABEL: atomicrmw_add_v2i32_result:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movq {{.*#+}} xmm1 = mem[0],zero
+; CHECK-NEXT: .p2align 4
+; CHECK-NEXT: .LBB6_1: # %atomicrmw.start
+; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
+; CHECK-NEXT: movq %xmm1, %rax
+; CHECK-NEXT: paddd %xmm0, %xmm1
+; CHECK-NEXT: movq %xmm1, %rcx
+; CHECK-NEXT: lock cmpxchgq %rcx, (%rdi)
+; CHECK-NEXT: movq %rax, %xmm1
+; CHECK-NEXT: jne .LBB6_1
+; CHECK-NEXT: # %bb.2: # %atomicrmw.end
+; CHECK-NEXT: movdqa %xmm1, %xmm0
+; CHECK-NEXT: retq
+ %r = atomicrmw add ptr %p, <2 x i32> %v seq_cst
+ ret <2 x i32> %r
+}
+
+; An idempotent RMW (or with zero) whose result is unused is a special case
+; in X86TargetLowering::lowerIdempotentRMWIntoFencedLoad.
+define void @atomicrmw_or_zero_v2i32_unused(ptr %p) {
+; CHECK-LABEL: atomicrmw_or_zero_v2i32_unused:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movq {{.*#+}} xmm0 = mem[0],zero
+; CHECK-NEXT: .p2align 4
+; CHECK-NEXT: .LBB7_1: # %atomicrmw.start
+; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
+; CHECK-NEXT: movq %xmm0, %rax
+; CHECK-NEXT: lock cmpxchgq %rax, (%rdi)
+; CHECK-NEXT: movq %rax, %xmm0
+; CHECK-NEXT: jne .LBB7_1
+; CHECK-NEXT: # %bb.2: # %atomicrmw.end
+; CHECK-NEXT: retq
+ %r = atomicrmw or ptr %p, <2 x i32> zeroinitializer seq_cst
+ ret void
+}
+
+; Same, but the result is used.
+define <2 x i32> @atomicrmw_or_zero_v2i32_used(ptr %p) {
+; CHECK-LABEL: atomicrmw_or_zero_v2i32_used:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movq {{.*#+}} xmm0 = mem[0],zero
+; CHECK-NEXT: .p2align 4
+; CHECK-NEXT: .LBB8_1: # %atomicrmw.start
+; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
+; CHECK-NEXT: movq %xmm0, %rax
+; CHECK-NEXT: lock cmpxchgq %rax, (%rdi)
+; CHECK-NEXT: movq %rax, %xmm0
+; CHECK-NEXT: jne .LBB8_1
+; CHECK-NEXT: # %bb.2: # %atomicrmw.end
+; CHECK-NEXT: retq
+ %r = atomicrmw or ptr %p, <2 x i32> zeroinitializer seq_cst
+ ret <2 x i32> %r
+}
More information about the llvm-commits
mailing list