[flang] [llvm] [flang][llvm][OpenMP][OpenACC] Add implicit casts to omp.atomic and acc.atomic (PR #131603)

Tom Eccles via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 17 07:11:42 PDT 2025


================
@@ -103,6 +104,61 @@ static void processOmpAtomicTODO(mlir::Type elementType,
   }
 }
 
+/// Emits an implicit cast for atomic statements
+static void emitImplicitCast(Fortran::lower::AbstractConverter &converter,
+                             mlir::Location loc, mlir::Value &fromAddress,
+                             mlir::Value &toAddress, mlir::Type &elementType) {
+  if (fromAddress.getType() == toAddress.getType())
+    return;
+  fir::FirOpBuilder &builder = converter.getFirOpBuilder();
+  mlir::Value alloca = builder.create<fir::AllocaOp>(
+      loc, fir::unwrapRefType(toAddress.getType()));
+  mlir::Value loadedVal = builder.create<fir::LoadOp>(loc, fromAddress);
+  mlir::Type toType = fir::unwrapRefType(toAddress.getType());
+  mlir::Type fromType = fir::unwrapRefType(fromAddress.getType());
+  if (!fir::isa_complex(toType) && !fir::isa_complex(fromType)) {
+    loadedVal = builder.create<fir::ConvertOp>(
+        loc, fir::unwrapRefType(toAddress.getType()), loadedVal);
+    builder.create<fir::StoreOp>(loc, loadedVal, alloca);
+  } else if (!fir::isa_complex(toType) && fir::isa_complex(fromType)) {
+    loadedVal = builder.create<fir::ExtractValueOp>(
+        loc, mlir::cast<mlir::ComplexType>(fromType).getElementType(),
+        loadedVal,
+        builder.getArrayAttr(
+            builder.getIntegerAttr(builder.getIndexType(), 0)));
+    loadedVal = builder.create<fir::ConvertOp>(loc, toType, loadedVal);
+    builder.create<fir::StoreOp>(loc, loadedVal, alloca);
+  } else if (fir::isa_complex(toType) && fir::isa_complex(fromType)) {
+    mlir::Value firstComp = builder.create<fir::ExtractValueOp>(
+        loc, mlir::cast<mlir::ComplexType>(fromType).getElementType(),
+        loadedVal,
+        builder.getArrayAttr(
+            builder.getIntegerAttr(builder.getIndexType(), 0)));
+    mlir::Value secondComp = builder.create<fir::ExtractValueOp>(
+        loc, mlir::cast<mlir::ComplexType>(fromType).getElementType(),
+        loadedVal,
+        builder.getArrayAttr(
+            builder.getIntegerAttr(builder.getIndexType(), 1)));
----------------
tblah wrote:

nit: maybe this could be made a bit cleaner with the helpers in `flang/include/flang/Optimizer/Builder/Complex.h`.

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


More information about the llvm-commits mailing list