Skip to content

Commit 7b98280

Browse files
Revert "[mlir][affine] Add fold logic when the affine.yield has IV as operand in the AffineForEmptyLoopFolder" (llvm#165607)
Reverts llvm#164064 Broke Windows on mlir-s390x-linux buildbot build, needs investigations.
1 parent 98d3a25 commit 7b98280

File tree

2 files changed

+5
-37
lines changed

2 files changed

+5
-37
lines changed

mlir/lib/Dialect/Affine/IR/AffineOps.cpp

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,21 +2610,6 @@ static std::optional<uint64_t> getTrivialConstantTripCount(AffineForOp forOp) {
26102610
return ub - lb <= 0 ? 0 : (ub - lb + step - 1) / step;
26112611
}
26122612

2613-
/// Calculate the constant value of the loop's induction variable for its last
2614-
/// trip.
2615-
static std::optional<int64_t>
2616-
getConstantInductionVarForLastTrip(AffineForOp forOp) {
2617-
std::optional<uint64_t> tripCount = getTrivialConstantTripCount(forOp);
2618-
if (!tripCount.has_value())
2619-
return std::nullopt;
2620-
if (tripCount.value() == 0)
2621-
return std::nullopt;
2622-
int64_t lb = forOp.getConstantLowerBound();
2623-
int64_t step = forOp.getStepAsInt();
2624-
int64_t lastTripIv = lb + (tripCount.value() - 1) * step;
2625-
return lastTripIv;
2626-
}
2627-
26282613
/// Fold the empty loop.
26292614
static SmallVector<OpFoldResult> AffineForEmptyLoopFolder(AffineForOp forOp) {
26302615
if (!llvm::hasSingleElement(*forOp.getBody()))
@@ -2637,22 +2622,18 @@ static SmallVector<OpFoldResult> AffineForEmptyLoopFolder(AffineForOp forOp) {
26372622
// results.
26382623
return forOp.getInits();
26392624
}
2640-
SmallVector<OpFoldResult, 4> replacements;
2625+
SmallVector<Value, 4> replacements;
26412626
auto yieldOp = cast<AffineYieldOp>(forOp.getBody()->getTerminator());
26422627
auto iterArgs = forOp.getRegionIterArgs();
26432628
bool hasValDefinedOutsideLoop = false;
26442629
bool iterArgsNotInOrder = false;
26452630
for (unsigned i = 0, e = yieldOp->getNumOperands(); i < e; ++i) {
26462631
Value val = yieldOp.getOperand(i);
26472632
BlockArgument *iterArgIt = llvm::find(iterArgs, val);
2648-
if (val == forOp.getInductionVar()) {
2649-
if (auto lastTripIv = getConstantInductionVarForLastTrip(forOp)) {
2650-
replacements.push_back(IntegerAttr::get(
2651-
IndexType::get(forOp.getContext()), lastTripIv.value()));
2652-
continue;
2653-
}
2633+
// TODO: It should be possible to perform a replacement by computing the
2634+
// last value of the IV based on the bounds and the step.
2635+
if (val == forOp.getInductionVar())
26542636
return {};
2655-
}
26562637
if (iterArgIt == iterArgs.end()) {
26572638
// `val` is defined outside of the loop.
26582639
assert(forOp.isDefinedOutsideOfLoop(val) &&
@@ -2675,7 +2656,7 @@ static SmallVector<OpFoldResult> AffineForEmptyLoopFolder(AffineForOp forOp) {
26752656
// out of order.
26762657
if (tripCount.has_value() && tripCount.value() >= 2 && iterArgsNotInOrder)
26772658
return {};
2678-
return replacements;
2659+
return llvm::to_vector_of<OpFoldResult>(replacements);
26792660
}
26802661

26812662
/// Canonicalize the bounds of the given loop.

mlir/test/Dialect/Affine/canonicalize.mlir

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -609,19 +609,6 @@ func.func @fold_zero_iter_loops(%in : index) -> index {
609609

610610
// -----
611611

612-
// CHECK-LABEL: func @fold_empty_loop_iv
613-
// CHECK-SAME: %[[INIT:.*]]: index
614-
func.func @fold_empty_loop_iv(%init: index) -> (index, index) {
615-
%res:2 = affine.for %i = 0 to 10 step 1 iter_args(%arg0 = %init, %arg1 = %init) -> (index, index) {
616-
affine.yield %i, %arg1 : index, index
617-
}
618-
// CHECK: %[[C9:.*]] = arith.constant 9 : index
619-
// CHECK: return %[[C9]], %[[INIT]] : index, index
620-
return %res#0, %res#1 : index, index
621-
}
622-
623-
// -----
624-
625612
// CHECK-DAG: #[[$SET:.*]] = affine_set<(d0, d1)[s0] : (d0 >= 0, -d0 + 1022 >= 0, d1 >= 0, -d1 + s0 - 2 >= 0)>
626613

627614
// CHECK-LABEL: func @canonicalize_affine_if

0 commit comments

Comments
 (0)