[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:00 PST 2026


================
@@ -11781,6 +11781,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);
----------------
MattPD wrote:

OpenMP 5.2 §4.3.1.3 requires C/C++ `==` comparisons to compare value representations after the usual arithmetic conversions, so the result must not depend on `x86_fp80` padding. OpenMP 5.1 §2.19.7 says to compare the bits comprising each object, as with `memcmp`. The PR's linked issue, https://github.com/llvm/llvm-project/issues/140080, points to GCC's `atomic-21.c`. Its source comment reads: "Verify == comparison for atomics is done as with memcmp with __builtin_clear_padding if needed."

Clang defaults bare `-fopenmp` to 5.1, and the added codegen test uses that mode. With `-fopenmp-version=52`, Clang still emits an `i128` compare-exchange over all 16 bytes, including the six padding bytes.

Could the comparison follow OpenMP 5.2's value-representation rule when 5.2 is selected, and could the intended padding treatment under 5.1 be stated explicitly? Please add runtime tests for zero and nonzero padding in 5.2 mode, with codegen checks for both modes.

```c
static int update(unsigned char padding) {
  long double x = 1.0L;
  unsigned char *bytes = (unsigned char *)&x;
  for (int i = 10; i < 16; ++i)
    bytes[i] = padding;
#pragma omp atomic compare
  x = x == 1.0L ? 2.0L : x;
  return x == 2.0L;
}

int main(void) {
  if (!update(0))
    return 1;
  if (!update(0xa5))
    return 2;
  return 0;
}
```

Save it as `repro.c` and run `clang -target x86_64-unknown-linux-gnu -O0 -fopenmp -fopenmp-version=52 -mcx16 repro.c -o repro && ./repro`. The expected exit status is 0. In the measured OpenMP 5.2 PR-head run, the atomic compare updated `x` with zero padding but not with `0xa5` padding, so the program exited 2.

The compiler at the PR's base revision asserts while compiling this source in both default OpenMP 5.1 mode and explicit OpenMP 5.2 mode.

https://github.com/llvm/llvm-project/pull/226567


More information about the cfe-commits mailing list