1 //===- Tiling.cpp - Implementation of linalg Tiling -----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the linalg dialect Tiling pass. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "PassDetail.h" 14 #include "mlir/Dialect/Affine/EDSC/Intrinsics.h" 15 #include "mlir/Dialect/Linalg/EDSC/FoldedIntrinsics.h" 16 #include "mlir/Dialect/Linalg/IR/LinalgTypes.h" 17 #include "mlir/Dialect/Linalg/Passes.h" 18 #include "mlir/Dialect/Linalg/Transforms/Transforms.h" 19 #include "mlir/Dialect/Linalg/Utils/Utils.h" 20 #include "mlir/Dialect/SCF/EDSC/Builders.h" 21 #include "mlir/Dialect/StandardOps/EDSC/Intrinsics.h" 22 #include "mlir/IR/AffineExpr.h" 23 #include "mlir/IR/AffineExprVisitor.h" 24 #include "mlir/IR/AffineMap.h" 25 #include "mlir/Support/LLVM.h" 26 #include "mlir/Transforms/FoldUtils.h" 27 28 #include "llvm/Support/CommandLine.h" 29 30 using namespace mlir; 31 using namespace mlir::edsc; 32 using namespace mlir::edsc::intrinsics; 33 using namespace mlir::linalg; 34 using namespace mlir::scf; 35 36 using folded_affine_min = FoldedValueBuilder<AffineMinOp>; 37 38 #define DEBUG_TYPE "linalg-tiling" 39 40 static bool isZero(Value v) { 41 if (auto cst = v.getDefiningOp<ConstantIndexOp>()) 42 return cst.getValue() == 0; 43 return false; 44 } 45 46 using LoopIndexToRangeIndexMap = DenseMap<int, int>; 47 48 // Creates a number of ranges equal to the number of non-zero in `tileSizes`. 49 // One for each loop of the LinalgOp that is tiled. The `tileSizes` argument has 50 // one entry per surrounding loop. It uses zero as the convention that a 51 // particular loop is not tiled. This convention simplifies implementations by 52 // avoiding affine map manipulations. 53 // The returned ranges correspond to the loop ranges, in the proper order, that 54 // are tiled and for which new loops will be created. Also the function returns 55 // a map from loop indices of the LinalgOp to the corresponding non-empty range 56 // indices of newly created loops. 57 static std::tuple<SmallVector<SubViewOp::Range, 4>, LoopIndexToRangeIndexMap> 58 makeTiledLoopRanges(OpBuilder &b, Location loc, AffineMap map, 59 ArrayRef<Value> allViewSizes, 60 ArrayRef<Value> allTileSizes) { 61 assert(allTileSizes.size() == map.getNumResults()); 62 // Apply `map` to get view sizes in loop order. 63 auto viewSizes = applyMapToValues(b, loc, map, allViewSizes); 64 SmallVector<Value, 4> tileSizes(allTileSizes.begin(), allTileSizes.end()); 65 66 // Traverse the tile sizes, which are in loop order, erase zeros everywhere. 67 LoopIndexToRangeIndexMap loopIndexToRangeIndex; 68 for (int idx = 0, e = tileSizes.size(), zerosCount = 0; idx < e; ++idx) { 69 if (isZero(tileSizes[idx - zerosCount])) { 70 viewSizes.erase(viewSizes.begin() + idx - zerosCount); 71 tileSizes.erase(tileSizes.begin() + idx - zerosCount); 72 ++zerosCount; 73 continue; 74 } 75 loopIndexToRangeIndex[idx] = idx - zerosCount; 76 } 77 78 // Create a new range with the applied tile sizes. 79 SmallVector<SubViewOp::Range, 4> res; 80 for (unsigned idx = 0, e = tileSizes.size(); idx < e; ++idx) 81 res.push_back(SubViewOp::Range{std_constant_index(0), viewSizes[idx], 82 tileSizes[idx]}); 83 return std::make_tuple(res, loopIndexToRangeIndex); 84 } 85 86 namespace { 87 88 // Helper visitor to determine whether an AffineExpr is tiled. 89 // This is achieved by traversing every AffineDimExpr with position `pos` and 90 // checking whether the corresponding `tileSizes[pos]` is non-zero. 91 // This also enforces only positive coefficients occur in multiplications. 92 // 93 // Example: 94 // `d0 + 2 * d1 + d3` is tiled by [0, 0, 0, 2] but not by [0, 0, 2, 0] 95 // 96 struct TileCheck : public AffineExprVisitor<TileCheck> { 97 TileCheck(ArrayRef<Value> tileSizes) : isTiled(false), tileSizes(tileSizes) {} 98 99 void visitDimExpr(AffineDimExpr expr) { 100 isTiled |= !isZero(tileSizes[expr.getPosition()]); 101 } 102 void visitAffineBinaryOpExpr(AffineBinaryOpExpr expr) { 103 visit(expr.getLHS()); 104 visit(expr.getRHS()); 105 if (expr.getKind() == mlir::AffineExprKind::Mul) 106 assert(expr.getRHS().cast<AffineConstantExpr>().getValue() > 0 && 107 "nonpositive multiplying coefficient"); 108 } 109 bool isTiled; 110 ArrayRef<Value> tileSizes; 111 }; 112 113 } // namespace 114 115 // IndexedGenericOp explicitly uses induction variables in the loop body. The 116 // values of the indices that are used in the loop body for any given access of 117 // input/output memref before `subview` op was applied should be invariant with 118 // respect to tiling. 119 // 120 // Therefore, if the operation is tiled, we have to transform the indices 121 // accordingly, i.e. offset them by the values of the corresponding induction 122 // variables that are captured implicitly in the body of the op. 123 // 124 // Example. `linalg.indexed_generic` before tiling: 125 // 126 // #id_2d = (i, j) -> (i, j) 127 // #pointwise_2d_trait = { 128 // indexing_maps = [#id_2d, #id_2d], 129 // iterator_types = ["parallel", "parallel"], 130 // n_views = [1, 1] 131 // } 132 // linalg.indexed_generic #pointwise_2d_trait %operand, %result { 133 // ^bb0(%i: index, %j: index, %operand_in: f32, %result_in: f32): 134 // <some operations that use %i, %j> 135 // }: memref<50x100xf32>, memref<50x100xf32> 136 // 137 // After tiling pass with tiles sizes 10 and 25: 138 // 139 // #strided = (i, j)[s0, s1, s2] -> (i * s1 + s0 + j * s2) 140 // 141 // %c1 = constant 1 : index 142 // %c0 = constant 0 : index 143 // %c25 = constant 25 : index 144 // %c10 = constant 10 : index 145 // operand_dim_0 = dim %operand, 0 : memref<50x100xf32> 146 // operand_dim_1 = dim %operand, 1 : memref<50x100xf32> 147 // scf.for %k = %c0 to operand_dim_0 step %c10 { 148 // scf.for %l = %c0 to operand_dim_1 step %c25 { 149 // %4 = std.subview %operand[%k, %l][%c10, %c25][%c1, %c1] 150 // : memref<50x100xf32> to memref<?x?xf32, #strided> 151 // %5 = std.subview %result[%k, %l][%c10, %c25][%c1, %c1] 152 // : memref<50x100xf32> to memref<?x?xf32, #strided> 153 // linalg.indexed_generic pointwise_2d_trait %4, %5 { 154 // ^bb0(%i: index, %j: index, %operand_in: f32, %result_in: f32): 155 // // Indices `k` and `l` are implicitly captured in the body. 156 // %transformed_i = addi %i, %k : index // index `i` is offset by %k 157 // %transformed_j = addi %j, %l : index // index `j` is offset by %l 158 // // Every use of %i, %j is replaced with %transformed_i, %transformed_j 159 // <some operations that use %transformed_i, %transformed_j> 160 // }: memref<?x?xf32, #strided>, memref<?x?xf32, #strided> 161 // } 162 // } 163 // 164 // TODO: Investigate whether mixing implicit and explicit indices 165 // does not lead to losing information. 166 static void transformIndexedGenericOpIndices( 167 OpBuilder &b, LinalgOp op, SmallVectorImpl<Value> &ivs, 168 const LoopIndexToRangeIndexMap &loopIndexToRangeIndex) { 169 assert(op.hasBufferSemantics() && "expected linalg op with buffer semantics"); 170 auto indexedGenericOp = dyn_cast<IndexedGenericOp>(op.getOperation()); 171 if (!indexedGenericOp) 172 return; 173 174 // `linalg.indexed_generic` comes in two flavours. One has a region with a 175 // single block that defines the loop body. The other has a `fun` attribute 176 // that refers to an existing function symbol. The `fun` function call will be 177 // inserted in the loop body in that case. 178 // 179 // TODO: Add support for `linalg.indexed_generic` with `fun` attribute. 180 auto ®ion = indexedGenericOp.region(); 181 if (region.empty()) { 182 indexedGenericOp.emitOpError("expected a region"); 183 return; 184 } 185 auto &block = region.front(); 186 187 OpBuilder::InsertionGuard g(b); 188 b.setInsertionPointToStart(&block); 189 for (unsigned i = 0; i < indexedGenericOp.getNumLoops(); ++i) { 190 auto rangeIndex = loopIndexToRangeIndex.find(i); 191 if (rangeIndex == loopIndexToRangeIndex.end()) 192 continue; 193 Value oldIndex = block.getArgument(i); 194 // Offset the index argument `i` by the value of the corresponding induction 195 // variable and replace all uses of the previous value. 196 Value newIndex = b.create<AddIOp>(indexedGenericOp.getLoc(), oldIndex, 197 ivs[rangeIndex->second]); 198 for (auto &use : oldIndex.getUses()) { 199 if (use.getOwner() == newIndex.getDefiningOp()) 200 continue; 201 use.set(newIndex); 202 } 203 } 204 } 205 206 static bool isTiled(AffineExpr expr, ArrayRef<Value> tileSizes) { 207 if (!expr) 208 return false; 209 TileCheck t(tileSizes); 210 t.visit(expr); 211 return t.isTiled; 212 } 213 214 // Checks whether the view with index `viewIndex` within `linalgOp` varies with 215 // respect to a non-zero `tileSize`. 216 static bool isTiled(AffineMap map, ArrayRef<Value> tileSizes) { 217 if (!map) 218 return false; 219 for (unsigned r = 0; r < map.getNumResults(); ++r) 220 if (isTiled(map.getResult(r), tileSizes)) 221 return true; 222 return false; 223 } 224 225 static SmallVector<Value, 4> makeTiledViews(OpBuilder &b, Location loc, 226 LinalgOp linalgOp, AffineMap map, 227 ArrayRef<Value> ivs, 228 ArrayRef<Value> tileSizes, 229 ArrayRef<Value> allViewSizes) { 230 assert(linalgOp.hasBufferSemantics() && 231 "expected linalg op with buffer semantics"); 232 assert(ivs.size() == static_cast<size_t>(llvm::count_if( 233 llvm::make_range(tileSizes.begin(), tileSizes.end()), 234 [](Value v) { return !isZero(v); })) && 235 "expected as many ivs as non-zero sizes"); 236 237 using namespace edsc::op; 238 239 auto viewSizes = applyMapToValues(b, loc, map, allViewSizes); 240 // Construct (potentially temporary) mins and maxes on which to apply maps 241 // that define tile subviews. 242 SmallVector<Value, 8> lbs, subViewSizes; 243 for (unsigned idx = 0, idxIvs = 0, e = tileSizes.size(); idx < e; ++idx) { 244 bool isTiled = !isZero(tileSizes[idx]); 245 lbs.push_back(isTiled ? ivs[idxIvs++] : (Value)std_constant_index(0)); 246 // Before composing, we need to make range a closed interval. 247 Value size = isTiled ? tileSizes[idx] : viewSizes[idx]; 248 subViewSizes.push_back(size - std_constant_index(1)); 249 } 250 251 auto *op = linalgOp.getOperation(); 252 253 SmallVector<Value, 4> res; 254 res.reserve(op->getNumOperands()); 255 auto viewIteratorBegin = linalgOp.getInputsAndOutputBuffers().begin(); 256 for (unsigned viewIndex = 0; viewIndex < linalgOp.getNumInputsAndOutputs(); 257 ++viewIndex) { 258 Value view = *(viewIteratorBegin + viewIndex); 259 auto viewType = view.getType().cast<MemRefType>(); 260 unsigned rank = viewType.getRank(); 261 auto mapAttr = linalgOp.indexing_maps()[viewIndex]; 262 auto map = mapAttr.cast<AffineMapAttr>().getValue(); 263 // If the view is not tiled, we can use it as is. 264 if (!isTiled(map, tileSizes)) { 265 res.push_back(view); 266 continue; 267 } 268 269 // Construct a new subview for the tile. 270 SmallVector<Value, 4> offsets, sizes, strides; 271 offsets.reserve(rank); 272 sizes.reserve(rank); 273 strides.reserve(rank); 274 for (unsigned r = 0; r < rank; ++r) { 275 if (!isTiled(map.getSubMap({r}), tileSizes)) { 276 offsets.push_back(std_constant_index(0)); 277 sizes.push_back(std_dim(view, r)); 278 strides.push_back(std_constant_index(1)); 279 continue; 280 } 281 282 // Tiling creates a new slice at the proper index, the slice step is 1 283 // (i.e. the slice view does not subsample, stepping occurs in the loop). 284 auto m = map.getSubMap({r}); 285 auto offset = applyMapToValues(b, loc, m, lbs).front(); 286 offsets.push_back(offset); 287 auto closedIntSize = applyMapToValues(b, loc, m, subViewSizes).front(); 288 // Resulting size needs to be made half open interval again. 289 auto size = closedIntSize + std_constant_index(1); 290 291 // The size of the subview should be trimmed to avoid out-of-bounds 292 // accesses, unless we statically know the subview size divides the view 293 // size evenly. 294 int64_t viewSize = viewType.getDimSize(r); 295 auto sizeCst = size.getDefiningOp<ConstantIndexOp>(); 296 if (ShapedType::isDynamic(viewSize) || !sizeCst || 297 (viewSize % sizeCst.getValue()) != 0) { 298 // Compute min(size, dim - offset) to avoid out-of-bounds accesses. 299 auto minMap = AffineMap::get( 300 /*dimCount=*/3, /*symbolCount=*/0, 301 {getAffineDimExpr(/*position=*/0, b.getContext()), 302 getAffineDimExpr(/*position=*/1, b.getContext()) - 303 getAffineDimExpr(/*position=*/2, b.getContext())}, 304 b.getContext()); 305 auto d = std_dim(view, r); 306 size = 307 affine_min(b.getIndexType(), minMap, ValueRange{size, d, offset}); 308 } 309 310 sizes.push_back(size); 311 strides.push_back(std_constant_index(1)); 312 } 313 314 res.push_back(b.create<SubViewOp>(loc, view, offsets, sizes, strides)); 315 } 316 317 return res; 318 } 319 320 template <typename LoopTy> 321 Optional<TiledLinalgOp> static tileLinalgOpImpl( 322 OpBuilder &b, LinalgOp op, const LinalgTilingOptions &options) { 323 OpBuilder::InsertionGuard g(b); 324 b.setInsertionPoint(op); 325 ScopedContext scope(b, op.getLoc()); 326 327 assert(op.hasBufferSemantics() && "expected linalg op with buffer semantics"); 328 // 1. Enforce the convention that "tiling by zero" skips tiling a particular 329 // dimension. This convention is significantly simpler to handle instead of 330 // adjusting affine maps to account for missing dimensions. 331 auto nLoops = op.getNumLoops(); 332 SmallVector<Value, 4> tileSizeVector = 333 options.tileSizeComputationFunction(b, op); 334 if (tileSizeVector.size() < nLoops) { 335 auto zero = std_constant_index(0); 336 tileSizeVector.append(nLoops - tileSizeVector.size(), zero); 337 } 338 339 ArrayRef<Value> tileSizes = tileSizeVector; 340 // Initial tile sizes may be too big, only take the first nLoops. 341 tileSizes = tileSizes.take_front(nLoops); 342 343 if (llvm::all_of(tileSizes, isZero)) 344 return llvm::None; 345 346 if (auto convOp = dyn_cast<linalg::ConvOp>(op.getOperation())) { 347 // For conv op only support tiling along batch dimension (which is the first 348 // loop). 349 if (convOp.padding() && !llvm::all_of(tileSizes.drop_front(), isZero)) 350 return llvm::None; 351 } 352 353 // If interchangeVector is empty, use the identity. Build the permutation map 354 // otherwise. 355 auto invPermutationMap = 356 AffineMap::getMultiDimIdentityMap(tileSizes.size(), b.getContext()); 357 if (!options.interchangeVector.empty()) 358 invPermutationMap = inversePermutation(AffineMap::getPermutationMap( 359 options.interchangeVector, b.getContext())); 360 if (!invPermutationMap) 361 return llvm::None; 362 363 // 2. Build the tiled loop ranges. 364 auto allViewSizes = getViewSizes(b, op); 365 // The flattened loopToOperandRangesMaps is expected to be an invertible 366 // permutation map (asserted in the inverse calculation). 367 auto mapsRange = op.indexing_maps().getAsRange<AffineMapAttr>(); 368 auto maps = llvm::to_vector<8>( 369 llvm::map_range(mapsRange, [](AffineMapAttr a) { return a.getValue(); })); 370 auto viewSizesToLoopsMap = inversePermutation(concatAffineMaps(maps)); 371 if (!viewSizesToLoopsMap) 372 return llvm::None; 373 374 SmallVector<SubViewOp::Range, 4> loopRanges; 375 LoopIndexToRangeIndexMap loopIndexToRangeIndex; 376 std::tie(loopRanges, loopIndexToRangeIndex) = makeTiledLoopRanges( 377 b, scope.getLocation(), viewSizesToLoopsMap, allViewSizes, tileSizes); 378 if (!options.interchangeVector.empty()) 379 applyPermutationToVector(loopRanges, options.interchangeVector); 380 381 // 3. Create the tiled loops. 382 LinalgOp res = op; 383 SmallVector<Value, 4> ivs; 384 SmallVector<Attribute, 4> iteratorTypes = 385 llvm::to_vector<4>(op.iterator_types().cast<ArrayAttr>().getValue()); 386 if (!options.interchangeVector.empty()) 387 applyPermutationToVector(iteratorTypes, options.interchangeVector); 388 GenerateLoopNest<LoopTy>::doit( 389 loopRanges, iteratorTypes, 390 [&](ValueRange localIvs) { 391 auto &b = ScopedContext::getBuilderRef(); 392 auto loc = ScopedContext::getLocation(); 393 ivs.assign(localIvs.begin(), localIvs.end()); 394 SmallVector<Value, 4> ivValues(ivs.begin(), ivs.end()); 395 396 // If we have to apply a permutation to the tiled loop nest, we have to 397 // reorder the induction variables This permutation is the right one 398 // assuming that loopRanges have previously been permuted by 399 // (i,j,k)->(k,i,j) So this permutation should be the inversePermutation 400 // of that one: (d0,d1,d2)->(d2,d0,d1) 401 if (!options.interchangeVector.empty()) 402 ivValues = applyMapToValues(b, loc, invPermutationMap, ivValues); 403 404 auto views = makeTiledViews(b, loc, op, viewSizesToLoopsMap, ivValues, 405 tileSizes, allViewSizes); 406 auto operands = getAssumedNonViewOperands(op); 407 views.append(operands.begin(), operands.end()); 408 res = op.clone(b, loc, views); 409 }, 410 options.distribution); 411 412 // 4. Transforms index arguments of `linalg.generic` w.r.t. to the tiling. 413 transformIndexedGenericOpIndices(b, res, ivs, loopIndexToRangeIndex); 414 415 // 5. Gather the newly created loops and return them with the new op. 416 SmallVector<Operation *, 8> loops; 417 loops.reserve(ivs.size()); 418 for (auto iv : ivs) { 419 if (iv.isa<BlockArgument>()) { 420 loops.push_back(iv.cast<BlockArgument>().getOwner()->getParentOp()); 421 assert(loops.back() && "no owner found for induction variable!"); 422 } else { 423 // TODO: Instead of doing this, try to recover the ops used instead of the 424 // loop. 425 loops.push_back(nullptr); 426 } 427 } 428 return TiledLinalgOp{res, loops}; 429 } 430 431 Optional<TiledLinalgOp> 432 mlir::linalg::tileLinalgOp(OpBuilder &b, LinalgOp op, 433 const LinalgTilingOptions &options) { 434 if (options.loopType == LinalgTilingLoopType::Loops) 435 return tileLinalgOpImpl<scf::ForOp>(b, op, options); 436 if (options.loopType == LinalgTilingLoopType::ParallelLoops) 437 return tileLinalgOpImpl<scf::ParallelOp>(b, op, options); 438 // TODO: Impl tiling to affine loops when it makes sense. 439 return llvm::None; 440 } 441 442 namespace { 443 /// Helper classes for type list expansion. 444 template <typename... OpTypes> 445 class CanonicalizationPatternList; 446 447 template <> 448 class CanonicalizationPatternList<> { 449 public: 450 static void insert(OwningRewritePatternList &patterns, MLIRContext *ctx) {} 451 }; 452 453 template <typename OpTy, typename... OpTypes> 454 class CanonicalizationPatternList<OpTy, OpTypes...> { 455 public: 456 static void insert(OwningRewritePatternList &patterns, MLIRContext *ctx) { 457 OpTy::getCanonicalizationPatterns(patterns, ctx); 458 CanonicalizationPatternList<OpTypes...>::insert(patterns, ctx); 459 } 460 }; 461 462 /// Helper classes for type list expansion. 463 template <typename... OpTypes> 464 class RewritePatternList; 465 466 template <> 467 class RewritePatternList<> { 468 public: 469 static void insert(OwningRewritePatternList &patterns, 470 const LinalgTilingOptions &options, MLIRContext *ctx) {} 471 }; 472 473 template <typename OpTy, typename... OpTypes> 474 class RewritePatternList<OpTy, OpTypes...> { 475 public: 476 static void insert(OwningRewritePatternList &patterns, 477 const LinalgTilingOptions &options, MLIRContext *ctx) { 478 patterns.insert<LinalgTilingPattern<OpTy>>( 479 ctx, options, LinalgMarker({}, Identifier::get("tiled", ctx))); 480 RewritePatternList<OpTypes...>::insert(patterns, options, ctx); 481 } 482 }; 483 } // namespace 484 485 OwningRewritePatternList 486 mlir::linalg::getLinalgTilingCanonicalizationPatterns(MLIRContext *ctx) { 487 OwningRewritePatternList patterns; 488 AffineApplyOp::getCanonicalizationPatterns(patterns, ctx); 489 AffineForOp::getCanonicalizationPatterns(patterns, ctx); 490 AffineMinOp::getCanonicalizationPatterns(patterns, ctx); 491 AffineMaxOp::getCanonicalizationPatterns(patterns, ctx); 492 scf::ForOp::getCanonicalizationPatterns(patterns, ctx); 493 scf::ParallelOp::getCanonicalizationPatterns(patterns, ctx); 494 ConstantIndexOp::getCanonicalizationPatterns(patterns, ctx); 495 SubViewOp::getCanonicalizationPatterns(patterns, ctx); 496 ViewOp::getCanonicalizationPatterns(patterns, ctx); 497 CanonicalizationPatternList< 498 #define GET_OP_LIST 499 #include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.cpp.inc" 500 >::insert(patterns, ctx); 501 return patterns; 502 } 503 504 /// Populate the given list with patterns that apply Linalg tiling. 505 static void insertTilingPatterns(OwningRewritePatternList &patterns, 506 const LinalgTilingOptions &options, 507 MLIRContext *ctx) { 508 RewritePatternList< 509 #define GET_OP_LIST 510 #include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.cpp.inc" 511 >::insert(patterns, options, ctx); 512 } 513 514 static void applyTilingToLoopPatterns(LinalgTilingLoopType loopType, 515 FuncOp funcOp, 516 ArrayRef<int64_t> tileSizes) { 517 auto options = 518 LinalgTilingOptions().setTileSizes(tileSizes).setLoopType(loopType); 519 MLIRContext *ctx = funcOp.getContext(); 520 OwningRewritePatternList patterns; 521 insertTilingPatterns(patterns, options, ctx); 522 applyPatternsAndFoldGreedily(funcOp, patterns); 523 applyPatternsAndFoldGreedily(funcOp, 524 getLinalgTilingCanonicalizationPatterns(ctx)); 525 // Drop the marker. 526 funcOp.walk([](LinalgOp op) { 527 op.removeAttr(LinalgTransforms::kLinalgTransformMarker); 528 }); 529 } 530 531 namespace { 532 struct LinalgTilingPass : public LinalgTilingBase<LinalgTilingPass> { 533 LinalgTilingPass() = default; 534 LinalgTilingPass(ArrayRef<int64_t> sizes) { tileSizes = sizes; } 535 536 void runOnFunction() override { 537 applyTilingToLoopPatterns(LinalgTilingLoopType::Loops, getFunction(), 538 tileSizes); 539 } 540 }; 541 542 struct LinalgTilingToParallelLoopsPass 543 : public LinalgTilingToParallelLoopsBase<LinalgTilingToParallelLoopsPass> { 544 LinalgTilingToParallelLoopsPass() = default; 545 LinalgTilingToParallelLoopsPass(ArrayRef<int64_t> sizes) { 546 tileSizes = sizes; 547 } 548 549 void runOnFunction() override { 550 applyTilingToLoopPatterns(LinalgTilingLoopType::ParallelLoops, 551 getFunction(), tileSizes); 552 } 553 }; 554 555 } // namespace 556 557 std::unique_ptr<OperationPass<FuncOp>> 558 mlir::createLinalgTilingPass(ArrayRef<int64_t> tileSizes) { 559 return std::make_unique<LinalgTilingPass>(tileSizes); 560 } 561 562 std::unique_ptr<OperationPass<FuncOp>> 563 mlir::createLinalgTilingToParallelLoopsPass(ArrayRef<int64_t> tileSizes) { 564 return std::make_unique<LinalgTilingToParallelLoopsPass>(tileSizes); 565 } 566