[flang-commits] [flang] [flang][hlfir] Extend InlineHLFIRCopy to inline copy_out with copy-back (PR #179096)

via flang-commits flang-commits at lists.llvm.org
Mon Feb 2 02:00:58 PST 2026


================
@@ -42,6 +46,50 @@ class InlineCopyInConversion : public mlir::OpRewritePattern<hlfir::CopyInOp> {
                   mlir::PatternRewriter &rewriter) const override;
 };
 
+// Helper function to inline a copy_out operation.
+// If copyBackDest is provided, generates copy-back loop then freemem.
+// Otherwise, just generates freemem (deallocation-only).
+// Generates: if (wasCopied) { [copy_back_loop;] freemem(...) }
+static void inlineCopyOut(fir::FirOpBuilder &builder, mlir::Location loc,
+                          mlir::Value tempBox, mlir::Value wasCopied,
+                          mlir::Value copyBackDest, mlir::Value shape,
+                          mlir::Type sequenceType) {
+  builder.genIfOp(loc, {}, wasCopied, /*withElseRegion=*/false).genThen([&]() {
+    mlir::Value box = fir::LoadOp::create(builder, loc, tempBox);
+
+    // If we need to copy back, generate the copy loop
+    if (copyBackDest) {
+      hlfir::Entity temp{box};
+      hlfir::Entity dest{copyBackDest};
+      llvm::SmallVector<mlir::Value> extents =
+          hlfir::getIndexExtents(loc, builder, shape);
+      hlfir::LoopNest loopNest =
+          hlfir::genLoopNest(loc, builder, extents, /*isUnordered=*/true,
+                             /*workshare=*/false, /*couldVectorize=*/false);
----------------
jeanPerier wrote:

Should probably use `flangomp::shouldUseWorkshareLowering(copyOut)` for consistency with the copy-in loops. 

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


More information about the flang-commits mailing list