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  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 
50 LogicalResult
52  const AnalysisState &state) {
53  IRRewriter rewriter(op->getContext());
54 
55  // It may be more efficient to walk in pre-order here, but the current
56  // implementation visits regions of ops even if they are not allowed or
57  // bufferizable, and existing tests rely on this behavior.
58  // For now, only exclude nested operations if they are in a different symbol
59  // table scope.
60  WalkResult result = op->walk([&](Operation *nestedOp) {
61  if (op->hasTrait<OpTrait::SymbolTable>() &&
62  nestedOp->getParentWithTrait<OpTrait::SymbolTable>() != op)
63  return WalkResult::skip();
64 
65  auto bufferizableOp = state.getOptions().dynCastBufferizableOp(nestedOp);
66  if (!bufferizableOp)
67  return WalkResult::skip();
68 
69  // Find inplacability conflicts and resolve them. (Typically with explicit
70  // tensor copies in the form of AllocTensorOps.)
71  rewriter.setInsertionPoint(nestedOp);
72  if (failed(bufferizableOp.resolveConflicts(rewriter, state)))
73  return WalkResult::interrupt();
74 
75  return WalkResult::advance();
76  });
77 
78  return failure(result.wasInterrupted());
79 }
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:442
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:750
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:798
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.
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.
llvm::LogicalResult analyzeModuleOp(ModuleOp moduleOp, OneShotAnalysisState &state, BufferizationStatistics *statistics=nullptr)
Analyze moduleOp and its nested ops.
Include the generated interface declarations.
Bufferization statistics for debugging.
Definition: Bufferize.h:34
Options for analysis-enabled bufferization.