@@ -2611,18 +2611,18 @@ static std::optional<uint64_t> getTrivialConstantTripCount(AffineForOp forOp) {
26112611}
26122612
26132613// / Calculate the constant value of the loop's induction variable for its last
2614- // / trip, construct an OpFoldResult using this value and return it.
2615- static OpFoldResult getConstantInductionVarForLastTrip (AffineForOp forOp) {
2614+ // / trip.
2615+ static std::optional<int64_t >
2616+ getConstantInductionVarForLastTrip (AffineForOp forOp) {
26162617 std::optional<uint64_t > tripCount = getTrivialConstantTripCount (forOp);
26172618 if (!tripCount.has_value ())
2618- return {} ;
2619+ return std:: nullopt ;
26192620 if (tripCount.value () == 0 )
2620- return {} ;
2621+ return std:: nullopt ;
26212622 int64_t lb = forOp.getConstantLowerBound ();
26222623 int64_t step = forOp.getStepAsInt ();
26232624 int64_t lastTripIv = lb + (tripCount.value () - 1 ) * step;
2624- return OpFoldResult (
2625- IntegerAttr::get (IndexType::get (forOp.getContext ()), lastTripIv));
2625+ return lastTripIv;
26262626}
26272627
26282628// / Fold the empty loop.
@@ -2645,11 +2645,10 @@ static SmallVector<OpFoldResult> AffineForEmptyLoopFolder(AffineForOp forOp) {
26452645 for (unsigned i = 0 , e = yieldOp->getNumOperands (); i < e; ++i) {
26462646 Value val = yieldOp.getOperand (i);
26472647 BlockArgument *iterArgIt = llvm::find (iterArgs, val);
2648- // TODO: It should be possible to perform a replacement by computing the
2649- // last value of the IV based on the bounds and the step.
26502648 if (val == forOp.getInductionVar ()) {
2651- if (OpFoldResult lastTripIv = getConstantInductionVarForLastTrip (forOp)) {
2652- replacements.push_back (lastTripIv);
2649+ if (auto lastTripIv = getConstantInductionVarForLastTrip (forOp)) {
2650+ replacements.push_back (IntegerAttr::get (
2651+ IndexType::get (forOp.getContext ()), lastTripIv.value ()));
26532652 continue ;
26542653 }
26552654 return {};
0 commit comments