26#include "llvm/ADT/MapVector.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/DebugLog.h"
29#include "llvm/Support/raw_ostream.h"
32#define DEBUG_TYPE "loop-utils"
37using llvm::SmallMapVector;
58 auto lbMap = forOp.getLowerBoundMap();
59 auto lb = AffineApplyOp::create(
b, forOp.getLoc(), lbMap,
60 forOp.getLowerBoundOperands());
69 int64_t step = forOp.getStepAsInt();
70 for (
unsigned i = 0, e = tripCountMap.
getNumResults(); i < e; i++) {
71 auto tripCountExpr = tripCountMap.
getResult(i);
72 bumpExprs[i] = (tripCountExpr - tripCountExpr % unrollFactor) * step;
76 AffineApplyOp::create(
b, forOp.getLoc(), bumpMap, tripCountOperands);
80 for (
unsigned i = 0, e = bumpExprs.size(); i < e; i++)
81 newUbExprs[i] =
b.getAffineDimExpr(0) +
b.getAffineDimExpr(i + 1);
83 cleanupLbOperands.clear();
84 cleanupLbOperands.push_back(lb);
85 cleanupLbOperands.append(bumpValues.begin(), bumpValues.end());
93 for (
auto v : bumpValues)
95 v.getDefiningOp()->erase();
107 AffineMap lbMap = forOp.getLowerBoundMap();
109 "expected single-result bounds");
117 {map.getResult(0), shiftedLbMap.getResult(0)}, forOp.getContext());
118 ValueRange lbOperands = forOp.getLowerBoundOperands();
120 operands.begin() + numDims);
121 llvm::append_range(maxOperands, lbOperands.take_front(numLbDims));
122 llvm::append_range(maxOperands,
124 llvm::append_range(maxOperands, lbOperands.drop_front(numLbDims));
132 if (
auto cst = dyn_cast<AffineConstantExpr>(diff); cst && cst.getValue() >= 0)
134 operands.assign(maxOperands.begin(), maxOperands.end());
142 auto iterOperands = forOp.getInits();
143 auto iterArgs = forOp.getRegionIterArgs();
144 for (
auto e : llvm::zip(iterOperands, iterArgs))
145 std::get<1>(e).replaceAllUsesWith(std::get<0>(e));
148 auto outerResults = forOp.getResults();
149 auto innerResults = forOp.getBody()->getTerminator()->getOperands();
150 for (
auto e : llvm::zip(outerResults, innerResults))
151 std::get<0>(e).replaceAllUsesWith(std::get<1>(e));
157 std::optional<APInt> tripCount = forOp.getStaticTripCount();
158 if (!tripCount || *tripCount != 1)
162 if (forOp.getLowerBoundMap().getNumResults() != 1)
166 auto iv = forOp.getInductionVar();
167 auto *parentBlock = forOp->getBlock();
168 if (!iv.use_empty()) {
169 if (forOp.hasConstantLowerBound()) {
170 auto func = forOp->getParentOfType<FunctionOpInterface>();
173 builder.setInsertionPointToStart(&
func.getFunctionBody().front());
175 builder.setInsertionPoint(forOp);
177 builder, forOp.getLoc(), forOp.getConstantLowerBound());
178 iv.replaceAllUsesWith(constOp);
180 auto lbOperands = forOp.getLowerBoundOperands();
181 auto lbMap = forOp.getLowerBoundMap();
185 iv.replaceAllUsesWith(lbOperands[0]);
188 AffineApplyOp::create(builder, forOp.getLoc(), lbMap, lbOperands);
189 iv.replaceAllUsesWith(affineApplyOp);
198 forOp.getBody()->back().erase();
200 forOp.getBody()->getOperations());
215 unsigned offset, AffineForOp srcForOp,
OpBuilder b) {
216 auto lbOperands = srcForOp.getLowerBoundOperands();
217 auto ubOperands = srcForOp.getUpperBoundOperands();
223 AffineForOp::create(
b, srcForOp.getLoc(), lbOperands, lbMap, ubOperands,
224 ubMap, srcForOp.getStepAsInt());
225 auto loopChunkIV = loopChunk.getInductionVar();
226 auto srcIV = srcForOp.getInductionVar();
231 for (
const auto &it : llvm::drop_begin(opGroupQueue, offset)) {
232 uint64_t shift = it.first;
233 auto ops = it.second;
238 if (!srcIV.use_empty() && shift != 0) {
239 auto ivRemap = AffineApplyOp::create(
240 bodyBuilder, srcForOp.getLoc(),
241 bodyBuilder.getSingleDimShiftAffineMap(
242 -
static_cast<int64_t>(srcForOp.getStepAsInt() * shift)),
244 operandMap.
map(srcIV, ivRemap);
246 operandMap.
map(srcIV, loopChunkIV);
249 bodyBuilder.clone(*op, operandMap);
252 return AffineForOp();
269 bool unrollPrologueEpilogue) {
270 assert(forOp.getBody()->getOperations().size() == shifts.size() &&
271 "too few/many shifts");
272 if (forOp.getBody()->begin() == std::prev(forOp.getBody()->end()))
279 auto mayBeConstTripCount = forOp.getStaticTripCount();
280 if (!mayBeConstTripCount) {
281 LLVM_DEBUG(forOp.emitRemark(
"non-constant trip count loop not handled"));
284 uint64_t tripCount = mayBeConstTripCount->getZExtValue();
287 "shifts will lead to an invalid transformation\n");
289 int64_t step = forOp.getStepAsInt();
291 unsigned numChildOps = shifts.size();
294 uint64_t maxShift = *llvm::max_element(shifts);
295 if (maxShift >= numChildOps) {
297 forOp.emitWarning(
"not shifting because shifts are unrealistically large");
304 std::vector<std::vector<Operation *>> sortedOpGroups(maxShift + 1);
306 for (
auto &op : forOp.getBody()->without_terminator()) {
307 auto shift = shifts[pos++];
308 sortedOpGroups[shift].push_back(&op);
316 AffineForOp prologue, epilogue;
321 std::vector<std::pair<uint64_t, ArrayRef<Operation *>>> opGroupQueue;
323 auto origLbMap = forOp.getLowerBoundMap();
324 uint64_t lbShift = 0;
326 for (uint64_t d = 0, e = sortedOpGroups.size(); d < e; ++d) {
328 if (sortedOpGroups[d].empty())
330 if (!opGroupQueue.empty()) {
332 "Queue expected to be empty when the first block is found");
337 if (lbShift + tripCount * step < d * step) {
339 b.getShiftedAffineMap(origLbMap, lbShift),
340 b.getShiftedAffineMap(origLbMap, lbShift + tripCount * step),
341 opGroupQueue, 0, forOp,
b);
343 opGroupQueue.clear();
344 lbShift += tripCount * step;
347 b.getShiftedAffineMap(origLbMap, d),
348 opGroupQueue, 0, forOp,
b);
355 AffineForOp::getCanonicalizationPatterns(patterns, res.
getContext());
358 res.getOperation(), std::move(patterns),
362 if (!erased && !prologue)
372 opGroupQueue.emplace_back(d, sortedOpGroups[d]);
377 for (
unsigned i = 0, e = opGroupQueue.size(); i < e; ++i) {
378 uint64_t ubShift = (opGroupQueue[i].first + tripCount) * step;
380 b.getShiftedAffineMap(origLbMap, ubShift),
381 opGroupQueue, i, forOp,
b);
390 if (unrollPrologueEpilogue && prologue)
392 if (unrollPrologueEpilogue && !epilogue && epilogue != prologue)
404 if (input.size() <= 1)
407 LDBG() <<
"Index set computation failed!";
411 LDBG() <<
"Non-hyperrectangular nests not supported for tiling!";
422 assert(input.size() == tileSizes.size() &&
"Too few/many tile sizes");
424 if (llvm::any_of(input,
425 [](AffineForOp op) {
return op.getNumResults() > 0; })) {
426 LDBG() <<
"Cannot tile nest where a loop has yield values";
432 LDBG() <<
"input loops not perfectly nested";
447 auto &ops = src.getBody()->getOperations();
448 dest.getBody()->getOperations().splice(loc, ops, ops.begin(),
449 std::prev(ops.end()));
461 AffineForOp rootAffineForOp,
unsigned width,
463 Location loc = rootAffineForOp.getLoc();
466 Operation *topLoop = rootAffineForOp.getOperation();
467 AffineForOp innermostPointLoop;
470 for (
unsigned i = 0; i < width; i++) {
473 AffineForOp pointLoop = AffineForOp::create(
b, loc, 0, 0);
474 pointLoop.getBody()->getOperations().splice(
477 tiledLoops[2 * width - 1 - i] = pointLoop;
478 topLoop = pointLoop.getOperation();
480 innermostPointLoop = pointLoop;
484 for (
unsigned i = width; i < 2 * width; i++) {
487 AffineForOp tileSpaceLoop = AffineForOp::create(
b, loc, 0, 0);
488 tileSpaceLoop.getBody()->getOperations().splice(
491 tiledLoops[2 * width - i - 1] = tileSpaceLoop;
492 topLoop = tileSpaceLoop.getOperation();
502 AffineForOp newInterTileLoop,
503 AffineForOp newIntraTileLoop,
513 assert(origLoop.hasConstantLowerBound() &&
514 "expected input loops to have constant lower bound.");
519 b.getAffineConstantExpr(origLoop.getConstantLowerBound());
526 ubOperands.reserve(
ub.getNumOperands() + 2);
532 ubOperands.push_back(
ub.getOperand(
j));
536 lbOperands.push_back(newInterTileLoop.getInductionVar());
537 ubOperands.push_back(newInterTileLoop.getInductionVar());
541 AffineExpr lbLoopIvExpr =
b.getAffineDimExpr(lbOperands.size() - 1);
542 AffineExpr ubLoopIvExpr =
b.getAffineDimExpr(ubOperands.size() - 1);
548 ubOperands.push_back(
ub.getOperand(origUbMap.
getNumDims() +
j));
551 lbOperands.push_back(tileSize);
552 ubOperands.push_back(tileSize);
564 lbBoundExprs.push_back(
565 ((lbLoopIvExpr - origLowerBoundExpr) * lbTileParameter) +
569 AffineExpr origLoopStep =
b.getAffineConstantExpr(origLoop.getStepAsInt());
573 ubBoundExprs.push_back(
574 ((ubLoopIvExpr - origLowerBoundExpr) * ubTileParameter) +
575 (ubTileParameter * origLoopStep) + origLowerBoundExpr);
577 ubBoundExprs.append(origUbMap.
getResults().begin(),
582 lbBoundExprs,
b.getContext());
583 newIntraTileLoop.setLowerBound(lbOperands, lbMap);
587 ubBoundExprs,
b.getContext());
588 newIntraTileLoop.setUpperBound(ubOperands, ubMap);
591 newIntraTileLoop.setStep(origLoop.getStepAsInt());
597 AffineForOp newLoop,
Value tileSize) {
598 OperandRange newLbOperands = origLoop.getLowerBoundOperands();
602 newLoop.setLowerBound(newLbOperands, origLoop.getLowerBoundMap());
613 assert(origLoop.hasConstantLowerBound() &&
614 "expected input loops to have constant lower bound.");
619 b.getAffineConstantExpr(origLoop.getConstantLowerBound());
624 ubOperands.reserve(
ub.getNumOperands() + 1);
627 ubOperands.push_back(
ub.getOperand(
j));
631 ubOperands.push_back(
ub.getOperand(origUbMap.
getNumDims() +
j));
634 ubOperands.push_back(tileSize);
646 if (origLoop.hasConstantUpperBound()) {
647 origUpperBound = origLoop.getConstantUpperBound();
650 origUpperBoundExpr =
b.getAffineConstantExpr(origUpperBound);
654 boundExprs.push_back(
656 (origUpperBoundExpr - origLowerBoundExpr).ceilDiv(tileParameter));
677 boundExprs.push_back(
679 (origUpperBoundExpr - origLowerBoundExpr).ceilDiv(tileParameter));
684 boundExprs,
b.getContext());
685 newLoop.setUpperBound(ubOperands, ubMap);
688 newLoop.setStep(origLoop.getStepAsInt());
700 assert(!origLoops.empty() &&
"expected atleast one loop in band");
701 assert(origLoops.size() == tileSizes.size() &&
702 "expected tiling parameter for each loop in band.");
705 unsigned width = origLoops.size();
708 for (
unsigned i = 0; i < width; ++i) {
713 for (
unsigned i = 0; i < width; ++i) {
715 newLoops[i + width], tileSizes[i]);
728 assert(!origLoops.empty());
729 assert(origLoops.size() == tileSizes.size());
732 unsigned width = origLoops.size();
735 for (
unsigned i = 0; i < width; i++) {
736 OperandRange newLbOperands = origLoops[i].getLowerBoundOperands();
737 OperandRange newUbOperands = origLoops[i].getUpperBoundOperands();
738 newLoops[i].setLowerBound(newLbOperands, origLoops[i].getLowerBoundMap());
739 newLoops[i].setUpperBound(newUbOperands, origLoops[i].getUpperBoundMap());
742 newLoops[i].setStep(tileSizes[i] * origLoops[i].getStepAsInt());
745 for (
unsigned i = 0; i < width; i++) {
747 AffineForOp forOp = origLoops[i];
748 std::optional<uint64_t> mayBeConstantCount = std::nullopt;
749 if (
auto staticTripCount = forOp.getStaticTripCount())
750 mayBeConstantCount = staticTripCount->getZExtValue();
753 newLoops[width + i].setLowerBound(
754 newLoops[i].getInductionVar(), lbMap);
756 newLoops[width + i].setStep(origLoops[i].getStepAsInt());
759 if (mayBeConstantCount && *mayBeConstantCount < tileSizes[i]) {
762 AffineMap ubMap =
b.getSingleDimShiftAffineMap(
763 *mayBeConstantCount * origLoops[i].getStepAsInt());
764 newLoops[width + i].setUpperBound(
765 newLoops[i].getInductionVar(), ubMap);
766 }
else if (largestDiv % tileSizes[i] != 0) {
776 ubOperands.reserve(
ub.getNumOperands() + 1);
779 ubOperands.push_back(
ub.getOperand(
j));
782 ubOperands.push_back(newLoops[i].getInductionVar());
786 ubOperands.push_back(
ub.getOperand(origUbMap.
getNumDims() +
j));
793 boundExprs.push_back(dim + tileSizes[i] * origLoops[i].getStepAsInt());
794 boundExprs.append(origUbMap.
getResults().begin(),
798 boundExprs,
b.getContext());
799 newLoops[width + i].setUpperBound(ubOperands, ubMap);
804 1, 0, dim + tileSizes[i] * origLoops[i].getStepAsInt());
805 newLoops[width + i].setUpperBound(newLoops[i].getInductionVar(), ubMap);
821 AffineForOp rootAffineForOp = origLoops[0];
824 unsigned width = input.size();
838 for (
unsigned i = 0; i < width; i++)
839 origLoopIVs[i].replaceAllUsesWith(tiledLoops[i + width].getInductionVar());
842 rootAffineForOp.erase();
845 *tiledNest = std::move(tiledLoops);
863 AffineForOp rootAffineForOp = origLoops[0];
864 unsigned width = input.size();
879 for (
unsigned i = 0; i < width; i++)
880 origLoopIVs[i].replaceAllUsesWith(tiledLoops[i + width].getInductionVar());
883 rootAffineForOp.erase();
886 *tiledNest = std::move(tiledLoops);
897 for (
unsigned i = 0; i < std::numeric_limits<unsigned>::max(); ++i) {
898 nestedLoops.push_back(root);
899 Block &body = root.getRegion().front();
900 if (body.
begin() != std::prev(body.
end(), 2))
903 root = dyn_cast<AffineForOp>(&body.
front());
911 std::optional<APInt> mayBeConstantTripCount = forOp.getStaticTripCount();
912 if (mayBeConstantTripCount.has_value()) {
913 uint64_t tripCount = mayBeConstantTripCount->getZExtValue();
926 uint64_t unrollFactor) {
927 std::optional<APInt> mayBeConstantTripCount = forOp.getStaticTripCount();
928 if (mayBeConstantTripCount.has_value() &&
929 mayBeConstantTripCount->ult(unrollFactor)) {
931 if (mayBeConstantTripCount->isZero())
943 Block *loopBodyBlock,
Value forOpIV, uint64_t unrollFactor,
953 annotateFn = defaultAnnotateFn;
962 for (
unsigned i = 1; i < unrollFactor; i++) {
966 operandMap.
map(iterArgs, lastYielded);
971 Value ivUnroll = ivRemapFn(i, forOpIV, builder);
972 operandMap.
map(forOpIV, ivUnroll);
976 for (
auto it = loopBodyBlock->
begin(); it != std::next(srcBlockEnd); it++) {
978 annotateFn(i, clonedOp, builder);
983 for (
unsigned i = 0, e = lastYielded.size(); i < e; i++)
989 for (
auto it = loopBodyBlock->
begin(); it != std::next(srcBlockEnd); it++)
990 annotateFn(0, &*it, builder);
999 uint64_t unrollFactor) {
1002 auto cleanupForOp = cast<AffineForOp>(builder.
clone(*forOp));
1006 auto results = forOp.getResults();
1007 auto cleanupResults = cleanupForOp.getResults();
1008 auto cleanupIterOperands = cleanupForOp.getInits();
1010 for (
auto e : llvm::zip(results, cleanupResults, cleanupIterOperands)) {
1011 std::get<0>(e).replaceAllUsesWith(std::get<1>(e));
1012 cleanupForOp->replaceUsesOfWith(std::get<2>(e), std::get<0>(e));
1026 cleanupForOp.setLowerBound(cleanupLbOperands, cleanupLbMap);
1032 forOp.setUpperBound(cleanupOperands, cleanupMap);
1039 AffineForOp forOp, uint64_t unrollFactor,
1041 bool cleanUpUnroll) {
1042 assert(unrollFactor > 0 &&
"unroll factor should be positive");
1044 std::optional<uint64_t> mayBeConstantTripCount = std::nullopt;
1045 if (
auto staticTripCount = forOp.getStaticTripCount())
1046 mayBeConstantTripCount = staticTripCount->getZExtValue();
1047 if (unrollFactor == 1) {
1054 if (llvm::hasSingleElement(forOp.getBody()->getOperations()))
1058 if (mayBeConstantTripCount && *mayBeConstantTripCount < unrollFactor) {
1059 if (cleanUpUnroll) {
1073 if (forOp.getLowerBoundMap().getNumResults() != 1 ||
1074 forOp.getUpperBoundMap().getNumResults() != 1)
1080 assert(
false &&
"cleanup loop lower bound map for single result lower "
1081 "and upper bound maps can always be determined");
1084 ValueRange iterArgs(forOp.getRegionIterArgs());
1085 auto yieldedValues = forOp.getBody()->getTerminator()->getOperands();
1088 int64_t step = forOp.getStepAsInt();
1089 forOp.setStep(step * unrollFactor);
1091 forOp.getBody(), forOp.getInductionVar(), unrollFactor,
1094 auto d0 = b.getAffineDimExpr(0);
1095 auto bumpMap = AffineMap::get(1, 0, d0 + i * step);
1096 return AffineApplyOp::create(b, forOp.getLoc(), bumpMap, iv);
1099 iterArgs, yieldedValues);
1107 uint64_t unrollJamFactor) {
1108 std::optional<APInt> mayBeConstantTripCount = forOp.getStaticTripCount();
1109 if (mayBeConstantTripCount.has_value() &&
1110 mayBeConstantTripCount->getZExtValue() < unrollJamFactor)
1118 auto walkResult = forOp.walk([&](AffineForOp aForOp) {
1119 for (
auto controlOperand : aForOp.getControlOperands()) {
1120 if (!forOp.isDefinedOutsideOfLoop(controlOperand))
1125 return !walkResult.wasInterrupted();
1130 uint64_t unrollJamFactor) {
1131 assert(unrollJamFactor > 0 &&
"unroll jam factor should be positive");
1133 std::optional<uint64_t> mayBeConstantTripCount = std::nullopt;
1134 if (
auto staticTripCount = forOp.getStaticTripCount())
1135 mayBeConstantTripCount = staticTripCount->getZExtValue();
1136 if (unrollJamFactor == 1) {
1143 if (llvm::hasSingleElement(forOp.getBody()->getOperations()))
1147 if (mayBeConstantTripCount && *mayBeConstantTripCount < unrollJamFactor) {
1148 LDBG() <<
"[failed] trip count < unroll-jam factor";
1164 forOp.walk([&](AffineForOp aForOp) {
1165 if (aForOp.getNumIterOperands() > 0)
1166 loopsWithIterArgs.push_back(aForOp);
1171 if (forOp.getNumIterOperands() > 0)
1181 if (forOp.getLowerBoundMap().getNumResults() != 1 ||
1182 forOp.getUpperBoundMap().getNumResults() != 1)
1185 assert(
false &&
"cleanup loop lower bound map for single result lower "
1186 "and upper bound maps can always be determined");
1198 for (AffineForOp oldForOp : loopsWithIterArgs) {
1200 ValueRange oldIterOperands = oldForOp.getInits();
1201 ValueRange oldIterArgs = oldForOp.getRegionIterArgs();
1203 cast<AffineYieldOp>(oldForOp.getBody()->getTerminator()).getOperands();
1206 for (
unsigned i = unrollJamFactor - 1; i >= 1; --i) {
1207 dupIterOperands.append(oldIterOperands.begin(), oldIterOperands.end());
1208 dupYieldOperands.append(oldYieldOperands.begin(), oldYieldOperands.end());
1212 bool forOpReplaced = oldForOp == forOp;
1213 AffineForOp newForOp =
1214 cast<AffineForOp>(*oldForOp.replaceWithAdditionalYields(
1215 rewriter, dupIterOperands,
false,
1217 return dupYieldOperands;
1219 newLoopsWithIterArgs.push_back(newForOp);
1224 ValueRange newIterArgs = newForOp.getRegionIterArgs();
1225 unsigned oldNumIterArgs = oldIterArgs.size();
1226 ValueRange newResults = newForOp.getResults();
1227 unsigned oldNumResults = newResults.size() / unrollJamFactor;
1228 assert(oldNumIterArgs == oldNumResults &&
1229 "oldNumIterArgs must be the same as oldNumResults");
1230 for (
unsigned i = unrollJamFactor - 1; i >= 1; --i) {
1231 for (
unsigned j = 0;
j < oldNumIterArgs; ++
j) {
1235 operandMaps[i - 1].map(newIterArgs[
j],
1236 newIterArgs[i * oldNumIterArgs +
j]);
1237 operandMaps[i - 1].map(newResults[
j],
1238 newResults[i * oldNumResults +
j]);
1244 int64_t step = forOp.getStepAsInt();
1245 forOp.setStep(step * unrollJamFactor);
1247 auto forOpIV = forOp.getInductionVar();
1249 for (
unsigned i = unrollJamFactor - 1; i >= 1; --i) {
1250 for (
auto &subBlock : subBlocks) {
1253 OpBuilder builder(subBlock.first->getBlock(), std::next(subBlock.second));
1262 AffineApplyOp::create(builder, forOp.getLoc(), bumpMap, forOpIV);
1263 operandMaps[i - 1].map(forOpIV, ivUnroll);
1266 for (
auto it = subBlock.first; it != std::next(subBlock.second); ++it)
1267 builder.
clone(*it, operandMaps[i - 1]);
1270 for (
auto newForOp : newLoopsWithIterArgs) {
1271 unsigned oldNumIterOperands =
1272 newForOp.getNumIterOperands() / unrollJamFactor;
1273 unsigned numControlOperands = newForOp.getNumControlOperands();
1274 auto yieldOp = cast<AffineYieldOp>(newForOp.getBody()->getTerminator());
1275 unsigned oldNumYieldOperands = yieldOp.getNumOperands() / unrollJamFactor;
1276 assert(oldNumIterOperands == oldNumYieldOperands &&
1277 "oldNumIterOperands must be the same as oldNumYieldOperands");
1278 for (
unsigned j = 0;
j < oldNumIterOperands; ++
j) {
1282 newForOp.setOperand(numControlOperands + i * oldNumIterOperands +
j,
1283 operandMaps[i - 1].lookupOrDefault(
1284 newForOp.getOperand(numControlOperands +
j)));
1286 i * oldNumYieldOperands +
j,
1287 operandMaps[i - 1].lookupOrDefault(yieldOp.getOperand(
j)));
1291 if (forOp.getNumResults() > 0) {
1297 auto loc = forOp.getLoc();
1298 unsigned oldNumResults = forOp.getNumResults() / unrollJamFactor;
1300 unsigned pos = reduction.iterArgPosition;
1301 Value lhs = forOp.getResult(pos);
1304 for (
unsigned i = unrollJamFactor - 1; i >= 1; --i) {
1305 rhs = forOp.getResult(i * oldNumResults + pos);
1311 assert(op &&
"Reduction op should have been created");
1315 forOp.getResult(pos).replaceAllUsesExcept(lhs, newOps);
1327 assert(&*forOpA.getBody()->begin() == forOpB.getOperation());
1328 auto &forOpABody = forOpA.getBody()->getOperations();
1329 auto &forOpBBody = forOpB.getBody()->getOperations();
1335 forOpABody, forOpABody.begin(),
1336 std::prev(forOpABody.end()));
1339 forOpABody.splice(forOpABody.begin(), forOpBBody, forOpBBody.begin(),
1340 std::prev(forOpBBody.end()));
1342 forOpBBody.splice(forOpBBody.begin(), forOpA->getBlock()->getOperations(),
1353 unsigned maxLoopDepth = loops.size();
1355 loopPermMapInv.resize(maxLoopDepth);
1356 for (
unsigned i = 0; i < maxLoopDepth; ++i)
1357 loopPermMapInv[loopPermMap[i]] = i;
1364 for (
const auto &depComps : depCompsVec) {
1365 assert(depComps.size() >= maxLoopDepth);
1368 for (
unsigned j = 0;
j < maxLoopDepth; ++
j) {
1369 unsigned permIndex = loopPermMapInv[
j];
1370 assert(depComps[permIndex].lb);
1371 int64_t depCompLb = *depComps[permIndex].lb;
1385 assert(loopPermMap.size() == loops.size() &&
"invalid loop perm map");
1386 unsigned maxLoopDepth = loops.size();
1387 if (maxLoopDepth == 1)
1393 if (llvm::any_of(loops, [](AffineForOp loop) {
1394 return loop.getNumIterOperands() > 0;
1400 std::vector<SmallVector<DependenceComponent, 2>> depCompsVec;
1407[[maybe_unused]]
bool
1409 assert(!loops.empty() &&
"no loops provided");
1412 auto hasTwoElements = [](
Block *block) {
1413 auto secondOpIt = std::next(block->begin());
1414 return secondOpIt != block->end() && &*secondOpIt == &block->back();
1417 auto enclosingLoop = loops.front();
1418 for (
auto loop : loops.drop_front()) {
1419 auto parentForOp = dyn_cast<AffineForOp>(loop->getParentOp());
1421 if (parentForOp != enclosingLoop || !hasTwoElements(parentForOp.getBody()))
1423 enclosingLoop = loop;
1432 assert(input.size() == permMap.size() &&
"invalid permutation map size");
1436 llvm::sort(checkPermMap);
1437 if (llvm::any_of(llvm::enumerate(checkPermMap),
1438 [](
const auto &en) {
return en.value() != en.index(); }))
1439 assert(
false &&
"invalid permutation map");
1442 if (input.size() < 2)
1450 for (
unsigned i = 0, e = input.size(); i < e; ++i)
1451 invPermMap.push_back({permMap[i], i});
1452 llvm::sort(invPermMap);
1456 if (permMap.back() != input.size() - 1) {
1457 Block *destBody = ((AffineForOp)input[invPermMap.back().second]).getBody();
1458 Block *srcBody = ((AffineForOp)input.back()).getBody();
1461 std::prev(srcBody->
end()));
1466 for (
int i = input.size() - 1; i >= 0; --i) {
1469 if (permMap[i] == 0) {
1474 auto *parentBlock = input[0]->getBlock();
1476 input[i]->getBlock()->getOperations(),
1483 unsigned parentPosInInput = invPermMap[permMap[i] - 1].second;
1484 if (i > 0 &&
static_cast<unsigned>(i - 1) == parentPosInInput)
1488 auto *destBody = ((AffineForOp)input[parentPosInInput]).getBody();
1489 destBody->getOperations().splice(destBody->begin(),
1490 input[i]->getBlock()->getOperations(),
1494 return invPermMap[0].second;
1503 if (loops.size() < 2)
1508 unsigned maxLoopDepth = loops.size();
1509 std::vector<SmallVector<DependenceComponent, 2>> depCompsVec;
1514 for (
auto &depComps : depCompsVec) {
1515 assert(depComps.size() >= maxLoopDepth);
1516 for (
unsigned j = 0;
j < maxLoopDepth; ++
j) {
1518 assert(depComp.
lb.has_value() && depComp.
ub.has_value());
1519 if (*depComp.
lb != 0 || *depComp.
ub != 0)
1520 isParallelLoop[
j] =
false;
1524 unsigned numParallelLoops = llvm::count(isParallelLoop,
true);
1529 unsigned nextSequentialLoop = numParallelLoops;
1530 unsigned nextParallelLoop = 0;
1531 for (
unsigned i = 0; i < maxLoopDepth; ++i) {
1532 if (isParallelLoop[i]) {
1533 loopPermMap[i] = nextParallelLoop++;
1535 loopPermMap[i] = nextSequentialLoop++;
1543 unsigned loopNestRootIndex =
permuteLoops(loops, loopPermMap);
1544 return loops[loopNestRootIndex];
1559 auto bounds = llvm::to_vector<4>(map->
getResults());
1560 bounds.push_back(
b.getAffineDimExpr(map->
getNumDims()) + offset);
1561 operands->insert(operands->begin() + map->
getNumDims(), iv);
1578 auto originalStep = forOp.getStepAsInt();
1579 auto scaledStep = originalStep * factor;
1580 forOp.setStep(scaledStep);
1585 auto lbMap = forOp.getLowerBoundMap();
1590 auto ubMap = forOp.getUpperBoundMap();
1595 auto iv = forOp.getInductionVar();
1597 for (
auto t : targets) {
1600 auto newForOp = AffineForOp::create(
b, t.getLoc(), lbOperands, lbMap,
1601 ubOperands, ubMap, originalStep);
1602 auto begin = t.getBody()->begin();
1604 auto nOps = t.getBody()->getOperations().size() - 2;
1605 newForOp.getBody()->getOperations().splice(
1606 newForOp.getBody()->getOperations().begin(),
1607 t.getBody()->getOperations(), begin, std::next(begin, nOps));
1609 newForOp.getRegion());
1610 innerLoops.push_back(newForOp);
1621 for (
auto it : llvm::zip(forOps, sizes)) {
1622 auto step =
stripmineSink(std::get<0>(it), std::get<1>(it), currentTargets);
1623 res.push_back(step);
1624 currentTargets = std::move(step);
1634 res.push_back(llvm::getSingleElement(loops));
1639 if (loops.size() < 2)
1642 AffineForOp innermost = loops.back();
1643 AffineForOp outermost = loops.front();
1648 for (AffineForOp loop : loops) {
1650 if (loop.getStepAsInt() != 1 || !loop.hasConstantLowerBound() ||
1651 loop.getConstantLowerBound() != 0)
1656 ub.getOperands().end());
1660 if (!llvm::hasSingleElement(origUbMap.
getResults()))
1661 prev = AffineMinOp::create(builder, loc, origUbMap, ubOperands);
1663 prev = AffineApplyOp::create(builder, loc, origUbMap, ubOperands);
1664 upperBoundSymbols.push_back(prev);
1668 for (AffineForOp loop : loops.drop_front()) {
1669 ub = loop.getUpperBound();
1670 origUbMap =
ub.getMap();
1671 ubOperands =
ub.getOperands();
1674 if (!llvm::hasSingleElement(origUbMap.
getResults()))
1675 upperBound = AffineMinOp::create(builder, loc, origUbMap, ubOperands);
1677 upperBound = AffineApplyOp::create(builder, loc, origUbMap, ubOperands);
1678 upperBoundSymbols.push_back(upperBound);
1680 operands.push_back(prev);
1681 operands.push_back(upperBound);
1683 prev = AffineApplyOp::create(
1687 builder.getAffineDimExpr(0) *
1688 builder.getAffineSymbolExpr(0)),
1694 1, builder.getAffineSymbolExpr(0), builder.getContext());
1695 outermost.setUpperBound(prev, newUbMap);
1697 builder.setInsertionPointToStart(outermost.getBody());
1707 Value previous = outermost.getInductionVar();
1708 for (
unsigned idx = loops.size(); idx > 0; --idx) {
1709 if (idx != loops.size()) {
1711 operands.push_back(previous);
1712 operands.push_back(upperBoundSymbols[idx]);
1713 previous = AffineApplyOp::create(builder, loc,
1716 builder.getAffineDimExpr(0).floorDiv(
1717 builder.getAffineSymbolExpr(0))),
1722 Value inductionVariable;
1724 inductionVariable = previous;
1727 applyOperands.push_back(previous);
1728 applyOperands.push_back(upperBoundSymbols[idx - 1]);
1729 inductionVariable = AffineApplyOp::create(
1733 builder.getAffineDimExpr(0) % builder.getAffineSymbolExpr(0)),
1737 inductionVariable, loops.back().getRegion());
1742 AffineForOp secondOutermostLoop = loops[1];
1743 innermost.getBody()->back().erase();
1744 outermost.getBody()->getOperations().splice(
1746 innermost.getBody()->getOperations());
1747 for (
auto [iter, init] :
1748 llvm::zip_equal(secondOutermostLoop.getRegionIterArgs(),
1749 secondOutermostLoop.getInits())) {
1750 iter.replaceAllUsesWith(init);
1753 secondOutermostLoop.erase();
1757void mlir::affine::mapLoopToProcessorIds(scf::ForOp forOp,
1760 assert(processorId.size() == numProcessors.size());
1761 if (processorId.empty())
1771 Value linearIndex = processorId.front();
1772 for (
unsigned i = 1, e = processorId.size(); i < e; ++i) {
1773 auto mulApplyOp = AffineApplyOp::create(
1774 b, loc, mulMap,
ValueRange{linearIndex, numProcessors[i]});
1775 linearIndex = AffineApplyOp::create(
b, loc, addMap,
1779 auto mulApplyOp = AffineApplyOp::create(
1780 b, loc, mulMap,
ValueRange{linearIndex, forOp.getStep()});
1781 Value lb = AffineApplyOp::create(
1782 b, loc, addMap,
ValueRange{mulApplyOp, forOp.getLowerBound()});
1783 forOp.setLowerBound(lb);
1785 Value step = forOp.getStep();
1786 for (
auto numProcs : numProcessors)
1787 step = AffineApplyOp::create(
b, loc, mulMap,
ValueRange{numProcs, step});
1788 forOp.setStep(step);
1799 Block **copyPlacementBlock,
1804 cst->getValues(cst->getNumDimVars(), cst->getNumDimAndSymbolVars(), &symbols);
1810 auto it = enclosingAffineOps.rbegin();
1811 AffineForOp lastInvariantFor;
1812 for (
auto e = enclosingAffineOps.rend(); it != e; ++it) {
1817 LDBG() <<
"memref definition will end up not dominating hoist location";
1821 auto affineFor = dyn_cast<AffineForOp>(enclosingOp);
1826 if (llvm::is_contained(symbols, affineFor.getInductionVar()))
1828 lastInvariantFor = affineFor;
1831 if (it != enclosingAffineOps.rbegin()) {
1833 *copyOutPlacementStart = std::next(*copyInPlacementStart);
1834 *copyPlacementBlock = lastInvariantFor->getBlock();
1836 *copyInPlacementStart = begin;
1837 *copyOutPlacementStart = end;
1838 *copyPlacementBlock = █
1856 if (bufferShape.size() <= 1)
1861 for (
int d = bufferShape.size() - 1; d >= 1; d--) {
1864 numEltPerStride *= bufferShape[d];
1868 if (bufferShape[d] < dimSize && bufferShape[d - 1] > 1) {
1869 strideInfos->push_back({stride, numEltPerStride});
1894 assert(llvm::all_of(lbMaps, [&](
AffineMap lbMap) {
1897 assert(llvm::all_of(ubMaps, [&](
AffineMap ubMap) {
1901 unsigned rank = cast<MemRefType>(
memref.getType()).getRank();
1903 assert(rank != 0 &&
"non-zero rank memref expected");
1904 assert(lbMaps.size() == rank &&
"wrong number of lb maps");
1905 assert(ubMaps.size() == rank &&
"wrong number of ub maps");
1912 for (
unsigned d = 0; d < rank; ++d) {
1913 auto forOp = createCanonicalizedAffineForOp(
b, loc, lbOperands, lbMaps[d],
1914 ubOperands, ubMaps[d]);
1920 auto fastBufOffsetMap =
1922 auto offset = AffineApplyOp::create(
b, loc, fastBufOffsetMap, lbOperands);
1927 b.getAffineDimExpr(2 * d));
1928 fastBufMapOperands.push_back(offset);
1929 fastBufMapOperands.push_back(forOp.getInductionVar());
1930 mayBeDeadApplys.push_back(offset);
1933 memIndices.push_back(forOp.getInductionVar());
1943 for (
auto applyOp : mayBeDeadApplys)
1944 if (applyOp.use_empty())
1949 auto load = AffineLoadOp::create(
b, loc,
memref, memIndices);
1951 fastBufMapOperands);
1957 AffineLoadOp::create(
b, loc, fastMemRef,
fastBufMap, fastBufMapOperands);
1958 AffineStoreOp::create(
b, loc,
load,
memref, memIndices);
1990 auto f = begin->getParentOfType<FunctionOpInterface>();
1991 OpBuilder topBuilder(f.getFunctionBody());
2003 Operation *lastCopyOp = end->getPrevNode();
2007 bool isCopyOutAtEndOfBlock = (end == copyOutPlacementStart);
2010 OpBuilder prologue(copyPlacementBlock, copyInPlacementStart);
2012 OpBuilder epilogue(copyPlacementBlock, copyOutPlacementStart);
2020 auto loc = region.
loc;
2022 auto memRefType = cast<MemRefType>(
memref.getType());
2024 if (!memRefType.getLayout().isIdentity()) {
2025 LDBG() <<
"Non-identity layout map not yet supported";
2035 unsigned rank = memRefType.getRank();
2037 LDBG() <<
"Non-zero ranked memrefs supported";
2046 std::optional<int64_t> numElements =
2049 LDBG() <<
"Non-constant region size not supported";
2055 LDBG() <<
"Max lower bound for memref region start not supported";
2059 if (*numElements == 0) {
2060 LDBG() <<
"Nothing to copy";
2065 for (
unsigned i = 0; i < rank; ++i) {
2067 if (lbMaps[i].getNumResults() == 0 || ubMaps[i].getNumResults() == 0) {
2068 LDBG() <<
"Missing lower or upper bound for region along dimension: "
2088 fastBufOffsets.reserve(rank);
2089 for (
unsigned d = 0; d < rank; d++) {
2090 assert(lbs[d].getNumSymbols() == cst->
getNumCols() - rank - 1 &&
2091 "incorrect bound size");
2095 if (lbs[d].isSingleConstant()) {
2096 auto indexVal = lbs[d].getSingleConstantResult();
2097 if (indexVal == 0) {
2098 memIndices.push_back(zeroIndex);
2100 memIndices.push_back(
2110 for (
unsigned i = 0, e = lbs[d].getNumSymbols(); i < e; ++i)
2111 symReplacements[i] = top.getAffineDimExpr(i);
2112 lbs[d] = lbs[d].replaceDimsAndSymbols(
2113 {}, symReplacements, lbs[d].getNumSymbols(),
2115 memIndices.push_back(
2116 AffineApplyOp::create(
b, loc, lbs[d], regionSymbols));
2119 bufIndices.push_back(zeroIndex);
2123 fastBufOffsets.push_back(lbs[d].getResult(0));
2130 bool existingBuf = fastBufferMap.count(
memref) > 0;
2132 AffineMap fastBufferLayout =
b.getMultiDimIdentityMap(rank);
2133 auto fastMemRefType =
2134 MemRefType::get(fastBufferShape, memRefType.getElementType(),
2140 memref::AllocOp::create(prologue, loc, fastMemRefType).getResult();
2142 fastBufferMap[
memref] = fastMemRef;
2146 *sizeInBytes = maySizeInBytes.value_or(0);
2149 <<
"Creating fast buffer of type " << fastMemRefType
2150 <<
" and size " << llvm::divideCeil(*sizeInBytes, 1024)
2154 fastMemRef = fastBufferMap[
memref];
2160 Value numEltPerDmaStride;
2167 if (dmaStrideInfos.size() > 1) {
2168 LDBG() <<
"Only up to one level of stride supported";
2172 if (!dmaStrideInfos.empty()) {
2176 top, loc, dmaStrideInfos[0].numEltPerStride);
2181 auto memAffineMap =
b.getMultiDimIdentityMap(memIndices.size());
2183 auto bufAffineMap =
b.getMultiDimIdentityMap(bufIndices.size());
2189 generatePointWiseCopy(loc,
memref, fastMemRef, lbMaps,
2190 regionSymbols, ubMaps,
2191 regionSymbols, fastBufOffsets,
2195 copyNests.insert(copyNest);
2199 if (region.
isWrite() && isCopyOutAtEndOfBlock)
2204 auto tagMemRefType = MemRefType::get({1}, top.getIntegerType(32), {},
2206 auto tagMemRef = memref::AllocOp::create(prologue, loc, tagMemRefType);
2209 auto tagAffineMap =
b.getMultiDimIdentityMap(tagIndices.size());
2213 AffineDmaStartOp::create(
b, loc,
memref, memAffineMap, memIndices,
2214 fastMemRef, bufAffineMap, bufIndices, tagMemRef,
2215 tagAffineMap, tagIndices, numElementsSSA,
2216 dmaStride, numEltPerDmaStride);
2219 auto op = AffineDmaStartOp::create(
2220 b, loc, fastMemRef, bufAffineMap, bufIndices,
memref, memAffineMap,
2221 memIndices, tagMemRef, tagAffineMap, tagIndices, numElementsSSA,
2222 dmaStride, numEltPerDmaStride);
2225 if (isCopyOutAtEndOfBlock)
2230 AffineDmaWaitOp::create(
b, loc, tagMemRef, tagAffineMap, zeroIndex,
2234 auto tagDeallocOp = memref::DeallocOp::create(epilogue, loc, tagMemRef);
2235 if (*nEnd == end && isCopyOutAtEndOfBlock)
2243 auto bufDeallocOp = memref::DeallocOp::create(epilogue, loc, fastMemRef);
2246 if (!copyOptions.
generateDma && *nEnd == end && isCopyOutAtEndOfBlock)
2258 remapExprs.reserve(rank);
2259 for (
unsigned i = 0; i < rank; i++) {
2263 auto dimExpr =
b.getAffineDimExpr(regionSymbols.size() + i);
2264 remapExprs.push_back(dimExpr - fastBufOffsets[i]);
2266 auto indexRemap =
AffineMap::get(regionSymbols.size() + rank, 0, remapExprs,
2271 bool isBeginAtStartOfBlock = (begin == block->
begin());
2272 if (!isBeginAtStartOfBlock)
2273 prevOfBegin = std::prev(begin);
2275 auto userFilterFn = [&](
Operation *user) {
2282 (
void)replaceAllMemRefUsesWith(
memref, fastMemRef,
2287 *nBegin = isBeginAtStartOfBlock ? block->
begin() : std::next(prevOfBegin);
2299 if (
auto loadOp = dyn_cast<AffineLoadOp>(op)) {
2300 rank = loadOp.getMemRefType().getRank();
2301 region->
memref = loadOp.getMemRef();
2303 }
else if (
auto storeOp = dyn_cast<AffineStoreOp>(op)) {
2304 rank = storeOp.getMemRefType().getRank();
2305 region->
memref = storeOp.getMemRef();
2308 assert(
false &&
"expected load or store op");
2311 auto memRefType = cast<MemRefType>(region->
memref.
getType());
2312 if (!memRefType.hasStaticShape())
2321 ivs.resize(numParamLoopIVs);
2325 regionCst->setValues(rank, rank + numParamLoopIVs, symbols);
2328 for (
unsigned d = 0; d < rank; d++) {
2329 auto dimSize = memRefType.getDimSize(d);
2330 assert(dimSize > 0 &&
"filtered dynamic shapes above");
2331 regionCst->addBound(BoundType::LB, d, 0);
2332 regionCst->addBound(BoundType::UB, d, dimSize - 1);
2340 std::optional<Value> filterMemRef,
2345 assert(begin->getBlock() == std::prev(end)->getBlock() &&
2346 "Inconsistent block begin/end args");
2347 assert(end != end->getBlock()->end() &&
"end can't be the block terminator");
2349 Block *block = begin->getBlock();
2355 LDBG() <<
"Generating copies at depth " << copyDepth;
2356 LDBG() <<
"from begin: "
2358 LDBG() <<
"to inclusive end: "
2364 SmallMapVector<Value, std::unique_ptr<MemRefRegion>, 4> readRegions;
2365 SmallMapVector<Value, std::unique_ptr<MemRefRegion>, 4> writeRegions;
2377 MemRefType memrefType;
2379 if (
auto loadOp = dyn_cast<AffineLoadOp>(opInst)) {
2380 memref = loadOp.getMemRef();
2381 memrefType = loadOp.getMemRefType();
2382 }
else if (
auto storeOp = dyn_cast<AffineStoreOp>(opInst)) {
2383 memref = storeOp.getMemRef();
2384 memrefType = storeOp.getMemRefType();
2390 if ((filterMemRef.has_value() && filterMemRef !=
memref) ||
2391 (isa_and_nonnull<IntegerAttr>(memrefType.getMemorySpace()) &&
2396 LDBG() <<
"memref definition is inside of the depth at "
2397 <<
"which copy-in/copy-out would happen";
2402 auto region = std::make_unique<MemRefRegion>(opInst->
getLoc());
2403 if (
failed(region->compute(opInst, copyDepth,
nullptr,
2405 LDBG() <<
"Error obtaining memory region: semi-affine maps?";
2406 LDBG() <<
"over-approximating to the entire memref";
2408 LDBG() <<
"non-constant memref sizes not yet supported";
2429 [&](
const SmallMapVector<Value, std::unique_ptr<MemRefRegion>, 4>
2431 const auto *
const it = targetRegions.find(region->memref);
2432 if (it == targetRegions.end())
2436 if (
failed(it->second->unionBoundingBox(*region))) {
2437 LDBG() <<
"Memory region bounding box failed; "
2438 <<
"over-approximating to the entire memref";
2441 LDBG() <<
"non-constant memref sizes not yet supported";
2445 it->second->getConstraints()->clearAndCopyFrom(
2446 *region->getConstraints());
2449 region->getConstraints()->clearAndCopyFrom(
2450 *it->second->getConstraints());
2455 bool existsInRead = updateRegion(readRegions);
2458 bool existsInWrite = updateRegion(writeRegions);
2463 if (region->isWrite() && !existsInWrite) {
2464 writeRegions[region->memref] = std::move(region);
2465 }
else if (!region->isWrite() && !existsInRead) {
2466 readRegions[region->memref] = std::move(region);
2471 LDBG() <<
"copy generation failed for one or more memref's in this block";
2475 uint64_t totalCopyBuffersSizeInBytes = 0;
2477 auto processRegions =
2478 [&](
const SmallMapVector<Value, std::unique_ptr<MemRefRegion>, 4>
2480 for (
const auto ®ionEntry : regions) {
2484 Block *copyPlacementBlock;
2486 *regionEntry.second, *block, begin, end, ©PlacementBlock,
2487 ©InPlacementStart, ©OutPlacementStart);
2489 uint64_t sizeInBytes;
2492 *regionEntry.second, block, begin, end, copyPlacementBlock,
2493 copyInPlacementStart, copyOutPlacementStart, copyOptions,
2494 fastBufferMap, copyNests, &sizeInBytes, &nBegin, &nEnd);
2495 if (succeeded(iRet)) {
2499 totalCopyBuffersSizeInBytes += sizeInBytes;
2501 ret = ret & succeeded(iRet);
2504 processRegions(readRegions);
2505 processRegions(writeRegions);
2508 LDBG() <<
"copy generation failed for one or more memref's in this block";
2514 if (llvm::DebugFlag && (forOp = dyn_cast<AffineForOp>(&*begin))) {
2515 LLVM_DEBUG(forOp.emitRemark()
2516 << llvm::divideCeil(totalCopyBuffersSizeInBytes, 1024)
2517 <<
" KiB of copy buffers in fast memory space for this block");
2522 "total size of all copy buffers' for this block exceeds fast memory "
2531LogicalResult mlir::affine::affineDataCopyGenerate(
2534 return affineDataCopyGenerate(forOp.getBody()->begin(),
2535 std::prev(forOp.getBody()->end()), copyOptions,
2536 filterMemRef, copyNests);
2539LogicalResult mlir::affine::generateCopyForMemRegion(
2543 auto begin = analyzedOp->getIterator();
2544 auto end = std::next(begin);
2548 auto err =
generateCopy(memrefRegion, block, begin, end, block, begin, end,
2549 copyOptions, fastBufferMap, copyNests,
2550 &
result.sizeInBytes, &begin, &end);
2554 const auto &en = fastBufferMap.find(memrefRegion.
memref);
2556 if (en == fastBufferMap.end())
2558 result.alloc = en->second.getDefiningOp();
2559 assert(
result.alloc &&
"fast buffer expected to be locally allocated");
2560 assert(copyNests.size() <= 1 &&
"At most one copy nest is expected.");
2561 result.copyNest = copyNests.empty() ?
nullptr : *copyNests.begin();
2570 assert(currLoopDepth <= depthToLoops.size() &&
"Unexpected currLoopDepth");
2571 if (currLoopDepth == depthToLoops.size())
2572 depthToLoops.emplace_back();
2574 for (
auto &op : *block) {
2575 if (
auto forOp = dyn_cast<AffineForOp>(op)) {
2576 depthToLoops[currLoopDepth].push_back(forOp);
2583void mlir::affine::gatherLoops(
2585 for (
auto &block :
func)
2589 if (!depthToLoops.empty()) {
2590 assert(depthToLoops.back().empty() &&
"Last loop level is not empty?");
2591 depthToLoops.pop_back();
2595AffineForOp mlir::affine::createCanonicalizedAffineForOp(
2608 return AffineForOp::create(
b, loc, lowerOperands, lbMap, upperOperands, ubMap,
2622 auto *context = loops[0].getContext();
2626 llvm::append_range(ops, loops);
2636 for (
auto loop : loops) {
2639 assert(loop.getStepAsInt() == 1 &&
"point loop step expected to be one");
2643 unsigned fullTileLbPos, fullTileUbPos;
2645 .getConstantBoundOnDimSize(0,
nullptr,
2647 nullptr, &fullTileLbPos,
2649 LDBG() <<
"Can't get constant diff pair for a loop";
2658 fullTileLb.assign(fLb.begin(), fLb.end());
2659 fullTileUb.assign(fUb.begin(), fUb.end());
2662 for (
auto lbIndex : lbIndices)
2663 for (
unsigned i = 0, e = cst.
getNumCols(); i < e; ++i)
2664 cst.
atIneq(lbIndex, i) = fullTileLb[i] - cst.
atIneq(lbIndex, i);
2667 for (
auto ubIndex : ubIndices)
2668 for (
unsigned i = 0, e = cst.
getNumCols(); i < e; ++i)
2669 cst.
atIneq(ubIndex, i) -= fullTileUb[i];
2692 return AffineIfOp::create(
b, loops[0].getLoc(), ifCondSet, setOperands,
2700 fullTileLoops.reserve(inputNest.size());
2705 for (
auto loop : inputNest) {
2707 if (loop.getStepAsInt() != 1) {
2708 LDBG() <<
"[tile separation] non-unit stride not implemented";
2716 unsigned lbPos, ubPos;
2718 .getConstantBoundOnDimSize(0,
nullptr,
2720 nullptr, &lbPos, &ubPos) ||
2722 LDBG() <<
"[tile separation] Can't get constant diff / "
2723 <<
"equalities not yet handled";
2732 cst.getIneqAsAffineValueMap(0, lbPos, lbVmap,
b.getContext());
2733 cst.getIneqAsAffineValueMap(0, ubPos, ubVmap,
b.getContext());
2734 AffineForOp fullTileLoop = createCanonicalizedAffineForOp(
2738 fullTileLoops.push_back(fullTileLoop);
2743 for (
const auto &loopEn : llvm::enumerate(inputNest))
2744 operandMap.
map(loopEn.value().getInductionVar(),
2745 fullTileLoops[loopEn.index()].getInductionVar());
2747 for (
auto &op : inputNest.back().getBody()->without_terminator())
2748 b.clone(op, operandMap);
2755 if (inputNest.empty())
2758 auto firstLoop = inputNest[0];
2761 auto prevLoop = firstLoop;
2762 for (
auto loop : inputNest.drop_front(1)) {
2763 assert(loop->getParentOp() == prevLoop &&
"input not contiguously nested");
2771 if (!fullTileLoops.empty())
2772 fullTileLoops.front().erase();
2780 fullTileLoops.front().erase();
2781 LDBG() <<
"All tiles are full tiles, or failure creating "
2782 <<
"separation condition";
2787 Block *thenBlock = ifOp.getThenBlock();
2788 AffineForOp outermostFullTileLoop = fullTileLoops[0];
2790 std::prev(thenBlock->
end()),
2791 outermostFullTileLoop->getBlock()->getOperations(),
2796 Block *elseBlock = ifOp.getElseBlock();
2798 firstLoop->getBlock()->getOperations(),
2802 *fullTileNest = std::move(fullTileLoops);
2807LogicalResult affine::coalescePerfectlyNestedAffineLoops(AffineForOp op) {
2808 LogicalResult
result(failure());
2811 if (loops.size() <= 1)
2819 for (
unsigned i = 0, e = loops.size(); i < e; ++i) {
2820 operandsDefinedAbove[i] = i;
2821 for (
unsigned j = 0;
j < i; ++
j) {
2823 operandsDefinedAbove[i] =
j;
2832 for (
unsigned end = loops.size(); end > 0; --end) {
2834 for (; start < end - 1; ++start) {
2836 *std::max_element(std::next(operandsDefinedAbove.begin(), start),
2837 std::next(operandsDefinedAbove.begin(), end));
2840 assert(maxPos == start &&
2841 "expected loop bounds to be known at the start of the band");
2849 if (start != end - 1)
2858 while (
auto loopOp = currentOp->
getParentOfType<LoopLikeOpInterface>()) {
2859 if (!loopOp.isDefinedOutsideOfLoop(operand.
get()))
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be the output argument nBegin is set to its the output argument nEnd is set *to the new end sizeInBytes is set to the size of the fast buffer *allocated *static LogicalResult generateCopy(const MemRefRegion ®ion, Block *block, Block::iterator begin, Block::iterator end, Block *copyPlacementBlock, Block::iterator copyInPlacementStart, Block::iterator copyOutPlacementStart, const AffineCopyOptions ©Options, DenseMap< Value, Value > &fastBufferMap, DenseSet< Operation * > ©Nests, uint64_t *sizeInBytes, Block::iterator *nBegin, Block::iterator *nEnd)
static LogicalResult performPreTilingChecks(MutableArrayRef< AffineForOp > input, ArrayRef< t > tileSizes)
Check if the input nest is supported for tiling and whether tiling would be legal or not.
static void constructParametricallyTiledIndexSetHyperRect(MutableArrayRef< AffineForOp > origLoops, MutableArrayRef< AffineForOp > newLoops, ArrayRef< Value > tileSizes)
Constructs and sets new loop bounds after tiling for the case of hyper-rectangular index sets,...
fullyComposeAffineMapAndOperands & fastBufMap
static InFlightDiagnostic emitRemarkForBlock(Block &block)
static void constructTiledLoopNest(MutableArrayRef< AffineForOp > origLoops, AffineForOp rootAffineForOp, unsigned width, MutableArrayRef< AffineForOp > tiledLoops)
Constructs tiled loop nest, without setting the loop bounds and move the body of the original loop ne...
static bool getFullMemRefAsRegion(Operation *op, unsigned numParamLoopIVs, MemRefRegion *region)
Construct the memref region to just include the entire memref.
static bool checkLoopInterchangeDependences(const std::vector< SmallVector< DependenceComponent, 2 > > &depCompsVec, ArrayRef< AffineForOp > loops, ArrayRef< unsigned > loopPermMap)
static LogicalResult checkIfHyperRectangular(MutableArrayRef< AffineForOp > input)
Checks whether a loop nest is hyper-rectangular or not.
static void findHighestBlockForPlacement(const MemRefRegion ®ion, Block &block, Block::iterator &begin, Block::iterator &end, Block **copyPlacementBlock, Block::iterator *copyInPlacementStart, Block::iterator *copyOutPlacementStart)
Given a memref region, determine the lowest depth at which transfers can be placed for it,...
static void moveLoopBodyImpl(AffineForOp src, AffineForOp dest, Block::iterator loc)
Move the loop body of AffineForOp 'src' from 'src' into the specified location in destination's body,...
static void setInterTileBoundsParametric(OpBuilder &b, AffineForOp origLoop, AffineForOp newLoop, Value tileSize)
Set lower and upper bounds of inter-tile loops for parametric tiling.
static void setIntraTileBoundsParametric(OpBuilder &b, AffineForOp origLoop, AffineForOp newInterTileLoop, AffineForOp newIntraTileLoop, Value tileSize)
Set lower and upper bounds of intra-tile loops for parametric tiling.
static AffineMap getMaxWithLowerBound(AffineForOp forOp, AffineMap map, SmallVectorImpl< Value > &operands)
Returns the max of the bound given by map applied to operands and the lower bound of forOp,...
static LogicalResult createFullTiles(MutableArrayRef< AffineForOp > inputNest, SmallVectorImpl< AffineForOp > &fullTileLoops, OpBuilder b)
Create the full tile loop nest (along with its body).
static AffineForOp generateShiftedLoop(AffineMap lbMap, AffineMap ubMap, const std::vector< std::pair< uint64_t, ArrayRef< Operation * > > > &opGroupQueue, unsigned offset, AffineForOp srcForOp, OpBuilder b)
Generates an affine.for op with the specified lower and upper bounds while generating the right IV re...
static void getMultiLevelStrides(const MemRefRegion ®ion, ArrayRef< int64_t > bufferShape, SmallVectorImpl< StrideInfo > *strideInfos)
Returns striding information for a copy/transfer of this region with potentially multiple striding le...
static void constructTiledIndexSetHyperRect(MutableArrayRef< AffineForOp > origLoops, MutableArrayRef< AffineForOp > newLoops, ArrayRef< unsigned > tileSizes)
Constructs and sets new loop bounds after tiling for the case of hyper-rectangular index sets,...
static LogicalResult generateCleanupLoopForUnroll(AffineForOp forOp, uint64_t unrollFactor)
Helper to generate cleanup loop for unroll or unroll-and-jam when the trip count is not a multiple of...
static bool areInnerBoundsInvariant(AffineForOp forOp)
Check if all control operands of all loops are defined outside of forOp and return false if not.
static void moveLoopBody(AffineForOp src, AffineForOp dest)
Move the loop body of AffineForOp 'src' from 'src' to the start of dest body.
static AffineIfOp createSeparationCondition(MutableArrayRef< AffineForOp > loops, OpBuilder b)
Creates an AffineIfOp that encodes the conditional to choose between the constant trip count version ...
static void gatherLoopsInBlock(Block *block, unsigned currLoopDepth, std::vector< SmallVector< AffineForOp, 2 > > &depthToLoops)
Gathers all AffineForOps in 'block' at 'currLoopDepth' in 'depthToLoops'.
static void getCleanupLoopLowerBound(AffineForOp forOp, unsigned unrollFactor, AffineMap &cleanupLbMap, SmallVectorImpl< Value > &cleanupLbOperands)
Computes the cleanup loop lower bound of the loop being unrolled with the specified unroll factor; th...
static void augmentMapAndBounds(OpBuilder &b, Value iv, AffineMap *map, SmallVector< Value, 4 > *operands, int64_t offset=0)
static SmallVector< AffineForOp, 8 > stripmineSink(AffineForOp forOp, uint64_t factor, ArrayRef< AffineForOp > targets)
static void replaceIterArgsAndYieldResults(AffineForOp forOp)
Helper to replace uses of loop carried values (iter_args) and loop yield values while promoting singl...
Base type for affine expression.
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
AffineMap shiftDims(unsigned shift, unsigned offset=0) const
Replace dims[offset ... numDims) by dims[offset + shift ... shift + numDims).
unsigned getNumSymbols() const
unsigned getNumDims() const
ArrayRef< AffineExpr > getResults() const
unsigned getNumResults() const
unsigned getNumInputs() const
AffineMap shiftSymbols(unsigned shift, unsigned offset=0) const
Replace symbols[offset ... numSymbols) by symbols[offset + shift ... shift + numSymbols).
AffineExpr getResult(unsigned idx) const
Block represents an ordered list of Operations.
OpListType::iterator iterator
Operation * findAncestorOpInBlock(Operation &op)
Returns 'op' if 'op' lies in this block, or otherwise finds the ancestor operation of 'op' that lies ...
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
OpListType & getOperations()
RetT walk(FnT &&callback)
Walk all nested operations, blocks (including this block) or regions, depending on the type of callba...
Operation * getTerminator()
Get the terminator operation of this block.
Operation * getParentOp()
Returns the closest surrounding operation that contains this block.
AffineMap getDimIdentityMap()
AffineExpr getAffineDimExpr(unsigned position)
IntegerSet getAsIntegerSet(MLIRContext *context) const
Returns the constraint system as an integer set.
void getValues(unsigned start, unsigned end, SmallVectorImpl< Value > *values) const
Returns the Values associated with variables in range [start, end).
This class allows control over how the GreedyPatternRewriteDriver works.
This is a utility class for mapping one set of IR entities to another.
auto lookupOrDefault(T from) const
Lookup a mapped value within the map.
void map(Value from, Value to)
Inserts a new mapping for 'from' to 'to'.
IRValueT get() const
Return the current value being used by this operand.
This class coordinates rewriting a piece of IR outside of a pattern rewrite, providing a way to keep ...
This class represents a diagnostic that is inflight and set to be reported.
An integer set representing a conjunction of one or more affine equalities and inequalities.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
This class helps build Operations.
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
static OpBuilder atBlockTerminator(Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to before the block terminator.
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
This class represents an operand of an operation.
Set of flags used to control the behavior of the various IR print methods (e.g.
A wrapper class that allows for printing an operation with a set of flags, useful to act as a "stream...
This class implements the operand iterators for the Operation class.
Operation is the basic unit of execution within MLIR.
bool isBeforeInBlock(Operation *other)
Given an operation 'other' that is within the same parent block, return whether the current operation...
InFlightDiagnostic emitWarning(const Twine &message={})
Emit a warning about this operation, reporting up to any diagnostic handlers that may be listening.
Block * getBlock()
Returns the operation block that contains this operation.
Location getLoc()
The source location the operation was defined or derived from.
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
void setOperands(ValueRange operands)
Replace the current operands of this operation with the ones provided in 'operands'.
Operation * clone(IRMapping &mapper, const CloneOptions &options=CloneOptions::all())
Create a deep copy of this operation, remapping any operands that use values outside of the operation...
Region * getParentRegion()
Returns the region to which the instruction belongs.
InFlightDiagnostic emitRemark(const Twine &message={})
Emit a remark about this operation, reporting up to any diagnostic handlers that may be listening.
ParentT getParentOfType()
Find the first parent operation of the given type, or nullptr if there is no ancestor operation.
MLIRContext * getContext() const
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
bool use_empty() const
Returns true if this value has no uses.
Type getType() const
Return the type of this value.
static WalkResult advance()
static WalkResult interrupt()
AffineBound represents a lower or upper bound in the for operation.
Value getOperand(unsigned idx)
unsigned getNumOperands()
An AffineValueMap is an affine map plus its ML value operands and results for analysis purposes.
ArrayRef< Value > getOperands() const
AffineMap getAffineMap() const
FlatAffineValueConstraints is an extension of FlatLinearValueConstraints with helper functions for Af...
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
Operation * getOwner() const
Return the owner of this operand.
An IntegerRelation represents the set of points from a PresburgerSpace that satisfy a list of affine ...
void removeIndependentConstraints(unsigned pos, unsigned num)
Removes constraints that are independent of (i.e., do not have a coefficient) variables in the range ...
void removeTrivialRedundancy()
Removes duplicate constraints, trivially true constraints, and constraints that can be detected as re...
ArrayRef< DynamicAPInt > getInequality(unsigned idx) const
DynamicAPInt atIneq(unsigned i, unsigned j) const
Returns the value at the specified inequality row and column.
bool isHyperRectangular(unsigned pos, unsigned num) const
Returns true if the set can be trivially detected as being hyper-rectangular on the specified contigu...
unsigned getNumVars() const
void setDimSymbolSeparation(unsigned newSymbolCount)
Changes the partition between dimensions and symbols.
unsigned getNumDimAndSymbolVars() const
unsigned getNumCols() const
Returns the number of columns in the constraint system.
void getLowerAndUpperBoundIndices(unsigned pos, SmallVectorImpl< unsigned > *lbIndices, SmallVectorImpl< unsigned > *ubIndices, SmallVectorImpl< unsigned > *eqIndices=nullptr, unsigned offset=0, unsigned num=0) const
Gather positions of all lower and upper bounds of the variable at pos, and optionally any equalities ...
void removeVar(VarKind kind, unsigned pos)
Removes variables of the specified kind with the specified pos (or within the specified range) from t...
LogicalResult loopUnrollFull(AffineForOp forOp)
Unrolls this for operation completely if the trip count is known to be constant.
LogicalResult promoteIfSingleIteration(AffineForOp forOp)
Promotes the loop body of a AffineForOp to its containing block if the loop was known to have a singl...
LogicalResult loopUnrollJamUpToFactor(AffineForOp forOp, uint64_t unrollJamFactor)
Unrolls and jams this loop by the specified factor or by the trip count (if constant),...
void extractForInductionVars(ArrayRef< AffineForOp > forInsts, SmallVectorImpl< Value > *ivs)
Extracts the induction variables from a list of AffineForOps and places them in the output argument i...
LogicalResult loopUnrollByFactor(AffineForOp forOp, uint64_t unrollFactor, function_ref< void(unsigned, Operation *, OpBuilder)> annotateFn=nullptr, bool cleanUpUnroll=false)
Unrolls this for operation by the specified unroll factor.
void getEnclosingAffineOps(Operation &op, SmallVectorImpl< Operation * > *ops)
Populates 'ops' with affine operations enclosing op ordered from outermost to innermost while stoppin...
LogicalResult getIndexSet(MutableArrayRef< Operation * > ops, FlatAffineValueConstraints *domain)
Builds a system of constraints with dimensional variables corresponding to the loop IVs of the forOps...
void getPerfectlyNestedLoops(SmallVectorImpl< AffineForOp > &nestedLoops, AffineForOp root)
Get perfectly nested sequence of loops starting at root of loop nest (the first op being another Affi...
LogicalResult affineForOpBodySkew(AffineForOp forOp, ArrayRef< uint64_t > shifts, bool unrollPrologueEpilogue=false)
Skew the operations in an affine.for's body with the specified operation-wise shifts.
void getTripCountMapAndOperands(AffineForOp forOp, AffineMap *map, SmallVectorImpl< Value > *operands)
Returns the trip count of the loop as an affine map with its corresponding operands if the latter is ...
bool isValidLoopInterchangePermutation(ArrayRef< AffineForOp > loops, ArrayRef< unsigned > loopPermMap)
Checks if the loop interchange permutation 'loopPermMap', of the perfectly nested sequence of loops i...
void getSupportedReductions(AffineForOp forOp, SmallVectorImpl< LoopReduction > &supportedReductions)
Populate supportedReductions with descriptors of the supported reductions.
void canonicalizeMapAndOperands(AffineMap *map, SmallVectorImpl< Value > *operands)
Modifies both map and operands in-place so as to:
LogicalResult loopUnrollUpToFactor(AffineForOp forOp, uint64_t unrollFactor)
Unrolls this loop by the specified unroll factor or its trip count, whichever is lower.
void getDependenceComponents(AffineForOp forOp, unsigned maxLoopDepth, std::vector< SmallVector< DependenceComponent, 2 > > *depCompsVec)
Returns in 'depCompsVec', dependence components for dependences between all load and store ops in loo...
unsigned permuteLoops(ArrayRef< AffineForOp > inputNest, ArrayRef< unsigned > permMap)
Performs a loop permutation on a perfectly nested loop nest inputNest (where the contained loops appe...
LogicalResult loopUnrollJamByFactor(AffineForOp forOp, uint64_t unrollJamFactor)
Unrolls and jams this loop by the specified factor.
LogicalResult tilePerfectlyNestedParametric(MutableArrayRef< AffineForOp > input, ArrayRef< Value > tileSizes, SmallVectorImpl< AffineForOp > *tiledNest=nullptr)
Tiles the specified band of perfectly nested loops creating tile-space loops and intra-tile loops,...
void fullyComposeAffineMapAndOperands(AffineMap *map, SmallVectorImpl< Value > *operands, bool composeAffineMin=false)
Given an affine map map and its input operands, this method composes into map, maps of AffineApplyOps...
void canonicalizeSetAndOperands(IntegerSet *set, SmallVectorImpl< Value > *operands)
Canonicalizes an integer set the same way canonicalizeMapAndOperands does for affine maps.
bool isPerfectlyNested(ArrayRef< AffineForOp > loops)
Returns true if loops is a perfectly nested loop nest, where loops appear in it from outermost to inn...
void getAffineForIVs(Operation &op, SmallVectorImpl< AffineForOp > *loops)
Populates 'loops' with IVs of the affine.for ops surrounding 'op' ordered from the outermost 'affine....
uint64_t getLargestDivisorOfTripCount(AffineForOp forOp)
Returns the greatest known integral divisor of the trip count.
std::optional< uint64_t > getIntOrFloatMemRefSizeInBytes(MemRefType memRefType)
Returns the size of a memref with element type int or float in bytes if it's statically shaped,...
int64_t numEnclosingInvariantLoops(OpOperand &operand)
Performs explicit copying for the contiguous sequence of operations in the block iterator range [‘beg...
unsigned getNestingDepth(Operation *op)
Returns the nesting depth of this operation, i.e., the number of loops surrounding this operation.
bool isOpwiseShiftValid(AffineForOp forOp, ArrayRef< uint64_t > shifts)
Checks where SSA dominance would be violated if a for op's body operations are shifted by the specifi...
SmallVector< SmallVector< AffineForOp, 8 >, 8 > tile(ArrayRef< AffineForOp > forOps, ArrayRef< uint64_t > sizes, ArrayRef< AffineForOp > targets)
Performs tiling fo imperfectly nested loops (with interchange) by strip-mining the forOps by sizes an...
AffineForOp sinkSequentialLoops(AffineForOp forOp)
LogicalResult tilePerfectlyNested(MutableArrayRef< AffineForOp > input, ArrayRef< unsigned > tileSizes, SmallVectorImpl< AffineForOp > *tiledNest=nullptr)
Tiles the specified band of perfectly nested loops creating tile-space loops and intra-tile loops.
void interchangeLoops(AffineForOp forOpA, AffineForOp forOpB)
Performs loop interchange on 'forOpA' and 'forOpB'.
Value getReductionOp(AtomicRMWKind op, OpBuilder &builder, Location loc, Value lhs, Value rhs)
Returns the value obtained by applying the reduction operation kind associated with a binary AtomicRM...
Include the generated interface declarations.
AffineMap simplifyAffineMap(AffineMap map)
Simplifies an affine map by simplifying its underlying AffineExpr results.
void replaceAllUsesInRegionWith(Value orig, Value replacement, Region ®ion)
Replace all uses of orig within the given region with replacement.
void generateUnrolledLoop(Block *loopBodyBlock, Value iv, uint64_t unrollFactor, function_ref< Value(unsigned, Value, OpBuilder)> ivRemapFn, function_ref< void(unsigned, Operation *, OpBuilder)> annotateFn, ValueRange iterArgs, ValueRange yieldedValues, IRMapping *clonedToSrcOpsMap=nullptr, SmallVectorImpl< SmallVector< Value > > *unrolledYieldedValues=nullptr)
Generate unrolled copies of an scf loop's 'loopBodyBlock', with 'iterArgs' and 'yieldedValues' as the...
AffineMap removeDuplicateExprs(AffineMap map)
Returns a map with the same dimension and symbol count as map, but whose results are the unique affin...
llvm::DenseSet< ValueT, ValueInfoT > DenseSet
LogicalResult applyOpPatternsGreedily(ArrayRef< Operation * > ops, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr, bool *allErased=nullptr)
Rewrite the specified ops by repeatedly applying the highest benefit patterns in a greedy worklist dr...
void bindSymbols(MLIRContext *ctx, AffineExprTy &...exprs)
Bind a list of AffineExpr references to SymbolExpr at positions: [0 .
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
bool areValuesDefinedAbove(Range values, Region &limit)
Check if all values in the provided range are defined above the limit region.
@ ExistingAndNewOps
Only pre-existing and newly created ops are processed.
AffineExpr simplifyAffineExpr(AffineExpr expr, unsigned numDims, unsigned numSymbols)
Simplify an affine expression by flattening and some amount of simple analysis.
LogicalResult coalesceLoops(MutableArrayRef< scf::ForOp > loops)
Replace a perfect nest of "for" loops with a single linearized loop.
llvm::function_ref< Fn > function_ref
SmallVector< std::pair< Block::iterator, Block::iterator > > subBlocks
Explicit copy / DMA generation options for mlir::affineDataCopyGenerate.
uint64_t fastMemCapacityBytes
std::optional< int64_t > ub
std::optional< int64_t > lb
A description of a (parallelizable) reduction in an affine loop.
A region of a memref's data space; this is typically constructed by analyzing load/store op's on this...
std::optional< int64_t > getConstantBoundingSizeAndShape(SmallVectorImpl< int64_t > *shape=nullptr, SmallVectorImpl< AffineMap > *lbs=nullptr) const
Returns a constant upper bound on the number of elements in this region if bounded by a known constan...
void getLowerAndUpperBound(unsigned pos, AffineMap &lbMap, AffineMap &ubMap) const
Gets the lower and upper bound map for the dimensional variable at pos.
FlatAffineValueConstraints * getConstraints()
Value memref
Memref that this region corresponds to.
Location loc
If there is more than one load/store op associated with the region, the location information would co...
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.