[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 =
+ copyIn.getWasCopied().getUsers().begin().getCurrent().getUser();
+
+ if (!mlir::isa<hlfir::CopyOutOp>(copyOut))
+ return rewriter.notifyMatchFailure(copyIn,
+ "CopyInOp has no direct CopyOut");
+
+ // Only inline the copy_in when copy_out does not need to be done, i.e. in
+ // case of intent(in).
+ if (::llvm::cast<hlfir::CopyOutOp>(copyOut).getVar())
+ return rewriter.notifyMatchFailure(copyIn, "CopyIn needs a copy-out");
----------------
tblah wrote:
Instead of doing `isa` and then a `cast`, you can do it all in one with `dyn_cast` and then check if the result is `nullptr`.
https://github.com/llvm/llvm-project/pull/138718
More information about the flang-commits
mailing list