71#include "llvm/ADT/StringExtras.h"
72#include "llvm/Support/Debug.h"
76#define GEN_PASS_DEF_ACCLOOPTILING
77#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
81#define DEBUG_TYPE "acc-loop-tile"
87 ACCLoopTilingImpl(
MLIRContext *context, int32_t defaultTileSize,
90 defaultTileSize(defaultTileSize), accSupport(accSupport) {}
97 LogicalResult checkTileSizeTypes(acc::LoopOp loop,
99 auto ivTypes = loop.getBody().getArgumentTypes();
100 for (
auto [tileSize, ivType] :
101 llvm::zip(llvm::reverse(tileSizes), ivTypes)) {
102 Type tileType = tileSize.getType();
106 if (constVal && *constVal < 0)
110 auto tileIntType = dyn_cast<IntegerType>(tileType);
111 auto ivIntType = dyn_cast<IntegerType>(ivType);
112 if (tileIntType && ivIntType) {
113 if (tileIntType.getWidth() > ivIntType.getWidth()) {
114 accSupport.
emitNYI(loop.getLoc(),
115 "tile size type (i" +
116 std::to_string(tileIntType.getWidth()) +
117 ") is wider than loop IV type (i" +
118 std::to_string(ivIntType.getWidth()) +
")");
126 void emitTilingRemarks(acc::LoopOp loop,
ArrayRef<Value> tileSizes)
const {
131 auto getTileSizeStr = [&](
Value v) -> std::string {
134 if (name.empty() || name ==
"-1")
139 for (
Value v : tileSizes)
140 tileStrs.push_back(getTileSizeStr(v));
141 return "Tiling " + std::to_string(tileSizes.size()) +
142 "-level loop nest with tile(" + llvm::join(tileStrs,
",") +
149 for (
Value tileSize : tileSizes) {
151 if (val && *val < 0) {
155 return "Picking default tile size " +
156 std::to_string(defaultTileSize) +
157 " for unknown tile size '*'";
164 static void clearTileOperands(acc::LoopOp loop,
PatternRewriter &rewriter) {
166 loop.getTileOperandsMutable().clear();
167 loop.removeTileOperandsSegmentsAttr();
168 loop.removeTileOperandsDeviceTypeAttr();
172 LogicalResult matchAndRewrite(acc::LoopOp origLoop,
175 if (origLoop.getTileValues().empty())
181 if (origLoop.getUnstructured()) {
182 clearTileOperands(origLoop, rewriter);
187 origLoop.getTileValues().end());
192 if (origLoop.getCollapseAttr()) {
193 accSupport.
emitNYI(origLoop.getLoc(),
194 "a tile clause combined with a collapse clause on the "
200 if (failed(checkTileSizeTypes(origLoop, tileSizes)))
205 emitTilingRemarks(origLoop, tileSizes);
207 LLVM_DEBUG(llvm::dbgs() <<
"\nBefore tiling:\n" << *origLoop <<
"\n");
209 clearTileOperands(origLoop, rewriter);
216 LLVM_DEBUG(llvm::dbgs() <<
"\nAfter tiling:\n " << *origLoop <<
"\n");
221 int32_t defaultTileSize;
227 using ACCLoopTilingBase<ACCLoopTiling>::ACCLoopTilingBase;
230 ACCLoopTilingBase<ACCLoopTiling>::getDependentDialects(registry);
234 void runOnOperation()
override {
235 func::FuncOp funcOp = getOperation();
240 patterns.
insert<ACCLoopTilingImpl>(context, defaultTileSize, accSupport);
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
This class allows control over how the GreedyPatternRewriteDriver works.
GreedyRewriteConfig & setMaxIterations(int64_t iterations)
GreedyRewriteConfig & setUseTopDownTraversal(bool use=true)
MLIRContext is the top-level object for a collection of MLIR operations.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
RewritePatternSet & insert(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
remark::detail::InFlightRemark emitRemark(Operation *op, std::function< std::string()> messageFn, llvm::StringRef category="openacc")
Emit an OpenACC remark with lazy message generation.
std::string getVariableName(Value v, VariableNameConfig config={})
Get the variable name for a given value.
InFlightDiagnostic emitNYI(Location loc, const Twine &message)
Report a case that is not yet supported by the implementation.
mlir::acc::LoopOp tileACCLoops(mlir::acc::LoopOp tileLoop, const llvm::SmallVector< mlir::Value > &tileSizes, int32_t defaultTileSize, mlir::RewriterBase &rewriter)
Tile a single fused acc.loop that carries all associated induction variables (one IV per tile dimensi...
void registerAllExtensions(DialectRegistry ®istry)
Register the standard OpenACC models for GPU, LLVM and MemRef IR.
Include the generated interface declarations.
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
LogicalResult applyPatternsGreedily(Region ®ion, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...