MLIR  14.0.0git
VectorInterfaceImpl.cpp
Go to the documentation of this file.
1 //===- VectorInterfaceImpl.cpp - Vector Impl. of BufferizableOpInterface --===//
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 
12 #include "mlir/IR/Dialect.h"
13 #include "mlir/IR/Operation.h"
14 
15 using namespace mlir::bufferization;
16 
17 namespace mlir {
18 namespace linalg {
19 namespace comprehensive_bufferize {
20 namespace vector_ext {
21 
22 /// Bufferization of vector.transfer_read. Replaced with a new
23 /// vector.transfer_read that operates on a memref.
25  : public BufferizableOpInterface::ExternalModel<TransferReadOpInterface,
26  vector::TransferReadOp> {
28  const BufferizationState &state) const {
29  assert(opOperand.get().getType().isa<RankedTensorType>() &&
30  "only tensor types expected");
31  return true;
32  }
33 
35  const BufferizationState &state) const {
36  assert(opOperand.get().getType().isa<RankedTensorType>() &&
37  "only tensor types expected");
38  return false;
39  }
40 
42  const BufferizationState &state) const {
43  return OpResult();
44  }
45 
47  const BufferizationState &state) const {
48  auto readOp = cast<vector::TransferReadOp>(op);
49  assert(readOp.getShapedType().isa<TensorType>() &&
50  "only tensor types expected");
51 
52  // TransferReadOp always reads from the bufferized op.source().
53  Value buffer =
54  *state.getBuffer(rewriter, readOp->getOpOperand(0) /*source*/);
55  replaceOpWithNewBufferizedOp<vector::TransferReadOp>(
56  rewriter, readOp, readOp.getVectorType(), buffer, readOp.indices(),
57  readOp.permutation_map(), readOp.padding(), readOp.mask(),
58  readOp.in_boundsAttr());
59  return success();
60  }
61 };
62 
63 /// Bufferization of vector.transfer_write. Replace with a new
64 /// vector.transfer_write that operates on a memref.
66  : public BufferizableOpInterface::ExternalModel<TransferWriteOpInterface,
67  vector::TransferWriteOp> {
69  const BufferizationState &state) const {
70  assert(opOperand.get().getType().isa<TensorType>() &&
71  "only tensor types expected");
72  return true;
73  }
74 
76  const BufferizationState &state) const {
77  assert(opOperand.get().getType().isa<TensorType>() &&
78  "only tensor types expected");
79  return true;
80  }
81 
83  const BufferizationState &state) const {
84  assert(opOperand.get().getType().isa<TensorType>() &&
85  "only tensor types expected");
86  return op->getOpResult(0);
87  }
88 
90  const BufferizationState &state) const {
92  }
93 
95  const BufferizationState &state) const {
96  auto writeOp = cast<vector::TransferWriteOp>(op);
97  assert(writeOp.getShapedType().isa<TensorType>() &&
98  "only tensor types expected");
99 
100  // Create a new transfer_write on buffer that doesn't have a return value.
101  // Leave the previous transfer_write to dead code as it still has uses at
102  // this point.
103  FailureOr<Value> resultBuffer =
104  state.getBuffer(rewriter, op->getOpOperand(1) /*source*/);
105  if (failed(resultBuffer))
106  return failure();
107  rewriter.create<vector::TransferWriteOp>(
108  writeOp.getLoc(), writeOp.vector(), *resultBuffer, writeOp.indices(),
109  writeOp.permutation_mapAttr(), writeOp.in_boundsAttr());
110  replaceOpWithBufferizedValues(rewriter, op, *resultBuffer);
111 
112  return success();
113  }
114 };
115 
116 } // namespace vector_ext
117 } // namespace comprehensive_bufferize
118 } // namespace linalg
119 } // namespace mlir
120 
123  registry.addOpInterface<vector::TransferReadOp,
125  registry.addOpInterface<vector::TransferWriteOp,
127 }
Include the generated interface declarations.
OpTy create(Location location, Args &&...args)
Create an operation of specific op type at the current insertion point.
Definition: Builders.h:430
LogicalResult bufferize(Operation *op, RewriterBase &rewriter, const BufferizationState &state) const
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:28
This is a value defined by a result of an operation.
Definition: Value.h:423
BufferRelation bufferRelation(Operation *op, OpResult opResult, const BufferizationState &state) const
void registerBufferizableOpInterfaceExternalModels(DialectRegistry &registry)
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value...
Definition: LogicalResult.h:72
void replaceOpWithBufferizedValues(RewriterBase &rewriter, Operation *op, ValueRange values)
Replace an op with replacement values.
bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand, const BufferizationState &state) const
bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand, const BufferizationState &state) const
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
void addOpInterface()
Add an external op interface model for an op that belongs to a dialect, both provided as template par...
Definition: Dialect.h:382
OpResult getAliasingOpResult(Operation *op, OpOperand &opOperand, const BufferizationState &state) const
This class provides support for representing a failure result, or a valid value of type T...
Definition: LogicalResult.h:77
OpResult getAliasingOpResult(Operation *op, OpOperand &opOperand, const BufferizationState &state) const
FailureOr< Value > getBuffer(RewriterBase &rewriter, OpOperand &opOperand, bool forceInPlace=false, Optional< Operation *> customCopyInsertionPoint=None) const
Return the buffer (memref) for a given OpOperand (tensor).
IRValueT get() const
Return the current value being used by this operand.
Definition: UseDefLists.h:133
Tensor types represent multi-dimensional arrays, and have two variants: RankedTensorType and Unranked...
Definition: BuiltinTypes.h:73
bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand, const BufferizationState &state) const
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:84
BufferizationState provides a variety of helper functions for dealing with tensor values and memref b...
Type getType() const
Return the type of this value.
Definition: Value.h:117
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
Definition: Dialect.h:282
OpOperand & getOpOperand(unsigned idx)
Definition: Operation.h:257
OpResult getOpResult(unsigned idx)
Definition: Operation.h:290
bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand, const BufferizationState &state) const
This class represents an operand of an operation.
Definition: Value.h:249
LogicalResult bufferize(Operation *op, RewriterBase &rewriter, const BufferizationState &state) const
BufferRelation
Specify fine-grain relationship between buffers to enable more analysis.
bool isa() const
Definition: Types.h:234
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Definition: PatternMatch.h:688