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());
122 AffineForOp rootForOp = band[0];
132 std::fill(tileSizes->begin(), tileSizes->end(),
133 LoopTiling::kDefaultTileSize);
134 if (avoidMaxMinBounds)
137 rootForOp.emitWarning(
"memory footprint unknown: using default tile "
138 "sizes adjusted to trip count divisors"));
143 uint64_t cacheSizeBytes = cacheSizeInKiB * 1024;
145 if (excessFactor <= 1) {
147 std::fill(tileSizes->begin(), tileSizes->end(), 1);
159 static_cast<unsigned>(floorl(std::pow(excessFactor, 1.0 / band.size())));
161 unsigned cumulProductOfTileSizes = 1;
162 for (
unsigned i = 0, e = band.size(); i < e; i++) {
164 (*tileSizes)[i] = tSize;
168 1U,
static_cast<unsigned>(excessFactor / cumulProductOfTileSizes));
169 cumulProductOfTileSizes *= (*tileSizes)[i];
171 if (avoidMaxMinBounds)
175 void LoopTiling::runOnOperation() {
177 std::vector<SmallVector<AffineForOp, 6>> bands;
181 for (
auto &band : bands) {
183 band.front().emitRemark(
"tiling nest is invalid due to dependences");
190 getTileSizes(band, &tileSizes);
191 if (llvm::DebugFlag) {
192 auto diag = band[0].emitRemark(
"using tile sizes [");
193 for (
unsigned tSize : tileSizes)
194 diag << tSize <<
' ';
200 assert(!band.empty() &&
"guaranteed to succeed on empty bands");
201 LLVM_DEBUG(band.front()->emitRemark(
"loop tiling failed!\n"));
207 auto intraTileLoops =
210 assert(!intraTileLoops.empty() &&
211 "guaranteed to succeed on empty bands");
212 LLVM_DEBUG(intraTileLoops.front()->emitRemark(
213 "separation post tiling failed!\n"));
219 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.