26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Debug.h"
32 #define GEN_PASS_DEF_AFFINELOOPTILING
33 #include "mlir/Dialect/Affine/Passes.h.inc"
40 #define DEBUG_TYPE "affine-loop-tile"
45 struct LoopTiling :
public affine::impl::AffineLoopTilingBase<LoopTiling> {
46 LoopTiling() =
default;
47 explicit LoopTiling(uint64_t cacheSizeBytes,
bool avoidMaxMinBounds =
true)
48 : avoidMaxMinBounds(avoidMaxMinBounds) {
49 this->cacheSizeInKiB = cacheSizeBytes / 1024;
52 void runOnOperation()
override;
57 constexpr
static unsigned kDefaultTileSize = 4;
60 bool avoidMaxMinBounds =
true;
67 std::unique_ptr<OperationPass<func::FuncOp>>
69 return std::make_unique<LoopTiling>(cacheSizeBytes);
71 std::unique_ptr<OperationPass<func::FuncOp>>
73 return std::make_unique<LoopTiling>();
80 assert(band.size() == tileSizes->size() &&
"invalid tile size count");
81 for (
unsigned i = 0, e = band.size(); i < e; i++) {
82 unsigned &tSizeAdjusted = (*tileSizes)[i];
88 uint64_t constTripCount = *mayConst;
89 if (constTripCount > 1 && tSizeAdjusted > constTripCount / 2)
90 tSizeAdjusted = constTripCount / 2;
91 while (constTripCount % tSizeAdjusted != 0)
109 tileSizes->assign(band.size(), tileSize);
114 if (!this->tileSizes.empty()) {
115 tileSizes->assign(this->tileSizes.begin(), this->tileSizes.end());
116 tileSizes->resize(band.size(), kDefaultTileSize);
119 tileSizes->resize(band.size());
123 if (cacheSizeInKiB == 0) {
124 std::fill(tileSizes->begin(), tileSizes->end(), 1);
129 AffineForOp rootForOp = band[0];
139 std::fill(tileSizes->begin(), tileSizes->end(),
140 LoopTiling::kDefaultTileSize);
141 if (avoidMaxMinBounds)
144 rootForOp.emitWarning(
"memory footprint unknown: using default tile "
145 "sizes adjusted to trip count divisors"));
150 uint64_t cacheSizeBytes = cacheSizeInKiB * 1024;
152 if (excessFactor <= 1) {
154 std::fill(tileSizes->begin(), tileSizes->end(), 1);
166 static_cast<unsigned>(floorl(std::pow(excessFactor, 1.0 / band.size())));
168 unsigned cumulProductOfTileSizes = 1;
169 for (
unsigned i = 0, e = band.size(); i < e; i++) {
171 (*tileSizes)[i] = tSize;
175 1U,
static_cast<unsigned>(excessFactor / cumulProductOfTileSizes));
176 cumulProductOfTileSizes *= (*tileSizes)[i];
178 if (avoidMaxMinBounds)
182 void LoopTiling::runOnOperation() {
184 std::vector<SmallVector<AffineForOp, 6>> bands;
188 for (
auto &band : bands) {
190 band.front().emitRemark(
"tiling nest is invalid due to dependences");
197 getTileSizes(band, &tileSizes);
198 if (llvm::DebugFlag) {
199 auto diag = band[0].emitRemark(
"using tile sizes [");
200 for (
unsigned tSize : tileSizes)
201 diag << tSize <<
' ';
207 assert(!band.empty() &&
"guaranteed to succeed on empty bands");
208 LLVM_DEBUG(band.front()->emitRemark(
"loop tiling failed!\n"));
214 auto intraTileLoops =
217 assert(!intraTileLoops.empty() &&
218 "guaranteed to succeed on empty bands");
219 LLVM_DEBUG(intraTileLoops.front()->emitRemark(
220 "separation post tiling failed!\n"));
226 constexpr
unsigned LoopTiling::kDefaultTileSize;
static void adjustToDivisorsOfTripCounts(ArrayRef< AffineForOp > band, SmallVectorImpl< unsigned > *tileSizes)
Reduces each tile size to the largest divisor of the corresponding trip count (if the trip count is k...
static std::string diag(const llvm::Value &value)
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
std::optional< uint64_t > getConstantTripCount(AffineForOp forOp)
Returns the trip count of the loop if it's a constant, std::nullopt otherwise.
bool isTilingValid(ArrayRef< AffineForOp > loops)
Checks whether hyper-rectangular loop tiling of the nest represented by loops is valid.
std::optional< int64_t > getMemoryFootprintBytes(AffineForOp forOp, int memorySpace=-1)
Gets the memory footprint of all data touched in the specified memory space in bytes; if the memory s...
std::unique_ptr< OperationPass< func::FuncOp > > createLoopTilingPass(uint64_t cacheSizeBytes)
Creates a pass to perform tiling on loop nests.
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 getTileableBands(func::FuncOp f, std::vector< SmallVector< AffineForOp, 6 >> *bands)
Identify valid and profitable bands of loops to tile.
LogicalResult separateFullTiles(MutableArrayRef< AffineForOp > nest, SmallVectorImpl< AffineForOp > *fullTileNest=nullptr)
Separates full tiles from partial tiles for a perfect nest nest by generating a conditional guard tha...
llvm::TypeSize divideCeil(llvm::TypeSize numerator, uint64_t denominator)
Divides the known min value of the numerator by the denominator and rounds the result up to the next ...
Include the generated interface declarations.