[clang] [llvm] [openmp] [OMPIRBuilder] Fix atomic compare `==` on `x86-64` long double (PR #226567)
Akash Manna via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 23:19:04 PST 2026
https://github.com/akash-manna-sky updated https://github.com/llvm/llvm-project/pull/226567
>From c2ac33e6fa1237f7d7fd171f1832f16194014475 Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Sat, 26 Sep 2026 00:22:57 +0530
Subject: [PATCH 1/4] [OMPIRBuilder] Perform atomic compare on padded FP types
at their allocation size
createAtomicCompare cast floating-point operands to an integer of the
type's scalar width, so long double became i80. IRBuilder then filled
the missing cmpxchg alignment from the store size of i80, which is 10,
and Align asserted. An i80 atomic would not have passed the verifier
either.
Derive the integer type from the DataLayout allocation size instead, so
x86_fp80 is handled as i128 with its 16-byte alignment, and zero-fill
the padding bits when widening. Types without padding are unaffected.
Fixes #140080
---
clang/docs/ReleaseNotes.md | 1 +
.../atomic_compare_long_double_codegen.c | 54 ++++++++++++++++++
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 56 +++++++++++++++----
3 files changed, 99 insertions(+), 12 deletions(-)
create mode 100644 clang/test/OpenMP/atomic_compare_long_double_codegen.c
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index f4a34a37aff52e..ae537970b70550 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -547,6 +547,7 @@ features cannot lower the translation-unit ABI level;
- Fixed a crash when an `asm` label names the register for a global variable of incomplete type. (#GH219746)
- Fixed an ICE hat occurred when using `__imag int/float` as lvalue in assignment. (#GH119498)
- Fixed an assertion failure in `-Wsign-compare` when a negated or complemented vector of unsigned integers was compared against a signed constant. (#GH203575)
+- Fixed an assertion failure when `#pragma omp atomic compare` was applied to a `long double` on x86-64, whose 80-bit value is stored in 16 bytes. (#GH140080)
#### Bug Fixes to Compiler Builtins
diff --git a/clang/test/OpenMP/atomic_compare_long_double_codegen.c b/clang/test/OpenMP/atomic_compare_long_double_codegen.c
new file mode 100644
index 00000000000000..410920f71e49de
--- /dev/null
+++ b/clang/test/OpenMP/atomic_compare_long_double_codegen.c
@@ -0,0 +1,54 @@
+// RUN: %clang_cc1 -verify -triple x86_64-unknown-linux-gnu -fopenmp -x c -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -verify -triple x86_64-unknown-linux-gnu -fopenmp-simd -x c -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
+// expected-no-diagnostics
+
+// GH140080: x86_fp80 occupies 16 bytes, so the cmpxchg must be on i128 with
+// align 16, not on i80 (store size 10 is not a power of two).
+
+// CHECK-LABEL: define {{.*}}void @f(
+// CHECK: [[LD_ADDR:%.*]] = alloca x86_fp80, align 16
+// CHECK: cmpxchg ptr [[LD_ADDR]], i128 0, i128 302222231531620438900736 monotonic monotonic, align 16
+// CHECK-NOT: i80
+// CHECK: ret void
+void f(long double ld) {
+#pragma omp atomic compare
+ ld = ld == 0.0L ? 1.0L : ld;
+}
+
+// CHECK-LABEL: define {{.*}}void @g(
+// CHECK: [[X:%.*]] = load ptr, ptr %x.addr, align 8
+// CHECK-NEXT: [[E:%.*]] = load x86_fp80, ptr %e.addr, align 16
+// CHECK-NEXT: [[D:%.*]] = load x86_fp80, ptr %d.addr, align 16
+// CHECK-NEXT: [[E_BITS:%.*]] = bitcast x86_fp80 [[E]] to i80
+// CHECK-NEXT: [[E_INT:%.*]] = zext i80 [[E_BITS]] to i128
+// CHECK-NEXT: [[D_BITS:%.*]] = bitcast x86_fp80 [[D]] to i80
+// CHECK-NEXT: [[D_INT:%.*]] = zext i80 [[D_BITS]] to i128
+// CHECK-NEXT: cmpxchg ptr [[X]], i128 [[E_INT]], i128 [[D_INT]] monotonic monotonic, align 16
+void g(long double *x, long double e, long double d) {
+#pragma omp atomic compare
+ *x = *x == e ? d : *x;
+}
+
+// CHECK-LABEL: define {{.*}}void @h(
+// CHECK: [[RES:%.*]] = cmpxchg ptr {{%.*}}, i128 {{%.*}}, i128 {{%.*}} monotonic monotonic, align 16
+// CHECK-NEXT: [[OLD_INT:%.*]] = extractvalue { i128, i1 } [[RES]], 0
+// CHECK-NEXT: [[OLD_BITS:%.*]] = trunc i128 [[OLD_INT]] to i80
+// CHECK-NEXT: [[OLD:%.*]] = bitcast i80 [[OLD_BITS]] to x86_fp80
+// CHECK-NEXT: store x86_fp80 [[OLD]], ptr {{%.*}}, align 16
+void h(long double *x, long double *v, long double e, long double d) {
+#pragma omp atomic compare capture
+ {
+ *v = *x;
+ *x = *x == e ? d : *x;
+ }
+}
+
+// CHECK-LABEL: define {{.*}}void @f_double(
+// CHECK: [[D_ADDR:%.*]] = alloca double, align 8
+// CHECK: cmpxchg ptr [[D_ADDR]], i64 0, i64 4607182418800017408 monotonic monotonic, align 8
+void f_double(double d) {
+#pragma omp atomic compare
+ d = d == 0.0 ? 1.0 : d;
+}
+
+// SIMD-ONLY0-NOT: {{__kmpc|__tgt}}
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 8b196c461f195b..4bf9007ac0ed3b 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -11700,6 +11700,38 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createAtomicCapture(
return Builder.saveIP();
}
+/// The integer type used to perform an atomic operation on a value of type
+/// \p FPTy: its allocation size, not its scalar width (x86_fp80 is 80 bits in
+/// 16 bytes, and an i80 atomic is not a legal power-of-two access).
+static IntegerType *getAtomicIntTypeForFP(Type *FPTy, const DataLayout &DL) {
+ return IntegerType::get(FPTy->getContext(), DL.getTypeAllocSizeInBits(FPTy));
+}
+
+/// Reinterpret \p V as \p IntTy, zero-filling any padding bits.
+static Value *castFPToAtomicInt(IRBuilderBase &Builder, const DataLayout &DL,
+ Value *V, IntegerType *IntTy) {
+ unsigned ValueBits = V->getType()->getPrimitiveSizeInBits();
+ if (ValueBits == IntTy->getBitWidth())
+ return Builder.CreateBitCast(V, IntTy);
+ // zext/trunc match the memory layout only on little-endian targets, which
+ // are the only ones with padded FP types.
+ assert(DL.isLittleEndian() && "padded FP type on a big-endian target");
+ Value *Bits =
+ Builder.CreateBitCast(V, IntegerType::get(V->getContext(), ValueBits));
+ return Builder.CreateZExt(Bits, IntTy);
+}
+
+static Value *castAtomicIntToFP(IRBuilderBase &Builder, const DataLayout &DL,
+ Value *V, Type *FPTy, const Twine &Name = "") {
+ unsigned ValueBits = FPTy->getPrimitiveSizeInBits();
+ if (ValueBits == V->getType()->getIntegerBitWidth())
+ return Builder.CreateBitCast(V, FPTy, Name);
+ assert(DL.isLittleEndian() && "padded FP type on a big-endian target");
+ Value *Bits =
+ Builder.CreateTrunc(V, IntegerType::get(V->getContext(), ValueBits));
+ return Builder.CreateBitCast(Bits, FPTy, Name);
+}
+
OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
const LocationDescription &Loc, AtomicOpValue &X, AtomicOpValue &V,
AtomicOpValue &R, Value *E, Value *D, AtomicOrdering AO,
@@ -11765,16 +11797,16 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
// br ExitBB
// ExitBB:
// phi merge
- IntegerType *IntCastTy =
- IntegerType::get(M.getContext(), X.ElemTy->getScalarSizeInBits());
- Value *EBCast = Builder.CreateBitCast(E, IntCastTy);
- Value *DBCast = Builder.CreateBitCast(D, IntCastTy);
+ const DataLayout &DL = M.getDataLayout();
+ IntegerType *IntCastTy = getAtomicIntTypeForFP(X.ElemTy, DL);
+ Value *EBCast = castFPToAtomicInt(Builder, DL, E, IntCastTy);
+ Value *DBCast = castFPToAtomicInt(Builder, DL, D, IntCastTy);
// Load X atomically.
LoadInst *XCurr = Builder.CreateLoad(IntCastTy, X.Var,
X.Var->getName() + ".atomic.load");
XCurr->setAtomic(AtomicOrdering::Monotonic);
- Value *XFP = Builder.CreateBitCast(XCurr, X.ElemTy);
+ Value *XFP = castAtomicIntToFP(Builder, DL, XCurr, X.ElemTy);
// IEEE 754: NaN != NaN, but cmpxchg would succeed if E and X have
// the same NaN bit pattern. Skip cmpxchg when either is NaN.
@@ -11854,16 +11886,16 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
Builder.SetInsertPoint(&*ExitBB->getFirstNonPHIIt());
}
- OldValue = Builder.CreateBitCast(OldIntPHI, X.ElemTy,
- X.Var->getName() + ".atomic.old.fp");
+ OldValue = castAtomicIntToFP(Builder, DL, OldIntPHI, X.ElemTy,
+ X.Var->getName() + ".atomic.old.fp");
SuccessOrFail = SuccessPHI;
} else {
AtomicCmpXchgInst *Result = nullptr;
+ const DataLayout &DL = M.getDataLayout();
if (!IsInteger) {
- IntegerType *IntCastTy =
- IntegerType::get(M.getContext(), X.ElemTy->getScalarSizeInBits());
- Value *EBCast = Builder.CreateBitCast(E, IntCastTy);
- Value *DBCast = Builder.CreateBitCast(D, IntCastTy);
+ IntegerType *IntCastTy = getAtomicIntTypeForFP(X.ElemTy, DL);
+ Value *EBCast = castFPToAtomicInt(Builder, DL, E, IntCastTy);
+ Value *DBCast = castFPToAtomicInt(Builder, DL, D, IntCastTy);
Result = Builder.CreateAtomicCmpXchg(X.Var, EBCast, DBCast,
MaybeAlign(), AO, Failure);
} else {
@@ -11875,7 +11907,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
if (V.Var) {
OldValue = Builder.CreateExtractValue(Result, /*Idxs=*/0);
if (!IsInteger)
- OldValue = Builder.CreateBitCast(OldValue, X.ElemTy);
+ OldValue = castAtomicIntToFP(Builder, DL, OldValue, X.ElemTy);
assert(OldValue->getType() == V.ElemTy &&
"OldValue and V must be of same type");
if (IsPostfixUpdate) {
>From 0831cb73a0cd1f2c74b21b57e1a0675c391524ad Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Sat, 26 Sep 2026 00:27:13 +0530
Subject: [PATCH 2/4] Reposition the release notes to avoid the conflicts
---
clang/docs/ReleaseNotes.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index ae537970b70550..fec29a1440581d 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -533,6 +533,7 @@ features cannot lower the translation-unit ABI level;
- Fixed USR generation for declarations whose signature mentions a class-type
non-type template parameter. (#GH212351)
- Fixed an assertion caused by Microsoft integer literals exceeding the maximum value. (#GH212504)
+- Fixed an assertion failure when `#pragma omp atomic compare` was applied to a `long double` on x86-64, whose 80-bit value is stored in 16 bytes. (#GH140080)
- Fixed an assertion failure when a value of a Unicode character type (`char8_t`, `char16_t`, `char32_t`) was implicitly splatted to a vector of the same element type, e.g. when comparing an `ext_vector_type` of `char32_t` with one of its elements. (#GH202317)
- Fixed a crash when checking scalar type with excess braces. (#GH69213), (#GH137845), (#GH198767), (#GH207566), (#GH106180)
- Fixed an assertion crash when instantiating a nested requirement with an invalid constraint. (#GH213575)
@@ -547,7 +548,6 @@ features cannot lower the translation-unit ABI level;
- Fixed a crash when an `asm` label names the register for a global variable of incomplete type. (#GH219746)
- Fixed an ICE hat occurred when using `__imag int/float` as lvalue in assignment. (#GH119498)
- Fixed an assertion failure in `-Wsign-compare` when a negated or complemented vector of unsigned integers was compared against a signed constant. (#GH203575)
-- Fixed an assertion failure when `#pragma omp atomic compare` was applied to a `long double` on x86-64, whose 80-bit value is stored in 16 bytes. (#GH140080)
#### Bug Fixes to Compiler Builtins
>From 11dc9fc541036b5f747d03513384f4d72c08116e Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Fri, 6 Nov 2026 12:39:00 +0530
Subject: [PATCH 3/4] [OMPIRBuilder] Ignore padding and honor alignment in
atomic compare ==
Review follow-up: for a floating-point type whose storage is wider than
its value (x86_fp80), load x, compare only the value bits and use the
loaded pattern as the cmpxchg expected value, retrying if x changed, so
the padding bytes never take part in the comparison. Carry the alignment
of x, v and r in AtomicOpValue so an under-aligned x is not given the
natural alignment of its type. Non-padded types keep their previous IR.
---
clang/docs/ReleaseNotes.md | 2 +-
clang/lib/CodeGen/CGStmtOpenMP.cpp | 8 +-
.../atomic_compare_long_double_codegen.c | 57 +++++-
.../llvm/Frontend/OpenMP/OMPIRBuilder.h | 3 +
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 190 ++++++++++++------
.../Frontend/OpenMPIRBuilderTest.cpp | 70 +++++++
.../atomic/omp-atomic-compare-long-double.c | 89 ++++++++
7 files changed, 339 insertions(+), 80 deletions(-)
create mode 100644 openmp/runtime/test/atomic/omp-atomic-compare-long-double.c
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 52093d4b07b0de..7f56256a3e2981 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -787,7 +787,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Fixed USR generation for declarations whose signature mentions a class-type
non-type template parameter. (#GH212351)
- Fixed an assertion caused by Microsoft integer literals exceeding the maximum value. (#GH212504)
-- Fixed an assertion failure when `#pragma omp atomic compare` was applied to a `long double` on x86-64, whose 80-bit value is stored in 16 bytes. (#GH140080)
+- Fixed an assertion failure when `#pragma omp atomic compare` used an equality comparison on a `long double` on x86-64, whose 80-bit value is stored in 16 bytes. The padding bytes no longer take part in the comparison, and the atomic operation uses the alignment of `x` instead of assuming the natural alignment of its type. (#GH140080)
- Fixed an assertion failure when a value of a Unicode character type (`char8_t`, `char16_t`, `char32_t`) was implicitly splatted to a vector of the same element type, e.g. when comparing an `ext_vector_type` of `char32_t` with one of its elements. (#GH202317)
- Fixed a crash when checking scalar type with excess braces. (#GH69213), (#GH137845), (#GH198767), (#GH207566), (#GH106180)
- Fixed an assertion failure when a global variable in a non-default address space, such as one declared with `__seg_gs`, is mapped into an OpenMP `target` region. (#GH140069)
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index e63957df3e4069..560eed649f2176 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -7355,21 +7355,23 @@ static void emitOMPAtomicCompareExpr(
llvm::OpenMPIRBuilder::AtomicOpValue XOpVal{
XAddr.emitRawPointer(CGF), XAddr.getElementType(),
X->getType()->hasSignedIntegerRepresentation(),
- X->getType().isVolatileQualified()};
+ X->getType().isVolatileQualified(), XAddr.getAlignment().getAsAlign()};
llvm::OpenMPIRBuilder::AtomicOpValue VOpVal, ROpVal;
if (V) {
LValue LV = CGF.EmitLValue(V);
Address Addr = LV.getAddress();
VOpVal = {Addr.emitRawPointer(CGF), Addr.getElementType(),
V->getType()->hasSignedIntegerRepresentation(),
- V->getType().isVolatileQualified()};
+ V->getType().isVolatileQualified(),
+ Addr.getAlignment().getAsAlign()};
}
if (R) {
LValue LV = CGF.EmitLValue(R);
Address Addr = LV.getAddress();
ROpVal = {Addr.emitRawPointer(CGF), Addr.getElementType(),
R->getType()->hasSignedIntegerRepresentation(),
- R->getType().isVolatileQualified()};
+ R->getType().isVolatileQualified(),
+ Addr.getAlignment().getAsAlign()};
}
if (FailAO == llvm::AtomicOrdering::NotAtomic) {
diff --git a/clang/test/OpenMP/atomic_compare_long_double_codegen.c b/clang/test/OpenMP/atomic_compare_long_double_codegen.c
index 410920f71e49de..be8120e488b96e 100644
--- a/clang/test/OpenMP/atomic_compare_long_double_codegen.c
+++ b/clang/test/OpenMP/atomic_compare_long_double_codegen.c
@@ -1,15 +1,29 @@
// RUN: %clang_cc1 -verify -triple x86_64-unknown-linux-gnu -fopenmp -x c -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -verify -triple x86_64-unknown-linux-gnu -fopenmp -fopenmp-version=52 -x c -emit-llvm %s -o - | FileCheck %s
// RUN: %clang_cc1 -verify -triple x86_64-unknown-linux-gnu -fopenmp-simd -x c -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
// expected-no-diagnostics
-// GH140080: x86_fp80 occupies 16 bytes, so the cmpxchg must be on i128 with
-// align 16, not on i80 (store size 10 is not a power of two).
+// GH140080: x86_fp80 holds 80 value bits in 16 bytes, so only the value bits
+// are compared and the loaded pattern is the expected value of the cmpxchg.
// CHECK-LABEL: define {{.*}}void @f(
// CHECK: [[LD_ADDR:%.*]] = alloca x86_fp80, align 16
-// CHECK: cmpxchg ptr [[LD_ADDR]], i128 0, i128 302222231531620438900736 monotonic monotonic, align 16
-// CHECK-NOT: i80
-// CHECK: ret void
+// CHECK: [[LOAD:%.*]] = load atomic i128, ptr [[LD_ADDR]] monotonic, align 16
+// CHECK-NEXT: br label %[[CONT:.*]]
+// CHECK: [[CONT]]:
+// CHECK-NEXT: [[CURR:%.*]] = phi i128 [ [[LOAD]], %entry ], [ [[CAS_OLD:%[0-9]+]], %[[CAS:[^ ]+]] ]
+// CHECK-NEXT: [[BITS:%.*]] = trunc i128 [[CURR]] to i80
+// CHECK-NEXT: [[EQ:%.*]] = icmp eq i80 [[BITS]], 0
+// CHECK-NEXT: br i1 [[EQ]], label %[[CAS]], label %[[EXIT:.*]]
+// CHECK: [[CAS]]:
+// CHECK-NEXT: [[RES:%.*]] = cmpxchg ptr [[LD_ADDR]], i128 [[CURR]], i128 302222231531620438900736 monotonic monotonic, align 16
+// CHECK-NEXT: [[CAS_OLD]] = extractvalue { i128, i1 } [[RES]], 0
+// CHECK-NEXT: [[OK:%.*]] = extractvalue { i128, i1 } [[RES]], 1
+// CHECK-NEXT: br i1 [[OK]], label %[[EXIT]], label %[[CONT]]
+// CHECK: [[EXIT]]:
+// CHECK-NEXT: phi i128 [ [[CURR]], %[[CONT]] ], [ [[CAS_OLD]], %[[CAS]] ]
+// CHECK-NEXT: phi i1 [ false, %[[CONT]] ], [ true, %[[CAS]] ]
+// CHECK-NEXT: ret void
void f(long double ld) {
#pragma omp atomic compare
ld = ld == 0.0L ? 1.0L : ld;
@@ -20,21 +34,27 @@ void f(long double ld) {
// CHECK-NEXT: [[E:%.*]] = load x86_fp80, ptr %e.addr, align 16
// CHECK-NEXT: [[D:%.*]] = load x86_fp80, ptr %d.addr, align 16
// CHECK-NEXT: [[E_BITS:%.*]] = bitcast x86_fp80 [[E]] to i80
-// CHECK-NEXT: [[E_INT:%.*]] = zext i80 [[E_BITS]] to i128
// CHECK-NEXT: [[D_BITS:%.*]] = bitcast x86_fp80 [[D]] to i80
// CHECK-NEXT: [[D_INT:%.*]] = zext i80 [[D_BITS]] to i128
-// CHECK-NEXT: cmpxchg ptr [[X]], i128 [[E_INT]], i128 [[D_INT]] monotonic monotonic, align 16
+// CHECK-NEXT: [[LOAD:%.*]] = load atomic i128, ptr [[X]] monotonic, align 16
+// CHECK: [[CURR:%.*]] = phi i128 [ [[LOAD]], %entry ], [ [[CAS_OLD:%[0-9]+]], %{{.*}} ]
+// CHECK-NEXT: [[BITS:%.*]] = trunc i128 [[CURR]] to i80
+// CHECK-NEXT: icmp eq i80 [[BITS]], [[E_BITS]]
+// CHECK: [[RES:%.*]] = cmpxchg ptr [[X]], i128 [[CURR]], i128 [[D_INT]] monotonic monotonic, align 16
+// CHECK-NEXT: [[CAS_OLD]] = extractvalue { i128, i1 } [[RES]], 0
void g(long double *x, long double e, long double d) {
#pragma omp atomic compare
*x = *x == e ? d : *x;
}
// CHECK-LABEL: define {{.*}}void @h(
-// CHECK: [[RES:%.*]] = cmpxchg ptr {{%.*}}, i128 {{%.*}}, i128 {{%.*}} monotonic monotonic, align 16
-// CHECK-NEXT: [[OLD_INT:%.*]] = extractvalue { i128, i1 } [[RES]], 0
+// CHECK: [[V:%.*]] = load ptr, ptr %v.addr, align 8
+// CHECK: cmpxchg ptr {{%.*}}, i128 {{%.*}}, i128 {{%.*}} monotonic monotonic, align 16
+// CHECK: [[OLD_INT:%.*]] = phi i128
+// CHECK-NEXT: phi i1
// CHECK-NEXT: [[OLD_BITS:%.*]] = trunc i128 [[OLD_INT]] to i80
// CHECK-NEXT: [[OLD:%.*]] = bitcast i80 [[OLD_BITS]] to x86_fp80
-// CHECK-NEXT: store x86_fp80 [[OLD]], ptr {{%.*}}, align 16
+// CHECK-NEXT: store x86_fp80 [[OLD]], ptr [[V]], align 16
void h(long double *x, long double *v, long double e, long double d) {
#pragma omp atomic compare capture
{
@@ -43,9 +63,24 @@ void h(long double *x, long double *v, long double e, long double d) {
}
}
+struct __attribute__((packed)) P {
+ char c;
+ long double x;
+};
+
+// CHECK-LABEL: define {{.*}}void @p(
+// CHECK: [[X:%.*]] = getelementptr inbounds {{.*}}%struct.P, ptr {{%.*}}, i32 0, i32 1
+// CHECK: load atomic i128, ptr [[X]] monotonic, align 1
+// CHECK: cmpxchg ptr [[X]], i128 {{%.*}}, i128 {{%.*}} monotonic monotonic, align 1
+void p(struct P *s, long double e, long double d) {
+#pragma omp atomic compare
+ s->x = s->x == e ? d : s->x;
+}
+
// CHECK-LABEL: define {{.*}}void @f_double(
// CHECK: [[D_ADDR:%.*]] = alloca double, align 8
-// CHECK: cmpxchg ptr [[D_ADDR]], i64 0, i64 4607182418800017408 monotonic monotonic, align 8
+// CHECK-NEXT: store double
+// CHECK-NEXT: cmpxchg ptr [[D_ADDR]], i64 0, i64 4607182418800017408 monotonic monotonic, align 8
void f_double(double d) {
#pragma omp atomic compare
d = d == 0.0 ? 1.0 : d;
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
index a1ed0d2b065759..9fb64f4e027abc 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -4060,6 +4060,9 @@ class OpenMPIRBuilder {
Type *ElemTy = nullptr;
bool IsSigned = false;
bool IsVolatile = false;
+ /// Alignment of the memory \p Var points to. When unset, the natural
+ /// alignment of \p ElemTy is assumed.
+ MaybeAlign Alignment;
};
/// Emit atomic Read for : V = X --- Only Scalar data types.
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 8f997504d47074..70e443f5c44440 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -11781,38 +11781,6 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createAtomicCapture(
return Builder.saveIP();
}
-/// The integer type used to perform an atomic operation on a value of type
-/// \p FPTy: its allocation size, not its scalar width (x86_fp80 is 80 bits in
-/// 16 bytes, and an i80 atomic is not a legal power-of-two access).
-static IntegerType *getAtomicIntTypeForFP(Type *FPTy, const DataLayout &DL) {
- return IntegerType::get(FPTy->getContext(), DL.getTypeAllocSizeInBits(FPTy));
-}
-
-/// Reinterpret \p V as \p IntTy, zero-filling any padding bits.
-static Value *castFPToAtomicInt(IRBuilderBase &Builder, const DataLayout &DL,
- Value *V, IntegerType *IntTy) {
- unsigned ValueBits = V->getType()->getPrimitiveSizeInBits();
- if (ValueBits == IntTy->getBitWidth())
- return Builder.CreateBitCast(V, IntTy);
- // zext/trunc match the memory layout only on little-endian targets, which
- // are the only ones with padded FP types.
- assert(DL.isLittleEndian() && "padded FP type on a big-endian target");
- Value *Bits =
- Builder.CreateBitCast(V, IntegerType::get(V->getContext(), ValueBits));
- return Builder.CreateZExt(Bits, IntTy);
-}
-
-static Value *castAtomicIntToFP(IRBuilderBase &Builder, const DataLayout &DL,
- Value *V, Type *FPTy, const Twine &Name = "") {
- unsigned ValueBits = FPTy->getPrimitiveSizeInBits();
- if (ValueBits == V->getType()->getIntegerBitWidth())
- return Builder.CreateBitCast(V, FPTy, Name);
- assert(DL.isLittleEndian() && "padded FP type on a big-endian target");
- Value *Bits =
- Builder.CreateTrunc(V, IntegerType::get(V->getContext(), ValueBits));
- return Builder.CreateBitCast(Bits, FPTy, Name);
-}
-
OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
const LocationDescription &Loc, AtomicOpValue &X, AtomicOpValue &V,
AtomicOpValue &R, Value *E, Value *D, AtomicOrdering AO,
@@ -11842,6 +11810,12 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
}
bool IsInteger = E->getType()->isIntegerTy();
+ // x86_fp80 holds 80 value bits in 16 bytes; the padding must not take part
+ // in the comparison.
+ const DataLayout &DL = M.getDataLayout();
+ bool HasPadding = X.ElemTy->isFloatingPointTy() &&
+ X.ElemTy->getPrimitiveSizeInBits().getFixedValue() !=
+ DL.getTypeAllocSizeInBits(X.ElemTy).getFixedValue();
if (Op == OMPAtomicCompareOp::EQ) {
// OldValue and SuccessOrFail are set below and used in the shared V.Var /
@@ -11849,7 +11823,86 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
Value *OldValue = nullptr;
Value *SuccessOrFail = nullptr;
- if (!IsInteger && HandleFPNegZero) {
+ if (HasPadding) {
+ // Load x, compare its value bits only, and use the loaded pattern as the
+ // expected value of the cmpxchg; retry if x changed in between.
+ assert(DL.isLittleEndian() && "padded FP type on a big-endian target");
+ LLVMContext &Ctx = M.getContext();
+ IntegerType *IntCastTy = IntegerType::get(
+ Ctx, DL.getTypeAllocSizeInBits(X.ElemTy).getFixedValue());
+ IntegerType *ValueIntTy = IntegerType::get(
+ Ctx, X.ElemTy->getPrimitiveSizeInBits().getFixedValue());
+ Value *EBits = Builder.CreateBitCast(E, ValueIntTy);
+ Value *DInt =
+ Builder.CreateZExt(Builder.CreateBitCast(D, ValueIntTy), IntCastTy);
+
+ LoadInst *XLoad = Builder.CreateAlignedLoad(
+ IntCastTy, X.Var, X.Alignment, X.Var->getName() + ".atomic.load");
+ XLoad->setAtomic(Failure);
+
+ BasicBlock *CurBB = Builder.GetInsertBlock();
+ Function *F = CurBB->getParent();
+ Instruction *CurBBTI = CurBB->getTerminatorOrNull();
+ CurBBTI = CurBBTI ? CurBBTI : Builder.CreateUnreachable();
+ BasicBlock *ExitBB =
+ CurBB->splitBasicBlock(CurBBTI, X.Var->getName() + ".atomic.exit");
+ BasicBlock *ContBB =
+ BasicBlock::Create(Ctx, X.Var->getName() + ".atomic.cont", F, ExitBB);
+ BasicBlock *CasBB =
+ BasicBlock::Create(Ctx, X.Var->getName() + ".atomic.cas", F, ExitBB);
+
+ CurBB->getTerminator()->eraseFromParent();
+ Builder.SetInsertPoint(CurBB);
+ Builder.CreateBr(ContBB);
+
+ Builder.SetInsertPoint(ContBB);
+ PHINode *XCurr =
+ Builder.CreatePHI(IntCastTy, 2, X.Var->getName() + ".atomic.curr");
+ XCurr->addIncoming(XLoad, CurBB);
+ Value *XBits = Builder.CreateTrunc(XCurr, ValueIntTy);
+ Value *Equal;
+ if (HandleFPNegZero) {
+ // Compare as values: -0.0 == +0.0 and NaN != NaN.
+ Value *XFP = Builder.CreateBitCast(XBits, X.ElemTy);
+ Equal = Builder.CreateFCmpOEQ(XFP, E, "atomic.eq");
+ } else {
+ // OpenMP: == compares the bits of the values, as with memcmp.
+ Equal = Builder.CreateICmpEQ(XBits, EBits, "atomic.eq");
+ }
+ Builder.CreateCondBr(Equal, CasBB, ExitBB);
+
+ Builder.SetInsertPoint(CasBB);
+ AtomicCmpXchgInst *Result = Builder.CreateAtomicCmpXchg(
+ X.Var, XCurr, DInt, X.Alignment, AO, Failure);
+ Result->setWeak(IsWeak);
+ Value *CasOld = Builder.CreateExtractValue(Result, /*Idxs=*/0);
+ Value *CasOk = Builder.CreateExtractValue(Result, /*Idxs=*/1);
+ Builder.CreateCondBr(CasOk, ExitBB, ContBB);
+ XCurr->addIncoming(CasOld, CasBB);
+
+ Builder.SetInsertPoint(ExitBB->begin());
+ PHINode *OldIntPHI =
+ Builder.CreatePHI(IntCastTy, 2, X.Var->getName() + ".atomic.old");
+ OldIntPHI->addIncoming(XCurr, ContBB);
+ OldIntPHI->addIncoming(CasOld, CasBB);
+ PHINode *SuccessPHI = Builder.CreatePHI(Builder.getInt1Ty(), 2,
+ X.Var->getName() + ".atomic.ok");
+ SuccessPHI->addIncoming(Builder.getFalse(), ContBB);
+ SuccessPHI->addIncoming(Builder.getTrue(), CasBB);
+
+ if (isa<UnreachableInst>(ExitBB->getTerminator())) {
+ CurBBTI->eraseFromParent();
+ Builder.SetInsertPoint(ExitBB);
+ } else {
+ Builder.SetInsertPoint(&*ExitBB->getFirstNonPHIIt());
+ }
+
+ if (V.Var)
+ OldValue = Builder.CreateBitCast(
+ Builder.CreateTrunc(OldIntPHI, ValueIntTy), X.ElemTy,
+ X.Var->getName() + ".atomic.old.fp");
+ SuccessOrFail = SuccessPHI;
+ } else if (!IsInteger && HandleFPNegZero) {
// IEEE 754 special cases for cmpxchg (which is bitwise):
// 1. -0.0 == +0.0 but they have different bit patterns.
// 2. NaN != NaN but identical NaN bit patterns would match.
@@ -11878,16 +11931,16 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
// br ExitBB
// ExitBB:
// phi merge
- const DataLayout &DL = M.getDataLayout();
- IntegerType *IntCastTy = getAtomicIntTypeForFP(X.ElemTy, DL);
- Value *EBCast = castFPToAtomicInt(Builder, DL, E, IntCastTy);
- Value *DBCast = castFPToAtomicInt(Builder, DL, D, IntCastTy);
+ IntegerType *IntCastTy =
+ IntegerType::get(M.getContext(), X.ElemTy->getScalarSizeInBits());
+ Value *EBCast = Builder.CreateBitCast(E, IntCastTy);
+ Value *DBCast = Builder.CreateBitCast(D, IntCastTy);
// Load X atomically.
- LoadInst *XCurr = Builder.CreateLoad(IntCastTy, X.Var,
- X.Var->getName() + ".atomic.load");
+ LoadInst *XCurr = Builder.CreateAlignedLoad(
+ IntCastTy, X.Var, X.Alignment, X.Var->getName() + ".atomic.load");
XCurr->setAtomic(AtomicOrdering::Monotonic);
- Value *XFP = castAtomicIntToFP(Builder, DL, XCurr, X.ElemTy);
+ Value *XFP = Builder.CreateBitCast(XCurr, X.ElemTy);
// IEEE 754: NaN != NaN, but cmpxchg would succeed if E and X have
// the same NaN bit pattern. Skip cmpxchg when either is NaN.
@@ -11932,7 +11985,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
// ZeroBB: cmpxchg with X's loaded bit-pattern.
Builder.SetInsertPoint(ZeroBB);
AtomicCmpXchgInst *ResZero = Builder.CreateAtomicCmpXchg(
- X.Var, XCurr, DBCast, MaybeAlign(), AO, Failure);
+ X.Var, XCurr, DBCast, X.Alignment, AO, Failure);
ResZero->setWeak(IsWeak);
Value *OldZero = Builder.CreateExtractValue(ResZero, /*Idxs=*/0);
Value *OkZero = Builder.CreateExtractValue(ResZero, /*Idxs=*/1);
@@ -11941,7 +11994,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
// NormalBB: original bitwise cmpxchg.
Builder.SetInsertPoint(NormalBB);
AtomicCmpXchgInst *ResNormal = Builder.CreateAtomicCmpXchg(
- X.Var, EBCast, DBCast, MaybeAlign(), AO, Failure);
+ X.Var, EBCast, DBCast, X.Alignment, AO, Failure);
ResNormal->setWeak(IsWeak);
Value *OldNormal = Builder.CreateExtractValue(ResNormal, /*Idxs=*/0);
Value *OkNormal = Builder.CreateExtractValue(ResNormal, /*Idxs=*/1);
@@ -11967,32 +12020,33 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
Builder.SetInsertPoint(&*ExitBB->getFirstNonPHIIt());
}
- OldValue = castAtomicIntToFP(Builder, DL, OldIntPHI, X.ElemTy,
- X.Var->getName() + ".atomic.old.fp");
+ OldValue = Builder.CreateBitCast(OldIntPHI, X.ElemTy,
+ X.Var->getName() + ".atomic.old.fp");
SuccessOrFail = SuccessPHI;
} else {
AtomicCmpXchgInst *Result = nullptr;
- const DataLayout &DL = M.getDataLayout();
if (!IsInteger) {
- IntegerType *IntCastTy = getAtomicIntTypeForFP(X.ElemTy, DL);
- Value *EBCast = castFPToAtomicInt(Builder, DL, E, IntCastTy);
- Value *DBCast = castFPToAtomicInt(Builder, DL, D, IntCastTy);
- Result = Builder.CreateAtomicCmpXchg(X.Var, EBCast, DBCast,
- MaybeAlign(), AO, Failure);
+ IntegerType *IntCastTy =
+ IntegerType::get(M.getContext(), X.ElemTy->getScalarSizeInBits());
+ Value *EBCast = Builder.CreateBitCast(E, IntCastTy);
+ Value *DBCast = Builder.CreateBitCast(D, IntCastTy);
+ Result = Builder.CreateAtomicCmpXchg(X.Var, EBCast, DBCast, X.Alignment,
+ AO, Failure);
} else {
Result =
- Builder.CreateAtomicCmpXchg(X.Var, E, D, MaybeAlign(), AO, Failure);
+ Builder.CreateAtomicCmpXchg(X.Var, E, D, X.Alignment, AO, Failure);
}
Result->setWeak(IsWeak);
if (V.Var) {
OldValue = Builder.CreateExtractValue(Result, /*Idxs=*/0);
if (!IsInteger)
- OldValue = castAtomicIntToFP(Builder, DL, OldValue, X.ElemTy);
+ OldValue = Builder.CreateBitCast(OldValue, X.ElemTy);
assert(OldValue->getType() == V.ElemTy &&
"OldValue and V must be of same type");
if (IsPostfixUpdate) {
- Builder.CreateStore(OldValue, V.Var, V.IsVolatile);
+ Builder.CreateAlignedStore(OldValue, V.Var, V.Alignment,
+ V.IsVolatile);
} else {
SuccessOrFail = Builder.CreateExtractValue(Result, /*Idxs=*/1);
if (IsFailOnly) {
@@ -12009,7 +12063,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
Builder.CreateCondBr(SuccessOrFail, ExitBB, ContBB);
Builder.SetInsertPoint(ContBB);
- Builder.CreateStore(OldValue, V.Var);
+ Builder.CreateAlignedStore(OldValue, V.Var, V.Alignment);
Builder.CreateBr(ExitBB);
if (UnreachableInst *ExitTI =
@@ -12022,7 +12076,8 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
} else {
Value *CapturedValue =
Builder.CreateSelect(SuccessOrFail, E, OldValue);
- Builder.CreateStore(CapturedValue, V.Var, V.IsVolatile);
+ Builder.CreateAlignedStore(CapturedValue, V.Var, V.Alignment,
+ V.IsVolatile);
}
}
}
@@ -12037,18 +12092,20 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
Value *ResultCast =
R.IsSigned ? Builder.CreateSExt(SuccessFailureVal, R.ElemTy)
: Builder.CreateZExt(SuccessFailureVal, R.ElemTy);
- Builder.CreateStore(ResultCast, R.Var, R.IsVolatile);
+ Builder.CreateAlignedStore(ResultCast, R.Var, R.Alignment,
+ R.IsVolatile);
}
}
- // For the HandleFPNegZero path, handle V.Var and R.Var using the
- // pre-computed OldValue and SuccessOrFail.
- if (HandleFPNegZero && !IsInteger) {
+ // For the HandleFPNegZero and padded paths, handle V.Var and R.Var using
+ // the pre-computed OldValue and SuccessOrFail.
+ if (!IsInteger && (HandleFPNegZero || HasPadding)) {
if (V.Var) {
assert(OldValue->getType() == V.ElemTy &&
"OldValue and V must be of same type");
if (IsPostfixUpdate) {
- Builder.CreateStore(OldValue, V.Var, V.IsVolatile);
+ Builder.CreateAlignedStore(OldValue, V.Var, V.Alignment,
+ V.IsVolatile);
} else {
if (IsFailOnly) {
BasicBlock *CurBB = Builder.GetInsertBlock();
@@ -12064,7 +12121,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
Builder.CreateCondBr(SuccessOrFail, ExitBB, ContBB);
Builder.SetInsertPoint(ContBB);
- Builder.CreateStore(OldValue, V.Var);
+ Builder.CreateAlignedStore(OldValue, V.Var, V.Alignment);
Builder.CreateBr(ExitBB);
if (UnreachableInst *ExitTI =
@@ -12077,7 +12134,8 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
} else {
Value *CapturedValue =
Builder.CreateSelect(SuccessOrFail, E, OldValue);
- Builder.CreateStore(CapturedValue, V.Var, V.IsVolatile);
+ Builder.CreateAlignedStore(CapturedValue, V.Var, V.Alignment,
+ V.IsVolatile);
}
}
}
@@ -12090,7 +12148,8 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
Value *ResultCast = R.IsSigned
? Builder.CreateSExt(SuccessOrFail, R.ElemTy)
: Builder.CreateZExt(SuccessOrFail, R.ElemTy);
- Builder.CreateStore(ResultCast, R.Var, R.IsVolatile);
+ Builder.CreateAlignedStore(ResultCast, R.Var, R.Alignment,
+ R.IsVolatile);
}
}
} else {
@@ -12134,7 +12193,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
}
AtomicRMWInst *OldValue =
- Builder.CreateAtomicRMW(NewOp, X.Var, E, MaybeAlign(), AO);
+ Builder.CreateAtomicRMW(NewOp, X.Var, E, X.Alignment, AO);
if (V.Var) {
Value *CapturedValue = nullptr;
if (IsPostfixUpdate) {
@@ -12166,7 +12225,8 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
Value *NonAtomicCmp = Builder.CreateCmp(Pred, OldValue, E);
CapturedValue = Builder.CreateSelect(NonAtomicCmp, E, OldValue);
}
- Builder.CreateStore(CapturedValue, V.Var, V.IsVolatile);
+ Builder.CreateAlignedStore(CapturedValue, V.Var, V.Alignment,
+ V.IsVolatile);
}
}
diff --git a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
index 3a8f3198370e80..25205dc7c19983 100644
--- a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -4528,6 +4528,76 @@ TEST_F(OpenMPIRBuilderTest, OMPAtomicCompareCapture) {
EXPECT_FALSE(verifyModule(*M, &errs()));
}
+TEST_F(OpenMPIRBuilderTest, OMPAtomicComparePaddedFPUnderAligned) {
+ // x86_fp80 is 80 value bits in 16 bytes; x is aligned to 1 here.
+ OpenMPIRBuilder OMPBuilder(*M);
+ OMPBuilder.Config.IsTargetDevice = false;
+ OMPBuilder.initialize();
+ F->setName("func");
+ IRBuilder<> Builder(BB);
+
+ LLVMContext &Ctx = M->getContext();
+ Type *FP80Ty = Type::getX86_FP80Ty(Ctx);
+ IntegerType *Int128 = Type::getInt128Ty(Ctx);
+ ASSERT_EQ(M->getDataLayout().getTypeAllocSizeInBits(FP80Ty).getFixedValue(),
+ 128u);
+
+ AllocaInst *XVal = Builder.CreateAlloca(FP80Ty);
+ XVal->setName("x");
+ XVal->setAlignment(Align(1));
+ AllocaInst *VVal = Builder.CreateAlloca(FP80Ty);
+ VVal->setName("v");
+
+ OpenMPIRBuilder::AtomicOpValue X = {XVal, FP80Ty, false, false, Align(1)};
+ OpenMPIRBuilder::AtomicOpValue V = {VVal, FP80Ty, false, false};
+ OpenMPIRBuilder::AtomicOpValue NoR = {nullptr, nullptr, false, false};
+ Constant *E = ConstantFP::get(FP80Ty, 1.0);
+ Constant *D = ConstantFP::get(FP80Ty, 2.0);
+
+ // { v = x; x = x == e ? d : x; }
+ bool SavedHandleFPNegZero = OMPBuilder.setHandleFPNegZero(true);
+ Builder.restoreIP(OMPBuilder.createAtomicCompare(
+ Builder, X, V, NoR, E, D, AtomicOrdering::Monotonic,
+ OMPAtomicCompareOp::EQ, /*IsXBinopExpr=*/true,
+ /*IsPostfixUpdate=*/true, /*IsFailOnly=*/false));
+ OMPBuilder.setHandleFPNegZero(SavedHandleFPNegZero);
+
+ Builder.CreateRetVoid();
+ OMPBuilder.finalize();
+ EXPECT_FALSE(verifyModule(*M, &errs()));
+
+ unsigned NumAtomicLoads = 0, NumCmpXchgs = 0, NumFCmps = 0;
+ StoreInst *VStore = nullptr;
+ for (BasicBlock &Block : *F) {
+ for (Instruction &I : Block) {
+ if (auto *LI = dyn_cast<LoadInst>(&I)) {
+ if (!LI->isAtomic())
+ continue;
+ ++NumAtomicLoads;
+ EXPECT_EQ(LI->getPointerOperand(), XVal);
+ EXPECT_EQ(LI->getType(), Int128);
+ EXPECT_EQ(LI->getAlign().value(), 1u);
+ } else if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
+ ++NumCmpXchgs;
+ EXPECT_EQ(CXI->getPointerOperand(), XVal);
+ EXPECT_EQ(CXI->getNewValOperand()->getType(), Int128);
+ EXPECT_EQ(CXI->getAlign().value(), 1u);
+ } else if (auto *FCI = dyn_cast<FCmpInst>(&I)) {
+ ++NumFCmps;
+ EXPECT_EQ(FCI->getPredicate(), CmpInst::FCMP_OEQ);
+ } else if (auto *SI = dyn_cast<StoreInst>(&I)) {
+ if (SI->getPointerOperand() == VVal)
+ VStore = SI;
+ }
+ }
+ }
+ EXPECT_EQ(NumAtomicLoads, 1u);
+ EXPECT_EQ(NumCmpXchgs, 1u);
+ EXPECT_EQ(NumFCmps, 1u);
+ ASSERT_NE(VStore, nullptr);
+ EXPECT_EQ(VStore->getValueOperand()->getType(), FP80Ty);
+}
+
TEST_F(OpenMPIRBuilderTest, OMPAtomicRWStructType) {
// Test for issue #165184: atomic read/write on struct types should use
// element type size, not pointer size.
diff --git a/openmp/runtime/test/atomic/omp-atomic-compare-long-double.c b/openmp/runtime/test/atomic/omp-atomic-compare-long-double.c
new file mode 100644
index 00000000000000..0ffada8c210eb5
--- /dev/null
+++ b/openmp/runtime/test/atomic/omp-atomic-compare-long-double.c
@@ -0,0 +1,89 @@
+// `omp atomic compare` with == on an x86-64 long double must ignore the six
+// padding bytes of its 16-byte storage, and must not treat -0.0 as +0.0.
+//
+// RUN: %libomp-compile -mcx16 && %libomp-run
+// RUN: %libomp-compile -mcx16 -fopenmp-version=52 && %libomp-run
+// UNSUPPORTED: gcc
+// REQUIRES: x86_64-target-arch
+
+#include <stdio.h>
+
+#if defined(__x86_64__) && __LDBL_MANT_DIG__ == 64 && \
+ __SIZEOF_LONG_DOUBLE__ == 16
+#define PADDED_LONG_DOUBLE 1
+#else
+#define PADDED_LONG_DOUBLE 0
+#endif
+
+#if PADDED_LONG_DOUBLE
+static void set_padding(long double *p, unsigned char value) {
+ unsigned char *bytes = (unsigned char *)p;
+ for (int i = 10; i < 16; ++i)
+ bytes[i] = value;
+}
+
+static int update(unsigned char padding) {
+ long double x = 1.0L, v;
+ set_padding(&x, padding);
+#pragma omp atomic compare
+ x = x == 1.0L ? 2.0L : x;
+#pragma omp atomic read
+ v = x;
+ return v == 2.0L;
+}
+
+static int capture(unsigned char padding) {
+ long double x = 1.0L, v = 0.0L, w;
+ set_padding(&x, padding);
+#pragma omp atomic compare capture
+ {
+ v = x;
+ x = x == 1.0L ? 2.0L : x;
+ }
+#pragma omp atomic read
+ w = x;
+ return v == 1.0L && w == 2.0L;
+}
+
+static int negative_zero(void) {
+ long double x = 0.0L, e = -0.0L, v;
+#pragma omp atomic compare
+ x = x == e ? 5.0L : x;
+#pragma omp atomic read
+ v = x;
+ if (v != 0.0L)
+ return 0;
+#pragma omp atomic compare
+ x = x == 0.0L ? 5.0L : x;
+#pragma omp atomic read
+ v = x;
+ return v == 5.0L;
+}
+#endif
+
+int main(void) {
+ int ret = 0;
+#if PADDED_LONG_DOUBLE
+ if (!update(0x00)) {
+ printf("Error: compare failed with zero padding\n");
+ ret++;
+ }
+ if (!update(0xa5)) {
+ printf("Error: compare failed with nonzero padding\n");
+ ret++;
+ }
+ if (!capture(0xa5)) {
+ printf("Error: compare capture failed with nonzero padding\n");
+ ret++;
+ }
+ if (!negative_zero()) {
+ printf("Error: -0.0 compared equal to +0.0\n");
+ ret++;
+ }
+ if (ret == 0)
+ printf("passed\n");
+#else
+ printf("Unsupported long double layout, skipping test...\n");
+#endif
+ return ret;
+}
>From 50745eb900759c5b4627949778e53f4d19958906 Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Fri, 6 Nov 2026 12:48:27 +0530
Subject: [PATCH 4/4] Fix the formatting.
---
openmp/runtime/test/atomic/omp-atomic-compare-long-double.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/openmp/runtime/test/atomic/omp-atomic-compare-long-double.c b/openmp/runtime/test/atomic/omp-atomic-compare-long-double.c
index 0ffada8c210eb5..de313577f8669c 100644
--- a/openmp/runtime/test/atomic/omp-atomic-compare-long-double.c
+++ b/openmp/runtime/test/atomic/omp-atomic-compare-long-double.c
@@ -8,7 +8,7 @@
#include <stdio.h>
-#if defined(__x86_64__) && __LDBL_MANT_DIG__ == 64 && \
+#if defined(__x86_64__) && __LDBL_MANT_DIG__ == 64 && \
__SIZEOF_LONG_DOUBLE__ == 16
#define PADDED_LONG_DOUBLE 1
#else
More information about the cfe-commits
mailing list