MLIR  21.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  const BufferizationState &bufferizationState,
32  BufferizationStatistics *statistics) {
33  OneShotAnalysisState analysisState(op, options);
34  // Run normal One-Shot Bufferize analysis or One-Shot Module Bufferize
35  // analysis depending on whether function boundary bufferization is enabled or
36  // not.
37  if (options.bufferizeFunctionBoundaries) {
38  if (failed(analyzeModuleOp(cast<ModuleOp>(op), analysisState, statistics)))
39  return failure();
40  } else {
41  if (failed(analyzeOp(op, analysisState, statistics)))
42  return failure();
43  }
44 
45  if (options.testAnalysisOnly)
46  return success();
47 
48  return insertTensorCopies(op, analysisState, bufferizationState);
49 }
50 
52  Operation *op, const AnalysisState &analysisState,
53  const BufferizationState &bufferizationState) {
54  IRRewriter rewriter(op->getContext());
55 
56  // It may be more efficient to walk in pre-order here, but the current
57  // implementation visits regions of ops even if they are not allowed or
58  // bufferizable, and existing tests rely on this behavior.
59  // For now, only exclude nested operations if they are in a different symbol
60  // table scope.
61  WalkResult result = op->walk([&](Operation *nestedOp) {
62  if (op->hasTrait<OpTrait::SymbolTable>() &&
63  nestedOp->getParentWithTrait<OpTrait::SymbolTable>() != op)
64  return WalkResult::skip();
65 
66  auto bufferizableOp =
67  analysisState.getOptions().dynCastBufferizableOp(nestedOp);
68  if (!bufferizableOp)
69  return WalkResult::skip();
70 
71  // Find inplacability conflicts and resolve them. (Typically with explicit
72  // tensor copies in the form of AllocTensorOps.)
73  rewriter.setInsertionPoint(nestedOp);
74  if (failed(bufferizableOp.resolveConflicts(rewriter, analysisState,
75  bufferizationState)))
76  return WalkResult::interrupt();
77 
78  return WalkResult::advance();
79  });
80 
81  return failure(result.wasInterrupted());
82 }
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:730
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition: Builders.h:396
A trait used to provide symbol table functionalities to a region operation.
Definition: SymbolTable.h:451
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition: Operation.h:749
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:797
MLIRContext * getContext()
Return the context this operation is associated with.
Definition: Operation.h:216
Operation * getParentWithTrait()
Returns the closest surrounding parent operation with trait Trait.
Definition: Operation.h:248
A utility result that is used to signal how to proceed with an ongoing walk:
Definition: Visitors.h:33
static WalkResult skip()
Definition: Visitors.h:52
static WalkResult advance()
Definition: Visitors.h:51
bool wasInterrupted() const
Returns true if the walk was interrupted.
Definition: Visitors.h:55
static WalkResult interrupt()
Definition: Visitors.h:50
AnalysisState provides a variety of helper functions for dealing with tensor values.
const BufferizationOptions & getOptions() const
Return a reference to the BufferizationOptions.
BufferizationState provides information about the state of the IR during the bufferization process.
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, const BufferizationState &bufferizationState, BufferizationStatistics *statistics=nullptr)
Resolve RaW and other conflicts by inserting bufferization.alloc_tensor ops.
llvm::LogicalResult analyzeModuleOp(ModuleOp moduleOp, OneShotAnalysisState &state, BufferizationStatistics *statistics=nullptr)
Analyze moduleOp and its nested ops.
Include the generated interface declarations.
BufferizableOpInterface dynCastBufferizableOp(Operation *op) const
Try to cast the given op to BufferizableOpInterface if the op is allow listed.
Bufferization statistics for debugging.
Definition: Bufferize.h:35
Options for analysis-enabled bufferization.