[flang-commits] [flang] [flang] Inline hlfir.copy_in for trivial types (PR #138718)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Tue May 6 10:13:47 PDT 2025


================
@@ -127,6 +128,121 @@ class InlineHLFIRAssignConversion
   }
 };
 
+class InlineCopyInConversion : public mlir::OpRewritePattern<hlfir::CopyInOp> {
+public:
+  using mlir::OpRewritePattern<hlfir::CopyInOp>::OpRewritePattern;
+
+  llvm::LogicalResult
+  matchAndRewrite(hlfir::CopyInOp copyIn,
+                  mlir::PatternRewriter &rewriter) const override;
+};
+
+llvm::LogicalResult
+InlineCopyInConversion::matchAndRewrite(hlfir::CopyInOp copyIn,
+                                        mlir::PatternRewriter &rewriter) const {
+  fir::FirOpBuilder builder(rewriter, copyIn.getOperation());
+  mlir::Location loc = copyIn.getLoc();
+  hlfir::Entity inputVariable{copyIn.getVar()};
+  if (!fir::isa_trivial(inputVariable.getFortranElementType()))
+    return rewriter.notifyMatchFailure(copyIn,
+                                       "CopyInOp's data type is not trivial");
+
+  if (fir::isPointerType(inputVariable.getType()))
+    return rewriter.notifyMatchFailure(
+        copyIn, "CopyInOp's input variable is a pointer");
+
+  // There should be exactly one user of WasCopied - the corresponding
+  // CopyOutOp.
+  if (copyIn.getWasCopied().getUses().empty())
+    return rewriter.notifyMatchFailure(copyIn,
+                                       "CopyInOp's WasCopied has no uses");
+  // The copy out should always be present, either to actually copy or just
+  // deallocate memory.
+  auto *copyOut =
----------------
tblah wrote:

nit: spell out this auto because the type is not obvious (and in other places)

The guidelines are a bit ambiguous:
https://llvm.org/docs/CodingStandards.html#use-auto-type-deduction-to-make-code-more-readable

What is usually done in practice is to only use `auto` when the type is written in the initializer e.g.
```
auto copyOut = mlir::dyn_cast<hlfir::CopyOutOp>(...)
```

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


More information about the flang-commits mailing list