Skip to content

Commit 0436d9f

Browse files
author
Aviad Cohen
committed
[mlir][scf]: Expose emitNormalizedLoopBounds/denormalizeInductionVariable util functions
* Also updated normarlize/denormalize loop bounds to be folded if possible.
1 parent de37c06 commit 0436d9f

File tree

3 files changed

+55
-55
lines changed

3 files changed

+55
-55
lines changed

mlir/include/mlir/Dialect/SCF/Utils/Utils.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,31 @@ LogicalResult loopUnrollByFactor(
120120
scf::ForOp forOp, uint64_t unrollFactor,
121121
function_ref<void(unsigned, Operation *, OpBuilder)> annotateFn = nullptr);
122122

123+
/// This structure is to pass and return sets of loop parameters without
124+
/// confusing the order.
125+
struct LoopParams {
126+
OpFoldResult lowerBound;
127+
OpFoldResult upperBound;
128+
OpFoldResult step;
129+
};
130+
131+
/// Transform a loop with a strictly positive step
132+
/// for %i = %lb to %ub step %s
133+
/// into a 0-based loop with step 1
134+
/// for %ii = 0 to ceildiv(%ub - %lb, %s) step 1 {
135+
/// %i = %ii * %s + %lb
136+
/// Insert the induction variable remapping in the body of `inner`, which is
137+
/// expected to be either `loop` or another loop perfectly nested under `loop`.
138+
/// Insert the definition of new bounds immediate before `outer`, which is
139+
/// expected to be either `loop` or its parent in the loop nest.
140+
LoopParams emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
141+
Value lb, Value ub, Value step);
142+
143+
/// Get back the original induction variable values after loop normalization.
144+
void denormalizeInductionVariable(RewriterBase &rewriter, Location loc,
145+
Value normalizedIv, Value origLb,
146+
Value origStep);
147+
123148
/// Tile a nest of standard for loops rooted at `rootForOp` by finding such
124149
/// parametric tile sizes that the outer loops have a fixed number of iterations
125150
/// as defined in `sizes`.

mlir/lib/Dialect/SCF/Utils/Utils.cpp

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "mlir/Dialect/SCF/IR/SCF.h"
1919
#include "mlir/IR/BuiltinOps.h"
2020
#include "mlir/IR/IRMapping.h"
21+
#include "mlir/IR/OpDefinition.h"
2122
#include "mlir/IR/PatternMatch.h"
2223
#include "mlir/Interfaces/SideEffectInterfaces.h"
2324
#include "mlir/Support/MathExtras.h"
@@ -29,16 +30,6 @@
2930

3031
using namespace mlir;
3132

