Skip to content

Commit 1446f96

Browse files
committed
Add an option to control this conversion.
1 parent 45a330f commit 1446f96

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ std::unique_ptr<mlir::Pass> createVScaleAttrPass();
5353
std::unique_ptr<mlir::Pass>
5454
createVScaleAttrPass(std::pair<unsigned, unsigned> vscaleAttr);
5555

56+
void populateFIRToSCFRewrites(mlir::RewritePatternSet &patterns,
57+
bool parallelUnordered = false);
58+
5659
void populateCfgConversionRewrites(mlir::RewritePatternSet &patterns,
5760
bool forceLoopToExecuteOnce = false,
5861
bool setNSW = true);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ def FIRToSCFPass : Pass<"fir-to-scf"> {
8585
let dependentDialects = [
8686
"fir::FIROpsDialect", "mlir::scf::SCFDialect"
8787
];
88+
let options = [Option<"parallelUnordered", "parallel-unordered", "bool",
89+
/*default=*/"false",
90+
"Whether to convert `fir.do_loop` with the unordered "
91+
"attribute to `scf.parallel`. Defaults to false.">];
8892
}
8993

9094
def AnnotateConstantOperands : Pass<"annotate-constant"> {

flang/lib/Optimizer/Transforms/FIRToSCF.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ class FIRToSCFPass : public fir::impl::FIRToSCFPassBase<FIRToSCFPass> {
2525
struct DoLoopConversion : public mlir::OpRewritePattern<fir::DoLoopOp> {
2626
using OpRewritePattern<fir::DoLoopOp>::OpRewritePattern;
2727

28+
DoLoopConversion(mlir::MLIRContext *context,
29+
bool parallelUnorderedLoop = false,
30+
mlir::PatternBenefit benefit = 1)
31+
: OpRewritePattern<fir::DoLoopOp>(context, benefit),
32+
parallelUnorderedLoop(parallelUnorderedLoop) {}
33+
2834
mlir::LogicalResult
2935
matchAndRewrite(fir::DoLoopOp doLoopOp,
3036
mlir::PatternRewriter &rewriter) const override {
@@ -57,7 +63,7 @@ struct DoLoopConversion : public mlir::OpRewritePattern<fir::DoLoopOp> {
5763

5864
// Create the scf.for or scf.parallel operation
5965
mlir::Operation *scfLoopOp = nullptr;
60-
if (isUnordered) {
66+
if (isUnordered && parallelUnorderedLoop) {
6167
scfLoopOp = mlir::scf::ParallelOp::create(rewriter, loc, {zero},
6268
{tripCount}, {one}, iterArgs);
6369
} else {
@@ -99,6 +105,9 @@ struct DoLoopConversion : public mlir::OpRewritePattern<fir::DoLoopOp> {
99105
rewriter.replaceOp(doLoopOp, scfLoopOp);
100106
return mlir::success();
101107
}
108+
109+
private:
110+
bool parallelUnorderedLoop;
102111
};
103112

104113
struct IterWhileConversion : public mlir::OpRewritePattern<fir::IterWhileOp> {
@@ -210,10 +219,15 @@ struct IfConversion : public mlir::OpRewritePattern<fir::IfOp> {
210219
};
211220
} // namespace
212221

222+
void fir::populateFIRToSCFRewrites(mlir::RewritePatternSet &patterns,
223+
bool parallelUnordered) {
224+
patterns.add<IterWhileConversion, IfConversion>(patterns.getContext());
225+
patterns.add<DoLoopConversion>(patterns.getContext(), parallelUnordered);
226+
}
227+
213228
void FIRToSCFPass::runOnOperation() {
214229
mlir::RewritePatternSet patterns(&getContext());
215-
patterns.add<DoLoopConversion, IterWhileConversion, IfConversion>(
216-
patterns.getContext());
230+
fir::populateFIRToSCFRewrites(patterns, parallelUnordered);
217231
walkAndApplyPatterns(getOperation(), std::move(patterns));
218232
}
219233

flang/test/Fir/FirToSCF/do-loop.fir

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: fir-opt %s --fir-to-scf --split-input-file | FileCheck %s
1+
// RUN: fir-opt %s --fir-to-scf --split-input-file | FileCheck %s --check-prefixes=CHECK,NO-PARALLEL
2+
// RUN: fir-opt %s --fir-to-scf='parallel-unordered' --split-input-file | FileCheck %s --check-prefixes=CHECK,PARALLEL
23

34
// CHECK-LABEL: func.func @simple_loop(
45
// CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<100xi32>>) {
@@ -165,12 +166,13 @@ func.func @loop_with_final_value(%arg0: !fir.ref<!fir.array<100xi32>>, %arg1: !f
165166
// CHECK: %[[DIVSI_0:.*]] = arith.divsi %[[ADDI_0]], %[[CONSTANT_0]] : index
166167
// CHECK: %[[CONSTANT_3:.*]] = arith.constant 0 : index
167168
// CHECK: %[[CONSTANT_4:.*]] = arith.constant 1 : index
168-
// CHECK: scf.parallel (%[[VAL_0:.*]]) = (%[[CONSTANT_3]]) to (%[[DIVSI_0]]) step (%[[CONSTANT_4]]) {
169+
// PARALLEL: scf.parallel (%[[VAL_0:.*]]) = (%[[CONSTANT_3]]) to (%[[DIVSI_0]]) step (%[[CONSTANT_4]]) {
170+
// NO-PARALLEL: scf.for %[[VAL_0:.*]] = %[[CONSTANT_3]] to %[[DIVSI_0]] step %[[CONSTANT_4]] {
169171
// CHECK: %[[MULI_0:.*]] = arith.muli %[[VAL_0]], %[[CONSTANT_0]] : index
170172
// CHECK: %[[ADDI_1:.*]] = arith.addi %[[CONSTANT_0]], %[[MULI_0]] : index
171173
// CHECK: %[[ARRAY_COOR_0:.*]] = fir.array_coor %[[ARG0]](%[[SHAPE_0]]) %[[ADDI_1]] : (!fir.ref<!fir.array<100xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
172174
// CHECK: fir.store %[[CONSTANT_2]] to %[[ARRAY_COOR_0]] : !fir.ref<i32>
173-
// CHECK: scf.reduce
175+
// PARALLEL: scf.reduce
174176
// CHECK: }
175177
// CHECK: return
176178
// CHECK: }

0 commit comments

Comments
 (0)