1 //===- LinalgTransforms.cpp - Linalg transformations as patterns ----------===// 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 logic and helpers to expose Linalg transforms as rewrite 10 // patterns. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "mlir/Dialect/Linalg/Transforms/Transforms.h" 15 #include "mlir/Dialect/Affine/Utils.h" 16 #include "mlir/Dialect/Linalg/Analysis/DependenceAnalysis.h" 17 #include "mlir/Dialect/Linalg/IR/LinalgOps.h" 18 #include "mlir/Dialect/Linalg/Utils/Utils.h" 19 #include "mlir/Dialect/Tensor/IR/Tensor.h" 20 #include "mlir/Dialect/Utils/StaticValueUtils.h" 21 #include "mlir/Dialect/Utils/StructuredOpsUtils.h" 22 #include "mlir/Dialect/Vector/VectorOps.h" 23 #include "mlir/IR/AffineExpr.h" 24 #include "mlir/IR/Matchers.h" 25 #include "mlir/Pass/Pass.h" 26 #include "mlir/Support/LLVM.h" 27 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" 28 #include "llvm/ADT/ScopeExit.h" 29 #include "llvm/Support/Debug.h" 30 #include "llvm/Support/raw_ostream.h" 31 #include <type_traits> 32 33 #define DEBUG_TYPE "linalg-transforms" 34 35 using namespace mlir; 36 using namespace mlir::linalg; 37 38 #define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE << "]: ") 39 40 //===----------------------------------------------------------------------===// 41 // Transformations exposed as rewrite patterns. 42 //===----------------------------------------------------------------------===// 43 // Marker used as attribute name in generated Linalg rewriting transformations. 44 const StringLiteral mlir::linalg::LinalgTransforms::kLinalgTransformMarker = 45 "__internal_linalg_transform__"; 46 47 mlir::linalg::LinalgTransformationFilter::LinalgTransformationFilter( 48 ArrayRef<Identifier> matchDisjunction, Optional<Identifier> replacement) 49 : matchDisjunction(matchDisjunction.begin(), matchDisjunction.end()), 50 replacement(replacement) {} 51 52 mlir::linalg::LinalgTransformationFilter::LinalgTransformationFilter( 53 FilterFunction f, ArrayRef<Identifier> matchDisjunction, 54 Optional<Identifier> replacement) 55 : filters(), 56 matchDisjunction(matchDisjunction.begin(), matchDisjunction.end()), 57 replacement(replacement) { 58 if (f) 59 filters.push_back(f); 60 } 61 62 LogicalResult mlir::linalg::LinalgTransformationFilter::checkAndNotify( 63 PatternRewriter &rewriter, Operation *op) const { 64 if (llvm::any_of(filters, 65 [&](const FilterFunction &f) { return failed(f(op)); })) 66 return failure(); 67 68 auto attr = op->template getAttrOfType<StringAttr>( 69 LinalgTransforms::kLinalgTransformMarker); 70 71 if (!attr) { 72 // 1. Has no filter case and matchDisjunction is empty. 73 if (matchDisjunction.empty()) 74 return success(); 75 76 // 2. Has no filter but was expecting a filter. 77 return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) { 78 diag << " does not have any filter from list: "; 79 interleaveComma(matchDisjunction, diag); 80 }); 81 } 82 83 // 4. Match explicit filter. 84 for (auto filter : matchDisjunction) 85 if (attr.getValue() == filter) 86 return success(); 87 88 // 5. Fail to match. 89 return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) { 90 diag << " does not have any filter from list: "; 91 interleaveComma(matchDisjunction, diag); 92 }); 93 } 94 95 void mlir::linalg::LinalgTransformationFilter:: 96 replaceLinalgTransformationFilter(PatternRewriter &rewriter, 97 Operation *op) const { 98 if (replacement.hasValue()) 99 op->setAttr(LinalgTransforms::kLinalgTransformMarker, 100 rewriter.getStringAttr(replacement.getValue().strref())); 101 else 102 op->removeAttr(Identifier::get(LinalgTransforms::kLinalgTransformMarker, 103 rewriter.getContext())); 104 } 105 106 LinalgTilingOptions & 107 mlir::linalg::LinalgTilingOptions::setTileSizes(ArrayRef<int64_t> ts) { 108 SmallVector<int64_t, 4> tileSizes(ts.begin(), ts.end()); 109 tileSizeComputationFunction = [tileSizes](OpBuilder &b, Operation *op) { 110 OpBuilder::InsertionGuard guard(b); 111 b.setInsertionPointToStart( 112 &op->getParentOfType<FuncOp>().getBody().front()); 113 return llvm::to_vector<4>(map_range(tileSizes, [&](int64_t s) { 114 Value v = b.create<ConstantIndexOp>(op->getLoc(), s); 115 return v; 116 })); 117 }; 118 return *this; 119 } 120 121 /// Try to compute a static bounding box for `operand` 122 /// Return success if either: 123 /// 1. The operand is already statically shaped, `result` is left unchanged. 124 /// 2. The operand is (partially) dynamic, `result` is the result of a freshly 125 /// created PadTensorOp. 126 /// Return failure if the operand cannot be padded to a static shape. 127 static LogicalResult padOperandToSmallestStaticBoundingBox( 128 PatternRewriter &rewriter, linalg::LinalgOp opToPad, OpOperand *opOperand, 129 const PaddingValueComputationFunction &paddingFunc, Value &result) { 130 // Already static shape, no need to pad. 131 if (llvm::none_of(opToPad.getShape(opOperand), ShapedType::isDynamic)) 132 return success(); 133 auto sliceOp = opOperand->get().getDefiningOp<tensor::ExtractSliceOp>(); 134 // Not a slice op, cannot construct a static bounding box. 135 if (!sliceOp) 136 return failure(); 137 SmallVector<int64_t> staticSizes; 138 staticSizes.reserve(opToPad.getRank(opOperand)); 139 auto shapedOp = cast<OffsetSizeAndStrideOpInterface>(sliceOp.getOperation()); 140 for (auto size : shapedOp.getMixedSizes()) { 141 auto indexAttr = size.is<Attribute>() 142 ? size.get<Attribute>().dyn_cast<IntegerAttr>() 143 : linalg::getSmallestBoundingIndex(size.get<Value>()); 144 // SmallestBoundingIndex must exist for all sizes. 145 // For now return an error if we can't find it. 146 if (!indexAttr) 147 return rewriter.notifyMatchFailure( 148 opToPad, "No constant bounding box can be found for padding"); 149 staticSizes.push_back(indexAttr.getInt()); 150 } 151 Value pad = paddingFunc(rewriter, *opOperand); 152 auto staticTensorType = RankedTensorType::get( 153 staticSizes, getElementTypeOrSelf(opOperand->get())); 154 result = linalg::PadTensorOp::createPadHighOp( 155 staticTensorType, opOperand->get(), pad, opToPad->getLoc(), rewriter); 156 return success(); 157 } 158 159 LogicalResult 160 linalg::rewriteAsPaddedOp(PatternRewriter &rewriter, LinalgOp opToPad, 161 const PaddingValueComputationFunction &paddingFunc, 162 LinalgOp &paddedOp) { 163 Location loc = opToPad->getLoc(); 164 165 // If the op is fully static, it does not need padding. 166 // TODO: there are cases where we may still want to pad to larger sizes. 167 assert(opToPad.hasTensorSemantics() && 168 "expected operation to have tensor semantics"); 169 if (!opToPad.hasDynamicShape()) 170 return success(); 171 172 OpBuilder::InsertionGuard g(rewriter); 173 // Set IP after op because we also take the dims of the original output. 174 rewriter.setInsertionPointAfter(opToPad); 175 // Make a copy of the shaped operands and update it. 176 SmallVector<Value> newOperands; 177 newOperands.reserve(opToPad.getNumInputsAndOutputs()); 178 for (OpOperand *opOperand : opToPad.getInputAndOutputOperands()) { 179 Value paddedOperand; 180 // If padding was requested but the shape cannot be bounded statically then 181 // the pattern fails to apply. 182 if (failed(padOperandToSmallestStaticBoundingBox( 183 rewriter, opToPad, opOperand, paddingFunc, paddedOperand))) 184 return failure(); 185 newOperands.push_back(paddedOperand ? paddedOperand : opOperand->get()); 186 } 187 188 // Clone `opToPad` to operate on the statically padded shapes. 189 auto resultTensorTypes = 190 ValueRange(newOperands).take_back(opToPad.getNumOutputs()).getTypes(); 191 paddedOp = opToPad.clone(rewriter, loc, resultTensorTypes, newOperands); 192 193 // Recover the slice out of the new static results. This keeps the original 194 // linalg op around because it uses the dims of the original results. 195 // This later folds away. 196 SmallVector<Value> paddedSubviewResults; 197 paddedSubviewResults.reserve(opToPad->getNumResults()); 198 SetVector<Operation *> newUsersOfOpToPad; 199 for (auto it : llvm::zip(opToPad->getResults(), paddedOp->getResults())) { 200 auto rank = std::get<0>(it).getType().cast<RankedTensorType>().getRank(); 201 SmallVector<OpFoldResult> offsets(rank, rewriter.getIndexAttr(0)); 202 auto sizes = llvm::to_vector<4>(llvm::map_range( 203 llvm::seq<unsigned>(0, rank), [&](unsigned d) -> OpFoldResult { 204 auto dimOp = rewriter.create<tensor::DimOp>(loc, std::get<0>(it), d); 205 newUsersOfOpToPad.insert(dimOp); 206 return dimOp.getResult(); 207 })); 208 SmallVector<OpFoldResult> strides(rank, rewriter.getIndexAttr(1)); 209 paddedSubviewResults.push_back(rewriter.create<tensor::ExtractSliceOp>( 210 loc, std::get<1>(it), offsets, sizes, strides)); 211 } 212 // Replace the transient `opToPad` locally, except for uses that we just 213 // created for the purpose of extracting the dims. 214 rewriter.replaceOpWithIf(opToPad, paddedSubviewResults, [&](OpOperand &opOp) { 215 return !newUsersOfOpToPad.contains(opOp.getOwner()); 216 }); 217 return success(); 218 } 219 220 /// Linalg base tiling pattern. 221 mlir::linalg::LinalgBaseTilingPattern::LinalgBaseTilingPattern( 222 StringRef opName, MLIRContext *context, LinalgTilingOptions options, 223 LinalgTransformationFilter filter, PatternBenefit benefit) 224 : RewritePattern(opName, benefit, context), filter(filter), 225 options(options) {} 226 227 mlir::linalg::LinalgBaseTilingPattern::LinalgBaseTilingPattern( 228 MLIRContext *context, LinalgTilingOptions options, 229 LinalgTransformationFilter filter, PatternBenefit benefit) 230 : RewritePattern(MatchAnyOpTypeTag(), benefit, context), filter(filter), 231 options(options) {} 232 233 LogicalResult mlir::linalg::LinalgBaseTilingPattern::matchAndRewriteBase( 234 Operation *op, PatternRewriter &rewriter, TiledLinalgOp &result) const { 235 LinalgOp linalgOp = dyn_cast<LinalgOp>(op); 236 if (!linalgOp) 237 return failure(); 238 if (failed(filter.checkAndNotify(rewriter, linalgOp))) 239 return failure(); 240 241 Optional<TiledLinalgOp> res = tileLinalgOp(rewriter, linalgOp, options); 242 243 if (!res) 244 return failure(); 245 246 // Setup RAII guard to return properly. 247 LinalgOp tiledOp = res->op; 248 auto guard = llvm::make_scope_exit([&]() { 249 // Return relevant information to derived pattern. 250 result = *res; 251 // Replace filter on both tiledOp and tiledAndPaddedOp, if necessary. 252 filter.replaceLinalgTransformationFilter(rewriter, tiledOp); 253 if (tiledOp != res->op) 254 filter.replaceLinalgTransformationFilter(rewriter, res->op); 255 }); 256 257 // Consider padding on the fly only if the op has tensor semantics. 258 if (!options.paddingValueComputationFunction || 259 !linalgOp.hasTensorSemantics()) 260 return success(); 261 262 // Try to pad on the fly by rewriting res->op as a padded op. If successful, 263 // `res.op` is rewritten in static form with padded operands. 264 LinalgOp paddedOp; 265 if (succeeded(rewriteAsPaddedOp(rewriter, res->op, 266 options.paddingValueComputationFunction, 267 paddedOp))) { 268 res->op = paddedOp; 269 // Do not perform replacement of `linalgOp`, let the derived patterns 270 // do this as they see fit, from the resulting TiledLinalgOp. 271 return success(); 272 } 273 // Set so RAII guard does not propagate TiledLinalgOp to `result`. 274 return failure(); 275 } 276 277 static ValueRange getTiledOpResult(TiledLinalgOp tiledOp) { 278 if (tiledOp.loops.empty()) 279 return tiledOp.op.getOperation()->getResults(); 280 return tiledOp.loops.front()->getResults(); 281 } 282 283 static ValueRange 284 getTiledAndFusedOpResult(TiledAndFusedLinalgOps tiledAndFusedOp) { 285 if (tiledAndFusedOp.fusedLoops.empty()) 286 return tiledAndFusedOp.op.getOperation()->getResults(); 287 return tiledAndFusedOp.fusedLoops.front()->getResults(); 288 } 289 290 mlir::linalg::LinalgBaseTileAndFusePattern::LinalgBaseTileAndFusePattern( 291 StringRef opName, MLIRContext *context, 292 const LinalgDependenceGraph &dependenceGraph, 293 LinalgTilingOptions tilingOptions, LinalgFusionOptions fusionOptions, 294 LinalgTransformationFilter filter, LinalgTransformationFilter fusedOpMarker, 295 LinalgTransformationFilter originalOpMarker, PatternBenefit benefit) 296 : RewritePattern(opName, benefit, context, {}), 297 dependenceGraph(dependenceGraph), tilingOptions(tilingOptions), 298 fusionOptions(fusionOptions), filter(filter), 299 fusedOpMarker(fusedOpMarker), originalOpMarker(originalOpMarker) {} 300 301 LogicalResult mlir::linalg::LinalgBaseTileAndFusePattern::matchAndRewrite( 302 Operation *op, PatternRewriter &rewriter) const { 303 LinalgOp linalgOp = dyn_cast<LinalgOp>(op); 304 // TODO: remove hasIndexSemantics check once index ops are supported. 305 if (!linalgOp || linalgOp.hasIndexSemantics()) 306 return failure(); 307 if (failed(filter.checkAndNotify(rewriter, linalgOp))) 308 return failure(); 309 310 DenseSet<Operation *> producers; 311 producers.insert(linalgOp); 312 for (auto dependence : dependenceGraph.getDependentOperationsInto(linalgOp)) { 313 Optional<unsigned> operandNumber = dependence.getIndexingOpViewOperandNum(); 314 // When looking at dependences into, indexingOp is always OpOperand. We 315 // could assert, but continue if this is not the case. 316 if (!operandNumber) 317 continue; 318 if (!fusionOptions.indicesToFuse.count(operandNumber.getValue())) 319 continue; 320 if (isa<LinalgOp>(dependence.getDependentOp())) 321 producers.insert(dependence.getDependentOp()); 322 } 323 324 SmallVector<LinalgOp, 1> fusionOps; 325 for (auto it = op->getBlock()->begin(), ie = Block::iterator(op); it != ie; 326 ++it) { 327 auto producerLinalgOp = dyn_cast<LinalgOp>(&(*it)); 328 if (producerLinalgOp && producers.count(producerLinalgOp)) 329 fusionOps.push_back(producerLinalgOp); 330 } 331 fusionOps.push_back(linalgOp); 332 333 SmallVector<Value, 4> tileSizes = 334 tilingOptions.tileSizeComputationFunction(rewriter, op); 335 LinalgTilingOptions instanceTilingOptions = tilingOptions; 336 instanceTilingOptions.setTileSizes(tileSizes); 337 Optional<TiledAndFusedLinalgOps> tiledAndFusedOps = tileAndFuseLinalgOps( 338 rewriter, fusionOps, dependenceGraph, instanceTilingOptions); 339 if (!tiledAndFusedOps) 340 return failure(); 341 342 // Tile the unfused loops; 343 SmallVector<Value, 4> unfusedLoopTileSizes; 344 Value zero = rewriter.create<ConstantIndexOp>(op->getLoc(), 0); 345 for (auto tileSize : enumerate(tileSizes)) { 346 if (tiledAndFusedOps->fusedLoopDims.count(tileSize.index())) 347 unfusedLoopTileSizes.push_back(zero); 348 else 349 unfusedLoopTileSizes.push_back(tileSize.value()); 350 } 351 // Tile the loop only if there is a non-zero tile size. 352 if (unfusedLoopTileSizes.size() > linalgOp.getNumLoops()) 353 unfusedLoopTileSizes.resize(linalgOp.getNumLoops()); 354 if (llvm::any_of(unfusedLoopTileSizes, [](Value val) { 355 if (auto cst = val.getDefiningOp<ConstantIndexOp>()) 356 return cst.getValue() != 0; 357 return true; 358 })) { 359 LinalgTilingOptions unfusedTilingOptions = tilingOptions; 360 unfusedTilingOptions.setTileSizes(unfusedLoopTileSizes); 361 Optional<TiledLinalgOp> unfusedTiledOp = 362 tileLinalgOp(rewriter, tiledAndFusedOps->op, unfusedTilingOptions); 363 if (!unfusedTiledOp) 364 return failure(); 365 rewriter.replaceOp(tiledAndFusedOps->op, 366 getTiledOpResult(unfusedTiledOp.getValue())); 367 tiledAndFusedOps->op = unfusedTiledOp->op; 368 } 369 op->replaceAllUsesWith(getTiledAndFusedOpResult(tiledAndFusedOps.getValue())); 370 371 filter.replaceLinalgTransformationFilter(rewriter, 372 tiledAndFusedOps->op.getOperation()); 373 for (auto fusedOp : tiledAndFusedOps->fusedProducers) { 374 fusedOpMarker.replaceLinalgTransformationFilter(rewriter, 375 fusedOp.getOperation()); 376 } 377 for (auto origProducerOp : ArrayRef<LinalgOp>(fusionOps).drop_back()) { 378 originalOpMarker.replaceLinalgTransformationFilter( 379 rewriter, origProducerOp.getOperation()); 380 } 381 rewriter.updateRootInPlace(op, [&]() { 382 originalOpMarker.replaceLinalgTransformationFilter(rewriter, op); 383 }); 384 return success(); 385 } 386 387 /// Linalg generic interchange pattern. 388 mlir::linalg::GenericOpInterchangePattern::GenericOpInterchangePattern( 389 MLIRContext *context, ArrayRef<unsigned> interchangeVector, 390 LinalgTransformationFilter filter, PatternBenefit benefit) 391 : OpRewritePattern(context, benefit), filter(filter), 392 interchangeVector(interchangeVector.begin(), interchangeVector.end()) {} 393 394 LogicalResult mlir::linalg::GenericOpInterchangePattern::matchAndRewrite( 395 GenericOp genericOp, PatternRewriter &rewriter) const { 396 if (failed(filter.checkAndNotify(rewriter, genericOp))) 397 return failure(); 398 if (failed(interchangeGenericOpPrecondition(genericOp, interchangeVector))) 399 return failure(); 400 401 // TODO: figure out how this interplays with named ops. In particular this 402 // should break the named op property. 403 rewriter.updateRootInPlace(genericOp, [&]() { 404 interchangeGenericOp(rewriter, genericOp, interchangeVector); 405 // New filter if specified. 406 filter.replaceLinalgTransformationFilter(rewriter, genericOp); 407 }); 408 return success(); 409 } 410 411 mlir::linalg::LinalgBasePromotionPattern::LinalgBasePromotionPattern( 412 StringRef opName, MLIRContext *context, LinalgPromotionOptions options, 413 LinalgTransformationFilter filter, PatternBenefit benefit) 414 : RewritePattern(opName, benefit, context, {}), filter(filter), 415 options(options) {} 416 417 LogicalResult mlir::linalg::LinalgBasePromotionPattern::matchAndRewrite( 418 Operation *op, PatternRewriter &rewriter) const { 419 if (failed(filter.checkAndNotify(rewriter, op))) 420 return failure(); 421 if (failed(promoteSubviewsPrecondition(op, options))) 422 return failure(); 423 424 // TODO: We cannot use root update here. This pattern is creating other ops, 425 // so if the promotion fails, those need to be cleaned up, which doesnt seem 426 // to be happening here. So to fail properly, we should be cloning the op and 427 // deleting the previous op. This needs more investigation. 428 rewriter.startRootUpdate(op); 429 Optional<LinalgOp> promotedOp = promoteSubViews(rewriter, op, options); 430 if (!promotedOp) { 431 rewriter.cancelRootUpdate(op); 432 return op->emitError("subview promotion failed"); 433 } 434 rewriter.finalizeRootUpdate(op); 435 filter.replaceLinalgTransformationFilter(rewriter, op); 436 return success(); 437 } 438 439 mlir::linalg::LinalgBaseVectorizationPattern::LinalgBaseVectorizationPattern( 440 MLIRContext *context, LinalgTransformationFilter filter, 441 PatternBenefit benefit) 442 : RewritePattern(MatchAnyOpTypeTag(), benefit, context), filter(filter) {} 443 444 mlir::linalg::LinalgBaseVectorizationPattern::LinalgBaseVectorizationPattern( 445 StringRef opName, MLIRContext *context, LinalgTransformationFilter filter, 446 PatternBenefit benefit) 447 : RewritePattern(opName, benefit, context, {}), filter(filter) {} 448 449 LogicalResult mlir::linalg::LinalgBaseVectorizationPattern::matchAndRewrite( 450 Operation *op, PatternRewriter &rewriter) const { 451 LinalgOp linalgOp = dyn_cast<LinalgOp>(op); 452 if (!linalgOp) 453 return failure(); 454 if (failed(filter.checkAndNotify(rewriter, linalgOp))) 455 return failure(); 456 SmallVector<Value> newResults; 457 if (failed(vectorizeLinalgOp(rewriter, op, newResults))) 458 return failure(); 459 if (!newResults.empty()) 460 rewriter.replaceOp(op, newResults); 461 else 462 rewriter.eraseOp(op); 463 return success(); 464 } 465 466 LogicalResult mlir::linalg::applyStagedPatterns( 467 Operation *op, ArrayRef<FrozenRewritePatternSet> stage1Patterns, 468 const FrozenRewritePatternSet &stage2Patterns, 469 function_ref<LogicalResult(Operation *)> stage3Lambda) { 470 unsigned iteration = 0; 471 (void)iteration; 472 for (const auto &patterns : stage1Patterns) { 473 LLVM_DEBUG(DBGS() << "Before 1st stage, iter: " << ++iteration << "\n" 474 << *op); 475 if (failed(applyPatternsAndFoldGreedily(op, patterns))) { 476 LLVM_DEBUG(DBGS() << "Underlying first stage rewrite did not converge"); 477 return failure(); 478 } 479 LLVM_DEBUG(DBGS() << "After 1st stage, iter: " << ++iteration << "\n" 480 << *op); 481 if (failed(applyPatternsAndFoldGreedily(op, stage2Patterns))) { 482 LLVM_DEBUG(DBGS() << "Underlying 2nd stage rewrite did not converge"); 483 return failure(); 484 } 485 LLVM_DEBUG(DBGS() << "After 2nd stage, iter : " << iteration << "\n" 486 << *op); 487 if (stage3Lambda) { 488 if (failed(stage3Lambda(op))) 489 return failure(); 490 LLVM_DEBUG(DBGS() << "After 3rd stage, iter : " << iteration << "\n" 491 << *op); 492 } 493 } 494 return success(); 495 } 496 497 /// Traverse the `dims` and substitute known min or max expressions returned by 498 /// the lambda |getMinMaxExpr|. 499 static AffineMap substitute(AffineMap map, SmallVectorImpl<Value> &dims, 500 SmallVectorImpl<Value> &symbols, 501 GetMinMaxExprFn getMinMaxExpr) { 502 auto exprs = llvm::to_vector<4>(map.getResults()); 503 for (AffineExpr &expr : exprs) { 504 bool substituted = true; 505 while (substituted) { 506 substituted = false; 507 for (unsigned dimIdx = 0; dimIdx < dims.size(); ++dimIdx) { 508 Value dim = dims[dimIdx]; 509 auto minMax = getMinMaxExpr(dim, dims, symbols); 510 if (!minMax) 511 continue; 512 AffineExpr dimExpr = getAffineDimExpr(dimIdx, expr.getContext()); 513 LLVM_DEBUG(DBGS() << "Subst: " << dim << " @ " << dimExpr << "\n"); 514 LLVM_DEBUG(DBGS() << "Before: " << expr << "\n"); 515 // Substitute occurrences of `dimExpr` by either the min expression or 516 // the max expression depending on whether the value is used with a 517 // positive or negative coefficient. 518 AffineExpr substitutedExpr = 519 substWithMin(expr, dimExpr, minMax->first, minMax->second); 520 LLVM_DEBUG(DBGS() << "After: " << substitutedExpr << "\n"); 521 substituted = (substitutedExpr != expr); 522 expr = substitutedExpr; 523 } 524 } 525 526 // Cleanup and simplify the results. 527 // This needs to happen outside of the loop iterating on dims.size() since 528 // it modifies dims. 529 SmallVector<Value, 4> operands(dims.begin(), dims.end()); 530 operands.append(symbols.begin(), symbols.end()); 531 auto map = AffineMap::get(dims.size(), symbols.size(), exprs, 532 exprs.front().getContext()); 533 534 LLVM_DEBUG({ 535 DBGS() << "Map to simplify: " << map << "\n"; 536 DBGS() << "Operands:\n"; 537 for (Value v : operands) 538 DBGS() << v << "\n"; 539 }); 540 541 // Pull in affine.apply operations and compose them fully into the 542 // result. 543 fullyComposeAffineMapAndOperands(&map, &operands); 544 canonicalizeMapAndOperands(&map, &operands); 545 map = simplifyAffineMap(map); 546 // Assign the results. 547 exprs.assign(map.getResults().begin(), map.getResults().end()); 548 dims.assign(operands.begin(), operands.begin() + map.getNumDims()); 549 symbols.assign(operands.begin() + map.getNumDims(), operands.end()); 550 551 LLVM_DEBUG(DBGS() << "Map simplified: " << map << "\n"); 552 } 553 554 assert(!exprs.empty() && "Unexpected empty exprs"); 555 return AffineMap::get(dims.size(), symbols.size(), exprs, map.getContext()); 556 } 557 558 /// Traverse the dims of the AffineMap of `affineMinOp` and substitute 559 /// dimensions with known range by new expressions involving the min or max 560 /// expression: 561 /// - If the AffineDimExpr mapped to a known value has a positive sign, it 562 /// is replaced by the min expression. 563 /// - If the AffineDimExpr mapped to a known value has a negative sign, it is 564 /// replaced by the max expression. 565 /// All known values are iteratively replaced. 566 /// This is used as an intermediate step in computing bounding boxes and 567 /// canonicalize AffineMinOps. All dim and symbol operands are assumed to have 568 /// positive values (positive orthant assumptions). 569 /// Return a new AffineMap, dims and symbols that have been canonicalized and 570 /// simplified. 571 AffineMapAndOperands 572 mlir::linalg::substituteMin(AffineMinOp affineMinOp, 573 GetMinMaxExprFn getMinMaxExpr) { 574 AffineMapAndOperands res{affineMinOp.getAffineMap(), 575 SmallVector<Value>(affineMinOp.getDimOperands()), 576 SmallVector<Value>(affineMinOp.getSymbolOperands())}; 577 res.map = substitute(affineMinOp.getAffineMap(), res.dims, res.symbols, 578 getMinMaxExpr); 579 return res; 580 } 581 582 LogicalResult AffineMinRangeCanonicalizationPattern::matchAndRewrite( 583 AffineMinOp minOp, PatternRewriter &rewriter) const { 584 LLVM_DEBUG(DBGS() << "Canonicalize AffineMinSCF: " << *minOp.getOperation() 585 << "\n"); 586 587 auto affineMapAndOperands = substituteMin(minOp, getMinMaxFn); 588 AffineMap map = affineMapAndOperands.map; 589 590 LLVM_DEBUG(DBGS() << "Resulting map: " << map << "\n"); 591 592 // Check whether any of the expressions, when subtracted from all other 593 // expressions, produces only >= 0 constants. If so, it is the min. 594 for (auto e : minOp.getAffineMap().getResults()) { 595 LLVM_DEBUG(DBGS() << "Candidate min: " << e << "\n"); 596 if (!e.isSymbolicOrConstant()) 597 continue; 598 599 auto isNonPositive = [](AffineExpr e) { 600 if (auto cst = e.dyn_cast<AffineConstantExpr>()) 601 return cst.getValue() < 0; 602 return true; 603 }; 604 605 // Build the subMap and check everything is statically known to be 606 // positive. 607 SmallVector<AffineExpr, 4> subExprs; 608 subExprs.reserve(map.getNumResults()); 609 for (auto ee : map.getResults()) 610 subExprs.push_back(ee - e); 611 MLIRContext *ctx = minOp.getContext(); 612 AffineMap subMap = simplifyAffineMap( 613 AffineMap::get(map.getNumDims(), map.getNumSymbols(), subExprs, ctx)); 614 LLVM_DEBUG(DBGS() << "simplified subMap: " << subMap << "\n"); 615 if (llvm::any_of(subMap.getResults(), isNonPositive)) 616 continue; 617 618 // Static min found. 619 if (auto cst = e.dyn_cast<AffineConstantExpr>()) { 620 rewriter.replaceOpWithNewOp<ConstantIndexOp>(minOp, cst.getValue()); 621 } else { 622 auto resultMap = AffineMap::get(0, map.getNumSymbols(), {e}, ctx); 623 SmallVector<Value> resultOperands = affineMapAndOperands.dims; 624 llvm::append_range(resultOperands, affineMapAndOperands.symbols); 625 canonicalizeMapAndOperands(&resultMap, &resultOperands); 626 resultMap = simplifyAffineMap(resultMap); 627 rewriter.replaceOpWithNewOp<AffineApplyOp>(minOp, resultMap, 628 resultOperands); 629 } 630 return success(); 631 } 632 633 return failure(); 634 } 635 636 static SmallVector<StringRef> getNParallelLoopsAttrs(unsigned nParallelLoops) { 637 return SmallVector<StringRef>(nParallelLoops, getParallelIteratorTypeName()); 638 } 639 640 /// Rewrite a PadTensorOp into a sequence of InitTensorOp, FillOp (to initialize 641 /// with pad_val) and GenericOp (to copy contents). 642 LogicalResult PadTensorOpTransformationPattern::matchAndRewrite( 643 linalg::PadTensorOp padOp, PatternRewriter &rewriter) const { 644 645 auto inputShapedType = padOp.source().getType().cast<ShapedType>(); 646 auto resultShapedType = padOp.result().getType().cast<ShapedType>(); 647 648 // Bail on non-static shapes. 649 if (!inputShapedType.hasStaticShape()) 650 return failure(); 651 if (!resultShapedType.hasStaticShape()) 652 return failure(); 653 654 // Only support padding with a constant for now, i.e. either: 655 // 1. A BBarg from a different block. 656 // 2. A value defined outside of the current block. 657 Block &block = padOp.region().front(); 658 auto yieldOp = cast<YieldOp>(block.getTerminator()); 659 assert(yieldOp.getNumOperands() == 1 && "expected single operand yield"); 660 Value padValue = yieldOp.values().front(); 661 Operation *definingOp = padValue.getDefiningOp(); 662 if (definingOp && definingOp->getBlock() == &block) 663 return failure(); 664 if (!definingOp && padValue.cast<BlockArgument>().getOwner() == &block) 665 return failure(); 666 667 // Create tensor with the padded shape 668 Location loc = padOp.getLoc(); 669 SmallVector<Value> indices(resultShapedType.getRank(), 670 rewriter.create<ConstantIndexOp>(loc, 0)); 671 Value initTensor = rewriter.create<InitTensorOp>( 672 loc, resultShapedType.getShape(), resultShapedType.getElementType()); 673 674 // Initialize tensor with the pad value 675 Value tmpTensor = 676 rewriter.create<linalg::FillOp>(loc, padValue, initTensor).result(); 677 678 // Copy original contents into new tensor 679 // Uses linalg.generic, but could be done with tensor.insert_slice 680 SmallVector<AffineExpr, 4> outputExprs; 681 for (unsigned i = 0; i < resultShapedType.getRank(); ++i) { 682 outputExprs.push_back(getAffineDimExpr(i, rewriter.getContext()) + 683 padOp.static_low()[i].cast<IntegerAttr>().getInt()); 684 } 685 686 SmallVector<AffineMap, 2> transferMaps = { 687 rewriter.getMultiDimIdentityMap(inputShapedType.getRank()), 688 AffineMap::get(resultShapedType.getRank(), 689 /*symbolCount=*/0, outputExprs, rewriter.getContext())}; 690 691 rewriter.replaceOpWithNewOp<linalg::GenericOp>( 692 padOp, resultShapedType, padOp.source(), tmpTensor, transferMaps, 693 getNParallelLoopsAttrs(resultShapedType.getRank()), 694 [&](OpBuilder &nestedBuilder, Location nestedLoc, ValueRange args) { 695 nestedBuilder.create<linalg::YieldOp>(nestedLoc, args[0]); 696 }); 697 698 return success(); 699 } 700 701 /// Filling `dest` using FillOp constant padding value if possible. 702 /// Otherwise, generate a tensor::GenerateOp. 703 Value GeneralizePadTensorOpPattern::createFillOrGenerateOp( 704 PatternRewriter &rewriter, PadTensorOp padOp, Value dest, 705 const SmallVector<Value> &dynSizes) const { 706 auto padValue = padOp.getConstantPaddingValue(); 707 if (padValue) 708 return rewriter.create<FillOp>(padOp.getLoc(), padValue, dest).result(); 709 710 // Fill could not be optimized: Lower to tensor::GenerateOp with region. 711 auto generateOp = rewriter.create<tensor::GenerateOp>( 712 padOp.getLoc(), padOp.getResultType(), dynSizes); 713 // Copy region to new op. 714 BlockAndValueMapping bvm; 715 padOp.region().cloneInto(&generateOp.getRegion(), bvm); 716 // Rewrite linalg::YieldOp to tensor::YieldOp. 717 OpBuilder::InsertionGuard guard(rewriter); 718 auto yieldOp = 719 dyn_cast<linalg::YieldOp>(generateOp.getRegion().front().getTerminator()); 720 assert(yieldOp && "malformed PadTensorOp: expected YieldOp terminator"); 721 assert(yieldOp.values().size() == 1); 722 rewriter.setInsertionPoint(yieldOp); 723 rewriter.replaceOpWithNewOp<tensor::YieldOp>(yieldOp, yieldOp.values()[0]); 724 return generateOp; 725 } 726 727 LogicalResult 728 GeneralizePadTensorOpPattern::matchAndRewrite(PadTensorOp padOp, 729 PatternRewriter &rewriter) const { 730 // Given an OpFoldResult, return an index-typed value. 731 auto getIdxValue = [&](OpFoldResult ofr) { 732 if (auto val = ofr.dyn_cast<Value>()) 733 return val; 734 return rewriter 735 .create<ConstantIndexOp>( 736 padOp.getLoc(), ofr.get<Attribute>().cast<IntegerAttr>().getInt()) 737 .getResult(); 738 }; 739 740 auto resultType = padOp.getResultType(); 741 // Compute size of InitTensorOp. Any combination of static/dynamic is 742 // supported. 743 SmallVector<Value> dynSizes; 744 SmallVector<int64_t> staticSizes; 745 for (unsigned dim = 0; dim < resultType.getRank(); ++dim) { 746 if (resultType.isDynamicDim(dim)) { 747 auto srcSize = rewriter.createOrFold<tensor::DimOp>(padOp.getLoc(), 748 padOp.source(), dim); 749 // Add low and high padding value. 750 auto plusLow = rewriter.createOrFold<AddIOp>( 751 padOp.getLoc(), srcSize, getIdxValue(padOp.getMixedLowPad()[dim])); 752 auto plusHigh = rewriter.createOrFold<AddIOp>( 753 padOp.getLoc(), plusLow, getIdxValue(padOp.getMixedHighPad()[dim])); 754 dynSizes.push_back(plusHigh); 755 } 756 staticSizes.push_back(resultType.getDimSize(dim)); 757 } 758 759 // Init tensor and fill it with padding. 760 Value init = rewriter.create<InitTensorOp>( 761 padOp.getLoc(), dynSizes, staticSizes, resultType.getElementType()); 762 Value fill = createFillOrGenerateOp(rewriter, padOp, init, dynSizes); 763 764 // Try optimize the copy of source. 765 if (optimizeCopyFn && optimizeCopyFn(rewriter, padOp, fill).succeeded()) 766 return success(); 767 768 // PadTensorOps cannot be optimized. Generate a InsertSliceOp instead 769 // for copying the PadOp source. 770 auto sourceType = padOp.getSourceType(); 771 // Compute size of source of PadTensorOp. 772 SmallVector<OpFoldResult> srcSizes; 773 for (unsigned dim = 0; dim < sourceType.getRank(); ++dim) { 774 if (sourceType.isDynamicDim(dim)) { 775 srcSizes.push_back(rewriter.createOrFold<tensor::DimOp>( 776 padOp.getLoc(), padOp.source(), dim)); 777 } else { 778 srcSizes.push_back(rewriter.getIndexAttr(sourceType.getDimSize(dim))); 779 } 780 } 781 // Strides of InsertSliceOp are all 1. 782 SmallVector<OpFoldResult> strides(sourceType.getRank(), 783 rewriter.getIndexAttr(1)); 784 rewriter.replaceOpWithNewOp<tensor::InsertSliceOp>( 785 padOp, padOp.source(), fill, padOp.getMixedLowPad(), srcSizes, strides); 786 787 return success(); 788 } 789 790 /// Given an OpFoldResult, return a Value. If the OpFoldResult is an Attribute, 791 /// it must be of type Integer. 792 static Value asValue(OpBuilder &builder, Location loc, OpFoldResult ofr) { 793 if (auto val = ofr.dyn_cast<Value>()) 794 return val; 795 auto intVal = getConstantIntValue(ofr); 796 assert(intVal && "expected Value or IntegerAttr"); 797 return builder.create<ConstantIndexOp>(loc, *intVal); 798 } 799 800 LogicalResult ExtractSliceOfPadTensorSwapPattern::matchAndRewrite( 801 tensor::ExtractSliceOp sliceOp, PatternRewriter &rewriter) const { 802 auto padOp = sliceOp.source().getDefiningOp<PadTensorOp>(); 803 if (!padOp) 804 return failure(); 805 // Only unit stride supported. 806 if (!sliceOp.hasUnitStride()) 807 return failure(); 808 // Only constant padding value supported. 809 Value padValue = padOp.getConstantPaddingValue(); 810 if (!padValue) 811 return failure(); 812 813 // Helper variables and functions for various arithmetic operations. These are 814 // used extensively for computing new offset/length and padding values. 815 Location loc = sliceOp.getLoc(); 816 AffineExpr dim0, dim1; 817 bindDims(rewriter.getContext(), dim0, dim1); 818 // Add two integers. 819 auto addMap = AffineMap::get(2, 0, {dim0 + dim1}); 820 auto add = [&](Value v1, Value v2) { 821 return rewriter.createOrFold<AffineApplyOp>(loc, addMap, 822 ValueRange{v1, v2}); 823 }; 824 // Subtract two integers. 825 auto subMap = AffineMap::get(2, 0, {dim0 - dim1}); 826 auto sub = [&](Value v1, Value v2) { 827 return rewriter.createOrFold<AffineApplyOp>(loc, subMap, 828 ValueRange{v1, v2}); 829 }; 830 // Take the minimum of two integers. 831 auto idMap = AffineMap::getMultiDimIdentityMap(2, rewriter.getContext()); 832 auto min = [&](Value v1, Value v2) { 833 return rewriter.createOrFold<AffineMinOp>(loc, idMap, ValueRange{v1, v2}); 834 }; 835 // Take the maximum of two integers. 836 auto max = [&](Value v1, Value v2) { 837 return rewriter.createOrFold<AffineMaxOp>(loc, idMap, ValueRange{v1, v2}); 838 }; 839 // Zero index-typed integer. 840 auto zero = rewriter.create<ConstantIndexOp>(loc, 0); 841 842 // Helper function for filling static/dynamic low/high padding indices vectors 843 // of PadTensorOp. 844 auto appendIndex = [&](Value val, SmallVector<Value> &dynIndices, 845 SmallVector<int64_t> &staticIndices) { 846 if (auto constInt = getConstantIntValue(val)) { 847 staticIndices.push_back(*constInt); 848 } else { 849 staticIndices.push_back(ShapedType::kDynamicSize); 850 dynIndices.push_back(val); 851 } 852 }; 853 854 // Compute new offsets, lengths, low padding, high padding. 855 SmallVector<OpFoldResult> newOffsets, newLengths, newStrides; 856 SmallVector<Value> newLows, newHighs; 857 SmallVector<int64_t> staticNewLows, staticNewHighs; 858 // Set to true if the original data source is not read at all. 859 bool hasZeroLen = false; 860 // Same as hasZeroLen, but for dynamic dimension sizes. This condition 861 // is true if the original data source turns out to be unused at runtime. 862 Value dynHasZeroLenCond; 863 864 int64_t rank = padOp.getSourceType().getRank(); 865 for (unsigned dim = 0; dim < rank; ++dim) { 866 auto low = asValue(rewriter, loc, padOp.getMixedLowPad()[dim]); 867 bool hasLowPad = getConstantIntValue(low) != static_cast<int64_t>(0); 868 auto high = asValue(rewriter, loc, padOp.getMixedHighPad()[dim]); 869 bool hasHighPad = getConstantIntValue(high) != static_cast<int64_t>(0); 870 auto offset = asValue(rewriter, loc, sliceOp.getMixedOffsets()[dim]); 871 auto length = asValue(rewriter, loc, sliceOp.getMixedSizes()[dim]); 872 auto srcSize = 873 rewriter.createOrFold<tensor::DimOp>(loc, padOp.source(), dim); 874 875 // The new amount of low padding is `low - offset`. Except for the case 876 // where none of the low padding is read. In that case, the new amount of 877 // low padding is zero. 878 // 879 // Optimization: If low = 0, then newLow = 0. 880 Value newLow = hasLowPad ? max(zero, sub(low, offset)) : zero; 881 appendIndex(newLow, newLows, staticNewLows); 882 883 // Start reading the data from position `offset - low`. Since the original 884 // read may have started in the low padding zone, this value could be 885 // negative. Therefore, start reading from: 886 // 887 // max(offset - low, 0) 888 // 889 // The original read could also have started in the high padding zone. 890 // In that case, set the offset to the end of source tensor. The new 891 // ExtractSliceOp length will be zero in that case. (Effectively reading no 892 // data from the source.) 893 // 894 // Optimization: If low = 0, then the formula can be simplified. 895 Value newOffset = hasLowPad ? min(max(sub(offset, low), zero), srcSize) 896 : min(offset, srcSize); 897 newOffsets.push_back(getAsOpFoldResult(newOffset)); 898 899 // The original ExtractSliceOp was reading until position `offset + length`. 900 // Therefore, the corresponding position within the source tensor is: 901 // 902 // offset + length - low 903 // 904 // In case the original ExtractSliceOp stopped reading within the low 905 // padding zone, this value can be negative. In that case, the end position 906 // of the read should be zero. (Similar to newOffset.) 907 // 908 // The original read could also have stopped in the high padding zone. 909 // In that case, set the end positition of the read should be the end of the 910 // source tensor. (Similar to newOffset.) 911 // 912 // endLoc = min(max(offset - low + length, 0), srcSize) 913 // 914 // The new ExtractSliceOp length is `endLoc - newOffset`. 915 // 916 // Optimization: If low = 0, then the formula can be simplified. 917 Value endLoc = hasLowPad 918 ? min(max(add(sub(offset, low), length), zero), srcSize) 919 : min(add(offset, length), srcSize); 920 Value newLength = sub(endLoc, newOffset); 921 newLengths.push_back(getAsOpFoldResult(newLength)); 922 923 // Check if newLength is zero. In that case, no SubTensorOp should be 924 // executed. 925 if (auto newLengthInt = getConstantIntValue(newLength)) { 926 hasZeroLen |= *newLengthInt == 0; 927 } else { 928 Value check = rewriter.create<CmpIOp>( 929 loc, CmpIPredicate::eq, newLength, zero); 930 dynHasZeroLenCond = 931 dynHasZeroLenCond 932 ? rewriter.create<OrOp>(loc, check, dynHasZeroLenCond) 933 : check; 934 } 935 936 // The amount of high padding is simply the number of elements remaining, 937 // so that the result has the same length as the original ExtractSliceOp. 938 // As an optimization, if the original high padding is zero, then the new 939 // high padding must also be zero. 940 Value newHigh = hasHighPad ? sub(sub(length, newLength), newLow) : zero; 941 appendIndex(newHigh, newHighs, staticNewHighs); 942 943 // Only unit stride supported. 944 newStrides.push_back(rewriter.getIndexAttr(1)); 945 } 946 947 // Insert cast to ensure that types match. (May be folded away.) 948 auto castResult = [&](Value val) -> Value { 949 auto castOp = rewriter.create<tensor::CastOp>(loc, sliceOp.getType(), val); 950 return castOp; 951 }; 952 953 // In cases where the original data source is unused: Emit a GenerateOp and 954 // do not generate a SliceOp. (The result shape of the SliceOp would 955 // have a dimension of size 0, the semantics of which is unclear.) 956 auto createGenerateOp = [&]() { 957 // The shape of the GenerateOp is the same as the existing SliceOp. 958 RankedTensorType type = sliceOp.getType(); 959 SmallVector<Value> dynDims; 960 for (unsigned i = 0; i < type.getRank(); ++i) { 961 if (type.isDynamicDim(i)) 962 dynDims.push_back(asValue(rewriter, loc, sliceOp.getMixedSizes()[i])); 963 } 964 965 // Create GenerateOp. 966 auto generateOp = rewriter.create<tensor::GenerateOp>(loc, type, dynDims); 967 968 // Copy region to new op. 969 BlockAndValueMapping bvm; 970 padOp.region().cloneInto(&generateOp.getRegion(), bvm); 971 // Rewrite linalg::YieldOp to tensor::YieldOp. 972 { 973 OpBuilder::InsertionGuard guard(rewriter); 974 auto yieldOp = dyn_cast<linalg::YieldOp>( 975 generateOp.getRegion().front().getTerminator()); 976 assert(yieldOp && "malformed PadTensorOp: expected YieldOp terminator"); 977 assert(yieldOp.values().size() == 1); 978 rewriter.setInsertionPoint(yieldOp); 979 rewriter.replaceOpWithNewOp<tensor::YieldOp>( 980 yieldOp, yieldOp.values()[0]); 981 } 982 983 return castResult(generateOp); 984 }; 985 986 // Emit a SliceOp and a PadTensorOp. Should not be used in cases where 987 // the result shape of the new SliceOp has a zero dimension. 988 auto createPadTensorOfSubTensor = [&]() { 989 // Create pad_tensor(subtensor(x)). 990 auto newSliceOp = rewriter.create<tensor::ExtractSliceOp>( 991 loc, padOp.source(), newOffsets, newLengths, newStrides); 992 auto newPadTensorOp = rewriter.create<PadTensorOp>( 993 loc, newSliceOp, staticNewLows, staticNewHighs, newLows, newHighs); 994 995 // Copy region to new PadTensorOp. 996 BlockAndValueMapping bvm; 997 padOp.region().cloneInto(&newPadTensorOp.getRegion(), bvm); 998 999 // Cast result and return. 1000 return castResult(newPadTensorOp); 1001 }; 1002 1003 // Rewrite subtensor(pad_tensor(x)) into a GenerateOp it is statically known 1004 // that the original data source x is not used. 1005 if (hasZeroLen) { 1006 rewriter.replaceOp(sliceOp, createGenerateOp()); 1007 return success(); 1008 } 1009 1010 // If there are dynamic dimensions: Generate an scf.if check to avoid creating 1011 // SliceOps with result dimensions of size 0 at runtime. 1012 if (dynHasZeroLenCond) { 1013 auto result = rewriter.create<scf::IfOp>( 1014 loc, sliceOp.getType(), dynHasZeroLenCond, 1015 /*thenBuilder=*/ 1016 [&](OpBuilder &b, Location loc) { 1017 b.create<scf::YieldOp>(loc, createGenerateOp()); 1018 }, 1019 /*elseBuilder=*/ 1020 [&](OpBuilder &b, Location loc) { 1021 b.create<scf::YieldOp>(loc, createPadTensorOfSubTensor()); 1022 }); 1023 rewriter.replaceOp(sliceOp, result.getResult(0)); 1024 return success(); 1025 } 1026 1027 // All shapes are static and the data source is actually used. Rewrite into 1028 // pad_tensor(subtensor(x)). 1029 rewriter.replaceOp(sliceOp, createPadTensorOfSubTensor()); 1030 return success(); 1031 } 1032