[clang] [llvm] [OMPIRBuilder] Perform atomic compare on padded FP types at their allocation size (PR #226567)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 21:50:01 PST 2026
================
@@ -11935,16 +11967,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);
----------------
MattPD wrote:
Is an `align 16` compare-exchange on a packed member intended as the implementation-defined behavior? OpenMP 5.1 ยง2.19.7 leaves behavior implementation-defined when `x` is not size-aligned. The packed member has alignment 1, while the emitted `cmpxchg` declares `align 16`.
Clang's `emitOMPAtomicCompareExpr` retains an `Address` in `XAddr`; `getAlignment()` exposes the lvalue alignment, but `AtomicOpValue` does not carry that alignment to `createAtomicCompare`. `Builder.CreateAtomicCmpXchg` receives the empty `MaybeAlign()` and infers `align 16` from the `i128` store size.
The `HandleFPNegZero` branch also loads the widened `IntCastTy` from `X.Var` and passes an empty `MaybeAlign()` to both compare-exchanges. MLIR enables that branch.
Could both paths avoid assigning 16-byte alignment to an under-aligned pointer, using an operation supported for its alignment or diagnosing when the target cannot? Please add a packed-`long double` codegen test for the Clang path and an `OpenMPIRBuilder` unit test enabling `HandleFPNegZero` with an under-aligned `x86_fp80` operand.
```c
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
struct __attribute__((packed)) P { char c; long double x; };
int main(void) {
struct P p __attribute__((aligned(16)));
memset(&p, 0, sizeof p);
p.x = 1.0L;
long double e = 1.0L, d = 2.0L;
for (int i = 10; i < 16; ++i) {
((unsigned char *)&e)[i] = 0;
((unsigned char *)&d)[i] = 0;
}
printf("address=%p mod16=%" PRIuPTR "\n", (void *)&p.x,
(uintptr_t)&p.x % 16);
fflush(stdout);
#pragma omp atomic compare
p.x = p.x == e ? d : p.x;
printf("after=%Lg\n", p.x);
return p.x == 2.0L ? 0 : 1;
}
```
Save it as `repro.c`, then compile and run it:
```sh
clang -O0 -fopenmp -mcx16 repro.c -o repro
./repro
```
The `aligned(16)` declaration of `p`, combined with the packed member layout, places `p.x` at an address with remainder 1 modulo 16. The PR head emits `cmpxchg i128` with `align 16` for this member. With the command above, the PR-head executable printed an address remainder of 1 and then faulted at `cmpxchg16b`.
A packed `__int128` member also faulted at `cmpxchg16b` on both revisions, so the underlying alignment issue predates this PR; this change newly exposes it for `long double`.
The compiler at the PR's base revision asserts while compiling this source with default OpenMP 5.1.
https://github.com/llvm/llvm-project/pull/226567
More information about the cfe-commits
mailing list