Skip to content

Commit

Permalink
[flang][HLFIR] Adapt SimplifyHLFIRIntrinsics to run on all top level …
Browse files Browse the repository at this point in the history
…ops (#92573)

This means that this pass will also run on hlfir intrinsics 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 20, 2024
1 parent 2141907 commit 605ae4e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 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 @@ -25,7 +25,6 @@ namespace hlfir {
std::unique_ptr<mlir::Pass> createConvertHLFIRtoFIRPass();
std::unique_ptr<mlir::Pass> createBufferizeHLFIRPass();
std::unique_ptr<mlir::Pass> createLowerHLFIRIntrinsicsPass();
std::unique_ptr<mlir::Pass> createSimplifyHLFIRIntrinsicsPass();
std::unique_ptr<mlir::Pass> createInlineElementalsPass();
std::unique_ptr<mlir::Pass> createLowerHLFIROrderedAssignmentsPass();
std::unique_ptr<mlir::Pass> createOptimizedBufferizationPass();
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 @@ -46,9 +46,8 @@ def LowerHLFIROrderedAssignments : Pass<"lower-hlfir-ordered-assignments", "::ml
];
}

def SimplifyHLFIRIntrinsics : Pass<"simplify-hlfir-intrinsics", "::mlir::func::FuncOp"> {
def SimplifyHLFIRIntrinsics : Pass<"simplify-hlfir-intrinsics"> {
let summary = "Simplify HLFIR intrinsic operations that don't need to result in runtime calls";
let constructor = "hlfir::createSimplifyHLFIRIntrinsicsPass()";
}

def InlineElementals : Pass<"inline-elementals", "::mlir::func::FuncOp"> {
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 @@ -317,7 +317,8 @@ inline void createHLFIRToFIRPassPipeline(
mlir::PassManager &pm, llvm::OptimizationLevel optLevel = defaultOptLevel) {
if (optLevel.isOptimizingForSpeed()) {
addCanonicalizerPassWithoutRegionSimplification(pm);
pm.addPass(hlfir::createSimplifyHLFIRIntrinsicsPass());
addNestedPassToAllTopLevelOperations(
pm, hlfir::createSimplifyHLFIRIntrinsics);
}
pm.addPass(hlfir::createInlineElementalsPass());
if (optLevel.isOptimizingForSpeed()) {
Expand Down
11 changes: 3 additions & 8 deletions flang/lib/Optimizer/HLFIR/Transforms/SimplifyHLFIRIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class SimplifyHLFIRIntrinsics
: public hlfir::impl::SimplifyHLFIRIntrinsicsBase<SimplifyHLFIRIntrinsics> {
public:
void runOnOperation() override {
mlir::func::FuncOp func = this->getOperation();
mlir::MLIRContext *context = &getContext();
mlir::RewritePatternSet patterns(context);
patterns.insert<TransposeAsElementalConversion>(context);
Expand All @@ -108,16 +107,12 @@ class SimplifyHLFIRIntrinsics
});
target.markUnknownOpDynamicallyLegal(
[](mlir::Operation *) { return true; });
if (mlir::failed(
mlir::applyFullConversion(func, target, std::move(patterns)))) {
mlir::emitError(func->getLoc(),
if (mlir::failed(mlir::applyFullConversion(getOperation(), target,
std::move(patterns)))) {
mlir::emitError(getOperation()->getLoc(),
"failure in HLFIR intrinsic simplification");
signalPassFailure();
}
}
};
} // namespace

std::unique_ptr<mlir::Pass> hlfir::createSimplifyHLFIRIntrinsicsPass() {
return std::make_unique<SimplifyHLFIRIntrinsics>();
}
9 changes: 8 additions & 1 deletion flang/test/Driver/mlir-pass-pipeline.f90
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@

! ALL: Fortran::lower::VerifierPass
! O2-NEXT: Canonicalizer
! O2-NEXT: 'func.func' Pipeline
! O2-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
! O2-NEXT: 'fir.global' Pipeline
! O2-NEXT: SimplifyHLFIRIntrinsics
! ALL: 'func.func' Pipeline
! O2-NEXT: SimplifyHLFIRIntrinsics
! ALL: InlineElementals
! O2-NEXT: 'omp.declare_reduction' Pipeline
! O2-NEXT: SimplifyHLFIRIntrinsics
! O2-NEXT: 'omp.private' Pipeline
! O2-NEXT: SimplifyHLFIRIntrinsics
! ALL: LowerHLFIROrderedAssignments
! ALL-NEXT: LowerHLFIRIntrinsics
! ALL-NEXT: BufferizeHLFIR
Expand Down
7 changes: 7 additions & 0 deletions flang/test/Fir/basic-program.fir
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ func.func @_QQmain() {
// PASSES: Pass statistics report

// PASSES: Canonicalizer
// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
// PASSES-NEXT: 'fir.global' Pipeline
// PASSES-NEXT: SimplifyHLFIRIntrinsics
// PASSES-NEXT: 'func.func' Pipeline
// PASSES-NEXT: SimplifyHLFIRIntrinsics
// PASSES-NEXT: InlineElementals
// PASSES-NEXT: 'omp.declare_reduction' Pipeline
// PASSES-NEXT: SimplifyHLFIRIntrinsics
// PASSES-NEXT: 'omp.private' Pipeline
// PASSES-NEXT: SimplifyHLFIRIntrinsics
// PASSES-NEXT: Canonicalizer
// PASSES-NEXT: CSE
// PASSES-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
Expand Down

0 comments on commit 605ae4e

Please sign in to comment.