Skip to content

[flang] de-duplicate CFGConversion pass #89783

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 1 commit into from
Apr 24, 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
5 changes: 1 addition & 4 deletions flang/include/flang/Optimizer/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ namespace fir {
#define GEN_PASS_DECL_ANNOTATECONSTANTOPERANDS
#define GEN_PASS_DECL_ARRAYVALUECOPY
#define GEN_PASS_DECL_CHARACTERCONVERSION
#define GEN_PASS_DECL_CFGCONVERSIONONFUNC
#define GEN_PASS_DECL_CFGCONVERSIONONREDUCTION
#define GEN_PASS_DECL_CFGCONVERSION
#define GEN_PASS_DECL_EXTERNALNAMECONVERSION
#define GEN_PASS_DECL_MEMREFDATAFLOWOPT
#define GEN_PASS_DECL_SIMPLIFYINTRINSICS
Expand All @@ -52,8 +51,6 @@ namespace fir {
std::unique_ptr<mlir::Pass> createAffineDemotionPass();
std::unique_ptr<mlir::Pass>
createArrayValueCopyPass(fir::ArrayValueCopyOptions options = {});
std::unique_ptr<mlir::Pass> createFirToCfgOnFuncPass();
std::unique_ptr<mlir::Pass> createFirToCfgOnReductionPass();
std::unique_ptr<mlir::Pass> createCharacterConversionPass();
std::unique_ptr<mlir::Pass> createExternalNameConversionPass();
std::unique_ptr<mlir::Pass>
Expand Down
11 changes: 1 addition & 10 deletions flang/include/flang/Optimizer/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ def CharacterConversion : Pass<"character-conversion"> {
];
}

class CFGConversionBase<string optExt, string operation>
: Pass<"cfg-conversion-on-" # optExt # "-opt", operation> {
def CFGConversion : Pass<"cfg-conversion"> {
let summary = "Convert FIR structured control flow ops to CFG ops.";
let description = [{
Transform the `fir.do_loop`, `fir.if`, `fir.iterate_while` and
Expand All @@ -157,14 +156,6 @@ class CFGConversionBase<string optExt, string operation>
];
}

def CFGConversionOnFunc : CFGConversionBase<"func", "mlir::func::FuncOp"> {
let constructor = "::fir::createFirToCfgOnFuncPass()";
}

def CFGConversionOnReduction : CFGConversionBase<"reduce", "mlir::omp::DeclareReductionOp"> {
let constructor = "::fir::createFirToCfgOnReductionPass()";
}

def ExternalNameConversion : Pass<"external-name-interop", "mlir::ModuleOp"> {
let summary = "Convert name for external interoperability";
let description = [{
Expand Down
15 changes: 8 additions & 7 deletions flang/include/flang/Tools/CLOptions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ DisableOption(BoxedProcedureRewrite, "boxed-procedure-rewrite",
DisableOption(ExternalNameConversion, "external-name-interop",
"convert names with external convention");

// TODO: remove once these are used for non-codegen passes
#if !defined(FLANG_EXCLUDE_CODEGEN)
using PassConstructor = std::unique_ptr<mlir::Pass>();

template <typename OP>
Expand All @@ -108,7 +106,12 @@ void addNestedPassToAllTopLevelOperations(
addNestedPassToOps<mlir::func::FuncOp, mlir::omp::DeclareReductionOp,
fir::GlobalOp>(pm, ctor);
}
#endif

void addNestedPassToAllTopLevelOperationsConditionally(mlir::PassManager &pm,
llvm::cl::opt<bool> &disabled, PassConstructor ctor) {
if (!disabled)
addNestedPassToAllTopLevelOperations(pm, ctor);
}

/// Generic for adding a pass to the pass manager if it is not disabled.
template <typename F>
Expand Down Expand Up @@ -146,10 +149,8 @@ static void addCanonicalizerPassWithoutRegionSimplification(
}

inline void addCfgConversionPass(mlir::PassManager &pm) {
addNestedPassConditionally<mlir::func::FuncOp>(
pm, disableCfgConversion, fir::createFirToCfgOnFuncPass);
addNestedPassConditionally<mlir::omp::DeclareReductionOp>(
pm, disableCfgConversion, fir::createFirToCfgOnReductionPass);
addNestedPassToAllTopLevelOperationsConditionally(
pm, disableCfgConversion, fir::createCFGConversion);
}

inline void addAVC(
Expand Down
26 changes: 4 additions & 22 deletions flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
#include "llvm/Support/CommandLine.h"

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

Expand Down Expand Up @@ -309,9 +308,10 @@ class CfgIterWhileConv : public mlir::OpRewritePattern<fir::IterWhileOp> {
};

/// Convert FIR structured control flow ops to CFG ops.
template <typename Pass, template <typename> class PassBase>
class CfgConversionTemplate : public PassBase<Pass> {
class CfgConversion : public fir::impl::CFGConversionBase<CfgConversion> {
public:
using CFGConversionBase<CfgConversion>::CFGConversionBase;

void runOnOperation() override {
auto *context = &this->getContext();
mlir::RewritePatternSet patterns(context);
Expand All @@ -333,14 +333,6 @@ class CfgConversionTemplate : public PassBase<Pass> {
}
};

class CfgConversionOnFunc
: public CfgConversionTemplate<CfgConversionOnFunc,
::fir::impl::CFGConversionOnFuncBase> {};

class CfgConversionOnReduction
: public CfgConversionTemplate<CfgConversionOnReduction,
::fir::impl::CFGConversionOnReductionBase> {
};
} // namespace

/// Expose conversion rewriters to other passes
Expand All @@ -349,13 +341,3 @@ void fir::populateCfgConversionRewrites(mlir::RewritePatternSet &patterns,
patterns.insert<CfgLoopConv, CfgIfConv, CfgIterWhileConv>(
patterns.getContext(), forceLoopToExecuteOnce);
}

/// Convert FIR's structured control flow ops to CFG ops. This
/// conversion enables the `createLowerToCFGPass` to transform these to CFG
/// form.
std::unique_ptr<mlir::Pass> fir::createFirToCfgOnFuncPass() {
return std::make_unique<CfgConversionOnFunc>();
}
std::unique_ptr<mlir::Pass> fir::createFirToCfgOnReductionPass() {
return std::make_unique<CfgConversionOnReduction>();
}
8 changes: 5 additions & 3 deletions flang/test/Driver/bbc-mlir-pass-pipeline.f90
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
! CHECK-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
! CHECK-NEXT: (S) 0 num-dce'd - Number of operations DCE'd

! CHECK-NEXT: Pipeline Collection : ['func.func', 'omp.declare_reduction']
! CHECK-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction']
! CHECK-NEXT: 'fir.global' Pipeline
! CHECK-NEXT: CFGConversion
! CHECK-NEXT: 'func.func' Pipeline
! CHECK-NEXT: PolymorphicOpConversion
! CHECK-NEXT: CFGConversionOnFunc
! CHECK-NEXT: CFGConversion
! CHECK-NEXT: 'omp.declare_reduction' Pipeline
! CHECK-NEXT: CFGConversionOnReduction
! CHECK-NEXT: CFGConversion

! CHECK-NEXT: SCFToControlFlow
! CHECK-NEXT: Canonicalizer
Expand Down
8 changes: 5 additions & 3 deletions flang/test/Driver/mlir-debug-pass-pipeline.f90
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@
! ALL-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
! ALL-NEXT: (S) 0 num-dce'd - Number of operations DCE'd

! ALL-NEXT: Pipeline Collection : ['func.func', 'omp.declare_reduction']
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction']
! ALL-NEXT: 'fir.global' Pipeline
! ALL-NEXT: CFGConversion
! ALL-NEXT: 'func.func' Pipeline
! ALL-NEXT: PolymorphicOpConversion
! ALL-NEXT: CFGConversionOnFunc
! ALL-NEXT: CFGConversion
! ALL-NEXT: 'omp.declare_reduction' Pipeline
! ALL-NEXT: CFGConversionOnReduction
! ALL-NEXT: CFGConversion
! ALL-NEXT: SCFToControlFlow
! ALL-NEXT: Canonicalizer
! ALL-NEXT: SimplifyRegionLite
Expand Down
8 changes: 5 additions & 3 deletions flang/test/Driver/mlir-pass-pipeline.f90
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@
! O2-NEXT: 'func.func' Pipeline
! O2-NEXT: PolymorphicOpConversion
! O2-NEXT: AddAliasTags
! ALL-NEXT: Pipeline Collection : ['func.func', 'omp.declare_reduction']
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction']
! ALL-NEXT: 'fir.global' Pipeline
! ALL-NEXT: CFGConversion
! ALL-NEXT: 'func.func' Pipeline
! NOTO2-NEXT: PolymorphicOpConversion
! ALL-NEXT: CFGConversionOnFunc
! ALL-NEXT: CFGConversion
! ALL-NEXT: 'omp.declare_reduction' Pipeline
! ALL-NEXT: CFGConversionOnReduction
! ALL-NEXT: CFGConversion

! ALL-NEXT: SCFToControlFlow
! ALL-NEXT: Canonicalizer
Expand Down
4 changes: 2 additions & 2 deletions flang/test/Fir/array-value-copy-2.fir
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: fir-opt --array-value-copy --cfg-conversion-on-func-opt %s | FileCheck %s
// RUN: fir-opt --array-value-copy="optimize-conflicts=true" --cfg-conversion-on-func-opt %s | FileCheck %s
// RUN: fir-opt --array-value-copy --cfg-conversion %s | FileCheck %s
// RUN: fir-opt --array-value-copy="optimize-conflicts=true" --cfg-conversion %s | FileCheck %s

// CHECK-LABEL: func @_QPslice1(
// CHECK-NOT: fir.allocmem
Expand Down
8 changes: 5 additions & 3 deletions flang/test/Fir/basic-program.fir
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ func.func @_QQmain() {

// PASSES-NEXT: AddAliasTags

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

// PASSES-NEXT: SCFToControlFlow
// PASSES-NEXT: Canonicalizer
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: fir-opt --split-input-file --cfg-conversion-on-func-opt --fir-to-llvm-ir="target=aarch64-unknown-linux-gnu" %s | FileCheck %s
// RUN: fir-opt --split-input-file --cfg-conversion --fir-to-llvm-ir="target=aarch64-unknown-linux-gnu" %s | FileCheck %s

func.func @_QPsb1(%arg0: !fir.ref<i32> {fir.bindc_name = "n"}, %arg1: !fir.ref<!fir.array<?xi32>> {fir.bindc_name = "arr"}) {
%c1_i64 = arith.constant 1 : i64
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Fir/loop01.fir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: fir-opt --split-input-file --cfg-conversion-on-func-opt %s | FileCheck %s
// RUN: fir-opt --split-input-file --cfg-conversion %s | FileCheck %s

func.func @x(%lb : index, %ub : index, %step : index, %b : i1, %addr : !fir.ref<index>) {
fir.do_loop %iv = %lb to %ub step %step unordered {
Expand Down
4 changes: 2 additions & 2 deletions flang/test/Fir/loop02.fir
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: fir-opt --cfg-conversion-on-func-opt="always-execute-loop-body=true" %s | FileCheck %s
// RUN: fir-opt --cfg-conversion-on-func-opt %s | FileCheck %s --check-prefix=NOOPT
// RUN: fir-opt --cfg-conversion="always-execute-loop-body=true" %s | FileCheck %s
// RUN: fir-opt --cfg-conversion %s | FileCheck %s --check-prefix=NOOPT

func.func @x(%addr : !fir.ref<index>) {
%bound = arith.constant 452 : index
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Lower/OpenMP/FIR/flush.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
! This test checks lowering of OpenMP Flush Directive.

!RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp %s -o - | FileCheck %s --check-prefixes="FIRDialect,OMPDialect"
!RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp %s -o - | fir-opt --cfg-conversion-on-func-opt | fir-opt --fir-to-llvm-ir | FileCheck %s --check-prefixes="LLVMIRDialect,OMPDialect"
!RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp %s -o - | fir-opt --cfg-conversion | fir-opt --fir-to-llvm-ir | FileCheck %s --check-prefixes="LLVMIRDialect,OMPDialect"

subroutine flush_standalone(a, b, c)
integer, intent(inout) :: a, b, c
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Lower/OpenMP/FIR/master.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
!RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp %s -o - | FileCheck %s --check-prefixes="FIRDialect,OMPDialect"
!RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp %s -o - | fir-opt --cfg-conversion-on-func-opt | fir-opt --fir-to-llvm-ir | FileCheck %s --check-prefixes="OMPDialect"
!RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp %s -o - | fir-opt --cfg-conversion | fir-opt --fir-to-llvm-ir | FileCheck %s --check-prefixes="OMPDialect"

!===============================================================================
! parallel construct with function call which has master construct internally
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Lower/OpenMP/FIR/parallel-sections.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
! REQUIRES: openmp_runtime

!RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp %s -o - | FileCheck %s --check-prefixes="FIRDialect,OMPDialect"
!RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp %s -o - | fir-opt --cfg-conversion-on-func-opt | fir-opt --fir-to-llvm-ir | FileCheck %s --check-prefixes="OMPDialect,LLVMDialect"
!RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp %s -o - | fir-opt --cfg-conversion | fir-opt --fir-to-llvm-ir | FileCheck %s --check-prefixes="OMPDialect,LLVMDialect"

!===============================================================================
! Parallel sections construct
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Transforms/omp-reduction-cfg-conversion.fir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: fir-opt --cfg-conversion-on-reduce-opt %s | FileCheck %s
// RUN: fir-opt --cfg-conversion %s | FileCheck %s

omp.declare_reduction @add_reduction_i_32_box_3_byref : !fir.ref<!fir.box<!fir.array<3xi32>>> init {
^bb0(%arg0: !fir.ref<!fir.box<!fir.array<3xi32>>>):
Expand Down