|
22 | 22 | #include "llvm/ADT/SmallBitVector.h" |
23 | 23 | #include "llvm/ADT/SmallVector.h" |
24 | 24 | #include <algorithm> |
| 25 | +#include <numeric> |
25 | 26 |
|
26 | 27 | using namespace mlir; |
27 | 28 | using namespace mlir::linalg; |
@@ -53,112 +54,180 @@ bool linalg::detail::canOpOperandsBeDroppedImpl( |
53 | 54 | // CopyOpInterface implementation |
54 | 55 | //===----------------------------------------------------------------------===// |
55 | 56 |
|
56 | | -bool linalg::isaCopyOpInterface(LinalgOp linalgOp) { |
57 | | - // Structural. |
58 | | - if (linalgOp.getNumParallelLoops() != linalgOp.getNumLoops()) |
| 57 | +bool linalg::isaCopyOpInterface(LinalgOp op) { |
| 58 | + // Check all loops are parallel and linalgOp is single input and output. |
| 59 | + if (!op.isAllParallelLoops() || !op.isSingleInputOutput()) |
59 | 60 | return false; |
60 | 61 |
|
61 | | - // Operands and maps. |
62 | | - if (linalgOp.getNumDpsInputs() != 1 || linalgOp.getNumDpsInits() != 1) |
63 | | - return false; |
64 | | - auto mapRange = linalgOp.getIndexingMapsArray(); |
| 62 | + auto mapRange = op.getIndexingMapsArray(); |
65 | 63 | if (mapRange.size() != 2 || !mapRange.front().isIdentity() || |
66 | 64 | !mapRange.back().isIdentity()) { |
67 | 65 | return false; |
68 | 66 | } |
69 | 67 | // Region. |
70 | | - return llvm::hasSingleElement(linalgOp.getBlock()->getOperations()); |
| 68 | + return llvm::hasSingleElement(op.getBlock()->getOperations()); |
71 | 69 | } |
72 | 70 |
|
73 | 71 | //===----------------------------------------------------------------------===// |
74 | 72 | // FillOpInterface implementation |
75 | 73 | //===----------------------------------------------------------------------===// |
76 | | -std::optional<Value> linalg::isaFillOpInterface(GenericOp genericOp) { |
| 74 | +std::optional<Value> linalg::isaFillOpInterface(GenericOp op) { |
77 | 75 | // Structural. |
78 | | - if (genericOp.getNumParallelLoops() != genericOp.getNumLoops() || |
79 | | - genericOp.getNumDpsInputs() != 1 || genericOp.getNumDpsInits() != 1) |
| 76 | + if (!op.isAllParallelLoops() || !op.isSingleInputOutput() || |
| 77 | + !op.isSingleYieldOp()) |
80 | 78 | return std::nullopt; |
81 | 79 |
|
82 | 80 | // Input should be referenced and init should not. |
83 | | - if (!genericOp.payloadUsesValueFromOperand(genericOp.getDpsInputOperand(0)) || |
84 | | - genericOp.payloadUsesValueFromOperand(genericOp.getDpsInitOperand(0))) |
| 81 | + if (!op.payloadUsesValueFromOperand(op.getDpsInputOperand(0)) || |
| 82 | + op.payloadUsesValueFromOperand(op.getDpsInitOperand(0))) |
85 | 83 | return std::nullopt; |
86 | 84 |
|
87 | | - OpOperand *value = genericOp.getDpsInputOperand(0); |
88 | | - if (!genericOp.isScalar(value)) |
| 85 | + OpOperand *value = op.getDpsInputOperand(0); |
| 86 | + if (!op.isScalar(value)) |
89 | 87 | return std::nullopt; |
| 88 | + return value->get(); |
| 89 | +} |
90 | 90 |
|
91 | | - Block *body = genericOp.getBody(); |
92 | | - if (body->getOperations().size() != 1) |
| 91 | +//===----------------------------------------------------------------------===// |
| 92 | +// BroadcastOpInterface implementation |
| 93 | +//===----------------------------------------------------------------------===// |
| 94 | +std::optional<SmallVector<int64_t>> |
| 95 | +linalg::isaBroadcastOpInterface(GenericOp op) { |
| 96 | + // Structural. |
| 97 | + if (!op.isAllParallelLoops() || !op.isSingleInputOutput() || |
| 98 | + !op.isSingleYieldOp()) |
93 | 99 | return std::nullopt; |
94 | 100 |
|
95 | | - auto yieldOp = dyn_cast<linalg::YieldOp>(body->back()); |
96 | | - if (!yieldOp || yieldOp.getNumOperands() != 1 || |
97 | | - yieldOp->getOperand(0) != body->getArgument(0)) |
| 101 | + auto srcTy = op.getDpsInputOperand(0)->get().getType(); |
| 102 | + auto dstTy = op.getDpsInitOperand(0)->get().getType(); |
| 103 | + if (!isa<MemRefType, RankedTensorType>(srcTy) || |
| 104 | + !isa<MemRefType, RankedTensorType>(dstTy)) |
98 | 105 | return std::nullopt; |
99 | | - return value->get(); |
| 106 | + |
| 107 | + // Check output is identity map. Broadcast could additionally be |
| 108 | + // employing permutation of indices and that would be expressible |
| 109 | + // in linalg.generic but is not expressible for named broadcast op. |
| 110 | + auto dstMap = op.getIndexingMapsArray()[1]; |
| 111 | + if (!dstMap.isIdentity()) |
| 112 | + return std::nullopt; |
| 113 | + |
| 114 | + SmallVector<int64_t> position; |
| 115 | + auto srcMap = op.getIndexingMapsArray()[0]; |
| 116 | + |
| 117 | + if (srcMap.getResults().size() >= dstMap.getResults().size()) |
| 118 | + return std::nullopt; |
| 119 | + |
| 120 | + // Check input map is monotonically increasing DimIds. |
| 121 | + for (unsigned i = 0; i < srcMap.getNumResults(); ++i) { |
| 122 | + auto expr = llvm::dyn_cast<AffineDimExpr>(srcMap.getResults()[i]); |
| 123 | + if (!expr) |
| 124 | + return std::nullopt; |
| 125 | + int64_t pos = expr.getPosition(); |
| 126 | + if (i > 0 && pos <= position[i - 1]) |
| 127 | + return std::nullopt; |
| 128 | + position.push_back(expr.getPosition()); |
| 129 | + } |
| 130 | + |
| 131 | + SmallVector<int64_t> broadcastedDims; |
| 132 | + auto numDims = srcMap.getNumDims(); |
| 133 | + // This is quadratic but number of items is generally small. |
| 134 | + for (auto dim : llvm::seq<int64_t>(0, numDims)) { |
| 135 | + if (!llvm::is_contained(position, dim)) |
| 136 | + broadcastedDims.push_back(dim); |
| 137 | + } |
| 138 | + return broadcastedDims; |
| 139 | +} |
| 140 | + |
| 141 | +//===----------------------------------------------------------------------===// |
| 142 | +// TranposeOpInterface implementation |
| 143 | +//===----------------------------------------------------------------------===// |
| 144 | +std::optional<SmallVector<int64_t>> |
| 145 | +linalg::isaTransposeOpInterface(GenericOp op) { |
| 146 | + // To specialize as a transpose op, the genericOp must be |
| 147 | + // all parallel loops, single input, single output, and its body |
| 148 | + // should be just a yield op, yielding input as output as is (no compute). |
| 149 | + if (!op.isAllParallelLoops() || !op.isSingleInputOutput() || |
| 150 | + !op.isSingleYieldOp()) |
| 151 | + return std::nullopt; |
| 152 | + |
| 153 | + auto mapRange = op.getIndexingMapsArray(); |
| 154 | + if (mapRange.size() != 2) |
| 155 | + return std::nullopt; |
| 156 | + |
| 157 | + auto mapOfInput = mapRange.front(); |
| 158 | + auto mapOfResult = mapRange.back(); |
| 159 | + |
| 160 | + // linalg.transpose permutes the dimensions of input using this |
| 161 | + // rule: dim(result, i) = dim(input, permutation[i]) |
| 162 | + if (!mapOfResult.isIdentity() || !mapOfInput.isPermutation()) |
| 163 | + return std::nullopt; |
| 164 | + |
| 165 | + SmallVector<int64_t> permutation(mapOfInput.getNumDims()); |
| 166 | + for (unsigned i = 0; i < mapOfInput.getNumDims(); ++i) { |
| 167 | + auto expr = llvm::cast<AffineDimExpr>(mapOfInput.getResults()[i]); |
| 168 | + permutation[expr.getPosition()] = i; |
| 169 | + } |
| 170 | + return permutation; |
100 | 171 | } |
101 | 172 |
|
102 | 173 | //===----------------------------------------------------------------------===// |
103 | 174 | // Elementwise Single Unary/Binary-OpInterface implementation |
104 | 175 | //===----------------------------------------------------------------------===// |
105 | | -static bool |
106 | | -isaElemwiseSingleUnaryOrBinaryOpInterface(linalg::GenericOp genericOp, |
107 | | - unsigned arity) { |
| 176 | +static bool isaElemwiseSingleUnaryOrBinaryOpInterface(linalg::GenericOp op, |
| 177 | + unsigned arity) { |
108 | 178 | // Check all loops are parallel. |
109 | | - if (genericOp.getNumParallelLoops() != genericOp.getNumLoops() || |
110 | | - genericOp.getNumLoops() < 1) |
| 179 | + if (!op.isAllParallelLoops() || op.getNumLoops() < 1) |
111 | 180 | return false; |
112 | 181 |
|
113 | 182 | // Check there are arity-inputs, 1-output and all are identity-maps. |
114 | | - if (genericOp.getNumDpsInputs() != arity || genericOp.getNumDpsInits() != 1 || |
115 | | - !llvm::all_of(genericOp.getIndexingMapsArray(), |
| 183 | + if (op.getNumDpsInputs() != arity || op.getNumDpsInits() != 1 || |
| 184 | + !llvm::all_of(op.getIndexingMapsArray(), |
116 | 185 | [](AffineMap map) { return map.isIdentity(); })) |
117 | 186 | return false; |
118 | 187 |
|
119 | 188 | // Init should not be referenced for elementwise operations. |
120 | | - if (genericOp.payloadUsesValueFromOperand(genericOp.getDpsInitOperand(0))) |
| 189 | + if (op.payloadUsesValueFromOperand(op.getDpsInitOperand(0))) |
121 | 190 | return false; |
122 | 191 |
|
123 | 192 | // A linalg.generic could be series of elementwise ops e.g. exp(neg(x)) such |
124 | 193 | // as resulting from producer-consumer fusion. Here, we restrict to two ops in |
125 | 194 | // the body, where the first is the elementwise single op and the second a |
126 | 195 | // yield. |
127 | | - Block *body = genericOp.getBody(); |
| 196 | + Block *body = op.getBody(); |
128 | 197 | if (body->getOperations().size() != 2) |
129 | 198 | return false; |
130 | 199 |
|
131 | | - Operation *op = &body->front(); |
132 | | - if (op->getNumOperands() != arity || op->getNumResults() != 1) |
| 200 | + Operation *oper = &body->front(); |
| 201 | + if (oper->getNumOperands() != arity || oper->getNumResults() != 1) |
133 | 202 | return false; |
134 | 203 |
|
135 | 204 | auto yieldOp = dyn_cast<linalg::YieldOp>(body->back()); |
136 | 205 | if (!yieldOp || yieldOp.getNumOperands() != 1 || |
137 | | - yieldOp->getOperand(0).getDefiningOp() != op) |
| 206 | + yieldOp->getOperand(0).getDefiningOp() != oper) |
138 | 207 | return false; |
139 | 208 | return true; |
140 | 209 | } |
141 | 210 |
|
142 | | -bool linalg::isaElemwiseSingleUnaryOpInterface(linalg::GenericOp genericOp) { |
| 211 | +bool linalg::isaElemwiseSingleUnaryOpInterface(linalg::GenericOp op) { |
143 | 212 | // All basic elemwise checks. |
144 | | - if (!isaElemwiseSingleUnaryOrBinaryOpInterface(genericOp, 1)) |
| 213 | + if (!isaElemwiseSingleUnaryOrBinaryOpInterface(op, 1)) |
145 | 214 | return false; |
146 | 215 |
|
147 | 216 | // Check input is actully used. |
148 | | - if (!genericOp.payloadUsesValueFromOperand(genericOp.getDpsInputOperand(0))) |
| 217 | + if (!op.payloadUsesValueFromOperand(op.getDpsInputOperand(0))) |
149 | 218 | return false; |
150 | 219 | return true; |
151 | 220 | } |
152 | 221 |
|
153 | | -bool linalg::isaElemwiseSingleBinaryOpInterface(linalg::GenericOp genericOp) { |
154 | | - if (!isaElemwiseSingleUnaryOrBinaryOpInterface(genericOp, 2)) |
| 222 | +bool linalg::isaElemwiseSingleBinaryOpInterface(linalg::GenericOp op) { |
| 223 | + if (!isaElemwiseSingleUnaryOrBinaryOpInterface(op, 2)) |
155 | 224 | return false; |
156 | 225 |
|
157 | 226 | // Check both inputs are used (elementwise). |
158 | | - OpOperand *inputOpOperand0 = genericOp.getDpsInputOperand(0); |
159 | | - OpOperand *inputOpOperand1 = genericOp.getDpsInputOperand(1); |
160 | | - if (!genericOp.payloadUsesValueFromOperand(inputOpOperand0) || |
161 | | - !genericOp.payloadUsesValueFromOperand(inputOpOperand1)) |
| 227 | + OpOperand *inputOpOperand0 = op.getDpsInputOperand(0); |
| 228 | + OpOperand *inputOpOperand1 = op.getDpsInputOperand(1); |
| 229 | + if (!op.payloadUsesValueFromOperand(inputOpOperand0) || |
| 230 | + !op.payloadUsesValueFromOperand(inputOpOperand1)) |
162 | 231 | return false; |
163 | 232 | return true; |
164 | 233 | } |
|
0 commit comments