Skip to content

Commit ea6a6e5

Browse files
author
yanming
committed
[flang][fir] Add affine optimization pass pipeline.
1 parent cd6c4b6 commit ea6a6e5

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

flang/include/flang/Optimizer/Passes/CommandLineOpts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern llvm::cl::opt<bool> disableCfgConversion;
4242
extern llvm::cl::opt<bool> disableFirAvc;
4343
extern llvm::cl::opt<bool> disableFirMao;
4444

45+
extern llvm::cl::opt<bool> enableAffineOpt;
4546
extern llvm::cl::opt<bool> disableFirAliasTags;
4647
extern llvm::cl::opt<bool> useOldAliasTags;
4748

flang/include/flang/Optimizer/Passes/Pipelines.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
#include "flang/Optimizer/Passes/CommandLineOpts.h"
1919
#include "flang/Optimizer/Transforms/Passes.h"
2020
#include "flang/Tools/CrossToolHelpers.h"
21+
#include "mlir/Conversion/AffineToStandard/AffineToStandard.h"
2122
#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
2223
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
24+
#include "mlir/Conversion/SCFToOpenMP/SCFToOpenMP.h"
25+
#include "mlir/Dialect/Affine/Passes.h"
2326
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
2427
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
2528
#include "mlir/Pass/PassManager.h"

flang/lib/Optimizer/Passes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_flang_library(flangPasses
2121
MLIRPass
2222
MLIRReconcileUnrealizedCasts
2323
MLIRSCFToControlFlow
24+
MLIRSCFToOpenMP
2425
MLIRSupport
2526
MLIRTransforms
2627
)

flang/lib/Optimizer/Passes/CommandLineOpts.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ cl::opt<bool> useOldAliasTags(
5555
cl::desc("Use a single TBAA tree for all functions and do not use "
5656
"the FIR alias tags pass"),
5757
cl::init(false), cl::Hidden);
58+
EnableOption(AffineOpt, "affine-opt", "affine optimization");
5859

5960
/// CodeGen Passes
6061
DisableOption(CodeGenRewrite, "codegen-rewrite", "rewrite FIR for codegen");