32-
namespace {
33-
// This structure is to pass and return sets of loop parameters without
34-
// confusing the order.
35-
struct LoopParams {
36-
Value lowerBound;
37-
Value upperBound;
38-
Value step;
39-
};
40-
} // namespace
41-
4233
SmallVector<scf::ForOp> mlir::replaceLoopNestWithNewYields(
4334
RewriterBase &rewriter, MutableArrayRef<scf::ForOp> loopNest,
4435
ValueRange newIterOperands, const NewYieldValuesFn &newYieldValuesFn,
@@ -473,17 +464,8 @@ LogicalResult mlir::loopUnrollByFactor(
473464
return success();
474465
}
475466

476-
/// Transform a loop with a strictly positive step
477-
/// for %i = %lb to %ub step %s
478-
/// into a 0-based loop with step 1
479-
/// for %ii = 0 to ceildiv(%ub - %lb, %s) step 1 {
480-
/// %i = %ii * %s + %lb
481-
/// Insert the induction variable remapping in the body of `inner`, which is
482-
/// expected to be either `loop` or another loop perfectly nested under `loop`.
483-
/// Insert the definition of new bounds immediate before `outer`, which is
484-
/// expected to be either `loop` or its parent in the loop nest.
485-
static LoopParams emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
486-
Value lb, Value ub, Value step) {
467+
LoopParams mlir::emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
468+
Value lb, Value ub, Value step) {
487469
// For non-index types, generate `arith` instructions
488470
// Check if the loop is already known to have a constant zero lower bound or
489471
// a constant one step.
@@ -501,26 +483,27 @@ static LoopParams emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
501483
if (isZeroBased && isStepOne)
502484
return {lb, ub, step};
503485

504-
Value diff = isZeroBased ? ub : rewriter.create<arith::SubIOp>(loc, ub, lb);
505-
Value newUpperBound =
506-
isStepOne ? diff : rewriter.create<arith::CeilDivSIOp>(loc, diff, step);
507-
508-
Value newLowerBound = isZeroBased
509-
? lb
510-
: rewriter.create<arith::ConstantOp>(
511-
loc, rewriter.getZeroAttr(lb.getType()));
512-
Value newStep = isStepOne
513-
? step
514-
: rewriter.create<arith::ConstantOp>(
515-
loc, rewriter.getIntegerAttr(step.getType(), 1));
486+
auto diff =
487+
isZeroBased ? ub : rewriter.createOrFold<arith::SubIOp>(loc, ub, lb);
488+
auto newUpperBound =
489+
isStepOne ? diff
490+
: rewriter.createOrFold<arith::CeilDivSIOp>(loc, diff, step);
491+
492+
auto newLowerBound = isZeroBased
493+
? lb
494+
: rewriter.createOrFold<arith::ConstantOp>(
495+
loc, rewriter.getZeroAttr(lb.getType()));
496+
auto newStep = isStepOne
497+
? step
498+
: rewriter.createOrFold<arith::ConstantOp>(
499+
loc, rewriter.getIntegerAttr(step.getType(), 1));
516500

517501
return {newLowerBound, newUpperBound, newStep};
518502
}
519503

