23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
29 #define GEN_PASS_DEF_AFFINELOOPTILING
30 #include "mlir/Dialect/Affine/Passes.h.inc"
37 #define DEBUG_TYPE "affine-loop-tile"
42 struct LoopTiling :
public affine::impl::AffineLoopTilingBase<LoopTiling> {
43 LoopTiling() =
default;
44 explicit LoopTiling(uint64_t cacheSizeBytes,
bool avoidMaxMinBounds =
true)
45 : avoidMaxMinBounds(avoidMaxMinBounds) {
46 this->cacheSizeInKiB = cacheSizeBytes / 1024;
49 void runOnOperation()
override;
54 constexpr
static unsigned kDefaultTileSize = 4;
57 bool avoidMaxMinBounds =
true;
68 for (AffineForOp forOp : f.getOps<AffineForOp>()) {
72 bands.push_back(band);
78 std::unique_ptr<OperationPass<func::FuncOp>>
80 return std::make_unique<LoopTiling>(cacheSizeBytes);
82 std::unique_ptr<OperationPass<func::FuncOp>>
84 return std::make_unique<LoopTiling>();
91 assert(band.size() == tileSizes->size() &&
"invalid tile size count");
92 for (
unsigned i = 0, e = band.size(); i < e; i++) {
93 unsigned &tSizeAdjusted = (*tileSizes)[i];
99 uint64_t constTripCount = *mayConst;
100 if (constTripCount > 1 && tSizeAdjusted > constTripCount / 2)
101 tSizeAdjusted = constTripCount / 2;
102 while (constTripCount % tSizeAdjusted != 0)
120 tileSizes->assign(band.size(), tileSize);
125 if (!this->tileSizes.empty()) {
126 tileSizes->assign(this->tileSizes.begin(), this->tileSizes.end());
127 tileSizes->resize(band.size(), kDefaultTileSize);
130 tileSizes->resize(band.size());
134 if (cacheSizeInKiB == 0) {
135 llvm::fill(*tileSizes, 1);
146 llvm::fill(*tileSizes, LoopTiling::kDefaultTileSize);
147 if (avoidMaxMinBounds)
150 AffineForOp rootForOp = band[0];
153 rootForOp.emitWarning(
"memory footprint unknown: using default tile "
154 "sizes adjusted to trip count divisors"));
159 uint64_t cacheSizeBytes = cacheSizeInKiB * 1024;
161 if (excessFactor <= 1) {
163 llvm::fill(*tileSizes, 1);
175 static_cast<unsigned>(floorl(std::pow(excessFactor, 1.0 / band.size())));
177 unsigned cumulProductOfTileSizes = 1;
178 for (
unsigned i = 0, e = band.size(); i < e; i++) {
180 (*tileSizes)[i] = tSize;
184 1U,
static_cast<unsigned>(excessFactor / cumulProductOfTileSizes));
185 cumulProductOfTileSizes *= (*tileSizes)[i];
187 if (avoidMaxMinBounds)
191 void LoopTiling::runOnOperation() {
193 std::vector<SmallVector<AffineForOp, 6>> bands;
197 for (
auto &band : bands) {
202 if (llvm::DebugFlag) {
203 auto diag = band[0].emitRemark(
"using tile sizes [");
204 llvm::interleaveComma(tileSizes, llvm::dbgs());
210 assert(!band.empty() &&
"guaranteed to succeed on empty bands");
211 LLVM_DEBUG(band.front()->emitRemark(
"loop tiling failed!\n"));
217 auto intraTileLoops =
220 assert(!intraTileLoops.empty() &&
221 "guaranteed to succeed on empty bands");
222 LLVM_DEBUG(intraTileLoops.front()->emitRemark(
223 "separation post tiling failed!"));
static SmallVector< Value > getTileSizes(Location loc, amx::TileType tType, RewriterBase &rewriter)
Maps the 2-dim vector shape to the two 16-bit tile sizes.
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 void getTopLevelTileableBands(func::FuncOp f, std::vector< SmallVector< AffineForOp, 6 >> &bands)
Get bands of loops that are valid to tile from the top-level of f.
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.
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...
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.
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.