MLIR  19.0.0git
SubsetOpInterface.cpp
Go to the documentation of this file.
1 //===- SubsetOpInterface.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 
12 
13 #include "mlir/Interfaces/SubsetOpInterface.cpp.inc"
14 
15 using namespace mlir;
16 
18  auto dstOp = dyn_cast<DestinationStyleOpInterface>(op);
19  assert(dstOp && "getDestination must be implemented for non-DPS ops");
20  assert(
21  dstOp.getNumDpsInits() == 1 &&
22  "getDestination must be implemented for ops with 0 or more than 1 init");
23  return *dstOp.getDpsInitOperand(0);
24 }
25 
27  auto dstOp = dyn_cast<DestinationStyleOpInterface>(op);
28  assert(dstOp && "getUpdatedDestination must be implemented for non-DPS ops");
29  auto insertionOp = cast<SubsetInsertionOpInterface>(op);
30  return dstOp.getTiedOpResult(&insertionOp.getDestinationOperand());
31 }
32 
34  Operation *op, Value candidate,
35  function_ref<bool(Value, Value)> equivalenceFn) {
36  assert(isa<SubsetInsertionOpInterface>(op) &&
37  "expected SubsetInsertionOpInterface");
38  if (!candidate.getDefiningOp<SubsetExtractionOpInterface>())
39  return false;
40  return cast<SubsetOpInterface>(op).operatesOnEquivalentSubset(
41  candidate.getDefiningOp<SubsetOpInterface>(), equivalenceFn);
42 }
43 
45  Operation *op, SubsetOpInterface candidate,
46  function_ref<bool(Value, Value)> equivalenceFn) {
47  auto subsetOp = cast<SubsetOpInterface>(op);
49  subsetOp.getAccessedHyperrectangularSlice();
50  assert(succeeded(slice) &&
51  "operatesOnEquivalentSubset must be implemented if "
52  "getAccessedHyperrectangularSlice is not implemented");
54  candidate.getAccessedHyperrectangularSlice();
55  if (failed(otherSlice))
56  return false;
57  if (!equivalenceFn(subsetOp.getTensorContainer(),
58  candidate.getTensorContainer()))
59  return false;
61  op->getContext(), *slice, *otherSlice);
62  return succeeded(equivalent) && *equivalent;
63 }
64 
66  Operation *op, SubsetOpInterface candidate,
67  function_ref<bool(Value, Value)> equivalenceFn) {
68  auto subsetOp = cast<SubsetOpInterface>(op);
70  subsetOp.getAccessedHyperrectangularSlice();
71  assert(succeeded(slice) &&
72  "defaultOperatesOnDisjointSubset must be implemented if "
73  "getAccessedHyperrectangularSlice is not implemented");
75  candidate.getAccessedHyperrectangularSlice();
76  if (failed(otherSlice))
77  return false;
78  if (!equivalenceFn(subsetOp.getTensorContainer(),
79  candidate.getTensorContainer()))
80  return false;
82  op->getContext(), *slice, *otherSlice);
83  return succeeded(overlapping) && !*overlapping;
84 }
85 
87  if (auto insertionOp = dyn_cast<::mlir::SubsetInsertionOpInterface>(op))
88  return insertionOp.getDestinationOperand().get();
89  return cast<::mlir::SubsetExtractionOpInterface>(op).getSourceOperand().get();
90 }
91 
93  if (!(isa<SubsetExtractionOpInterface>(op.getOperation()) ^
94  isa<SubsetInsertionOpInterface>(op.getOperation())))
95  return op->emitOpError(
96  "SubsetOpInterface ops must implement either "
97  "SubsetExtractionOpInterface or SubsetInsertionOpInterface");
98  return success();
99 }
100 
102 detail::verifySubsetExtractionOpInterface(SubsetExtractionOpInterface op) {
103  if (op->getNumResults() != 1)
104  return op->emitOpError(
105  "SubsetExtractionOpInterface ops must have one result");
106  return success();
107 }
This class provides support for representing a failure result, or a valid value of type T.
Definition: LogicalResult.h:78
This class represents an operand of an operation.
Definition: Value.h:267
This is a value defined by a result of an operation.
Definition: Value.h:457
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
MLIRContext * getContext()
Return the context this operation is associated with.
Definition: Operation.h:216
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
Definition: Operation.cpp:671
unsigned getNumResults()
Return the number of results held by this operation.
Definition: Operation.h:399
static FailureOr< bool > areEquivalentSlices(MLIRContext *ctx, HyperrectangularSlice slice1, HyperrectangularSlice slice2)
Return "true" if the given slices are guaranteed to be equivalent.
static FailureOr< bool > areOverlappingSlices(MLIRContext *ctx, HyperrectangularSlice slice1, HyperrectangularSlice slice2)
Return "true" if the given slices are guaranteed to be overlapping.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition: Value.cpp:20
LogicalResult verifySubsetExtractionOpInterface(SubsetExtractionOpInterface op)
Verify SubsetExtractionOpInterface.
bool defaultOperatesOnDisjointSubset(Operation *op, SubsetOpInterface candidate, function_ref< bool(Value, Value)> equivalenceFn)
Default implementation of SubsetOpInterface::operatesOnDisjointSubset.
bool defaultOperatesOnEquivalentSubset(Operation *op, SubsetOpInterface candidate, function_ref< bool(Value, Value)> equivalenceFn)
Default implementation of SubsetOpInterface::operatesOnEquivalentSubset.
Value getTensorContainer(Operation *op)
Return the container that the given subset op is operating on.
OpResult defaultGetUpdatedDestination(Operation *op)
Return the updated destination result of the op if it implements the DestinationStyleOpInterface.
LogicalResult verifySubsetOpInterface(SubsetOpInterface op)
Verify SubsetOpInterface.
bool defaultIsEquivalentSubset(Operation *op, Value candidate, function_ref< bool(Value, Value)> equivalenceFn)
Default implementation of SubsetInsertionOpInterface::isEquivalentSubset.
OpOperand & defaultGetDestinationOperand(Operation *op)
Return the destination/"init" operand of the op if it implements the DestinationStyleOpInterface and ...
Include the generated interface declarations.
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value.
Definition: LogicalResult.h:68
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