Skip to content

Commit

Permalink
[flang][HLFIR] Adapt OptimizedBufferization to run on all top level o…
Browse files Browse the repository at this point in the history
…ps (#92898)

This means that this pass will also run on hlfir elemental operations
which are not inside of functions.

See RFC:

https://discourse.llvm.org/t/rfc-add-an-interface-for-top-level-container-operations

Some of the changes are from moving the declaration and definition of
the constructor into tablegen (as requested during code review of
another pass).
  • Loading branch information
tblah authored May 22, 2024
1 parent e5936b2 commit 9807f25
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
1 change: 0 additions & 1 deletion flang/include/flang/Optimizer/HLFIR/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ std::unique_ptr<mlir::Pass> createConvertHLFIRtoFIRPass();
std::unique_ptr<mlir::Pass> createBufferizeHLFIRPass();
std::unique_ptr<mlir::Pass> createLowerHLFIRIntrinsicsPass();
std::unique_ptr<mlir::Pass> createLowerHLFIROrderedAssignmentsPass();
std::unique_ptr<mlir::Pass> createOptimizedBufferizationPass();

#define GEN_PASS_REGISTRATION
#include "flang/Optimizer/HLFIR/Passes.h.inc"
Expand Down
3 changes: 1 addition & 2 deletions flang/include/flang/Optimizer/HLFIR/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ def BufferizeHLFIR : Pass<"bufferize-hlfir", "::mlir::ModuleOp"> {
let constructor = "hlfir::createBufferizeHLFIRPass()";
}

def OptimizedBufferization : Pass<"opt-bufferization", "::mlir::func::FuncOp"> {
def OptimizedBufferization : Pass<"opt-bufferization"> {
let summary = "Special cases for hlfir.expr bufferization where we can avoid a temporary which would be created by the generic bufferization pass";
let constructor = "hlfir::createOptimizedBufferizationPass()";
}

def LowerHLFIRIntrinsics : Pass<"lower-hlfir-intrinsics", "::mlir::ModuleOp"> {
Expand Down
3 changes: 2 additions & 1 deletion flang/include/flang/Tools/CLOptions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ inline void createHLFIRToFIRPassPipeline(
if (optLevel.isOptimizingForSpeed()) {
addCanonicalizerPassWithoutRegionSimplification(pm);
pm.addPass(mlir::createCSEPass());
pm.addPass(hlfir::createOptimizedBufferizationPass());
addNestedPassToAllTopLevelOperations(
pm, hlfir::createOptimizedBufferization);
}
pm.addPass(hlfir::createLowerHLFIROrderedAssignmentsPass());
pm.addPass(hlfir::createLowerHLFIRIntrinsicsPass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,6 @@ class OptimizedBufferizationPass
OptimizedBufferizationPass> {
public:
void runOnOperation() override {
mlir::func::FuncOp func = getOperation();
mlir::MLIRContext *context = &getContext();

mlir::GreedyRewriteConfig config;
Expand All @@ -1062,15 +1061,11 @@ class OptimizedBufferizationPass
patterns.insert<MinMaxlocElementalConversion<hlfir::MaxlocOp>>(context);

if (mlir::failed(mlir::applyPatternsAndFoldGreedily(
func, std::move(patterns), config))) {
mlir::emitError(func.getLoc(),
getOperation(), std::move(patterns), config))) {
mlir::emitError(getOperation()->getLoc(),
"failure in HLFIR optimized bufferization");
signalPassFailure();
}
}
};
} // namespace

std::unique_ptr<mlir::Pass> hlfir::createOptimizedBufferizationPass() {
return std::make_unique<OptimizedBufferizationPass>();
}
7 changes: 7 additions & 0 deletions flang/test/Driver/mlir-pass-pipeline.f90
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@
! O2-NEXT: CSE
! O2-NEXT: (S) {{.*}} num-cse'd
! O2-NEXT: (S) {{.*}} num-dce'd
! O2-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
! O2-NEXT: 'fir.global' Pipeline
! O2-NEXT: OptimizedBufferization
! O2-NEXT: 'func.func' Pipeline
! O2-NEXT: OptimizedBufferization
! O2-NEXT: 'omp.declare_reduction' Pipeline
! O2-NEXT: OptimizedBufferization
! O2-NEXT: 'omp.private' Pipeline
! O2-NEXT: OptimizedBufferization
! ALL: LowerHLFIROrderedAssignments
! ALL-NEXT: LowerHLFIRIntrinsics
! ALL-NEXT: BufferizeHLFIR
Expand Down
9 changes: 8 additions & 1 deletion flang/test/Fir/basic-program.fir
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ func.func @_QQmain() {
// PASSES-NEXT: CSE
// PASSES-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
// PASSES-NEXT: (S) 0 num-dce'd - Number of operations DCE'd
// PASSES-NEXT: 'func.func' Pipeline
// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
// PASSES-NEXT: 'fir.global' Pipeline
// PASSES-NEXT: OptimizedBufferization
// PASSES-NEXT: 'func.func' Pipeline
// PASSES-NEXT: OptimizedBufferization
// PASSES-NEXT: 'omp.declare_reduction' Pipeline
// PASSES-NEXT: OptimizedBufferization
// PASSES-NEXT: 'omp.private' Pipeline
// PASSES-NEXT: OptimizedBufferization
// PASSES-NEXT: LowerHLFIROrderedAssignments
// PASSES-NEXT: LowerHLFIRIntrinsics
Expand Down

0 comments on commit 9807f25

Please sign in to comment.