MLIR 24.0.0git
SubsetOpInterfaceImpl.cpp
Go to the documentation of this file.
1//===- SubsetOpInterfaceImpl.cpp - Tensor subsets -------------------------===//
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
14
15using namespace mlir;
16using namespace mlir::tensor;
17
18namespace {
19struct ExtractSliceOpSubsetOpInterface
20 : public SubsetOpInterface::ExternalModel<ExtractSliceOpSubsetOpInterface,
21 tensor::ExtractSliceOp> {
22 FailureOr<HyperrectangularSlice>
23 getAccessedHyperrectangularSlice(Operation *op) const {
24 return HyperrectangularSlice(cast<OffsetSizeAndStrideOpInterface>(op));
25 }
26};
27
28struct ExtractSliceOpSubsetExtractionOpInterface
29 : public SubsetExtractionOpInterface::ExternalModel<
30 ExtractSliceOpSubsetExtractionOpInterface, tensor::ExtractSliceOp> {
31 OpOperand &getSourceOperand(Operation *op) const {
32 return cast<tensor::ExtractSliceOp>(op).getSourceMutable();
33 }
34};
35
36template <typename OpTy>
37struct InsertSliceLikeOpSubsetOpInterface
38 : public SubsetOpInterface::ExternalModel<
39 InsertSliceLikeOpSubsetOpInterface<OpTy>, OpTy> {
40 FailureOr<HyperrectangularSlice>
41 getAccessedHyperrectangularSlice(Operation *op) const {
42 return HyperrectangularSlice(cast<OffsetSizeAndStrideOpInterface>(op));
43 }
44};
45
46template <typename OpTy>
47struct InsertSliceLikeOpSubsetInsertionOpInterface
48 : public SubsetInsertionOpInterface::ExternalModel<
49 InsertSliceLikeOpSubsetInsertionOpInterface<OpTy>, OpTy> {
50 OpOperand &getSourceOperand(Operation *op) const {
51 return cast<OpTy>(op).getSourceMutable();
52 }
53
54 OpOperand &getDestinationOperand(Operation *op) const {
55 return cast<OpTy>(op).getDestMutable();
56 }
57
58 Value buildSubsetExtraction(Operation *op, OpBuilder &builder,
59 Location loc) const {
60 auto insertSliceOp = cast<OpTy>(op);
61 auto extractOp = tensor::ExtractSliceOp::create(
62 builder, loc, insertSliceOp.getSourceType(), insertSliceOp.getDest(),
63 insertSliceOp.getMixedOffsets(), insertSliceOp.getMixedSizes(),
64 insertSliceOp.getMixedStrides());
65 return extractOp.getResult();
66 }
67
68 SmallVector<Value>
69 getValuesNeededToBuildSubsetExtraction(Operation *op) const {
70 auto insertSliceOp = cast<OpTy>(op);
71 SmallVector<Value> neededValues;
72 // Collect all values that are needed to construct the replacement op.
73 neededValues.append(insertSliceOp.getOffsets().begin(),
74 insertSliceOp.getOffsets().end());
75 neededValues.append(insertSliceOp.getSizes().begin(),
76 insertSliceOp.getSizes().end());
77 neededValues.append(insertSliceOp.getStrides().begin(),
78 insertSliceOp.getStrides().end());
79 neededValues.push_back(insertSliceOp.getDest());
80 return neededValues;
81 }
82};
83} // namespace
84
86 DialectRegistry &registry) {
87 registry.addExtension(+[](MLIRContext *ctx, tensor::TensorDialect *dialect) {
88 // Note: `SubsetExtractionOpInterface` and `SubsetInsertionOpInterface`
89 // require `SubsetOpInterface`.
90 ExtractSliceOp::attachInterface<ExtractSliceOpSubsetOpInterface>(*ctx);
91 ExtractSliceOp::attachInterface<ExtractSliceOpSubsetExtractionOpInterface>(
92 *ctx);
93 InsertSliceOp::attachInterface<
94 InsertSliceLikeOpSubsetOpInterface<InsertSliceOp>>(*ctx);
95 InsertSliceOp::attachInterface<
96 InsertSliceLikeOpSubsetInsertionOpInterface<InsertSliceOp>>(*ctx);
97 ParallelInsertSliceOp::attachInterface<
98 InsertSliceLikeOpSubsetOpInterface<ParallelInsertSliceOp>>(*ctx);
99 ParallelInsertSliceOp::attachInterface<
100 InsertSliceLikeOpSubsetInsertionOpInterface<ParallelInsertSliceOp>>(
101 *ctx);
102 });
103}
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
Value buildSubsetExtraction(RewriterBase &rewriter, SubsetInsertionOpInterface op, tensor::EmptyOp emptyTensorOp, Operation *user)
This method builds and returns a subset extraction value for the destination tensor that the given op...
void registerSubsetOpInterfaceExternalModels(DialectRegistry &registry)
Include the generated interface declarations.