Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions mlir/lib/Dialect/Affine/IR/AffineOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2610,6 +2610,21 @@ 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.
static std::optional<int64_t>
getConstantInductionVarForLastTrip(AffineForOp forOp) {
std::optional<uint64_t> tripCount = getTrivialConstantTripCount(forOp);
if (!tripCount.has_value())
return std::nullopt;
if (tripCount.value() == 0)
return std::nullopt;
int64_t lb = forOp.getConstantLowerBound();
int64_t step = forOp.getStepAsInt();
int64_t lastTripIv = lb + (tripCount.value() - 1) * step;
return lastTripIv;
}

/// Fold the empty loop.
static SmallVector<OpFoldResult> AffineForEmptyLoopFolder(AffineForOp forOp) {
if (!llvm::hasSingleElement(*forOp.getBody()))
Expand All @@ -2622,18 +2637,22 @@ static SmallVector<OpFoldResult> AffineForEmptyLoopFolder(AffineForOp forOp) {
// results.
return forOp.getInits();
}
SmallVector<Value, 4> replacements;
SmallVector<OpFoldResult> replacements;
auto yieldOp = cast<AffineYieldOp>(forOp.getBody()->getTerminator());
auto iterArgs = forOp.getRegionIterArgs();
bool hasValDefinedOutsideLoop = false;
bool iterArgsNotInOrder = false;
for (unsigned i = 0, e = yieldOp->getNumOperands(); i < e; ++i) {
Value val = yieldOp.getOperand(i);
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())
if (val == forOp.getInductionVar()) {
if (auto lastTripIv = getConstantInductionVarForLastTrip(forOp)) {
replacements.push_back(IntegerAttr::get(
IndexType::get(forOp.getContext()), lastTripIv.value()));
continue;
}
return {};
}
if (iterArgIt == iterArgs.end()) {
// `val` is defined outside of the loop.
assert(forOp.isDefinedOutsideOfLoop(val) &&
Expand All @@ -2656,7 +2675,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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is any different than the previous version -- to_vector_of<T> should pick the same small size as SmallVector<T>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template <typename Out, typename R> SmallVector<Out> to_vector_of(R &&Range) {
return {adl_begin(Range), adl_end(Range)};

}

/// Canonicalize the bounds of the given loop.
Expand Down
13 changes: 13 additions & 0 deletions mlir/test/Dialect/Affine/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down