MLIR  19.0.0git
TensorCopyInsertion.cpp
Go to the documentation of this file.
1 //===- TensorCopyInsertion.cpp - Resolve Bufferization Conflicts w/ Copies ===//
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 
10 
18 
19 namespace mlir {
20 namespace bufferization {
21 #define GEN_PASS_DEF_TENSORCOPYINSERTION
22 #include "mlir/Dialect/Bufferization/Transforms/Passes.h.inc"
23 } // namespace bufferization
24 } // namespace mlir
25 
26 using namespace mlir;
27 using namespace mlir::bufferization;
28 
31  BufferizationStatistics *statistics) {
32  OneShotAnalysisState state(op, options);
33  // Run normal One-Shot Bufferize analysis or One-Shot Module Bufferize
34  // analysis depending on whether function boundary bufferization is enabled or
35  // not.
36  if (options.bufferizeFunctionBoundaries) {
37  if (failed(analyzeModuleOp(cast<ModuleOp>(op), state, statistics)))
38  return failure();
39  } else {
40  if (failed(analyzeOp(op, state, statistics)))
41  return failure();
42  }
43 
44  if (options.testAnalysisOnly)
45  return success();
46 
47  return insertTensorCopies(op, state);
48 }
49 
52  const AnalysisState &state) {
53  IRRewriter rewriter(op->getContext());
54 
55  WalkResult result = op->walk([&](Operation *op) {
56  auto bufferizableOp = state.getOptions().dynCastBufferizableOp(op);
57  if (!bufferizableOp)
58  return WalkResult::skip();
59 
60  // Find inplacability conflicts and resolve them. (Typically with explicit
61  // tensor copies in the form of AllocTensorOps.)
62  rewriter.setInsertionPoint(op);
63  if (failed(bufferizableOp.resolveConflicts(rewriter, state)))
64  return WalkResult::interrupt();
65 
66  return WalkResult::advance();
67  });
68 
69  return failure(result.wasInterrupted());
70 }
static llvm::ManagedStatic< PassManagerOptions > options
This class coordinates rewriting a piece of IR outside of a pattern rewrite, providing a way to keep ...
Definition: PatternMatch.h:731
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition: Builders.h:400
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
Definition: Operation.h:793
MLIRContext * getContext()
Return the context this operation is associated with.
Definition: Operation.h:216
A utility result that is used to signal how to proceed with an ongoing walk:
Definition: Visitors.h:34
static WalkResult skip()
Definition: Visitors.h:53
static WalkResult advance()
Definition: Visitors.h:52
bool wasInterrupted() const
Returns true if the walk was interrupted.
Definition: Visitors.h:56
static WalkResult interrupt()
Definition: Visitors.h:51
AnalysisState provides a variety of helper functions for dealing with tensor values.
State for analysis-enabled bufferization.
LogicalResult analyzeOp(Operation *op, OneShotAnalysisState &state, BufferizationStatistics *statistics=nullptr)
Analyze op and its nested ops.
LogicalResult insertTensorCopies(Operation *op, const OneShotBufferizationOptions &options, BufferizationStatistics *statistics=nullptr)
Resolve RaW and other conflicts by inserting bufferization.alloc_tensor ops.
LogicalResult analyzeModuleOp(ModuleOp moduleOp, OneShotAnalysisState &state, BufferizationStatistics *statistics=nullptr)
Analyze moduleOp and its nested ops.
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
Bufferization statistics for debugging.
Definition: Bufferize.h:34
Options for analysis-enabled bufferization.