1 //===- Bufferize.cpp - Bufferization utilities ----------------------------===// 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 #include "PassDetail.h" 10 11 #include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h" 12 #include "mlir/Dialect/Bufferization/IR/Bufferization.h" 13 #include "mlir/Dialect/Bufferization/Transforms/Bufferize.h" 14 #include "mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h" 15 #include "mlir/Dialect/Bufferization/Transforms/OneShotModuleBufferize.h" 16 #include "mlir/Dialect/Bufferization/Transforms/Passes.h" 17 #include "mlir/Dialect/Func/IR/FuncOps.h" 18 #include "mlir/Dialect/MemRef/IR/MemRef.h" 19 #include "mlir/IR/Operation.h" 20 #include "mlir/Pass/PassManager.h" 21 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" 22 #include "mlir/Transforms/Passes.h" 23 24 using namespace mlir; 25 using namespace mlir::bufferization; 26 27 //===----------------------------------------------------------------------===// 28 // BufferizeTypeConverter 29 //===----------------------------------------------------------------------===// 30 31 static Value materializeToTensor(OpBuilder &builder, TensorType type, 32 ValueRange inputs, Location loc) { 33 assert(inputs.size() == 1); 34 assert(inputs[0].getType().isa<BaseMemRefType>()); 35 return builder.create<bufferization::ToTensorOp>(loc, type, inputs[0]); 36 } 37 38 /// Registers conversions into BufferizeTypeConverter 39 BufferizeTypeConverter::BufferizeTypeConverter() { 40 // Keep all types unchanged. 41 addConversion([](Type type) { return type; }); 42 // Convert RankedTensorType to MemRefType. 43 addConversion([](RankedTensorType type) -> Type { 44 return MemRefType::get(type.getShape(), type.getElementType()); 45 }); 46 // Convert UnrankedTensorType to UnrankedMemRefType. 47 addConversion([](UnrankedTensorType type) -> Type { 48 return UnrankedMemRefType::get(type.getElementType(), 0); 49 }); 50 addArgumentMaterialization(materializeToTensor); 51 addSourceMaterialization(materializeToTensor); 52 addTargetMaterialization([](OpBuilder &builder, BaseMemRefType type, 53 ValueRange inputs, Location loc) -> Value { 54 assert(inputs.size() == 1 && "expected exactly one input"); 55 56 if (auto inputType = inputs[0].getType().dyn_cast<MemRefType>()) { 57 // MemRef to MemRef cast. 58 assert(inputType != type && "expected different types"); 59 // Unranked to ranked and ranked to unranked casts must be explicit. 60 auto rankedDestType = type.dyn_cast<MemRefType>(); 61 if (!rankedDestType) 62 return nullptr; 63 FailureOr<Value> replacement = 64 castOrReallocMemRefValue(builder, inputs[0], rankedDestType); 65 if (failed(replacement)) 66 return nullptr; 67 return *replacement; 68 } 69 70 if (inputs[0].getType().isa<TensorType>()) { 71 // Tensor to MemRef cast. 72 return builder.create<bufferization::ToMemrefOp>(loc, type, inputs[0]); 73 } 74 75 llvm_unreachable("only tensor/memref input types supported"); 76 }); 77 } 78 79 void mlir::bufferization::populateBufferizeMaterializationLegality( 80 ConversionTarget &target) { 81 target.addLegalOp<bufferization::ToTensorOp, bufferization::ToMemrefOp>(); 82 } 83 84 namespace { 85 // In a finalizing bufferize conversion, we know that all tensors have been 86 // converted to memrefs, thus, this op becomes an identity. 87 class BufferizeToTensorOp 88 : public OpConversionPattern<bufferization::ToTensorOp> { 89 public: 90 using OpConversionPattern::OpConversionPattern; 91 LogicalResult 92 matchAndRewrite(bufferization::ToTensorOp op, OpAdaptor adaptor, 93 ConversionPatternRewriter &rewriter) const override { 94 rewriter.replaceOp(op, adaptor.memref()); 95 return success(); 96 } 97 }; 98 } // namespace 99 100 namespace { 101 // In a finalizing bufferize conversion, we know that all tensors have been 102 // converted to memrefs, thus, this op becomes an identity. 103 class BufferizeToMemrefOp 104 : public OpConversionPattern<bufferization::ToMemrefOp> { 105 public: 106 using OpConversionPattern::OpConversionPattern; 107 LogicalResult 108 matchAndRewrite(bufferization::ToMemrefOp op, OpAdaptor adaptor, 109 ConversionPatternRewriter &rewriter) const override { 110 rewriter.replaceOp(op, adaptor.tensor()); 111 return success(); 112 } 113 }; 114 } // namespace 115 116 void mlir::bufferization::populateEliminateBufferizeMaterializationsPatterns( 117 BufferizeTypeConverter &typeConverter, RewritePatternSet &patterns) { 118 patterns.add<BufferizeToTensorOp, BufferizeToMemrefOp>(typeConverter, 119 patterns.getContext()); 120 } 121 122 namespace { 123 struct FinalizingBufferizePass 124 : public FinalizingBufferizeBase<FinalizingBufferizePass> { 125 using FinalizingBufferizeBase< 126 FinalizingBufferizePass>::FinalizingBufferizeBase; 127 128 void runOnOperation() override { 129 auto func = getOperation(); 130 auto *context = &getContext(); 131 132 BufferizeTypeConverter typeConverter; 133 RewritePatternSet patterns(context); 134 ConversionTarget target(*context); 135 136 populateEliminateBufferizeMaterializationsPatterns(typeConverter, patterns); 137 138 // If all result types are legal, and all block arguments are legal (ensured 139 // by func conversion above), then all types in the program are legal. 140 // 141 // We also check that the operand types are legal to avoid creating invalid 142 // IR. For example, this prevents 143 // populateEliminateBufferizeMaterializationsPatterns from updating the 144 // types of the operands to a return op without updating the enclosing 145 // function. 146 target.markUnknownOpDynamicallyLegal( 147 [&](Operation *op) { return typeConverter.isLegal(op); }); 148 149 if (failed(applyFullConversion(func, target, std::move(patterns)))) 150 signalPassFailure(); 151 } 152 }; 153 154 static BufferizationOptions::LayoutMapOption 155 parseLayoutMapOption(const std::string &s) { 156 if (s == "fully-dynamic-layout-map") 157 return BufferizationOptions::LayoutMapOption::FullyDynamicLayoutMap; 158 if (s == "identity-layout-map") 159 return BufferizationOptions::LayoutMapOption::IdentityLayoutMap; 160 if (s == "infer-layout-map") 161 return BufferizationOptions::LayoutMapOption::InferLayoutMap; 162 llvm_unreachable("invalid layout map option"); 163 } 164 165 struct OneShotBufferizePass 166 : public OneShotBufferizeBase<OneShotBufferizePass> { 167 OneShotBufferizePass() : OneShotBufferizeBase<OneShotBufferizePass>() {} 168 169 explicit OneShotBufferizePass(const OneShotBufferizationOptions &options) 170 : options(options) {} 171 172 void getDependentDialects(DialectRegistry ®istry) const override { 173 registry 174 .insert<bufferization::BufferizationDialect, memref::MemRefDialect>(); 175 registerAllocationOpInterfaceExternalModels(registry); 176 } 177 178 void runOnOperation() override { 179 OneShotBufferizationOptions opt; 180 if (!options) { 181 // Make new bufferization options if none were provided when creating the 182 // pass. 183 opt.dropEquivalentFuncResults = dropEquivalentFuncResults; 184 opt.allowReturnAllocs = allowReturnAllocs; 185 opt.allowUnknownOps = allowUnknownOps; 186 opt.alwaysAliasingWithDest = alwaysAliasingWithDest; 187 opt.analysisFuzzerSeed = analysisFuzzerSeed; 188 opt.createDeallocs = createDeallocs; 189 opt.functionBoundaryTypeConversion = 190 parseLayoutMapOption(functionBoundaryTypeConversion); 191 opt.printConflicts = printConflicts; 192 opt.testAnalysisOnly = testAnalysisOnly; 193 opt.bufferizeFunctionBoundaries = bufferizeFunctionBoundaries; 194 opt.promoteBufferResultsToOutParams = promoteBufferResultsToOutParams; 195 opt.unknownTypeConversion = parseLayoutMapOption(unknownTypeConversion); 196 197 BufferizationOptions::OpFilterEntry::FilterFn filterFn = 198 [&](Operation *op) { 199 // Filter may be specified via options. 200 if (this->dialectFilter.hasValue()) 201 return llvm::find(this->dialectFilter, 202 op->getDialect()->getNamespace()) != 203 this->dialectFilter.end(); 204 // No filter specified: All other ops are allowed. 205 return true; 206 }; 207 opt.allowOperationInFilter(filterFn); 208 } else { 209 opt = *options; 210 } 211 212 ModuleOp moduleOp = getOperation(); 213 if (opt.bufferizeFunctionBoundaries) { 214 if (failed(runOneShotModuleBufferize(moduleOp, opt))) { 215 signalPassFailure(); 216 return; 217 } 218 } else { 219 if (failed(runOneShotBufferize(moduleOp, opt))) { 220 signalPassFailure(); 221 return; 222 } 223 } 224 225 if (opt.testAnalysisOnly) 226 return; 227 228 OpPassManager cleanupPipeline("builtin.module"); 229 cleanupPipeline.addPass(createCanonicalizerPass()); 230 cleanupPipeline.addPass(createCSEPass()); 231 cleanupPipeline.addPass(createLoopInvariantCodeMotionPass()); 232 (void)runPipeline(cleanupPipeline, moduleOp); 233 } 234 235 private: 236 llvm::Optional<OneShotBufferizationOptions> options; 237 }; 238 } // namespace 239 240 namespace { 241 struct BufferizationBufferizePass 242 : public BufferizationBufferizeBase<BufferizationBufferizePass> { 243 void runOnOperation() override { 244 BufferizationOptions options = getPartialBufferizationOptions(); 245 options.allowDialectInFilter<BufferizationDialect>(); 246 247 if (failed(bufferizeOp(getOperation(), options))) 248 signalPassFailure(); 249 } 250 251 void getDependentDialects(DialectRegistry ®istry) const override { 252 registry 253 .insert<bufferization::BufferizationDialect, memref::MemRefDialect>(); 254 } 255 }; 256 } // namespace 257 258 std::unique_ptr<Pass> mlir::bufferization::createBufferizationBufferizePass() { 259 return std::make_unique<BufferizationBufferizePass>(); 260 } 261 262 std::unique_ptr<Pass> mlir::bufferization::createOneShotBufferizePass() { 263 return std::make_unique<OneShotBufferizePass>(); 264 } 265 266 std::unique_ptr<Pass> mlir::bufferization::createOneShotBufferizePass( 267 const OneShotBufferizationOptions &options) { 268 return std::make_unique<OneShotBufferizePass>(options); 269 } 270 271 std::unique_ptr<OperationPass<func::FuncOp>> 272 mlir::bufferization::createFinalizingBufferizePass() { 273 return std::make_unique<FinalizingBufferizePass>(); 274 } 275 276 //===----------------------------------------------------------------------===// 277 // BufferizableOpInterface-based Bufferization 278 //===----------------------------------------------------------------------===// 279 280 static bool isaTensor(Type t) { return t.isa<TensorType>(); } 281 282 /// Return true if the given op has a tensor result or a tensor operand. 283 static bool hasTensorSemantics(Operation *op) { 284 if (auto funcOp = dyn_cast<FunctionOpInterface>(op)) { 285 bool hasTensorArg = any_of(funcOp.getArgumentTypes(), isaTensor); 286 bool hasTensorResult = any_of(funcOp.getResultTypes(), isaTensor); 287 return hasTensorArg || hasTensorResult; 288 } 289 290 bool hasTensorResult = any_of(op->getResultTypes(), isaTensor); 291 bool hasTensorOperand = any_of(op->getOperandTypes(), isaTensor); 292 return hasTensorResult || hasTensorOperand; 293 } 294 295 LogicalResult 296 bufferization::finalizeBuffers(Operation *op, 297 const BufferizationOptions &options) { 298 // Create allocation ops for "leaking buffers", i.e., buffer allocations that 299 // escape block boundaries. If there are no leaking allocs, `hasLeakingAllocs` 300 // is set to `false`. 301 bool hasLeakingAllocs = false; 302 if (failed(createAllocDeallocOps(op, options, /*onlyLeakingAllocs=*/true, 303 &hasLeakingAllocs))) 304 return failure(); 305 306 // Promote returned buffers to "out" parameters. 307 // TODO: Pass options to support custom dealloc ops. 308 if (options.promoteBufferResultsToOutParams && isa<ModuleOp>(op) && 309 failed(promoteBufferResultsToOutParams(cast<ModuleOp>(op)))) 310 return failure(); 311 312 // Create deallocation ops for all "leaking buffers" and all buffer 313 // allocations that were added during the above promotion process. 314 // TODO: Pass options to support custom dealloc ops. 315 if (hasLeakingAllocs && options.createDeallocs && 316 failed(deallocateBuffers(op))) 317 return failure(); 318 319 // Deallocate all remaining buffers at the end of their parent blocks. 320 if (failed(createAllocDeallocOps(op, options))) 321 return failure(); 322 323 return success(); 324 } 325 326 LogicalResult bufferization::bufferizeOp(Operation *op, 327 const AnalysisState &analysisState) { 328 // Catch incorrect API usage. 329 assert((analysisState.hasDialectState( 330 func::FuncDialect::getDialectNamespace()) || 331 !analysisState.getOptions().bufferizeFunctionBoundaries) && 332 "must use ModuleBufferize to bufferize function boundaries"); 333 334 BufferizationState bufferizationState(analysisState); 335 if (failed(bufferizeOp(op, bufferizationState))) 336 return failure(); 337 if (failed(finalizeBuffers(op, analysisState.getOptions()))) 338 return failure(); 339 return success(); 340 } 341 342 namespace { 343 /// A rewriter that keeps track of extra information during bufferization. 344 class BufferizationRewriter : public IRRewriter { 345 public: 346 BufferizationRewriter(MLIRContext *ctx, DenseSet<Operation *> &erasedOps, 347 DenseSet<Operation *> &toMemrefOps, 348 const BufferizationOptions &options) 349 : IRRewriter(ctx), erasedOps(erasedOps), toMemrefOps(toMemrefOps), 350 options(options) {} 351 352 protected: 353 void notifyOperationRemoved(Operation *op) override { 354 IRRewriter::notifyOperationRemoved(op); 355 erasedOps.insert(op); 356 // Erase if present. 357 toMemrefOps.erase(op); 358 } 359 360 void notifyOperationInserted(Operation *op) override { 361 IRRewriter::notifyOperationInserted(op); 362 363 // Keep track of to_memref ops. 364 if (isa<ToMemrefOp>(op)) { 365 toMemrefOps.insert(op); 366 return; 367 } 368 369 // Skip to_tensor ops. 370 if (isa<ToTensorOp>(op)) 371 return; 372 373 // Adding new bufferizable ops is not allowed during bufferization. Such ops 374 // would not be analyzed and can lead to surprising behavior. 375 assert((!hasTensorSemantics(op) || !options.isOpAllowed(op)) && 376 "creating new tensor ops is not allowed during bufferization"); 377 } 378 379 private: 380 /// A set of all erased ops. 381 DenseSet<Operation *> &erasedOps; 382 383 /// A set of all to_memref ops. 384 DenseSet<Operation *> &toMemrefOps; 385 386 /// The bufferization options. 387 /// Used for debug modes. 388 LLVM_ATTRIBUTE_UNUSED 389 const BufferizationOptions &options; 390 }; 391 } // namespace 392 393 LogicalResult 394 bufferization::bufferizeOp(Operation *op, 395 BufferizationState &bufferizationState) { 396 const auto &options = bufferizationState.getOptions(); 397 assert(options.unknownTypeConversion != 398 BufferizationOptions::LayoutMapOption::InferLayoutMap && 399 "invalid layout map option"); 400 401 // Keep track of to_memref ops. 402 DenseSet<Operation *> toMemrefOps; 403 op->walk([&](ToMemrefOp toMemrefOp) { toMemrefOps.insert(toMemrefOp); }); 404 405 // Gather all bufferizable ops in top-to-bottom order. 406 // 407 // We should ideally know the exact memref type of all operands when 408 // bufferizing an op. (This is the case when bufferizing top-to-bottom.) 409 // Otherwise, we have to use a memref type with a fully dynamic layout map to 410 // avoid copies. We are currently missing patterns for layout maps to 411 // canonicalize away (or canonicalize to more precise layouts). 412 SmallVector<Operation *> worklist; 413 op->walk<WalkOrder::PreOrder>([&](Operation *op) { 414 if (hasTensorSemantics(op)) 415 worklist.push_back(op); 416 }); 417 418 // Keep track of all erased ops. 419 DenseSet<Operation *> erasedOps; 420 421 // Bufferize all ops. 422 BufferizationRewriter rewriter(op->getContext(), erasedOps, toMemrefOps, 423 bufferizationState.getOptions()); 424 for (unsigned i = 0; i < worklist.size(); ++i) { 425 Operation *op = worklist[i]; 426 // Skip ops that were erased. 427 if (erasedOps.contains(op)) 428 continue; 429 // Skip ops that are not bufferizable or not allowed. 430 auto bufferizableOp = options.dynCastBufferizableOp(op); 431 if (!bufferizableOp) 432 continue; 433 // Skip ops that no longer have tensor semantics. 434 if (!hasTensorSemantics(op)) 435 continue; 436 // Bufferize the op. 437 rewriter.setInsertionPoint(op); 438 if (failed(bufferizableOp.bufferize(rewriter, bufferizationState))) 439 return op->emitError("failed to bufferize op"); 440 } 441 442 // Fold all to_memref(to_tensor(x)) pairs. 443 for (Operation *op : toMemrefOps) { 444 rewriter.setInsertionPoint(op); 445 (void)bufferization::foldToMemrefToTensorPair(rewriter, 446 cast<ToMemrefOp>(op)); 447 } 448 449 /// Check the result of bufferization. Return an error if an op was not 450 /// bufferized, unless partial bufferization is allowed. 451 if (bufferizationState.getOptions().allowUnknownOps) 452 return success(); 453 454 for (Operation *op : worklist) { 455 // Skip ops that are entirely gone. 456 if (erasedOps.contains(op)) 457 continue; 458 // Ops that no longer have tensor semantics (because they were updated 459 // in-place) are allowed. 460 if (!hasTensorSemantics(op)) 461 continue; 462 // Continue ops that are not allowed. 463 if (!options.isOpAllowed(op)) 464 continue; 465 // Ops without any uses and no side effects will fold away. 466 if (op->getUses().empty() && MemoryEffectOpInterface::hasNoEffect(op)) 467 continue; 468 return op->emitError("op was not bufferized"); 469 } 470 471 return success(); 472 } 473 474 namespace { 475 /// This a "no analysis, always copy" AnalysisState. In the absence of an 476 /// analysis, a buffer must be copied each time it is written to. Therefore, all 477 /// OpOperands that bufferize to a memory write must bufferize out-of-place. 478 class AlwaysCopyAnalysisState : public AnalysisState { 479 public: 480 AlwaysCopyAnalysisState(const BufferizationOptions &options) 481 : AnalysisState(options) { 482 // Note: Allocations must be deallocated with a subsequent run of the buffer 483 // deallocation pass. 484 assert(!options.createDeallocs && 485 "cannot create deallocs with AlwaysCopyBufferizationState"); 486 } 487 488 AlwaysCopyAnalysisState(const AlwaysCopyAnalysisState &) = delete; 489 490 virtual ~AlwaysCopyAnalysisState() = default; 491 492 /// Return `true` if the given OpResult has been decided to bufferize inplace. 493 bool isInPlace(OpOperand &opOperand) const override { 494 // OpOperands that bufferize to a memory write are out-of-place, i.e., an 495 // alloc and copy is inserted. 496 return !bufferizesToMemoryWrite(opOperand); 497 } 498 499 /// Return true if `v1` and `v2` bufferize to equivalent buffers. 500 bool areEquivalentBufferizedValues(Value v1, Value v2) const override { 501 // There is no analysis, so we do not know if the values are equivalent. The 502 // conservative answer is "false". 503 return false; 504 } 505 506 /// Return `true` if the given tensor has undefined contents. 507 bool hasUndefinedContents(OpOperand *opOperand) const override { 508 // There is no analysis, so the conservative answer is "false". 509 return false; 510 } 511 512 /// Return true if the given tensor (or an aliasing tensor) is yielded from 513 /// the containing block. Also include all aliasing tensors in the same block. 514 bool isTensorYielded(Value tensor) const override { 515 // There is no analysis, so conservatively answer "true". 516 return true; 517 } 518 }; 519 } // namespace 520 521 LogicalResult bufferization::bufferizeOp(Operation *op, 522 const BufferizationOptions &options) { 523 AlwaysCopyAnalysisState state(options); 524 return bufferizeOp(op, state); 525 } 526 527 BufferizationOptions bufferization::getPartialBufferizationOptions() { 528 BufferizationOptions options; 529 options.allowUnknownOps = true; 530 options.createDeallocs = false; 531 options.unknownTypeConversion = 532 BufferizationOptions::LayoutMapOption::IdentityLayoutMap; 533 return options; 534 } 535