flang/lib/Optimizer/Passes/Pipelines.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,25 @@ void createDefaultFIROptimizerPassPipeline(mlir::PassManager &pm,
209209
if (pc.AliasAnalysis && !disableFirAliasTags && !useOldAliasTags)
210210
pm.addPass(fir::createAddAliasTags());
211211

212+
if (enableAffineOpt && pc.OptLevel.isOptimizingForSpeed()) {
213+
pm.addPass(fir::createPromoteToAffinePass());
214+
pm.addPass(mlir::createCSEPass());
215+
pm.addPass(mlir::affine::createAffineLoopInvariantCodeMotionPass());
216+
pm.addPass(mlir::affine::createAffineLoopNormalizePass());
217+
pm.addPass(mlir::affine::createSimplifyAffineStructuresPass());
218+
pm.addPass(mlir::affine::createAffineParallelize(
219+
mlir::affine::AffineParallelizeOptions{1, false}));
220+
pm.addPass(fir::createAffineDemotionPass());
221+
pm.addPass(mlir::createLowerAffinePass());
222+
if (pc.EnableOpenMP) {
223+
pm.addPass(mlir::createConvertSCFToOpenMPPass());
224+
pm.addPass(mlir::createCanonicalizerPass());
225+
}
226+
}
227+
212228
addNestedPassToAllTopLevelOperations<PassConstructor>(
213229
pm, fir::createStackReclaim);
230+
214231
// convert control flow to CFG form
215232
fir::addCfgConversionPass(pm, pc);
216233
pm.addPass(mlir::createSCFToControlFlowPass());

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
! -O0 is the default:
55
! RUN: %flang_fc1 -S -mmlir --mlir-pass-statistics -mmlir --mlir-pass-statistics-display=pipeline %s -O0 -o /dev/null 2>&1 | FileCheck --check-prefixes=ALL %s
66
! RUN: %flang_fc1 -S -mmlir --mlir-pass-statistics -mmlir --mlir-pass-statistics-display=pipeline %s -O2 -o /dev/null 2>&1 | FileCheck --check-prefixes=ALL,O2 %s
7+
! RUN: %flang_fc1 -S -mmlir --mlir-pass-statistics -mmlir --mlir-pass-statistics-display=pipeline -mllvm --enable-affine-opt %s -O2 -o /dev/null 2>&1 | FileCheck --check-prefixes=ALL,O2,AFFINE %s
78

89
! REQUIRES: asserts
910

@@ -105,6 +106,19 @@
105106
! ALL-NEXT: SimplifyFIROperations
106107
! O2-NEXT: AddAliasTags
107108

109+
! AFFINE-NEXT: 'func.func' Pipeline
110+
! AFFINE-NEXT: AffineDialectPromotion
111+
! AFFINE-NEXT: CSE
112+
! AFFINE-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
113+
! AFFINE-NEXT: (S) 0 num-dce'd - Number of operations DCE'd
114+
! AFFINE-NEXT: 'func.func' Pipeline
115+
! AFFINE-NEXT: AffineLoopInvariantCodeMotion
116+
! AFFINE-NEXT: AffineLoopNormalize
117+
! AFFINE-NEXT: SimplifyAffineStructures
118+
! AFFINE-NEXT: AffineParallelize
119+
! AFFINE-NEXT: AffineDialectDemotion
120+
! AFFINE-NEXT: LowerAffinePass
121+
108122
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'omp.declare_reduction', 'omp.private']
109123
! ALL-NEXT: 'fir.global' Pipeline
110124
! ALL-NEXT: StackReclaim
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
! RUN: %flang_fc1 -O1 -mllvm --enable-affine-opt -emit-llvm -fopenmp -o - %s \
2+
! RUN: | FileCheck %s
3+
4+
!CHECK-LABEL: entry:
5+
!CHECK: %[[VAL_0:.*]] = alloca { ptr }, align 8
6+
!CHECK: %[[VAL_1:.*]] = tail call i32 @__kmpc_global_thread_num(ptr nonnull @1)
7+
!CHECK: store ptr %[[VAL_2:.*]], ptr %[[VAL_0]], align 8
8+
!CHECK: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr nonnull @1, i32 1, ptr nonnull @foo_..omp_par, ptr nonnull %[[VAL_0]])
9+
!CHECK: ret void
10+
!CHECK: omp.par.entry:
11+
!CHECK: %[[VAL_3:.*]] = load ptr, ptr %[[VAL_4:.*]], align 8, !align !3
12+
!CHECK: %[[VAL_5:.*]] = alloca i32, align 4
13+
!CHECK: %[[VAL_6:.*]] = alloca i64, align 8
14+
!CHECK: %[[VAL_7:.*]] = alloca i64, align 8
15+
!CHECK: %[[VAL_8:.*]] = alloca i64, align 8
16+
!CHECK: store i64 0, ptr %[[VAL_6]], align 8
17+
!CHECK: store i64 99, ptr %[[VAL_7]], align 8
18+
!CHECK: store i64 1, ptr %[[VAL_8]], align 8
19+
!CHECK: %[[VAL_9:.*]] = tail call i32 @__kmpc_global_thread_num(ptr nonnull @1)
20+
!CHECK: call void @__kmpc_for_static_init_8u(ptr nonnull @1, i32 %[[VAL_9]], i32 34, ptr nonnull %[[VAL_5]], ptr nonnull %[[VAL_6]], ptr nonnull %[[VAL_7]], ptr nonnull %[[VAL_8]], i64 1, i64 0)
21+
!CHECK: %[[VAL_10:.*]] = load i64, ptr %[[VAL_6]], align 8
22+
!CHECK: %[[VAL_11:.*]] = load i64, ptr %[[VAL_7]], align 8
23+
!CHECK: %[[VAL_12:.*]] = sub i64 %[[VAL_11]], %[[VAL_10]]
24+
!CHECK: %[[VAL_13:.*]] = icmp eq i64 %[[VAL_12]], -1
25+
!CHECK: br i1 %[[VAL_13]], label %[[VAL_14:.*]], label %[[VAL_15:.*]]
26+
!CHECK: omp_loop.exit: ; preds = %[[VAL_16:.*]], %[[VAL_17:.*]]
27+
!CHECK: call void @__kmpc_for_static_fini(ptr nonnull @1, i32 %[[VAL_9]])
28+
!CHECK: %[[VAL_18:.*]] = call i32 @__kmpc_global_thread_num(ptr nonnull @1)
29+
!CHECK: call void @__kmpc_barrier(ptr nonnull @2, i32 %[[VAL_18]])
30+
!CHECK: ret void
31+
!CHECK: omp_loop.body: ; preds = %[[VAL_17]], %[[VAL_16]]
32+
!CHECK: %[[VAL_19:.*]] = phi i64 [ %[[VAL_20:.*]], %[[VAL_16]] ], [ 0, %[[VAL_17]] ]
33+
!CHECK: %[[VAL_21:.*]] = add i64 %[[VAL_19]], %[[VAL_10]]
34+
!CHECK: %[[VAL_22:.*]] = mul i64 %[[VAL_21]], 400
35+
!CHECK: %[[VAL_23:.*]] = getelementptr i8, ptr %[[VAL_3]], i64 %[[VAL_22]]
36+
!CHECK: br label %[[VAL_24:.*]]
37+
!CHECK: omp_loop.inc: ; preds = %[[VAL_24]]
38+
!CHECK: %[[VAL_20]] = add nuw i64 %[[VAL_19]], 1
39+
!CHECK: %[[VAL_25:.*]] = icmp eq i64 %[[VAL_19]], %[[VAL_12]]
40+
!CHECK: br i1 %[[VAL_25]], label %[[VAL_14]], label %[[VAL_15]]
41+
!CHECK: omp.loop_nest.region6: ; preds = %[[VAL_15]], %[[VAL_24]]
42+
!CHECK: %[[VAL_26:.*]] = phi i64 [ 0, %[[VAL_15]] ], [ %[[VAL_27:.*]], %[[VAL_24]] ]
43+
!CHECK: %[[VAL_28:.*]] = getelementptr i32, ptr %[[VAL_23]], i64 %[[VAL_26]]
44+
!CHECK: store i32 1, ptr %[[VAL_28]], align 4, !tbaa !4
45+
!CHECK: %[[VAL_27]] = add nuw nsw i64 %[[VAL_26]], 1
46+
!CHECK: %[[VAL_29:.*]] = icmp eq i64 %[[VAL_27]], 100
47+
!CHECK: br i1 %[[VAL_29]], label %[[VAL_16]], label %[[VAL_24]]
48+
49+
subroutine foo(a)
50+
integer, dimension(100, 100), intent(out) :: a
51+
a = 1
52+
end subroutine foo

0 commit comments

Comments
 (0)