================
@@ -11810,14 +11810,98 @@ 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 /
     // R.Var handling.
     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(
----------------
MattPD wrote:

`CGStmtOpenMP.cpp` forwards `X.IsVolatile` from 
`X->getType().isVolatileQualified()`. `createAtomicRead` and 
`createAtomicWrite` pass it to their scalar loads and stores on `X.Var`, and 
`emitAtomicUpdate` sets it on its `cmpxchg`. In `createAtomicCompare`, the call 
creating `XLoad` uses the four-argument `CreateAlignedLoad` overload with 
`isVolatile` defaulting to `false`, and the `cmpxchg` at line 11875 omits 
`setVolatile(X.IsVolatile)`. The other `createAtomicCompare` branches also omit 
`X.IsVolatile` on accesses to `X.Var`, at these lines: 11939, 11986, 11995, 
12032, 12036, and 12194.

```cpp
static __attribute__((noinline)) void compare_long(void) {
  long double x = 0;
  volatile long double *p = &x;
#pragma omp atomic compare
  if (*p == 1)
    *p = 2;
}

void call_long(void) { compare_long(); }
```

You can reproduce this on x86-64 Linux by saving the source as `repro.cpp` and 
running `clang -O2 -fopenmp -mcx16 -S -emit-llvm repro.cpp -o -`. The output 
should keep the volatile access to `*p`. At `-O0`, the PR head emits 
non-volatile `load atomic i128` and `cmpxchg`; at `-O2`, LLVM infers 
`memory(none)` and reduces the wrapper to `ret void`. The PR's base revision 
asserts while compiling this source for `long double`, and also reduces the 
wrapper to `ret void` when `long double` is replaced with `double`.

Could `createAtomicCompare` pass `X.IsVolatile` to both `CreateAlignedLoad` 
calls and call `setVolatile(X.IsVolatile)` on its `AtomicCmpXchgInst` and 
`AtomicRMWInst` instructions, with a codegen check asserting `load atomic 
volatile` and `cmpxchg volatile`?

https://github.com/llvm/llvm-project/pull/226567
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to