520-
/// Get back the original induction variable values after loop normalization
521-
static void denormalizeInductionVariable(RewriterBase &rewriter, Location loc,
522-
Value normalizedIv, Value origLb,
523-
Value origStep) {
504+
void mlir::denormalizeInductionVariable(RewriterBase &rewriter, Location loc,
505+
Value normalizedIv, Value origLb,
506+
Value origStep) {
524507
Value denormalizedIv;
525508
SmallPtrSet<Operation *, 2> preserve;
526509
bool isStepOne = isConstantIntValue(origStep, 1);
@@ -638,9 +621,9 @@ LogicalResult mlir::coalesceLoops(RewriterBase &rewriter,
638621
emitNormalizedLoopBounds(rewriter, loop.getLoc(), lb, ub, step);
639622

640623
rewriter.modifyOpInPlace(loop, [&]() {
641-
loop.setLowerBound(newLoopParams.lowerBound);
642-
loop.setUpperBound(newLoopParams.upperBound);
643-
loop.setStep(newLoopParams.step);
624+
loop.setLowerBound(cast<Value>(newLoopParams.lowerBound));
625+
loop.setUpperBound(cast<Value>(newLoopParams.upperBound));
626+
loop.setStep(cast<Value>(newLoopParams.step));
644627
});
645628

646629
rewriter.setInsertionPointToStart(innermost.getBody());
@@ -778,18 +761,15 @@ void mlir::collapseParallelLoops(
778761
llvm::sort(dims);
779762

780763
// Normalize ParallelOp's iteration pattern.
781-
SmallVector<Value, 3> normalizedLowerBounds, normalizedSteps,
782-
normalizedUpperBounds;
764+
SmallVector<Value, 3> normalizedUpperBounds;
783765
for (unsigned i = 0, e = loops.getNumLoops(); i < e; ++i) {
784766
OpBuilder::InsertionGuard g2(rewriter);
785767
rewriter.setInsertionPoint(loops);
786768
Value lb = loops.getLowerBound()[i];
787769
Value ub = loops.getUpperBound()[i];
788770
Value step = loops.getStep()[i];
789771
auto newLoopParams = emitNormalizedLoopBounds(rewriter, loc, lb, ub, step);
790-
normalizedLowerBounds.push_back(newLoopParams.lowerBound);
791-
normalizedUpperBounds.push_back(newLoopParams.upperBound);
792-
normalizedSteps.push_back(newLoopParams.step);
772+
normalizedUpperBounds.push_back(cast<Value>(newLoopParams.upperBound));
793773

794774
rewriter.setInsertionPointToStart(loops.getBody());
795775
denormalizeInductionVariable(rewriter, loc, loops.getInductionVars()[i], lb,

mlir/test/Dialect/Affine/loop-coalescing.mlir

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,27 @@ func.func @multi_use() {
7474

7575
func.func @unnormalized_loops() {
7676
// CHECK: %[[orig_step_i:.*]] = arith.constant 2
77-
// CHECK: %[[orig_step_j:.*]] = arith.constant 3
77+
78+
// CHECK: %[[orig_step_j_and_numiter_i:.*]] = arith.constant 3
7879
// CHECK: %[[orig_lb_i:.*]] = arith.constant 5
7980
// CHECK: %[[orig_lb_j:.*]] = arith.constant 7
80-
// CHECK: %[[orig_ub_i:.*]] = arith.constant 10
81-
// CHECK: %[[orig_ub_j:.*]] = arith.constant 17
8281
%c2 = arith.constant 2 : index
8382
%c3 = arith.constant 3 : index
8483
%c5 = arith.constant 5 : index
8584
%c7 = arith.constant 7 : index
8685
%c10 = arith.constant 10 : index
8786
%c17 = arith.constant 17 : index
8887

89-
// Number of iterations in the outer scf.
90-
// CHECK: %[[diff_i:.*]] = arith.subi %[[orig_ub_i]], %[[orig_lb_i]]
91-
// CHECK: %[[numiter_i:.*]] = arith.ceildivsi %[[diff_i]], %[[orig_step_i]]
92-
9388
// Normalized lower bound and step for the outer scf.
9489
// CHECK: %[[lb_i:.*]] = arith.constant 0
9590
// CHECK: %[[step_i:.*]] = arith.constant 1
9691

9792
// Number of iterations in the inner loop, the pattern is the same as above,
9893
// only capture the final result.
99-
// CHECK: %[[numiter_j:.*]] = arith.ceildivsi {{.*}}, %[[orig_step_j]]
94+
// CHECK: %[[numiter_j:.*]] = arith.constant 4
10095

10196
// New bounds of the outer scf.
102-
// CHECK: %[[range:.*]] = arith.muli %[[numiter_i]], %[[numiter_j]]
97+
// CHECK: %[[range:.*]] = arith.muli %[[orig_step_j_and_numiter_i:.*]], %[[numiter_j]]
10398
// CHECK: scf.for %[[i:.*]] = %[[lb_i]] to %[[range]] step %[[step_i]]
10499
scf.for %i = %c5 to %c10 step %c2 {
105100
// The inner loop has been removed.
@@ -108,7 +103,7 @@ func.func @unnormalized_loops() {
108103
// The IVs are rewritten.
109104
// CHECK: %[[normalized_j:.*]] = arith.remsi %[[i]], %[[numiter_j]]
110105
// CHECK: %[[normalized_i:.*]] = arith.divsi %[[i]], %[[numiter_j]]
111-
// CHECK: %[[scaled_j:.*]] = arith.muli %[[normalized_j]], %[[orig_step_j]]
106+
// CHECK: %[[scaled_j:.*]] = arith.muli %[[normalized_j]], %[[orig_step_j_and_numiter_i]]
112107
// CHECK: %[[orig_j:.*]] = arith.addi %[[scaled_j]], %[[orig_lb_j]]
113108
// CHECK: %[[scaled_i:.*]] = arith.muli %[[normalized_i]], %[[orig_step_i]]
114109
// CHECK: %[[orig_i:.*]] = arith.addi %[[scaled_i]], %[[orig_lb_i]]

0 commit comments

Comments
 (0)