Skip to content

Commit 1d4b5c1

Browse files
authored
[flang][cuda] Change how abstract result pass is scheduled on func.func and gpu.func (#119034)
Use `pm.nest` to schedule the pass on nested `func.func` and `gpu.func` in the `gpu.module`. AbstractResult pass is not meant to run on the whole gpu.module at once.
1 parent f15cc6f commit 1d4b5c1

File tree

6 files changed

+40
-61
lines changed

6 files changed

+40
-61
lines changed

flang/lib/Optimizer/Passes/Pipelines.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ namespace fir {
1616
void addNestedPassToAllTopLevelOperations(mlir::PassManager &pm,
1717
PassConstructor ctor) {
1818
addNestedPassToOps<mlir::func::FuncOp, mlir::omp::DeclareReductionOp,
19-
mlir::omp::PrivateClauseOp, fir::GlobalOp,
20-
mlir::gpu::GPUModuleOp>(pm, ctor);
19+
mlir::omp::PrivateClauseOp, fir::GlobalOp>(pm, ctor);
20+
}
21+
22+
void addPassToGPUModuleOperations(mlir::PassManager &pm, PassConstructor ctor) {
23+
mlir::OpPassManager &nestPM = pm.nest<mlir::gpu::GPUModuleOp>();
24+
nestPM.addNestedPass<mlir::func::FuncOp>(ctor());
25+
nestPM.addNestedPass<mlir::gpu::GPUFuncOp>(ctor());
2126
}
2227

2328
void addNestedPassToAllTopLevelOperationsConditionally(
@@ -266,6 +271,7 @@ void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
266271
llvm::StringRef inputFilename) {
267272
fir::addBoxedProcedurePass(pm);
268273
addNestedPassToAllTopLevelOperations(pm, fir::createAbstractResultOpt);
274+
addPassToGPUModuleOperations(pm, fir::createAbstractResultOpt);
269275
fir::addCodeGenRewritePass(
270276
pm, (config.DebugInfo != llvm::codegenoptions::NoDebugInfo));
271277
fir::addExternalNameConversionPass(pm, config.Underscoring);

flang/lib/Optimizer/Transforms/AbstractResult.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -460,17 +460,10 @@ class AbstractResultOpt
460460
const bool shouldBoxResult = this->passResultAsBox.getValue();
461461

462462
mlir::TypeSwitch<mlir::Operation *, void>(op)
463-
.Case<mlir::func::FuncOp, fir::GlobalOp>([&](auto op) {
464-
runOnSpecificOperation(op, shouldBoxResult, patterns, target);
465-
})
466-
.Case<mlir::gpu::GPUModuleOp>([&](auto op) {
467-
auto gpuMod = mlir::dyn_cast<mlir::gpu::GPUModuleOp>(*op);
468-
for (auto funcOp : gpuMod.template getOps<mlir::func::FuncOp>())
469-
runOnSpecificOperation(funcOp, shouldBoxResult, patterns, target);
470-
for (auto gpuFuncOp : gpuMod.template getOps<mlir::gpu::GPUFuncOp>())
471-
runOnSpecificOperation(gpuFuncOp, shouldBoxResult, patterns,
472-
target);
473-
});
463+
.Case<mlir::func::FuncOp, fir::GlobalOp, mlir::gpu::GPUFuncOp>(
464+
[&](auto op) {
465+
runOnSpecificOperation(op, shouldBoxResult, patterns, target);
466+
});
474467

475468
// Convert the calls and, if needed, the ReturnOp in the function body.
476469
target.addLegalDialect<fir::FIROpsDialect, mlir::arith::ArithDialect,

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
! CHECK-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
1818
! CHECK-NEXT: (S) 0 num-dce'd - Number of operations DCE'd
1919

20-
! CHECK-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
20+
! CHECK-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
2121
! CHECK-NEXT: 'fir.global' Pipeline
2222
! CHECK-NEXT: CharacterConversion
2323
! CHECK-NEXT: 'func.func' Pipeline
2424
! CHECK-NEXT: ArrayValueCopy
2525
! CHECK-NEXT: CharacterConversion
26-
! CHECK-NEXT: 'gpu.module' Pipeline
27-
! CHECK-NEXT: CharacterConversion
2826
! CHECK-NEXT: 'omp.declare_reduction' Pipeline
2927
! CHECK-NEXT: CharacterConversion
3028
! CHECK-NEXT: 'omp.private' Pipeline
@@ -50,16 +48,13 @@
5048
! CHECK-NEXT: PolymorphicOpConversion
5149
! CHECK-NEXT: AssumedRankOpConversion
5250

53-
! CHECK-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
51+
! CHECK-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
5452
! CHECK-NEXT: 'fir.global' Pipeline
5553
! CHECK-NEXT: StackReclaim
5654
! CHECK-NEXT: CFGConversion
5755
! CHECK-NEXT: 'func.func' Pipeline
5856
! CHECK-NEXT: StackReclaim
5957
! CHECK-NEXT: CFGConversion
60-
! CHECK-NEXT: 'gpu.module' Pipeline
61-
! CHECK-NEXT: StackReclaim
62-
! CHECK-NEXT: CFGConversion
6358
! CHECK-NEXT: 'omp.declare_reduction' Pipeline
6459
! CHECK-NEXT: StackReclaim
6560
! CHECK-NEXT: CFGConversion

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@
2828
! ALL: Pass statistics report
2929

3030
! ALL: Fortran::lower::VerifierPass
31-
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
31+
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
3232
! ALL-NEXT: 'fir.global' Pipeline
3333
! ALL-NEXT: InlineElementals
3434
! ALL-NEXT: 'func.func' Pipeline
3535
! ALL-NEXT: InlineElementals
36-
! ALL-NEXT: 'gpu.module' Pipeline
37-
! ALL-NEXT: InlineElementals
3836
! ALL-NEXT: 'omp.declare_reduction' Pipeline
3937
! ALL-NEXT: InlineElementals
4038
! ALL-NEXT: 'omp.private' Pipeline
@@ -51,14 +49,12 @@
5149
! ALL-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
5250
! ALL-NEXT: (S) 0 num-dce'd - Number of operations DCE'd
5351

54-
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
52+
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
5553
! ALL-NEXT: 'fir.global' Pipeline
5654
! ALL-NEXT: CharacterConversion
5755
! ALL-NEXT: 'func.func' Pipeline
5856
! ALL-NEXT: ArrayValueCopy
5957
! ALL-NEXT: CharacterConversion
60-
! ALL-NEXT: 'gpu.module' Pipeline
61-
! ALL-NEXT: CharacterConversion
6258
! ALL-NEXT: 'omp.declare_reduction' Pipeline
6359
! ALL-NEXT: CharacterConversion
6460
! ALL-NEXT: 'omp.private' Pipeline
@@ -82,16 +78,13 @@
8278
! ALL-NEXT: PolymorphicOpConversion
8379
! ALL-NEXT: AssumedRankOpConversion
8480

85-
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
81+
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
8682
! ALL-NEXT: 'fir.global' Pipeline
8783
! ALL-NEXT: StackReclaim
8884
! ALL-NEXT: CFGConversion
8985
! ALL-NEXT: 'func.func' Pipeline
9086
! ALL-NEXT: StackReclaim
9187
! ALL-NEXT: CFGConversion
92-
! ALL-NEXT: 'gpu.module' Pipeline
93-
! ALL-NEXT: StackReclaim
94-
! ALL-NEXT: CFGConversion
9588
! ALL-NEXT: 'omp.declare_reduction' Pipeline
9689
! ALL-NEXT: StackReclaim
9790
! ALL-NEXT: CFGConversion
@@ -112,7 +105,11 @@
112105
! ALL-NEXT: 'func.func' Pipeline
113106
! ALL-NEXT: AbstractResultOpt
114107
! ALL-NEXT: 'gpu.module' Pipeline
115-
! ALL-NEXT: AbstractResultOpt
108+
! ALL-NEXT: Pipeline Collection : ['func.func', 'gpu.func']
109+
! ALL-NEXT: 'func.func' Pipeline
110+
! ALL-NEXT: AbstractResultOpt
111+
! ALL-NEXT: 'gpu.func' Pipeline
112+
! ALL-NEXT: AbstractResultOpt
116113
! ALL-NEXT: 'omp.declare_reduction' Pipeline
117114
! ALL-NEXT: AbstractResultOpt
118115
! ALL-NEXT: 'omp.private' Pipeline

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616

1717
! ALL: Fortran::lower::VerifierPass
1818
! O2-NEXT: Canonicalizer
19-
! ALL: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
19+
! ALL: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
2020
! ALL-NEXT:'fir.global' Pipeline
2121
! O2-NEXT: SimplifyHLFIRIntrinsics
2222
! ALL: InlineElementals
2323
! ALL-NEXT:'func.func' Pipeline
2424
! O2-NEXT: SimplifyHLFIRIntrinsics
2525
! ALL: InlineElementals
26-
! ALL-NEXT:'gpu.module' Pipeline
27-
! O2-NEXT: SimplifyHLFIRIntrinsics
28-
! ALL: InlineElementals
2926
! ALL-NEXT:'omp.declare_reduction' Pipeline
3027
! O2-NEXT: SimplifyHLFIRIntrinsics
3128
! ALL: InlineElementals
@@ -36,13 +33,11 @@
3633
! O2-NEXT: CSE
3734
! O2-NEXT: (S) {{.*}} num-cse'd
3835
! O2-NEXT: (S) {{.*}} num-dce'd
39-
! O2-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
36+
! O2-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
4037
! O2-NEXT: 'fir.global' Pipeline
4138
! O2-NEXT: OptimizedBufferization
4239
! O2-NEXT: 'func.func' Pipeline
4340
! O2-NEXT: OptimizedBufferization
44-
! O2-NEXT: 'gpu.module' Pipeline
45-
! O2-NEXT: OptimizedBufferization
4641
! O2-NEXT: 'omp.declare_reduction' Pipeline
4742
! O2-NEXT: OptimizedBufferization
4843
! O2-NEXT: 'omp.private' Pipeline
@@ -59,14 +54,12 @@
5954
! ALL-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
6055
! ALL-NEXT: (S) 0 num-dce'd - Number of operations DCE'd
6156

62-
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
57+
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
6358
! ALL-NEXT: 'fir.global' Pipeline
6459
! ALL-NEXT: CharacterConversion
6560
! ALL-NEXT: 'func.func' Pipeline
6661
! ALL-NEXT: ArrayValueCopy
6762
! ALL-NEXT: CharacterConversion
68-
! ALL-NEXT: 'gpu.module' Pipeline
69-
! ALL-NEXT: CharacterConversion
7063
! ALL-NEXT: 'omp.declare_reduction' Pipeline
7164
! ALL-NEXT: CharacterConversion
7265
! ALL-NEXT: 'omp.private' Pipeline
@@ -93,16 +86,13 @@
9386
! ALL-NEXT: AssumedRankOpConversion
9487
! O2-NEXT: AddAliasTags
9588

96-
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
89+
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
9790
! ALL-NEXT: 'fir.global' Pipeline
9891
! ALL-NEXT: StackReclaim
9992
! ALL-NEXT: CFGConversion
10093
! ALL-NEXT: 'func.func' Pipeline
10194
! ALL-NEXT: StackReclaim
10295
! ALL-NEXT: CFGConversion
103-
! ALL-NEXT: 'gpu.module' Pipeline
104-
! ALL-NEXT: StackReclaim
105-
! ALL-NEXT: CFGConversion
10696
! ALL-NEXT: 'omp.declare_reduction' Pipeline
10797
! ALL-NEXT: StackReclaim
10898
! ALL-NEXT: CFGConversion
@@ -124,7 +114,11 @@
124114
! ALL-NEXT: 'func.func' Pipeline
125115
! ALL-NEXT: AbstractResultOpt
126116
! ALL-NEXT: 'gpu.module' Pipeline
127-
! ALL-NEXT: AbstractResultOpt
117+
! ALL-NEXT: Pipeline Collection : ['func.func', 'gpu.func']
118+
! ALL-NEXT: 'func.func' Pipeline
119+
! ALL-NEXT: AbstractResultOpt
120+
! ALL-NEXT: 'gpu.func' Pipeline
121+
! ALL-NEXT: AbstractResultOpt
128122
! ALL-NEXT: 'omp.declare_reduction' Pipeline
129123
! ALL-NEXT: AbstractResultOpt
130124
! ALL-NEXT: 'omp.private' Pipeline

flang/test/Fir/basic-program.fir

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@ func.func @_QQmain() {
1717
// PASSES: Pass statistics report
1818

1919
// PASSES: Canonicalizer
20-
// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
20+
// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
2121
// PASSES-NEXT: 'fir.global' Pipeline
2222
// PASSES-NEXT: SimplifyHLFIRIntrinsics
2323
// PASSES-NEXT: InlineElementals
2424
// PASSES-NEXT: 'func.func' Pipeline
2525
// PASSES-NEXT: SimplifyHLFIRIntrinsics
2626
// PASSES-NEXT: InlineElementals
27-
// PASSES-NEXT: 'gpu.module' Pipeline
28-
// PASSES-NEXT: SimplifyHLFIRIntrinsics
29-
// PASSES-NEXT: InlineElementals
3027
// PASSES-NEXT: 'omp.declare_reduction' Pipeline
3128
// PASSES-NEXT: SimplifyHLFIRIntrinsics
3229
// PASSES-NEXT: InlineElementals
@@ -37,13 +34,11 @@ func.func @_QQmain() {
3734
// PASSES-NEXT: CSE
3835
// PASSES-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
3936
// PASSES-NEXT: (S) 0 num-dce'd - Number of operations DCE'd
40-
// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
37+
// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
4138
// PASSES-NEXT: 'fir.global' Pipeline
4239
// PASSES-NEXT: OptimizedBufferization
4340
// PASSES-NEXT: 'func.func' Pipeline
4441
// PASSES-NEXT: OptimizedBufferization
45-
// PASSES-NEXT: 'gpu.module' Pipeline
46-
// PASSES-NEXT: OptimizedBufferization
4742
// PASSES-NEXT: 'omp.declare_reduction' Pipeline
4843
// PASSES-NEXT: OptimizedBufferization
4944
// PASSES-NEXT: 'omp.private' Pipeline
@@ -57,14 +52,12 @@ func.func @_QQmain() {
5752
// PASSES-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
5853
// PASSES-NEXT: (S) 0 num-dce'd - Number of operations DCE'd
5954

60-
// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
55+
// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
6156
// PASSES-NEXT: 'fir.global' Pipeline
6257
// PASSES-NEXT: CharacterConversion
6358
// PASSES-NEXT: 'func.func' Pipeline
6459
// PASSES-NEXT: ArrayValueCopy
6560
// PASSES-NEXT: CharacterConversion
66-
// PASSES-NEXT: 'gpu.module' Pipeline
67-
// PASSES-NEXT: CharacterConversion
6861
// PASSES-NEXT: 'omp.declare_reduction' Pipeline
6962
// PASSES-NEXT: CharacterConversion
7063
// PASSES-NEXT: 'omp.private' Pipeline
@@ -91,16 +84,13 @@ func.func @_QQmain() {
9184
// PASSES-NEXT: AssumedRankOpConversion
9285
// PASSES-NEXT: AddAliasTags
9386

94-
// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']
87+
// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
9588
// PASSES-NEXT: 'fir.global' Pipeline
9689
// PASSES-NEXT: StackReclaim
9790
// PASSES-NEXT: CFGConversion
9891
// PASSES-NEXT: 'func.func' Pipeline
9992
// PASSES-NEXT: StackReclaim
10093
// PASSES-NEXT: CFGConversion
101-
// PASSES-NEXT: 'gpu.module' Pipeline
102-
// PASSES-NEXT: StackReclaim
103-
// PASSES-NEXT: CFGConversion
10494
// PASSES-NEXT: 'omp.declare_reduction' Pipeline
10595
// PASSES-NEXT: StackReclaim
10696
// PASSES-NEXT: CFGConversion
@@ -122,7 +112,11 @@ func.func @_QQmain() {
122112
// PASSES-NEXT: 'func.func' Pipeline
123113
// PASSES-NEXT: AbstractResultOpt
124114
// PASSES-NEXT: 'gpu.module' Pipeline
125-
// PASSES-NEXT: AbstractResultOpt
115+
// PASSES-NEXT: Pipeline Collection : ['func.func', 'gpu.func']
116+
// PASSES-NEXT: 'func.func' Pipeline
117+
// PASSES-NEXT: AbstractResultOpt
118+
// PASSES-NEXT: 'gpu.func' Pipeline
119+
// PASSES-NEXT: AbstractResultOpt
126120
// PASSES-NEXT: 'omp.declare_reduction' Pipeline
127121
// PASSES-NEXT: AbstractResultOpt
128122
// PASSES-NEXT: 'omp.private' Pipeline

0 commit comments

Comments
 (0)