Skip to content

[flang] Add stack reclaim pass to reclaim allocas in loop #95309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flang/include/flang/Optimizer/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace fir {
#define GEN_PASS_DECL_OPENACCDATAOPERANDCONVERSION
#define GEN_PASS_DECL_ADDDEBUGINFO
#define GEN_PASS_DECL_STACKARRAYS
#define GEN_PASS_DECL_STACKRECLAIM
#define GEN_PASS_DECL_LOOPVERSIONING
#define GEN_PASS_DECL_ADDALIASTAGS
#define GEN_PASS_DECL_OMPMAPINFOFINALIZATIONPASS
Expand Down
9 changes: 9 additions & 0 deletions flang/include/flang/Optimizer/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ def StackArrays : Pass<"stack-arrays", "mlir::ModuleOp"> {
let dependentDialects = [ "fir::FIROpsDialect" ];
}

def StackReclaim : Pass<"stack-reclaim"> {
let summary = "Insert stacksave/stackrestore in region with allocas";
let description = [{
Insert stacksave/stackrestore in loop region to reclaim alloca done in its
scope.
}];
let dependentDialects = [ "mlir::LLVM::LLVMDialect" ];
}

def AddAliasTags : Pass<"fir-add-alias-tags", "mlir::ModuleOp"> {
let summary = "Add tbaa tags to operations that implement FirAliasAnalysisOpInterface";
let description = [{
Expand Down
1 change: 1 addition & 0 deletions flang/include/flang/Tools/CLOptions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ inline void createDefaultFIROptimizerPassPipeline(
if (pc.AliasAnalysis && !disableFirAliasTags && !useOldAliasTags)
pm.addPass(fir::createAddAliasTags());

addNestedPassToAllTopLevelOperations(pm, fir::createStackReclaim);
// convert control flow to CFG form
fir::addCfgConversionPass(pm, pc);
pm.addPass(mlir::createConvertSCFToCFPass());
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Optimizer/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_flang_library(FIRTransforms
OMPFunctionFiltering.cpp
OMPMapInfoFinalization.cpp
OMPMarkDeclareTarget.cpp
StackReclaim.cpp
VScaleAttr.cpp
FunctionAttr.cpp
DebugTypeGenerator.cpp
Expand Down
52 changes: 52 additions & 0 deletions flang/lib/Optimizer/Transforms/StackReclaim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//===- StackReclaim.cpp -- Insert stacksave/stackrestore in region --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "flang/Common/Fortran.h"
#include "flang/Optimizer/Dialect/FIRDialect.h"
#include "flang/Optimizer/Dialect/FIROps.h"
#include "flang/Optimizer/Transforms/Passes.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/Matchers.h"
#include "mlir/Pass/Pass.h"

namespace fir {
#define GEN_PASS_DEF_STACKRECLAIM
#include "flang/Optimizer/Transforms/Passes.h.inc"
} // namespace fir

using namespace mlir;

namespace {

class StackReclaimPass : public fir::impl::StackReclaimBase<StackReclaimPass> {
public:
using StackReclaimBase<StackReclaimPass>::StackReclaimBase;

void runOnOperation() override;
};
} // namespace

void StackReclaimPass::runOnOperation() {
auto *op = getOperation();
auto *context = &getContext();
mlir::OpBuilder builder(context);
mlir::Type voidPtr = mlir::LLVM::LLVMPointerType::get(context);

op->walk([&](fir::DoLoopOp loopOp) {
mlir::Location loc = loopOp.getLoc();

if (!loopOp.getRegion().getOps<fir::AllocaOp>().empty()) {
builder.setInsertionPointToStart(&loopOp.getRegion().front());
auto stackSaveOp = builder.create<LLVM::StackSaveOp>(loc, voidPtr);

auto *terminator = loopOp.getRegion().back().getTerminator();
builder.setInsertionPoint(terminator);
builder.create<LLVM::StackRestoreOp>(loc, stackSaveOp);
}
});
}
4 changes: 4 additions & 0 deletions flang/test/Driver/bbc-mlir-pass-pipeline.f90
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@

! CHECK-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
! CHECK-NEXT: 'fir.global' Pipeline
! CHECK-NEXT: StackReclaim
! CHECK-NEXT: CFGConversion
! CHECK-NEXT: 'func.func' Pipeline
! CHECK-NEXT: StackReclaim
! CHECK-NEXT: CFGConversion
! CHECK-NEXT: 'omp.declare_reduction' Pipeline
! CHECK-NEXT: StackReclaim
! CHECK-NEXT: CFGConversion
! CHECK-NEXT: 'omp.private' Pipeline
! CHECK-NEXT: StackReclaim
! CHECK-NEXT: CFGConversion

! CHECK-NEXT: SCFToControlFlow
Expand Down
4 changes: 4 additions & 0 deletions flang/test/Driver/mlir-debug-pass-pipeline.f90
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@

! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
! ALL-NEXT: 'fir.global' Pipeline
! ALL-NEXT: StackReclaim
! ALL-NEXT: CFGConversion
! ALL-NEXT: 'func.func' Pipeline
! ALL-NEXT: StackReclaim
! ALL-NEXT: CFGConversion
! ALL-NEXT: 'omp.declare_reduction' Pipeline
! ALL-NEXT: StackReclaim
! ALL-NEXT: CFGConversion
! ALL-NEXT: 'omp.private' Pipeline
! ALL-NEXT: StackReclaim
! ALL-NEXT: CFGConversion
! ALL-NEXT: SCFToControlFlow
! ALL-NEXT: Canonicalizer
Expand Down
4 changes: 4 additions & 0 deletions flang/test/Driver/mlir-pass-pipeline.f90
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,16 @@

! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
! ALL-NEXT: 'fir.global' Pipeline
! ALL-NEXT: StackReclaim
! ALL-NEXT: CFGConversion
! ALL-NEXT: 'func.func' Pipeline
! ALL-NEXT: StackReclaim
! ALL-NEXT: CFGConversion
! ALL-NEXT: 'omp.declare_reduction' Pipeline
! ALL-NEXT: StackReclaim
! ALL-NEXT: CFGConversion
! ALL-NEXT: 'omp.private' Pipeline
! ALL-NEXT: StackReclaim
! ALL-NEXT: CFGConversion

! ALL-NEXT: SCFToControlFlow
Expand Down
4 changes: 4 additions & 0 deletions flang/test/Fir/basic-program.fir
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,16 @@ func.func @_QQmain() {

// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
// PASSES-NEXT: 'fir.global' Pipeline
// PASSES-NEXT: StackReclaim
// PASSES-NEXT: CFGConversion
// PASSES-NEXT: 'func.func' Pipeline
// PASSES-NEXT: StackReclaim
// PASSES-NEXT: CFGConversion
// PASSES-NEXT: 'omp.declare_reduction' Pipeline
// PASSES-NEXT: StackReclaim
// PASSES-NEXT: CFGConversion
// PASSES-NEXT: 'omp.private' Pipeline
// PASSES-NEXT: StackReclaim
// PASSES-NEXT: CFGConversion

// PASSES-NEXT: SCFToControlFlow
Expand Down
14 changes: 14 additions & 0 deletions flang/test/Transforms/stack-reclaime.fir
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: fir-opt --split-input-file --stack-reclaim %s | FileCheck %s

func.func @alloca_in_loop(%lb : index, %ub : index, %step : index, %b : i1, %addr : !fir.ref<index>) {
fir.do_loop %iv = %lb to %ub step %step unordered {
%0 = fir.alloca !fir.box<!fir.heap<!fir.char<1,?>>>
}
return
}

// CHECK-LABEL: func.func @alloca_in_loop
// CHECK: fir.do_loop
// CHECK: %[[STACKPTR:.*]] = llvm.intr.stacksave : !llvm.ptr
// CHECK: %{{.*}} = fir.alloca !fir.box<!fir.heap<!fir.char<1,?>>>
// CHECK: llvm.intr.stackrestore %0 : !llvm.ptr
Loading