-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[mlir][affine] Add fold logic when the affine.yield has IV as operand in the AffineForEmptyLoopFolder #164064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mlir][affine] Add fold logic when the affine.yield has IV as operand in the AffineForEmptyLoopFolder #164064
Conversation
|
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-affine Author: lonely eagle (linuxlonelyeagle) ChangesFull diff: https://github.com/llvm/llvm-project/pull/164064.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
index e0a53cd52f143..768ac0fe55823 100644
--- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
+++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
@@ -2610,6 +2610,19 @@ static std::optional<uint64_t> getTrivialConstantTripCount(AffineForOp forOp) {
return ub - lb <= 0 ? 0 : (ub - lb + step - 1) / step;
}
+/// Calculate the constant value of the loop's induction variable for its last
+/// trip, construct an OpFoldResult using this value and return it.
+static OpFoldResult getConstantInductionVarForLastTrip(AffineForOp forOp) {
+ std::optional<uint64_t> tripCount = getTrivialConstantTripCount(forOp);
+ if (!tripCount.has_value())
+ return {};
+ int64_t lb = forOp.getConstantLowerBound();
+ int64_t step = forOp.getStepAsInt();
+ int64_t lastTripIv = lb + (tripCount.value() - 1) * step;
+ return OpFoldResult(
+ IntegerAttr::get(IndexType::get(forOp.getContext()), lastTripIv));
+}
+
/// Fold the empty loop.
static SmallVector<OpFoldResult> AffineForEmptyLoopFolder(AffineForOp forOp) {
if (!llvm::hasSingleElement(*forOp.getBody()))
@@ -2622,7 +2635,7 @@ static SmallVector<OpFoldResult> AffineForEmptyLoopFolder(AffineForOp forOp) {
// results.
return forOp.getInits();
}
- SmallVector<Value, 4> replacements;
+ SmallVector<OpFoldResult, 4> replacements;
auto yieldOp = cast<AffineYieldOp>(forOp.getBody()->getTerminator());
auto iterArgs = forOp.getRegionIterArgs();
bool hasValDefinedOutsideLoop = false;
@@ -2632,8 +2645,15 @@ static SmallVector<OpFoldResult> AffineForEmptyLoopFolder(AffineForOp forOp) {
BlockArgument *iterArgIt = llvm::find(iterArgs, val);
// TODO: It should be possible to perform a replacement by computing the
// last value of the IV based on the bounds and the step.
- if (val == forOp.getInductionVar())
- return {};
+ if (val == forOp.getInductionVar()) {
+ OpFoldResult lastTripIv = getConstantInductionVarForLastTrip(forOp);
+ if (lastTripIv) {
+ replacements.push_back(lastTripIv);
+ continue;
+ } else {
+ return {};
+ }
+ }
if (iterArgIt == iterArgs.end()) {
// `val` is defined outside of the loop.
assert(forOp.isDefinedOutsideOfLoop(val) &&
@@ -2656,7 +2676,7 @@ static SmallVector<OpFoldResult> AffineForEmptyLoopFolder(AffineForOp forOp) {
// out of order.
if (tripCount.has_value() && tripCount.value() >= 2 && iterArgsNotInOrder)
return {};
- return llvm::to_vector_of<OpFoldResult>(replacements);
+ return replacements;
}
/// Canonicalize the bounds of the given loop.
diff --git a/mlir/test/Dialect/Affine/canonicalize.mlir b/mlir/test/Dialect/Affine/canonicalize.mlir
index 1169cd1c29d74..997f23b4bd669 100644
--- a/mlir/test/Dialect/Affine/canonicalize.mlir
+++ b/mlir/test/Dialect/Affine/canonicalize.mlir
@@ -609,6 +609,19 @@ func.func @fold_zero_iter_loops(%in : index) -> index {
// -----
+// CHECK-LABEL: func @fold_empty_loop_iv
+// CHECK-SAME: %[[INIT:.*]]: index
+func.func @fold_empty_loop_iv(%init: index) -> (index, index) {
+ %res:2 = affine.for %i = 0 to 10 step 1 iter_args(%arg0 = %init, %arg1 = %init) -> (index, index) {
+ affine.yield %i, %arg1 : index, index
+ }
+ // CHECK: %[[C9:.*]] = arith.constant 9 : index
+ // CHECK: return %[[C9]], %[[INIT]] : index, index
+ return %res#0, %res#1 : index, index
+}
+
+// -----
+
// CHECK-DAG: #[[$SET:.*]] = affine_set<(d0, d1)[s0] : (d0 >= 0, -d0 + 1022 >= 0, d1 >= 0, -d1 + s0 - 2 >= 0)>
// CHECK-LABEL: func @canonicalize_affine_if
|
|
ping for review @kuhar , thank you. |
kuhar
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but please get one more review from someone who has stake in the affine dialect
|
ping for review @bondhugula @matthias-springer thanks. |
3d3b3a9 to
c816d09
Compare
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/203/builds/27588 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/205/builds/26377 Here is the relevant piece of the build log for the reference |
…d has IV as operand in the AffineForEmptyLoopFolder" (#165607) Reverts llvm/llvm-project#164064 Broke Windows on mlir-s390x-linux buildbot build, needs investigations.
… in the AffineForEmptyLoopFolder (llvm#164064) Co-authored-by: Jakub Kuderski <[email protected]>
… operand in the AffineForEmptyLoopFolder" (llvm#165607) Reverts llvm#164064 Broke Windows on mlir-s390x-linux buildbot build, needs investigations.
… in the AffineForEmptyLoopFolder (llvm#164064) Co-authored-by: Jakub Kuderski <[email protected]>
… operand in the AffineForEmptyLoopFolder" (llvm#165607) Reverts llvm#164064 Broke Windows on mlir-s390x-linux buildbot build, needs investigations.
No description provided.