Skip to content

Commit ceca523

Browse files
authored
[flang] de-duplicate CFGConversion pass (#89783)
See RFC at https://discourse.llvm.org/t/rfc-add-an-interface-for-top-level-container-operations I previously did the same for the AbstractResult pass #88867
1 parent 18bf0c3 commit ceca523

File tree

16 files changed

+44
-65
lines changed

16 files changed

+44
-65
lines changed

flang/include/flang/Optimizer/Transforms/Passes.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ namespace fir {
3737
#define GEN_PASS_DECL_ANNOTATECONSTANTOPERANDS
3838
#define GEN_PASS_DECL_ARRAYVALUECOPY
3939
#define GEN_PASS_DECL_CHARACTERCONVERSION
40-
#define GEN_PASS_DECL_CFGCONVERSIONONFUNC
41-
#define GEN_PASS_DECL_CFGCONVERSIONONREDUCTION
40+
#define GEN_PASS_DECL_CFGCONVERSION
4241
#define GEN_PASS_DECL_EXTERNALNAMECONVERSION
4342
#define GEN_PASS_DECL_MEMREFDATAFLOWOPT
4443
#define GEN_PASS_DECL_SIMPLIFYINTRINSICS
@@ -53,8 +52,6 @@ namespace fir {
5352
std::unique_ptr<mlir::Pass> createAffineDemotionPass();
5453
std::unique_ptr<mlir::Pass>
5554
createArrayValueCopyPass(fir::ArrayValueCopyOptions options = {});
56-
std::unique_ptr<mlir::Pass> createFirToCfgOnFuncPass();
57-
std::unique_ptr<mlir::Pass> createFirToCfgOnReductionPass();
5855
std::unique_ptr<mlir::Pass> createCharacterConversionPass();
5956
std::unique_ptr<mlir::Pass> createExternalNameConversionPass();
6057
std::unique_ptr<mlir::Pass>

flang/include/flang/Optimizer/Transforms/Passes.td

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ def CharacterConversion : Pass<"character-conversion"> {
137137
];
138138
}
139139

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

160-
def CFGConversionOnFunc : CFGConversionBase<"func", "mlir::func::FuncOp"> {
161-
let constructor = "::fir::createFirToCfgOnFuncPass()";
162-
}
163-
164-
def CFGConversionOnReduction : CFGConversionBase<"reduce", "mlir::omp::DeclareReductionOp"> {
165-
let constructor = "::fir::createFirToCfgOnReductionPass()";
166-
}
167-
168159
def ExternalNameConversion : Pass<"external-name-interop", "mlir::ModuleOp"> {
169160
let summary = "Convert name for external interoperability";
170161
let description = [{

flang/include/flang/Tools/CLOptions.inc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ DisableOption(BoxedProcedureRewrite, "boxed-procedure-rewrite",
8787
DisableOption(ExternalNameConversion, "external-name-interop",
8888
"convert names with external convention");
8989

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

9492
template <typename OP>
@@ -108,7 +106,12 @@ void addNestedPassToAllTopLevelOperations(
108106
addNestedPassToOps<mlir::func::FuncOp, mlir::omp::DeclareReductionOp,
109107
fir::GlobalOp>(pm, ctor);
110108
}
111-
#endif
109+
110+
void addNestedPassToAllTopLevelOperationsConditionally(mlir::PassManager &pm,
111+
llvm::cl::opt<bool> &disabled, PassConstructor ctor) {
112+
if (!disabled)
113+
addNestedPassToAllTopLevelOperations(pm, ctor);
114+
}
112115

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

148151
inline void addCfgConversionPass(mlir::PassManager &pm) {
149-
addNestedPassConditionally<mlir::func::FuncOp>(
150-
pm, disableCfgConversion, fir::createFirToCfgOnFuncPass);
151-
addNestedPassConditionally<mlir::omp::DeclareReductionOp>(
152-
pm, disableCfgConversion, fir::createFirToCfgOnReductionPass);
152+
addNestedPassToAllTopLevelOperationsConditionally(
153+
pm, disableCfgConversion, fir::createCFGConversion);
153154
}
154155

155156
inline void addAVC(

flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
#include "llvm/Support/CommandLine.h"
2525

2626
namespace fir {
27-
#define GEN_PASS_DEF_CFGCONVERSIONONFUNC
28-
#define GEN_PASS_DEF_CFGCONVERSIONONREDUCTION
27+
#define GEN_PASS_DEF_CFGCONVERSION
2928
#include "flang/Optimizer/Transforms/Passes.h.inc"
3029
} // namespace fir
3130

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

311310
/// Convert FIR structured control flow ops to CFG ops.
312-
template <typename Pass, template <typename> class PassBase>
313-
class CfgConversionTemplate : public PassBase<Pass> {
311+
class CfgConversion : public fir::impl::CFGConversionBase<CfgConversion> {
314312
public:
313+
using CFGConversionBase<CfgConversion>::CFGConversionBase;
314+
315315
void runOnOperation() override {
316316
auto *context = &this->getContext();
317317
mlir::RewritePatternSet patterns(context);
@@ -333,14 +333,6 @@ class CfgConversionTemplate : public PassBase<Pass> {
333333
}
334334
};
335335

336-
class CfgConversionOnFunc
337-
: public CfgConversionTemplate<CfgConversionOnFunc,
338-
::fir::impl::CFGConversionOnFuncBase> {};
339-
340-
class CfgConversionOnReduction
341-
: public CfgConversionTemplate<CfgConversionOnReduction,
342-
::fir::impl::CFGConversionOnReductionBase> {
343-
};
344336
} // namespace
345337

346338
/// Expose conversion rewriters to other passes
@@ -349,13 +341,3 @@ void fir::populateCfgConversionRewrites(mlir::RewritePatternSet &patterns,
349341
patterns.insert<CfgLoopConv, CfgIfConv, CfgIterWhileConv>(
350342
patterns.getContext(), forceLoopToExecuteOnce);
351343
}
352-
353-
/// Convert FIR's structured control flow ops to CFG ops. This
354-
/// conversion enables the `createLowerToCFGPass` to transform these to CFG
355-
/// form.
356-
std::unique_ptr<mlir::Pass> fir::createFirToCfgOnFuncPass() {
357-
return std::make_unique<CfgConversionOnFunc>();
358-
}
359-
std::unique_ptr<mlir::Pass> fir::createFirToCfgOnReductionPass() {
360-
return std::make_unique<CfgConversionOnReduction>();
361-
}

flang/test/Driver/bbc-mlir-pass-pipeline.f90

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@
3838
! CHECK-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
3939
! CHECK-NEXT: (S) 0 num-dce'd - Number of operations DCE'd
4040

41-
! CHECK-NEXT: Pipeline Collection : ['func.func', 'omp.declare_reduction']
41+
! CHECK-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction']
42+
! CHECK-NEXT: 'fir.global' Pipeline
43+
! CHECK-NEXT: CFGConversion
4244
! CHECK-NEXT: 'func.func' Pipeline
4345
! CHECK-NEXT: PolymorphicOpConversion
44-
! CHECK-NEXT: CFGConversionOnFunc
46+
! CHECK-NEXT: CFGConversion
4547
! CHECK-NEXT: 'omp.declare_reduction' Pipeline
46-
! CHECK-NEXT: CFGConversionOnReduction
48+
! CHECK-NEXT: CFGConversion
4749

4850
! CHECK-NEXT: SCFToControlFlow
4951
! CHECK-NEXT: Canonicalizer

flang/test/Driver/mlir-debug-pass-pipeline.f90

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@
5858
! ALL-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
5959
! ALL-NEXT: (S) 0 num-dce'd - Number of operations DCE'd
6060

61-
! ALL-NEXT: Pipeline Collection : ['func.func', 'omp.declare_reduction']
61+
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction']
62+
! ALL-NEXT: 'fir.global' Pipeline
63+
! ALL-NEXT: CFGConversion
6264
! ALL-NEXT: 'func.func' Pipeline
6365
! ALL-NEXT: PolymorphicOpConversion
64-
! ALL-NEXT: CFGConversionOnFunc
66+
! ALL-NEXT: CFGConversion
6567
! ALL-NEXT: 'omp.declare_reduction' Pipeline
66-
! ALL-NEXT: CFGConversionOnReduction
68+
! ALL-NEXT: CFGConversion
6769
! ALL-NEXT: SCFToControlFlow
6870
! ALL-NEXT: Canonicalizer
6971
! ALL-NEXT: SimplifyRegionLite

flang/test/Driver/mlir-pass-pipeline.f90

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@
5252
! O2-NEXT: 'func.func' Pipeline
5353
! O2-NEXT: PolymorphicOpConversion
5454
! O2-NEXT: AddAliasTags
55-
! ALL-NEXT: Pipeline Collection : ['func.func', 'omp.declare_reduction']
55+
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction']
56+
! ALL-NEXT: 'fir.global' Pipeline
57+
! ALL-NEXT: CFGConversion
5658
! ALL-NEXT: 'func.func' Pipeline
5759
! NOTO2-NEXT: PolymorphicOpConversion
58-
! ALL-NEXT: CFGConversionOnFunc
60+
! ALL-NEXT: CFGConversion
5961
! ALL-NEXT: 'omp.declare_reduction' Pipeline
60-
! ALL-NEXT: CFGConversionOnReduction
62+
! ALL-NEXT: CFGConversion
6163

6264
! ALL-NEXT: SCFToControlFlow
6365
! ALL-NEXT: Canonicalizer

flang/test/Fir/array-value-copy-2.fir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: fir-opt --array-value-copy --cfg-conversion-on-func-opt %s | FileCheck %s
2-
// RUN: fir-opt --array-value-copy="optimize-conflicts=true" --cfg-conversion-on-func-opt %s | FileCheck %s
1+
// RUN: fir-opt --array-value-copy --cfg-conversion %s | FileCheck %s
2+
// RUN: fir-opt --array-value-copy="optimize-conflicts=true" --cfg-conversion %s | FileCheck %s
33

44
// CHECK-LABEL: func @_QPslice1(
55
// CHECK-NOT: fir.allocmem

flang/test/Fir/basic-program.fir

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ func.func @_QQmain() {
6060

6161
// PASSES-NEXT: AddAliasTags
6262

63-
// PASSES-NEXT: Pipeline Collection : ['func.func', 'omp.declare_reduction']
63+
// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction']
64+
// PASSES-NEXT: 'fir.global' Pipeline
65+
// PASSES-NEXT: CFGConversion
6466
// PASSES-NEXT: 'func.func' Pipeline
65-
// PASSES-NEXT: CFGConversionOnFunc
67+
// PASSES-NEXT: CFGConversion
6668
// PASSES-NEXT: 'omp.declare_reduction' Pipeline
67-
// PASSES-NEXT: CFGConversionOnReduction
69+
// PASSES-NEXT: CFGConversion
6870

6971
// PASSES-NEXT: SCFToControlFlow
7072
// PASSES-NEXT: Canonicalizer

flang/test/Fir/convert-to-llvm-openmp-and-fir.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: fir-opt --split-input-file --cfg-conversion-on-func-opt --fir-to-llvm-ir="target=aarch64-unknown-linux-gnu" %s | FileCheck %s
1+
// RUN: fir-opt --split-input-file --cfg-conversion --fir-to-llvm-ir="target=aarch64-unknown-linux-gnu" %s | FileCheck %s
22

33
func.func @_QPsb1(%arg0: !fir.ref<i32> {fir.bindc_name = "n"}, %arg1: !fir.ref<!fir.array<?xi32>> {fir.bindc_name = "arr"}) {
44
%c1_i64 = arith.constant 1 : i64

flang/test/Fir/loop01.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: fir-opt --split-input-file --cfg-conversion-on-func-opt %s | FileCheck %s
1+
// RUN: fir-opt --split-input-file --cfg-conversion %s | FileCheck %s
22

33
func.func @x(%lb : index, %ub : index, %step : index, %b : i1, %addr : !fir.ref<index>) {
44
fir.do_loop %iv = %lb to %ub step %step unordered {

flang/test/Fir/loop02.fir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: fir-opt --cfg-conversion-on-func-opt="always-execute-loop-body=true" %s | FileCheck %s
2-
// RUN: fir-opt --cfg-conversion-on-func-opt %s | FileCheck %s --check-prefix=NOOPT
1+
// RUN: fir-opt --cfg-conversion="always-execute-loop-body=true" %s | FileCheck %s
2+
// RUN: fir-opt --cfg-conversion %s | FileCheck %s --check-prefix=NOOPT
33

44
func.func @x(%addr : !fir.ref<index>) {
55
%bound = arith.constant 452 : index

flang/test/Lower/OpenMP/FIR/flush.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
! This test checks lowering of OpenMP Flush Directive.
22

33
!RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp %s -o - | FileCheck %s --check-prefixes="FIRDialect,OMPDialect"
4-
!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"
4+
!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"
55

66
subroutine flush_standalone(a, b, c)
77
integer, intent(inout) :: a, b, c

flang/test/Lower/OpenMP/FIR/master.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
!RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp %s -o - | FileCheck %s --check-prefixes="FIRDialect,OMPDialect"
2-
!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"
2+
!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"
33

44
!===============================================================================
55
! parallel construct with function call which has master construct internally

flang/test/Lower/OpenMP/FIR/parallel-sections.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
! REQUIRES: openmp_runtime
22

33
!RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp %s -o - | FileCheck %s --check-prefixes="FIRDialect,OMPDialect"
4-
!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"
4+
!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"
55

66
!===============================================================================
77
! Parallel sections construct

flang/test/Transforms/omp-reduction-cfg-conversion.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: fir-opt --cfg-conversion-on-reduce-opt %s | FileCheck %s
1+
// RUN: fir-opt --cfg-conversion %s | FileCheck %s
22

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

0 commit comments

Comments
 (0)