[flang-commits] [flang] [flang] Insert stacksave/stackrestore when alloca are present in loops (PR #95173)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Tue Jun 11 14:42:23 PDT 2024


================
@@ -72,6 +75,22 @@ class CfgLoopConv : public mlir::OpRewritePattern<fir::DoLoopOp> {
         rewriter.splitBlock(conditionalBlock, conditionalBlock->begin());
     auto *lastBlock = &loop.getRegion().back();
 
+    // Insert stacksave/stackrestore if there is fir.alloca operation in the
+    // loop.
+    if (hasAllocas) {
+      auto mod = loop.getOperation()->getParentOfType<mlir::ModuleOp>();
+      fir::FirOpBuilder builder(rewriter, mod);
+      builder.setInsertionPointToStart(firstBlock);
+      mlir::func::FuncOp stackSave = fir::factory::getLlvmStackSave(builder);
----------------
vzakhari wrote:

This seems to require making the pass a module pass.  This is quite unfortunate.

We can try to use LLVM dialect `stacksave/stackrestore` intrinsics operations, but they do not look too reliable to me, e.g. they do not have any side effects that prevent moving them around `fir.alloca`. At the same time, `fir.call` seems to have the same problem.

Would that be possible to insert stacksave/stackrestore calls during `hlfir.concat` bufferization?  It might be inefficient, but we will get correct code at least.  The redundant stacksave/stackrestore might be optimized after that.

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


More information about the flang-commits mailing list