[llvm] [mlir] [llvm][OpenMP] Add implicit cast to omp.atomic.read (PR #114659)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 14 03:52:30 PST 2025


================
@@ -265,6 +265,32 @@ computeOpenMPScheduleType(ScheduleKind ClauseKind, bool HasChunks,
   return Result;
 }
 
+/// Emit an implicit cast to convert \p XRead to type of variable \p V
+static llvm::Value *emitImplicitCast(IRBuilder<> &Builder, llvm::Value *XRead,
+                                     llvm::Value *V) {
+  llvm::Type *XReadType = XRead->getType();
+  llvm::Type *VType = V->getType();
+  if (llvm::AllocaInst *vAlloca = dyn_cast<llvm::AllocaInst>(V))
+    VType = vAlloca->getAllocatedType();
+
+  if (XReadType->isStructTy() && VType->isStructTy())
+    // No need to extract or convert. A direct
+    // `store` will suffice.
+    return XRead;
+
+  if (XRead->getType()->isStructTy())
+    XRead = Builder.CreateExtractValue(XRead, /*Idxs=*/0);
+  if (VType->isIntegerTy() && XReadType->isFloatingPointTy())
----------------
NimishMishra wrote:

Actually if you consider the test case:

```
program atomic_array
  complex :: x(2)
  integer :: y

  x(1) = (1.0, 1.0)
  x(2) = (2.0, 2.0)
  !$omp parallel
    !$omp atomic read
      y = x(1)
  !$omp end parallel
  print *, y
end program
```

Then we need to extract the real component of `complex` into a `float`, and store it to `integer`.  We thus need this check in place to ensure the conversion takes place.

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


More information about the llvm-commits mailing list