MLIR 24.0.0git
VectorDistribute.cpp
Go to the documentation of this file.
1//===- VectorDistribute.cpp - patterns to do vector distribution ----------===//
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
18#include "mlir/IR/AffineExpr.h"
19#include "mlir/IR/Attributes.h"
24#include "llvm/ADT/SetVector.h"
25#include "llvm/ADT/SmallBitVector.h"
26#include "llvm/ADT/SmallVectorExtras.h"
27#include "llvm/Support/FormatVariadic.h"
28#include <utility>
29
30using namespace mlir;
31using namespace mlir::vector;
32using namespace mlir::gpu;
33
34/// Currently the distribution map is implicit based on the vector shape. In the
35/// future it will be part of the op.
36/// Example:
37/// ```
38/// %0 = gpu.warp_execute_on_lane_0(%arg0) -> (vector<1x16x2xf32>) {
39/// ...
40/// gpu.yield %3 : vector<32x16x64xf32>
41/// }
42/// ```
43/// Would have an implicit map of:
44/// `(d0, d1, d2) -> (d0, d2)`
45static AffineMap calculateImplicitMap(VectorType sequentialType,
46 VectorType distributedType) {
48 perm.reserve(1);
49 // Check which dimensions of the sequential type are different than the
50 // dimensions of the distributed type to know the distributed dimensions. Then
51 // associate each distributed dimension to an ID in order.
52 for (unsigned i = 0, e = sequentialType.getRank(); i < e; i++) {
53 if (sequentialType.getDimSize(i) != distributedType.getDimSize(i))
54 perm.push_back(getAffineDimExpr(i, distributedType.getContext()));
55 }
56 auto map = AffineMap::get(sequentialType.getRank(), 0, perm,
57 distributedType.getContext());
58 return map;
59}
60
61/// Given a sequential and distributed vector type, returns the distributed
62/// dimension. This function expects that only a single dimension is
63/// distributed.
64static int getDistributedDim(VectorType sequentialType,
65 VectorType distributedType) {
66 assert(sequentialType.getRank() == distributedType.getRank() &&
67 "sequential and distributed vector types must have the same rank");
68 int64_t distributedDim = -1;
69 for (int64_t i = 0; i < sequentialType.getRank(); ++i) {
70 if (distributedType.getDimSize(i) != sequentialType.getDimSize(i)) {
71 // Keep this assert here in case WarpExecuteOnLane0Op gets extended to
72 // support distributing multiple dimensions in the future.
73 assert(distributedDim == -1 && "found multiple distributed dims");
74 distributedDim = i;
75 }
76 }
77 return distributedDim;
78}
79
80namespace {
81
82/// Helper struct to create the load / store operations that permit transit
83/// through the parallel / sequential and the sequential / parallel boundaries
84/// when performing `rewriteWarpOpToScfFor`.
85///
86/// The vector distribution dimension is inferred from the vector types.
87struct DistributedLoadStoreHelper {
88 DistributedLoadStoreHelper(Value sequentialVal, Value distributedVal,
89 Value laneId, Value zero)
90 : sequentialVal(sequentialVal), distributedVal(distributedVal),
91 laneId(laneId), zero(zero) {
92 sequentialVectorType = dyn_cast<VectorType>(sequentialVal.getType());
93 distributedVectorType = dyn_cast<VectorType>(distributedVal.getType());
94 if (sequentialVectorType && distributedVectorType)
95 distributionMap =
96 calculateImplicitMap(sequentialVectorType, distributedVectorType);
97 }
98
99 Value buildDistributedOffset(RewriterBase &b, Location loc, int64_t index) {
100 int64_t distributedSize = distributedVectorType.getDimSize(index);
101 AffineExpr tid = getAffineSymbolExpr(0, b.getContext());
102 return b.createOrFold<affine::AffineApplyOp>(loc, tid * distributedSize,
103 ArrayRef<Value>{laneId});
104 }
105
106 /// Create a store during the process of distributing the
107 /// `vector.warp_execute_on_thread_0` op.
108 /// Vector distribution assumes the following convention regarding the
109 /// temporary buffers that are created to transition values. This **must**
110 /// be properly specified in the `options.warpAllocationFn`:
111 /// 1. scalars of type T transit through a memref<1xT>.
112 /// 2. vectors of type V<shapexT> transit through a memref<shapexT>
113 Operation *buildStore(RewriterBase &b, Location loc, Value val,
114 Value buffer) {
115 assert((val == distributedVal || val == sequentialVal) &&
116 "Must store either the preregistered distributed or the "
117 "preregistered sequential value.");
118 // Scalar case can directly use memref.store.
119 if (!isa<VectorType>(val.getType()))
120 return memref::StoreOp::create(b, loc, val, buffer, zero);
121
122 // Vector case must use vector::TransferWriteOp which will later lower to
123 // vector.store of memref.store depending on further lowerings.
124 int64_t rank = sequentialVectorType.getRank();
125 SmallVector<Value> indices(rank, zero);
126 if (val == distributedVal) {
127 for (auto dimExpr : distributionMap.getResults()) {
128 int64_t index = cast<AffineDimExpr>(dimExpr).getPosition();
129 indices[index] = buildDistributedOffset(b, loc, index);
130 }
131 }
132 SmallVector<bool> inBounds(indices.size(), true);
133 return vector::TransferWriteOp::create(
134 b, loc, val, buffer, indices,
135 ArrayRef<bool>(inBounds.begin(), inBounds.end()));
136 }
137
138 /// Create a load during the process of distributing the
139 /// `vector.warp_execute_on_thread_0` op.
140 /// Vector distribution assumes the following convention regarding the
141 /// temporary buffers that are created to transition values. This **must**
142 /// be properly specified in the `options.warpAllocationFn`:
143 /// 1. scalars of type T transit through a memref<1xT>.
144 /// 2. vectors of type V<shapexT> transit through a memref<shapexT>
145 ///
146 /// When broadcastMode is true, the load is not distributed to account for
147 /// the broadcast semantics of the `gpu.warp_execute_on_lane_0` op.
148 ///
149 /// Example:
150 ///
151 /// ```
152 /// %r = gpu.warp_execute_on_lane_0(...) -> (f32) {
153 /// gpu.yield %cst : f32
154 /// }
155 /// // Both types are f32. The constant %cst is broadcasted to all lanes.
156 /// ```
157 /// This behavior described in more detail in the documentation of the op.
158 Value buildLoad(RewriterBase &b, Location loc, Type type, Value buffer) {
159
160 // Scalar case can directly use memref.store.
161 if (!isa<VectorType>(type))
162 return memref::LoadOp::create(b, loc, buffer, zero);
163
164 // Other cases must be vector atm.
165 // Vector case must use vector::TransferReadOp which will later lower to
166 // vector.read of memref.read depending on further lowerings.
167 assert((type == distributedVectorType || type == sequentialVectorType) &&
168 "Must store either the preregistered distributed or the "
169 "preregistered sequential type.");
170 SmallVector<Value> indices(sequentialVectorType.getRank(), zero);
171 if (type == distributedVectorType) {
172 for (auto dimExpr : distributionMap.getResults()) {
173 int64_t index = cast<AffineDimExpr>(dimExpr).getPosition();
174 indices[index] = buildDistributedOffset(b, loc, index);
175 }
176 }
177 SmallVector<bool> inBounds(indices.size(), true);
178 return vector::TransferReadOp::create(
179 b, loc, cast<VectorType>(type), buffer, indices,
180 /*padding=*/std::nullopt,
181 ArrayRef<bool>(inBounds.begin(), inBounds.end()));
182 }
183
184 Value sequentialVal, distributedVal, laneId, zero;
185 VectorType sequentialVectorType, distributedVectorType;
186 AffineMap distributionMap;
187};
188
189} // namespace
190
191// Clones `op` into a new operation that takes `operands` and returns
192// `resultTypes`.
194 Location loc, Operation *op,
195 ArrayRef<Value> operands,
196 ArrayRef<Type> resultTypes) {
197 OperationState res(loc, op->getName().getStringRef(), operands, resultTypes,
198 op->getDiscardableAttrDictionary().getValue());
199 res.propertiesAttr = op->getPropertiesAsAttribute();
200 return rewriter.create(res);
201}
202
203/// Reuse an args buffer from `buffers` to transit a value of `targetType`, or
204/// return null if none fits.
205static Value tryReuseArgBuffer(SmallVectorImpl<std::pair<Value, bool>> &buffers,
206 Type targetType, Location loc,
207 RewriterBase &rewriter) {
208 auto targetVecType = dyn_cast<VectorType>(targetType);
209 if (!targetVecType || !targetVecType.hasStaticShape())
210 return Value();
211
212 for (auto &entry : buffers) {
213 if (entry.second)
214 continue;
215 Value candidate = entry.first;
216 auto candidateMemref = dyn_cast<MemRefType>(candidate.getType());
217 if (!candidateMemref || !candidateMemref.hasStaticShape())
218 continue;
219 if (candidateMemref.getElementType() != targetVecType.getElementType())
220 continue;
221 // Skip rank mismatch up-front to avoid guessing a reshape.
222 if (candidateMemref.getRank() != targetVecType.getRank())
223 continue;
224 // The candidate must be at least as large as the target in every
225 // dimension, otherwise the leading subview below would run out of bounds.
226 // This also guarantees that the candidate has enough elements in total.
227 if (!llvm::all_of(llvm::seq<unsigned>(0, targetVecType.getRank()),
228 [&](unsigned i) {
229 return candidateMemref.getDimSize(i) >=
230 targetVecType.getDimSize(i);
231 }))
232 continue;
233
234 // All checks passed: claim the buffer.
235 entry.second = true;
236
237 // Same shape: reuse directly.
238 if (candidateMemref.getShape() == targetVecType.getShape())
239 return candidate;
240
241 // Larger candidate: take a leading subview of the target shape.
242 int64_t candidateRank = candidateMemref.getRank();
243 SmallVector<OpFoldResult> offsets(candidateRank, rewriter.getIndexAttr(0));
244 SmallVector<OpFoldResult> strides(candidateRank, rewriter.getIndexAttr(1));
246 sizes.reserve(candidateRank);
247 for (int64_t dim : targetVecType.getShape())
248 sizes.push_back(rewriter.getIndexAttr(dim));
249
250 return memref::SubViewOp::create(rewriter, loc, candidate, offsets, sizes,
251 strides);
252 }
253 return Value();
254}
255
256/// Return true if `block` is part of a CFG cycle, which
257/// `LoopLikeOpInterface` does not cover.
258static bool isBlockInCFGLoop(Block *block) {
259 llvm::SmallSetVector<Block *, 4> visited;
260 SmallVector<Block *> worklist(block->getSuccessors().begin(),
261 block->getSuccessors().end());
262 while (!worklist.empty()) {
263 Block *current = worklist.pop_back_val();
264 if (current == block)
265 return true;
266 if (!visited.insert(current))
267 continue;
268 for (Block *succ : current->getSuccessors())
269 worklist.push_back(succ);
270 }
271 return false;
272}
273
274/// Return true if `block` or any of its ancestor blocks is part of a CFG
275/// cycle, which `LoopLikeOpInterface` does not cover.
276static bool isInCFGLoop(Block *block) {
277 while (block) {
278 if (isBlockInCFGLoop(block))
279 return true;
280 Operation *parent = block->getParentOp();
281 block = parent ? parent->getBlock() : nullptr;
282 }
283 return false;
284}
285
286namespace {
287/// Rewrite a WarpExecuteOnLane0Op into a predicated scf.if op where the single
288/// thread `laneId` executes the entirety of the computation.
289///
290/// After the transformation:
291/// - the IR within the scf.if op can be thought of as executing sequentially
292/// (from the point of view of threads along `laneId`).
293/// - the IR outside of the scf.if op can be thought of as executing in
294/// parallel (from the point of view of threads along `laneId`).
295///
296/// Values that need to transit through the parallel / sequential and the
297/// sequential / parallel boundaries do so via reads and writes to a temporary
298/// memory location.
299///
300/// The transformation proceeds in multiple steps:
301/// 1. Create the scf.if op.
302/// 2. Insert appropriate (alloc, write)-pairs before the scf.if and reads
303/// within the scf.if to transit the values captured from above.
304/// 3. Synchronize before the scf.if to ensure all writes inserted in 2. are
305/// consistent within the scf.if.
306/// 4. Move the body of the WarpExecuteOnLane0Op inside the scf.if.
307/// 5. Insert appropriate writes within scf.if and reads after the scf.if to
308/// transit the values returned by the op.
309/// 6. Synchronize after the scf.if to ensure all writes inserted in 5. are
310/// consistent after the scf.if.
311/// 7. Perform late cleanups.
312///
313/// All this assumes the vector distribution occurs along the most minor
314/// distributed vector dimension.
315struct WarpOpToScfIfPattern : public WarpDistributionPattern {
316 WarpOpToScfIfPattern(MLIRContext *context,
317 const WarpExecuteOnLane0LoweringOptions &options,
318 PatternBenefit benefit = 1)
319 : WarpDistributionPattern(context, benefit), options(options) {}
320
321 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
322 PatternRewriter &rewriter) const override {
323 assert(warpOp.getBodyRegion().hasOneBlock() &&
324 "expected WarpOp with single block");
325 Block *warpOpBody = &warpOp.getBodyRegion().front();
326 Location loc = warpOp.getLoc();
327
328 // Passed all checks. Start rewriting.
329 OpBuilder::InsertionGuard g(rewriter);
330 rewriter.setInsertionPoint(warpOp);
331
332 // Step 1: Create scf.if op.
333 Value c0 = arith::ConstantIndexOp::create(rewriter, loc, 0);
334 Value isLane0 = arith::CmpIOp::create(
335 rewriter, loc, arith::CmpIPredicate::eq, warpOp.getLaneid(), c0);
336 auto ifOp = scf::IfOp::create(rewriter, loc, isLane0,
337 /*withElseRegion=*/false);
338 rewriter.eraseOp(ifOp.thenBlock()->getTerminator());
339
340 // Step 2: insert appropriate (alloc, write)-pairs before the scf.if and
341 // reads within the scf.if to transit the values captured from above.
342 SmallVector<Value> bbArgReplacements;
343 // Pool of args buffers paired with a "consumed" flag, so Step 5 can try
344 // to reuse them for yielded values' transit buffers. Skip reuse inside a
345 // loop (structured or CFG): thread convergence is only guaranteed within
346 // a barrier, so the next iteration's arg stores could race with this
347 // iteration's result loads.
348 SmallVector<std::pair<Value, /*consumed=*/bool>> reusableArgBuffers;
349 bool canReuseArgBuffers = !warpOp->getParentOfType<LoopLikeOpInterface>() &&
350 !isInCFGLoop(warpOp->getBlock());
351 if (canReuseArgBuffers)
352 reusableArgBuffers.reserve(warpOp.getArgs().size());
353 for (const auto &it : llvm::enumerate(warpOp.getArgs())) {
354 Value sequentialVal = warpOpBody->getArgument(it.index());
355 Value distributedVal = it.value();
356 DistributedLoadStoreHelper helper(sequentialVal, distributedVal,
357 warpOp.getLaneid(), c0);
358
359 // Create buffer before the ifOp.
360 rewriter.setInsertionPoint(ifOp);
361 Value buffer = options.warpAllocationFn(loc, rewriter, warpOp,
362 sequentialVal.getType());
363 if (canReuseArgBuffers)
364 reusableArgBuffers.emplace_back(buffer, /*consumed=*/false);
365 // Store distributed vector into buffer, before the ifOp.
366 helper.buildStore(rewriter, loc, distributedVal, buffer);
367 // Load sequential vector from buffer, inside the ifOp.
368 rewriter.setInsertionPointToStart(ifOp.thenBlock());
369 bbArgReplacements.push_back(
370 helper.buildLoad(rewriter, loc, sequentialVal.getType(), buffer));
371 }
372
373 // Step 3. Insert sync after all the stores and before all the loads.
374 if (!warpOp.getArgs().empty()) {
375 rewriter.setInsertionPoint(ifOp);
376 options.warpSynchronizationFn(loc, rewriter, warpOp);
377 }
378
379 // Step 4. Move body of warpOp to ifOp.
380 rewriter.mergeBlocks(warpOpBody, ifOp.thenBlock(), bbArgReplacements);
381
382 // Step 5. Insert appropriate writes within scf.if and reads after the
383 // scf.if to transit the values returned by the op.
384 // The args buffers from Step 2 are unused by now, try to reuse previous
385 // buffers.
386 SmallVector<Value> replacements;
387 auto yieldOp = cast<gpu::YieldOp>(ifOp.thenBlock()->getTerminator());
388 Location yieldLoc = yieldOp.getLoc();
389 for (const auto &it : llvm::enumerate(yieldOp.getOperands())) {
390 Value sequentialVal = it.value();
391 Value distributedVal = warpOp->getResult(it.index());
392 DistributedLoadStoreHelper helper(sequentialVal, distributedVal,
393 warpOp.getLaneid(), c0);
394
395 // Reuse an args buffer if possible, otherwise allocate.
396 rewriter.setInsertionPoint(ifOp);
397 Value buffer = tryReuseArgBuffer(reusableArgBuffers,
398 sequentialVal.getType(), loc, rewriter);
399 if (!buffer)
400 buffer = options.warpAllocationFn(loc, rewriter, warpOp,
401 sequentialVal.getType());
402
403 // Store yielded value into buffer, inside the ifOp, before the
404 // terminator.
405 rewriter.setInsertionPoint(yieldOp);
406 helper.buildStore(rewriter, loc, sequentialVal, buffer);
407
408 // Load distributed value from buffer, after the warpOp.
409 rewriter.setInsertionPointAfter(ifOp);
410 // Result type and yielded value type are the same. This is a broadcast.
411 // E.g.:
412 // %r = gpu.warp_execute_on_lane_0(...) -> (f32) {
413 // gpu.yield %cst : f32
414 // }
415 // Both types are f32. The constant %cst is broadcasted to all lanes.
416 // This is described in more detail in the documentation of the op.
417 replacements.push_back(
418 helper.buildLoad(rewriter, loc, distributedVal.getType(), buffer));
419 }
420
421 // Step 6. Insert sync after all the stores and before all the loads.
422 if (!yieldOp.getOperands().empty()) {
423 rewriter.setInsertionPointAfter(ifOp);
424 options.warpSynchronizationFn(loc, rewriter, warpOp);
425 }
426
427 // Step 7. Delete terminator and add empty scf.yield.
428 rewriter.eraseOp(yieldOp);
429 rewriter.setInsertionPointToEnd(ifOp.thenBlock());
430 scf::YieldOp::create(rewriter, yieldLoc);
431
432 // Compute replacements for WarpOp results.
433 rewriter.replaceOp(warpOp, replacements);
434
435 return success();
436 }
437
438private:
439 const WarpExecuteOnLane0LoweringOptions &options;
440};
441
442/// Return the distributed vector type based on the original type and the
443/// distribution map. The map is expected to have a dimension equal to the
444/// original type rank and should be a projection where the results are the
445/// distributed dimensions. If the number of results is zero there is no
446/// distribution (i.e. original type is returned).
447/// Otherwise, The number of results should be equal to the number
448/// of warp sizes which is currently limited to 1.
449/// Example: For a vector<16x32x64> distributed with a map(d0, d1, d2) -> (d1)
450/// and a warp size of 16 would distribute the second dimension (associated to
451/// d1) and return vector<16x2x64>
452static VectorType getDistributedType(VectorType originalType, AffineMap map,
453 int64_t warpSize) {
454 // If the map has zero results, return the original type.
455 if (map.getNumResults() == 0)
456 return originalType;
457 SmallVector<int64_t> targetShape(originalType.getShape());
458 for (unsigned i = 0, e = map.getNumResults(); i < e; i++) {
459 unsigned position = map.getDimPosition(i);
460 if (targetShape[position] % warpSize != 0) {
461 if (warpSize % targetShape[position] != 0) {
462 return VectorType();
463 }
464 warpSize /= targetShape[position];
465 targetShape[position] = 1;
466 continue;
467 }
468 targetShape[position] = targetShape[position] / warpSize;
469 warpSize = 1;
470 break;
471 }
472 if (warpSize != 1) {
473 return VectorType();
474 }
475 VectorType targetType =
476 VectorType::get(targetShape, originalType.getElementType());
477 return targetType;
478}
479
480/// Given a warpOp that contains ops with regions, the corresponding op's
481/// "inner" region and the distributionMapFn, get all values used by the op's
482/// region that are defined within the warpOp, but outside the inner region.
483/// Return the set of values, their types and their distributed types.
484std::tuple<llvm::SmallSetVector<Value, 32>, SmallVector<Type>,
486getInnerRegionEscapingValues(WarpExecuteOnLane0Op warpOp, Region &innerRegion,
487 DistributionMapFn distributionMapFn) {
488 llvm::SmallSetVector<Value, 32> escapingValues;
489 SmallVector<Type> escapingValueTypes;
490 SmallVector<Type> escapingValueDistTypes; // to yield from the new warpOp
491 if (innerRegion.empty())
492 return {std::move(escapingValues), std::move(escapingValueTypes),
493 std::move(escapingValueDistTypes)};
494 mlir::visitUsedValuesDefinedAbove(innerRegion, [&](OpOperand *operand) {
495 Operation *parent = operand->get().getParentRegion()->getParentOp();
496 if (warpOp->isAncestor(parent)) {
497 if (!escapingValues.insert(operand->get()))
498 return;
499 Type distType = operand->get().getType();
500 if (auto vecType = dyn_cast<VectorType>(distType)) {
501 AffineMap map = distributionMapFn(operand->get());
502 distType = getDistributedType(vecType, map,
503 map.isEmpty() ? 1 : warpOp.getWarpSize());
504 }
505 escapingValueTypes.push_back(operand->get().getType());
506 escapingValueDistTypes.push_back(distType);
507 }
508 });
509 return {std::move(escapingValues), std::move(escapingValueTypes),
510 std::move(escapingValueDistTypes)};
511}
512
513/// Distribute transfer_write ops based on the affine map returned by
514/// `distributionMapFn`. Writes of size more than `maxNumElementToExtract`
515/// will not be distributed (it should be less than the warp size).
516///
517/// Example:
518/// ```
519/// %0 = gpu.warp_execute_on_lane_0(%id){
520/// ...
521/// vector.transfer_write %v, %A[%c0] : vector<32xf32>, memref<128xf32>
522/// gpu.yield
523/// }
524/// ```
525/// To
526/// ```
527/// %r:3 = gpu.warp_execute_on_lane_0(%id) -> (vector<1xf32>) {
528/// ...
529/// gpu.yield %v : vector<32xf32>
530/// }
531/// vector.transfer_write %v, %A[%id] : vector<1xf32>, memref<128xf32>
532struct WarpOpTransferWrite : public WarpDistributionPattern {
533 WarpOpTransferWrite(MLIRContext *ctx, DistributionMapFn fn,
534 unsigned maxNumElementsToExtract, PatternBenefit b = 1)
535 : WarpDistributionPattern(ctx, b), distributionMapFn(std::move(fn)),
536 maxNumElementsToExtract(maxNumElementsToExtract) {}
537
538 /// Distribute the TransferWriteOp. Only 1D distributions and vector dims that
539 /// are multiples of the distribution ratio are supported at the moment.
540 LogicalResult tryDistributeOp(RewriterBase &rewriter,
541 vector::TransferWriteOp writeOp,
542 WarpExecuteOnLane0Op warpOp) const {
543 VectorType writtenVectorType = writeOp.getVectorType();
544
545 // 1. If the write is 0-D, we just clone it into a new WarpExecuteOnLane0Op
546 // to separate it from the rest.
547 if (writtenVectorType.getRank() == 0)
548 return failure();
549
550 // 2. Compute the distributed type.
551 AffineMap map = distributionMapFn(writeOp.getVector());
552 VectorType targetType =
553 getDistributedType(writtenVectorType, map, warpOp.getWarpSize());
554 if (!targetType)
555 return failure();
556
557 // 2.5 Compute the distributed type for the new mask;
558 VectorType maskType;
559 if (writeOp.getMask()) {
560 // TODO: Distribution of masked writes with non-trivial permutation maps
561 // requires the distribution of the mask to elementwise match the
562 // distribution of the permuted written vector. Currently the details
563 // of which lane is responsible for which element is captured strictly
564 // by shape information on the warp op, and thus requires materializing
565 // the permutation in IR.
566 if (!writeOp.getPermutationMap().isMinorIdentity())
567 return failure();
568 maskType =
569 getDistributedType(writeOp.getMaskType(), map, warpOp.getWarpSize());
570 }
571
572 // 3. clone the write into a new WarpExecuteOnLane0Op to separate it from
573 // the rest.
574 vector::TransferWriteOp newWriteOp =
575 cloneWriteOp(rewriter, warpOp, writeOp, targetType, maskType);
576
577 // 4. Reindex the write using the distribution map.
578 auto newWarpOp =
579 newWriteOp.getVector().getDefiningOp<WarpExecuteOnLane0Op>();
580
581 // Delinearize the lane id based on the way threads are divided across the
582 // vector. To get the number of threads per vector dimension, divide the
583 // sequential size by the distributed size along each dim.
584 rewriter.setInsertionPoint(newWriteOp);
585 SmallVector<OpFoldResult> delinearizedIdSizes;
586 for (auto [seqSize, distSize] :
587 llvm::zip_equal(writtenVectorType.getShape(), targetType.getShape())) {
588 assert(seqSize % distSize == 0 && "Invalid distributed vector shape");
589 delinearizedIdSizes.push_back(rewriter.getIndexAttr(seqSize / distSize));
590 }
591 SmallVector<Value> delinearized;
592 if (map.getNumResults() > 1) {
593 delinearized = mlir::affine::AffineDelinearizeIndexOp::create(
594 rewriter, newWarpOp.getLoc(), newWarpOp.getLaneid(),
595 delinearizedIdSizes)
596 .getResults();
597 } else {
598 // If there is only one map result, we can elide the delinearization
599 // op and use the lane id directly.
600 delinearized.append(targetType.getRank(), newWarpOp.getLaneid());
601 }
602
603 AffineMap indexMap = map.compose(newWriteOp.getPermutationMap());
604 Location loc = newWriteOp.getLoc();
605 SmallVector<Value> indices(newWriteOp.getIndices().begin(),
606 newWriteOp.getIndices().end());
607 for (auto it : llvm::zip(indexMap.getResults(), map.getResults())) {
608 AffineExpr d0, d1;
609 bindDims(newWarpOp.getContext(), d0, d1);
610 auto indexExpr = dyn_cast<AffineDimExpr>(std::get<0>(it));
611 if (!indexExpr)
612 continue;
613 unsigned indexPos = indexExpr.getPosition();
614 unsigned vectorPos = cast<AffineDimExpr>(std::get<1>(it)).getPosition();
615 Value laneId = delinearized[vectorPos];
616 auto scale =
617 rewriter.getAffineConstantExpr(targetType.getDimSize(vectorPos));
619 rewriter, loc, d0 + scale * d1, {indices[indexPos], laneId});
620 }
621 newWriteOp.getIndicesMutable().assign(indices);
622
623 return success();
624 }
625
626 /// Extract TransferWriteOps of vector<1x> into a separate warp op.
627 LogicalResult tryExtractOp(RewriterBase &rewriter,
628 vector::TransferWriteOp writeOp,
629 WarpExecuteOnLane0Op warpOp) const {
630 Location loc = writeOp.getLoc();
631 VectorType vecType = writeOp.getVectorType();
632
633 if (vecType.getNumElements() > maxNumElementsToExtract) {
634 return rewriter.notifyMatchFailure(
635 warpOp,
636 llvm::formatv(
637 "writes more elements ({0}) than allowed to extract ({1})",
638 vecType.getNumElements(), maxNumElementsToExtract));
639 }
640
641 // Do not process warp ops that contain only TransferWriteOps.
642 if (llvm::all_of(warpOp.getOps(),
643 llvm::IsaPred<vector::TransferWriteOp, gpu::YieldOp>))
644 return failure();
645
646 SmallVector<Value> yieldValues = {writeOp.getVector()};
647 SmallVector<Type> retTypes = {vecType};
648 SmallVector<size_t> newRetIndices;
649 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
650 rewriter, warpOp, yieldValues, retTypes, newRetIndices);
651 rewriter.setInsertionPointAfter(newWarpOp);
652
653 // Create a second warp op that contains only writeOp.
654 auto secondWarpOp = WarpExecuteOnLane0Op::create(rewriter, loc, TypeRange(),
655 newWarpOp.getLaneid(),
656 newWarpOp.getWarpSize());
657 Block &body = secondWarpOp.getBodyRegion().front();
658 rewriter.setInsertionPointToStart(&body);
659 auto newWriteOp =
660 cast<vector::TransferWriteOp>(rewriter.clone(*writeOp.getOperation()));
661 newWriteOp.getValueToStoreMutable().assign(
662 newWarpOp.getResult(newRetIndices[0]));
663 rewriter.eraseOp(writeOp);
664 gpu::YieldOp::create(rewriter, newWarpOp.getLoc());
665 return success();
666 }
667
668 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
669 PatternRewriter &rewriter) const override {
670 gpu::YieldOp yield = warpOp.getTerminator();
671 Operation *lastNode = yield->getPrevNode();
672 auto writeOp = dyn_cast_or_null<vector::TransferWriteOp>(lastNode);
673 if (!writeOp)
674 return failure();
675
676 Value maybeMask = writeOp.getMask();
677 if (!llvm::all_of(writeOp->getOperands(), [&](Value value) {
678 return writeOp.getVector() == value ||
679 (maybeMask && maybeMask == value) ||
680 warpOp.isDefinedOutsideOfRegion(value);
681 }))
682 return failure();
683
684 if (succeeded(tryDistributeOp(rewriter, writeOp, warpOp)))
685 return success();
686
687 // Masked writes not supported for extraction.
688 if (writeOp.getMask())
689 return failure();
690
691 if (succeeded(tryExtractOp(rewriter, writeOp, warpOp)))
692 return success();
693
694 return failure();
695 }
696
697private:
698 /// Clone `writeOp` assumed to be nested under `warpOp` into a new warp
699 /// execute op with the proper return type. The new write op is updated to
700 /// write the result of the new warp execute op. The old `writeOp` is deleted.
701 vector::TransferWriteOp cloneWriteOp(RewriterBase &rewriter,
702 WarpExecuteOnLane0Op warpOp,
703 vector::TransferWriteOp writeOp,
704 VectorType targetType,
705 VectorType maybeMaskType) const {
706 assert(writeOp->getParentOp() == warpOp &&
707 "write must be nested immediately under warp");
708 OpBuilder::InsertionGuard g(rewriter);
709 SmallVector<size_t> newRetIndices;
710 WarpExecuteOnLane0Op newWarpOp;
711 if (maybeMaskType) {
713 rewriter, warpOp, ValueRange{writeOp.getVector(), writeOp.getMask()},
714 TypeRange{targetType, maybeMaskType}, newRetIndices);
715 } else {
717 rewriter, warpOp, ValueRange{{writeOp.getVector()}},
718 TypeRange{targetType}, newRetIndices);
719 }
720 rewriter.setInsertionPointAfter(newWarpOp);
721 auto newWriteOp =
722 cast<vector::TransferWriteOp>(rewriter.clone(*writeOp.getOperation()));
723 rewriter.eraseOp(writeOp);
724 newWriteOp.getValueToStoreMutable().assign(
725 newWarpOp.getResult(newRetIndices[0]));
726 if (maybeMaskType)
727 newWriteOp.getMaskMutable().assign(newWarpOp.getResult(newRetIndices[1]));
728 return newWriteOp;
729 }
730
731 DistributionMapFn distributionMapFn;
732 unsigned maxNumElementsToExtract = 1;
733};
734
735/// Sink out elementwise op feeding into a warp op yield.
736/// ```
737/// %0 = gpu.warp_execute_on_lane_0(%arg0) -> (vector<1xf32>) {
738/// ...
739/// %3 = arith.addf %1, %2 : vector<32xf32>
740/// gpu.yield %3 : vector<32xf32>
741/// }
742/// ```
743/// To
744/// ```
745/// %r:3 = gpu.warp_execute_on_lane_0(%arg0) -> (vector<1xf32>,
746/// vector<1xf32>, vector<1xf32>) {
747/// ...
748/// %4 = arith.addf %2, %3 : vector<32xf32>
749/// gpu.yield %4, %2, %3 : vector<32xf32>, vector<32xf32>,
750/// vector<32xf32>
751/// }
752/// %0 = arith.addf %r#1, %r#2 : vector<1xf32>
753struct WarpOpElementwise : public WarpDistributionPattern {
754 using Base::Base;
755 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
756 PatternRewriter &rewriter) const override {
757 OpOperand *yieldOperand = getWarpResult(warpOp, [](Operation *op) {
759 });
760 if (!yieldOperand)
761 return failure();
762
763 Operation *elementWise = yieldOperand->get().getDefiningOp();
764 unsigned operandIndex = yieldOperand->getOperandNumber();
765 Value distributedVal = warpOp.getResult(operandIndex);
766 SmallVector<Value> yieldValues;
767 SmallVector<Type> retTypes;
768 Location loc = warpOp.getLoc();
769 for (OpOperand &operand : elementWise->getOpOperands()) {
770 Type targetType;
771 if (auto vecType = dyn_cast<VectorType>(distributedVal.getType())) {
772 // If the result type is a vector, the operands must also be vectors.
773 auto operandType = cast<VectorType>(operand.get().getType());
774 targetType =
775 VectorType::get(vecType.getShape(), operandType.getElementType());
776 } else {
777 auto operandType = operand.get().getType();
778 assert(!isa<VectorType>(operandType) &&
779 "unexpected yield of vector from op with scalar result type");
780 targetType = operandType;
781 }
782 retTypes.push_back(targetType);
783 yieldValues.push_back(operand.get());
784 }
785 SmallVector<size_t> newRetIndices;
786 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
787 rewriter, warpOp, yieldValues, retTypes, newRetIndices);
788 rewriter.setInsertionPointAfter(newWarpOp);
789 SmallVector<Value> newOperands(elementWise->getOperands().begin(),
790 elementWise->getOperands().end());
791 for (unsigned i : llvm::seq(unsigned(0), elementWise->getNumOperands())) {
792 newOperands[i] = newWarpOp.getResult(newRetIndices[i]);
793 }
794 OpBuilder::InsertionGuard g(rewriter);
795 rewriter.setInsertionPointAfter(newWarpOp);
796 Operation *newOp = cloneOpWithOperandsAndTypes(
797 rewriter, loc, elementWise, newOperands,
798 {newWarpOp.getResult(operandIndex).getType()});
799 rewriter.replaceAllUsesWith(newWarpOp.getResult(operandIndex),
800 newOp->getResult(0));
801 return success();
802 }
803};
804
805/// Sink out splat constant op feeding into a warp op yield.
806/// ```
807/// %0 = gpu.warp_execute_on_lane_0(%arg0) -> (vector<1xf32>) {
808/// ...
809/// %cst = arith.constant dense<2.0> : vector<32xf32>
810/// gpu.yield %cst : vector<32xf32>
811/// }
812/// ```
813/// To
814/// ```
815/// gpu.warp_execute_on_lane_0(%arg0 {
816/// ...
817/// }
818/// %0 = arith.constant dense<2.0> : vector<1xf32>
819struct WarpOpConstant : public WarpDistributionPattern {
820 using Base::Base;
821 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
822 PatternRewriter &rewriter) const override {
823 OpOperand *yieldOperand =
824 getWarpResult(warpOp, llvm::IsaPred<arith::ConstantOp>);
825 if (!yieldOperand)
826 return failure();
827 auto constantOp = yieldOperand->get().getDefiningOp<arith::ConstantOp>();
828 auto dense = dyn_cast<SplatElementsAttr>(constantOp.getValue());
829 if (!dense)
830 return failure();
831 // Notify the rewriter that the warp op is changing (see the comment on
832 // the WarpOpTransferRead pattern).
833 rewriter.startOpModification(warpOp);
834 unsigned operandIndex = yieldOperand->getOperandNumber();
835 Attribute scalarAttr = dense.getSplatValue<Attribute>();
836 auto newAttr = DenseElementsAttr::get(
837 cast<ShapedType>(warpOp.getResult(operandIndex).getType()), scalarAttr);
838 Location loc = warpOp.getLoc();
839 rewriter.setInsertionPointAfter(warpOp);
840 Value distConstant = arith::ConstantOp::create(rewriter, loc, newAttr);
841 rewriter.replaceAllUsesWith(warpOp.getResult(operandIndex), distConstant);
842 rewriter.finalizeOpModification(warpOp);
843 return success();
844 }
845};
846
847/// Sink out step op feeding into a warp op yield.
848/// Vector step op is treated similar to arith.constant, apart from
849/// the result that represents a sequence [0, vec_size).
850/// Due to the to vec_size == warp_size limitation,
851/// we can simply wrap the lane id into a vector (i.e., broadcast).
852/// Supporting vec_size != warp_size may involve preserving the step
853/// result and using additional arith ops (the exact details are TBD).
854/// ```
855/// %0 = gpu.warp_execute_on_lane_0(%arg0) -> (vector<1xindex>) {
856/// ...
857/// %cst = vector.step : vector<32xindex>
858/// gpu.yield %cst : vector<1xindex>
859/// }
860/// ```
861/// To
862/// ```
863/// gpu.warp_execute_on_lane_0(%arg0) {
864/// ...
865/// }
866/// %lane_id_vec = vector.broadcast %arg0 : index to vector<1xindex>
867struct WarpOpStep final : public WarpDistributionPattern {
868 using Base::Base;
869 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
870 PatternRewriter &rewriter) const override {
871 OpOperand *yieldOperand =
872 getWarpResult(warpOp, llvm::IsaPred<vector::StepOp>);
873 if (!yieldOperand)
874 return failure();
875 const unsigned operandIdx = yieldOperand->getOperandNumber();
876 auto stepOp = yieldOperand->get().getDefiningOp<vector::StepOp>();
877 VectorType resTy = stepOp.getResult().getType();
878 if (resTy.getNumElements() != static_cast<int64_t>(warpOp.getWarpSize()))
879 return rewriter.notifyMatchFailure(
880 warpOp,
881 llvm::formatv("Expected result size ({0}) to be of warp size ({1})",
882 resTy.getNumElements(), warpOp.getWarpSize()));
883 VectorType newVecTy =
884 cast<VectorType>(warpOp.getResult(operandIdx).getType());
885 rewriter.setInsertionPointAfter(warpOp);
886 Value laneIdVec = vector::BroadcastOp::create(rewriter, warpOp.getLoc(),
887 newVecTy, warpOp.getLaneid());
888 rewriter.replaceAllUsesWith(warpOp.getResult(operandIdx), laneIdVec);
889 return success();
890 }
891};
892
893/// Sink out transfer_read op feeding into a warp op yield.
894/// ```
895/// %0 = gpu.warp_execute_on_lane_0(%arg0) -> (vector<1xf32>) {
896/// ...
897// %2 = vector.transfer_read %src[%c0], %cst : memref<1024xf32>,
898// vector<32xf32>
899/// gpu.yield %2 : vector<32xf32>
900/// }
901/// ```
902/// To
903/// ```
904/// %dead = gpu.warp_execute_on_lane_0(%arg0) -> (vector<1xf32>,
905/// vector<1xf32>, vector<1xf32>) {
906/// ...
907/// %2 = vector.transfer_read %src[%c0], %cst : memref<1024xf32>,
908/// vector<32xf32> gpu.yield %2 : vector<32xf32>
909/// }
910/// %0 = vector.transfer_read %src[%c0], %cst : memref<1024xf32>, vector<1xf32>
911struct WarpOpTransferRead : public WarpDistributionPattern {
912 using Base::Base;
913 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
914 PatternRewriter &rewriter) const override {
915 // Try to find a distributable yielded read. Note that this pattern can
916 // still fail at the end after distribution, in which case this might have
917 // missed another distributable read.
918 OpOperand *operand = getWarpResult(warpOp, [](Operation *op) {
919 // Don't duplicate transfer_read ops when distributing.
920 return isa<vector::TransferReadOp>(op) && op->hasOneUse();
921 });
922 if (!operand)
923 return rewriter.notifyMatchFailure(
924 warpOp, "warp result is not a vector.transfer_read op");
925 auto read = operand->get().getDefiningOp<vector::TransferReadOp>();
926
927 // Source must be defined outside of the region.
928 if (!warpOp.isDefinedOutsideOfRegion(read.getBase()))
929 return rewriter.notifyMatchFailure(
930 read, "source must be defined outside of the region");
931
932 unsigned operandIndex = operand->getOperandNumber();
933 Value distributedVal = warpOp.getResult(operandIndex);
934
935 SmallVector<Value, 4> indices(read.getIndices().begin(),
936 read.getIndices().end());
937 auto sequentialType = cast<VectorType>(read.getResult().getType());
938 auto distributedType = cast<VectorType>(distributedVal.getType());
939 AffineMap map = calculateImplicitMap(sequentialType, distributedType);
940 AffineMap indexMap = map.compose(read.getPermutationMap());
941
942 // Try to delinearize the lane ID to match the rank expected for
943 // distribution.
944 SmallVector<Value> delinearizedIds;
945 if (!delinearizeLaneId(rewriter, read.getLoc(), sequentialType.getShape(),
946 distributedType.getShape(), warpOp.getWarpSize(),
947 warpOp.getLaneid(), delinearizedIds)) {
948 return rewriter.notifyMatchFailure(
949 read, "cannot delinearize lane ID for distribution");
950 }
951 assert(!delinearizedIds.empty() || map.getNumResults() == 0);
952
953 // Distribute indices and the mask (if present).
954 OpBuilder::InsertionGuard g(rewriter);
955 SmallVector<Value> additionalResults(indices.begin(), indices.end());
956 SmallVector<Type> additionalResultTypes(indices.size(),
957 rewriter.getIndexType());
958 additionalResults.push_back(read.getPadding());
959 additionalResultTypes.push_back(read.getPadding().getType());
960
961 bool hasMask = false;
962 if (read.getMask()) {
963 hasMask = true;
964 // TODO: Distribution of masked reads with non-trivial permutation maps
965 // requires the distribution of the mask to elementwise match the
966 // distribution of the permuted written vector. Currently the details
967 // of which lane is responsible for which element is captured strictly
968 // by shape information on the warp op, and thus requires materializing
969 // the permutation in IR.
970 if (!mlir::compressUnusedDims(read.getPermutationMap()).isIdentity())
971 return rewriter.notifyMatchFailure(
972 read, "non-trivial permutation maps not supported");
973 VectorType maskType =
974 getDistributedType(read.getMaskType(), map, warpOp.getWarpSize());
975 additionalResults.push_back(read.getMask());
976 additionalResultTypes.push_back(maskType);
977 }
978
979 SmallVector<size_t> newRetIndices;
980 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
981 rewriter, warpOp, additionalResults, additionalResultTypes,
982 newRetIndices);
983 distributedVal = newWarpOp.getResult(operandIndex);
984
985 // Distributed indices were appended first.
986 SmallVector<Value> newIndices;
987 for (int64_t i = 0, e = indices.size(); i < e; ++i)
988 newIndices.push_back(newWarpOp.getResult(newRetIndices[i]));
989
990 rewriter.setInsertionPointAfter(newWarpOp);
991 for (auto it : llvm::zip_equal(indexMap.getResults(), map.getResults())) {
992 AffineExpr d0, d1;
993 bindDims(read.getContext(), d0, d1);
994 auto indexExpr = dyn_cast<AffineDimExpr>(std::get<0>(it));
995 if (!indexExpr)
996 continue;
997 unsigned indexPos = indexExpr.getPosition();
998 unsigned vectorPos = cast<AffineDimExpr>(std::get<1>(it)).getPosition();
999 int64_t scale = distributedType.getDimSize(vectorPos);
1000 newIndices[indexPos] = affine::makeComposedAffineApply(
1001 rewriter, read.getLoc(), d0 + scale * d1,
1002 {newIndices[indexPos], delinearizedIds[vectorPos]});
1003 }
1004
1005 // Distributed padding value was appended right after the indices.
1006 Value newPadding = newWarpOp.getResult(newRetIndices[indices.size()]);
1007 // Distributed mask value was added at the end (if the op has a mask).
1008 Value newMask =
1009 hasMask ? newWarpOp.getResult(newRetIndices[newRetIndices.size() - 1])
1010 : Value();
1011 auto newRead = vector::TransferReadOp::create(
1012 rewriter, read.getLoc(), distributedVal.getType(), read.getBase(),
1013 newIndices, read.getPermutationMapAttr(), newPadding, newMask,
1014 read.getInBoundsAttr());
1015
1016 rewriter.replaceAllUsesWith(distributedVal, newRead);
1017 return success();
1018 }
1019};
1020
1021/// Remove any result that has no use along with the matching yieldOp operand.
1022// TODO: Move this in WarpExecuteOnLane0Op canonicalization.
1023struct WarpOpDeadResult : public WarpDistributionPattern {
1024 using Base::Base;
1025 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
1026 PatternRewriter &rewriter) const override {
1027 SmallVector<Type> newResultTypes;
1028 newResultTypes.reserve(warpOp->getNumResults());
1029 SmallVector<Value> newYieldValues;
1030 newYieldValues.reserve(warpOp->getNumResults());
1031 DenseMap<Value, int64_t> dedupYieldOperandPositionMap;
1032 DenseMap<OpResult, int64_t> dedupResultPositionMap;
1033 gpu::YieldOp yield = warpOp.getTerminator();
1034
1035 // Some values may be yielded multiple times and correspond to multiple
1036 // results. Deduplicating occurs by taking each result with its matching
1037 // yielded value, and:
1038 // 1. recording the unique first position at which the value with uses is
1039 // yielded.
1040 // 2. recording for the result, the first position at which the dedup'ed
1041 // value is yielded.
1042 // 3. skipping from the new result types / new yielded values any result
1043 // that has no use or whose yielded value has already been seen.
1044 for (OpResult result : warpOp.getResults()) {
1045 if (result.use_empty())
1046 continue;
1047 Value yieldOperand = yield.getOperand(result.getResultNumber());
1048 auto it = dedupYieldOperandPositionMap.insert(
1049 std::make_pair(yieldOperand, newResultTypes.size()));
1050 dedupResultPositionMap.insert(std::make_pair(result, it.first->second));
1051 if (!it.second)
1052 continue;
1053 newResultTypes.push_back(result.getType());
1054 newYieldValues.push_back(yieldOperand);
1055 }
1056 // No modification, exit early.
1057 if (yield.getNumOperands() == newYieldValues.size())
1058 return failure();
1059 // Move the body of the old warpOp to a new warpOp.
1060 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndReplaceReturns(
1061 rewriter, warpOp, newYieldValues, newResultTypes);
1062
1063 // Simplify the new warp op after dropping dead results.
1064 newWarpOp.getBody()->walk([&](Operation *op) {
1065 if (isOpTriviallyDead(op))
1066 rewriter.eraseOp(op);
1067 });
1068
1069 // Replace results of the old warpOp by the new, deduplicated results.
1070 SmallVector<Value> newValues;
1071 newValues.reserve(warpOp->getNumResults());
1072 for (OpResult result : warpOp.getResults()) {
1073 if (result.use_empty())
1074 newValues.push_back(Value());
1075 else
1076 newValues.push_back(
1077 newWarpOp.getResult(dedupResultPositionMap.lookup(result)));
1078 }
1079 rewriter.replaceOp(warpOp, newValues);
1080 return success();
1081 }
1082};
1083
1084// If an operand is directly yielded out of the region we can forward it
1085// directly and it doesn't need to go through the region.
1086struct WarpOpForwardOperand : public WarpDistributionPattern {
1087 using Base::Base;
1088 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
1089 PatternRewriter &rewriter) const override {
1090 gpu::YieldOp yield = warpOp.getTerminator();
1091 Value valForwarded;
1092 unsigned resultIndex;
1093 for (OpOperand &operand : yield->getOpOperands()) {
1094 Value result = warpOp.getResult(operand.getOperandNumber());
1095 if (result.use_empty())
1096 continue;
1097
1098 // Assume all the values coming from above are uniform.
1099 if (!warpOp.getBodyRegion().isAncestor(operand.get().getParentRegion())) {
1100 if (result.getType() != operand.get().getType())
1101 continue;
1102 valForwarded = operand.get();
1103 resultIndex = operand.getOperandNumber();
1104 break;
1105 }
1106 auto arg = dyn_cast<BlockArgument>(operand.get());
1107 if (!arg || arg.getOwner()->getParentOp() != warpOp.getOperation())
1108 continue;
1109 Value warpOperand = warpOp.getArgs()[arg.getArgNumber()];
1110 if (result.getType() != warpOperand.getType())
1111 continue;
1112 valForwarded = warpOperand;
1113 resultIndex = operand.getOperandNumber();
1114 break;
1115 }
1116 if (!valForwarded)
1117 return failure();
1118 // Notify the rewriter that the warp op is changing (see the comment on
1119 // the WarpOpTransferRead pattern).
1120 rewriter.startOpModification(warpOp);
1121 rewriter.replaceAllUsesWith(warpOp.getResult(resultIndex), valForwarded);
1122 rewriter.finalizeOpModification(warpOp);
1123 return success();
1124 }
1125};
1126
1127struct WarpOpBroadcast : public WarpDistributionPattern {
1128 using Base::Base;
1129 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
1130 PatternRewriter &rewriter) const override {
1131 OpOperand *operand =
1132 getWarpResult(warpOp, llvm::IsaPred<vector::BroadcastOp>);
1133 if (!operand)
1134 return failure();
1135 unsigned int operandNumber = operand->getOperandNumber();
1136 auto broadcastOp = operand->get().getDefiningOp<vector::BroadcastOp>();
1137 Location loc = broadcastOp.getLoc();
1138 auto destVecType =
1139 cast<VectorType>(warpOp->getResultTypes()[operandNumber]);
1140 Value broadcastSrc = broadcastOp.getSource();
1141 Type broadcastSrcType = broadcastSrc.getType();
1142
1143 // Check that the broadcast actually spans a set of values uniformly across
1144 // all threads. In other words, check that each thread can reconstruct
1145 // their own broadcast.
1146 // For that we simply check that the broadcast we want to build makes sense.
1147 if (vector::isBroadcastableTo(broadcastSrcType, destVecType) !=
1148 vector::BroadcastableToResult::Success)
1149 return failure();
1150 SmallVector<size_t> newRetIndices;
1151 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
1152 rewriter, warpOp, {broadcastSrc}, {broadcastSrcType}, newRetIndices);
1153 rewriter.setInsertionPointAfter(newWarpOp);
1154 Value broadcasted = vector::BroadcastOp::create(
1155 rewriter, loc, destVecType, newWarpOp->getResult(newRetIndices[0]));
1156 rewriter.replaceAllUsesWith(newWarpOp->getResult(operandNumber),
1157 broadcasted);
1158 return success();
1159 }
1160};
1161
1162/// Pattern to move shape cast out of the warp op. shape cast is basically a
1163/// no-op for warp distribution; we need to handle the shape though.
1164struct WarpOpShapeCast : public WarpDistributionPattern {
1165 using Base::Base;
1166 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
1167 PatternRewriter &rewriter) const override {
1168 OpOperand *operand =
1169 getWarpResult(warpOp, llvm::IsaPred<vector::ShapeCastOp>);
1170 if (!operand)
1171 return failure();
1172
1173 auto oldCastOp = operand->get().getDefiningOp<vector::ShapeCastOp>();
1174
1175 unsigned int operandNumber = operand->getOperandNumber();
1176 auto castDistributedType =
1177 cast<VectorType>(warpOp->getResultTypes()[operandNumber]);
1178 VectorType castOriginalType = oldCastOp.getSourceVectorType();
1179 VectorType castResultType = castDistributedType;
1180
1181 FailureOr<VectorType> maybeSrcType =
1182 inferDistributedSrcType(castDistributedType, castOriginalType);
1183 if (failed(maybeSrcType))
1184 return failure();
1185 castDistributedType = *maybeSrcType;
1186
1187 SmallVector<size_t> newRetIndices;
1188 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
1189 rewriter, warpOp, {oldCastOp.getSource()}, {castDistributedType},
1190 newRetIndices);
1191 rewriter.setInsertionPointAfter(newWarpOp);
1192 Value newCast = vector::ShapeCastOp::create(
1193 rewriter, oldCastOp.getLoc(), castResultType,
1194 newWarpOp->getResult(newRetIndices[0]));
1195 rewriter.replaceAllUsesWith(newWarpOp->getResult(operandNumber), newCast);
1196 return success();
1197 }
1198
1199private:
1200 static FailureOr<VectorType>
1201 inferDistributedSrcType(VectorType distributedType, VectorType srcType) {
1202 unsigned distributedRank = distributedType.getRank();
1203 unsigned srcRank = srcType.getRank();
1204 if (distributedRank == srcRank)
1205 // Nothing to do.
1206 return distributedType;
1207 if (distributedRank < srcRank) {
1208 // If the distributed type has a smaller rank than the original type,
1209 // prepend with unit dimensions to make the types the same length.
1210 SmallVector<int64_t> shape(srcRank - distributedRank, 1);
1211 llvm::append_range(shape, distributedType.getShape());
1212 return VectorType::get(shape, distributedType.getElementType());
1213 }
1214 // Handle the expanding shape_cast's.
1215 //
1216 // If the casted-from type has one rank, we can assert that the element
1217 // count in that rank will match the full thread-level element count of
1218 // the yielded type.
1219 // Note that getNumElements() will correctly "flatten" the shape of the
1220 // specific shape_cast's distributed type (its distribution may be
1221 // different from the overall warp size, e.g. if the cast is applied to
1222 // a result of a gather).
1223 if (srcRank == 1)
1224 return VectorType::get(distributedType.getNumElements(),
1225 srcType.getElementType());
1226 // Try to strip leading unit dimensions to match the ranks. We bail out
1227 // for more complex tile sizes, because those would require us to
1228 // determine the specific distribution parameters to threads, which is
1229 // unfeasible within this pattern.
1230 unsigned excessDims = distributedRank - srcRank;
1231 ArrayRef<int64_t> shape = distributedType.getShape();
1232 if (!llvm::all_of(shape.take_front(excessDims),
1233 [](int64_t d) { return d == 1; }))
1234 return failure();
1235 return VectorType::get(shape.drop_front(excessDims),
1236 distributedType.getElementType());
1237 }
1238};
1239
1240/// Sink out vector.create_mask / vector.constant_mask op feeding into a warp op
1241/// yield.
1242/// ```
1243/// %0 = ...
1244/// %1 = gpu.warp_execute_on_lane_0(%arg0) -> (vector<1xf32>) {
1245/// ...
1246/// %mask = vector.create_mask %0 : vector<32xi1>
1247/// // or %mask = vector.constant_mask[2] : vector<32xi1>
1248/// gpu.yield %mask : vector<32xi1>
1249/// }
1250/// ```
1251/// To
1252/// ```
1253/// %0 = ...
1254/// gpu.warp_execute_on_lane_0(%arg0) {
1255/// ...
1256/// }
1257/// %cmp = arith.cmpi ult, %laneid, %0
1258/// %ub = arith.select %cmp, %c0, %c1
1259/// %1 = vector.create_mask %ub : vector<1xi1>
1260template <typename OpType,
1261 typename = std::enable_if_t<llvm::is_one_of<
1262 OpType, vector::CreateMaskOp, vector::ConstantMaskOp>::value>>
1263struct WarpOpCreateMask : public WarpDistributionPattern {
1264 using Base::Base;
1265 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
1266 PatternRewriter &rewriter) const override {
1267 OpOperand *yieldOperand = getWarpResult(warpOp, (llvm::IsaPred<OpType>));
1268 if (!yieldOperand)
1269 return failure();
1270
1271 Operation *mask = yieldOperand->get().getDefiningOp<OpType>();
1272
1273 // Early exit if any values needed for calculating the new mask indices
1274 // are defined inside the warp op.
1275 if (mask->getOperands().size() &&
1276 !llvm::all_of(mask->getOperands(), [&](Value value) {
1277 return warpOp.isDefinedOutsideOfRegion(value);
1278 }))
1279 return failure();
1280
1281 Location loc = mask->getLoc();
1282 unsigned operandIndex = yieldOperand->getOperandNumber();
1283
1284 auto distType = cast<VectorType>(warpOp.getResult(operandIndex).getType());
1285 VectorType seqType = cast<VectorType>(mask->getResult(0).getType());
1286 ArrayRef<int64_t> seqShape = seqType.getShape();
1287 ArrayRef<int64_t> distShape = distType.getShape();
1288 SmallVector<Value> materializedOperands;
1289 if constexpr (std::is_same_v<OpType, vector::CreateMaskOp>) {
1290 materializedOperands.append(mask->getOperands().begin(),
1291 mask->getOperands().end());
1292 } else {
1293 auto constantMaskOp = cast<vector::ConstantMaskOp>(mask);
1294 auto dimSizes = constantMaskOp.getMaskDimSizesAttr().asArrayRef();
1295 for (auto dimSize : dimSizes)
1296 materializedOperands.push_back(
1297 arith::ConstantIndexOp::create(rewriter, loc, dimSize).getResult());
1298 }
1299
1300 rewriter.setInsertionPointAfter(warpOp);
1301
1302 // Delinearize the lane ID for constructing the distributed mask sizes.
1303 SmallVector<Value> delinearizedIds;
1304 if (!delinearizeLaneId(rewriter, loc, seqShape, distShape,
1305 warpOp.getWarpSize(), warpOp.getLaneid(),
1306 delinearizedIds))
1307 return rewriter.notifyMatchFailure(
1308 mask, "cannot delinearize lane ID for distribution");
1309 assert(!delinearizedIds.empty());
1310
1311 // Notify the rewriter that the warp op is changing (see the comment on
1312 // the WarpOpTransferRead pattern).
1313 rewriter.startOpModification(warpOp);
1314
1315 AffineExpr s0, s1;
1316 bindSymbols(rewriter.getContext(), s0, s1);
1317 SmallVector<Value> newOperands;
1318 for (int i = 0, e = distShape.size(); i < e; ++i) {
1319 // Get `mask_dim_range_upper_limit[i] - lane_id[i] * dist_sizes[i]` to
1320 // find the distance from the largest mask index owned by this lane to the
1321 // original mask size. `vector.create_mask` implicitly clamps mask
1322 // operands to the range [0, mask_vector_size[i]], or in other words, the
1323 // mask sizes are always in the range [0, mask_vector_size[i]).
1324 Value maskDimIdx = affine::makeComposedAffineApply(
1325 rewriter, loc, s1 - s0 * distShape[i],
1326 {delinearizedIds[i], materializedOperands[i]});
1327 newOperands.push_back(maskDimIdx);
1328 }
1329
1330 auto newMask =
1331 vector::CreateMaskOp::create(rewriter, loc, distType, newOperands);
1332 rewriter.replaceAllUsesWith(warpOp.getResult(operandIndex), newMask);
1333 rewriter.finalizeOpModification(warpOp);
1334 return success();
1335 }
1336};
1337
1338/// Sink out insert_strided_slice op feeding into a warp op yield.
1339/// ```
1340/// %0 = gpu.warp_execute_on_lane_0(%arg0) -> (vector<8x1xf32>) {
1341/// ...
1342/// %src = ... : vector<4x32xf32>
1343/// %dest = ... : vector<8x32xf32>
1344/// %insert = vector.insert_strided_slice %src, %dest, offsets = [0, 0],
1345/// strides = [1, 1] : vector<4x32xf32> into vector<8x32xf32>
1346/// gpu.yield %insert : vector<8x32xf32>
1347/// }
1348/// ```
1349/// To
1350/// ```
1351/// %0 = gpu.warp_execute_on_lane_0(%arg0) -> (vector<4x1xf32>,
1352/// vector<8x1xf32>) {
1353/// ...
1354/// %src = ... : vector<4x32xf32>
1355/// %dest = ... : vector<8x32xf32>
1356/// gpu.yield %src, %dest : vector<4x16xf32>, vector<8x16xf32>
1357/// }
1358/// %insert = vector.insert_strided_slice %0#0, %0#1,
1359/// offsets = [0, 0], strides = [1, 1] : vector<4x1xf32> into vector<8x1xf32>
1360/// ```
1361/// NOTE: Current support assumes that both src and dest vectors are distributed
1362/// to lanes and sinking the insert op does not require any cross lane
1363/// communication.
1364struct WarpOpInsertStridedSlice : public WarpDistributionPattern {
1365 using Base::Base;
1366 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
1367 PatternRewriter &rewriter) const override {
1368 OpOperand *operand =
1369 getWarpResult(warpOp, llvm::IsaPred<vector::InsertStridedSliceOp>);
1370 if (!operand)
1371 return failure();
1372 unsigned int operandNumber = operand->getOperandNumber();
1373 auto insertOp =
1374 operand->get().getDefiningOp<vector::InsertStridedSliceOp>();
1375 auto distributedType =
1376 cast<VectorType>(warpOp.getResult(operandNumber).getType());
1377 // Distributed type must be 2D or higher.
1378 // TODO: Support 1D distributed types.
1379 if (distributedType.getRank() < 2)
1380 return rewriter.notifyMatchFailure(
1381 insertOp, "result vector type must be 2D or higher");
1382 // Find the distributed dimension of the dest vector. There should be
1383 // exactly one.
1384 auto yieldedType = cast<VectorType>(operand->get().getType());
1385 int64_t destDistributedDim =
1386 getDistributedDim(yieldedType, distributedType);
1387 assert(destDistributedDim != -1 && "could not find distributed dimension");
1388
1389 VectorType srcType = insertOp.getSourceVectorType();
1390 VectorType destType = insertOp.getDestVectorType();
1391 // Currently we require that both source (kD) and dest (nD) vectors are
1392 // distributed. This requires that distributedDim (d) is contained in the
1393 // last k dims of the dest vector (d >= n - k).
1394 // TODO: Add support for case where source vector is not distributed.
1395 int64_t sourceDistributedDim =
1396 destDistributedDim - (destType.getRank() - srcType.getRank());
1397 if (sourceDistributedDim < 0)
1398 return rewriter.notifyMatchFailure(
1399 insertOp,
1400 "distributed dimension must be in the last k dims of dest vector");
1401 // Distributed dimension must be fully inserted.
1402 if (srcType.getDimSize(sourceDistributedDim) !=
1403 destType.getDimSize(destDistributedDim))
1404 return rewriter.notifyMatchFailure(
1405 insertOp, "distributed dimension must be fully inserted");
1406 SmallVector<int64_t> newSourceDistShape(
1407 insertOp.getSourceVectorType().getShape());
1408 newSourceDistShape[sourceDistributedDim] =
1409 distributedType.getDimSize(destDistributedDim);
1410 auto newSourceTy =
1411 VectorType::get(newSourceDistShape, distributedType.getElementType());
1412 VectorType newDestTy = distributedType;
1413 SmallVector<size_t> newRetIndices;
1414 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
1415 rewriter, warpOp, {insertOp.getValueToStore(), insertOp.getDest()},
1416 {newSourceTy, newDestTy}, newRetIndices);
1417 rewriter.setInsertionPointAfter(newWarpOp);
1418 Value distributedSource = newWarpOp->getResult(newRetIndices[0]);
1419 Value distributedDest = newWarpOp->getResult(newRetIndices[1]);
1420 // Create a new insert strided slice op that inserts distributed source into
1421 // distributed dest.
1422 Value newInsert = vector::InsertStridedSliceOp::create(
1423 rewriter, insertOp.getLoc(), distributedDest.getType(),
1424 distributedSource, distributedDest, insertOp.getOffsets(),
1425 insertOp.getStrides());
1426 rewriter.replaceAllUsesWith(newWarpOp->getResult(operandNumber), newInsert);
1427 return success();
1428 }
1429};
1430
1431/// Sink out extract_strided_slice op feeding into a warp op yield.
1432/// ```
1433/// %0 = gpu.warp_execute_on_lane_0(%arg0) -> (vector<16x1xf32>) {
1434/// ...
1435/// %src = ... : vector<64x32xf32>
1436/// %extract = vector.extract_strided_slice %src, offsets = [0], sizes = [16],
1437/// strides = [1] : vector<64x32xf32> to vector<16x32xf32>
1438/// gpu.yield %extract : vector<16x32xf32>
1439/// }
1440/// ```
1441/// To
1442/// ```
1443/// %0 = gpu.warp_execute_on_lane_0(%arg0) -> (vector<64x1xf32>) {
1444/// ...
1445/// %src = ... : vector<64x32xf32>
1446/// gpu.yield %src : vector<64x32xf32>
1447/// }
1448/// %extract = vector.extract_strided_slice %0, offsets = [0], sizes = [16],
1449/// strides = [1] : vector<64x1xf32> to vector<16x1xf32>
1450/// ```
1451/// NOTE: Current support assumes that the extraction happens only on non
1452/// distributed dimensions (does not require cross lane communication).
1453struct WarpOpExtractStridedSlice : public WarpDistributionPattern {
1454 using Base::Base;
1455 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
1456 PatternRewriter &rewriter) const override {
1457 OpOperand *operand =
1458 getWarpResult(warpOp, llvm::IsaPred<vector::ExtractStridedSliceOp>);
1459 if (!operand)
1460 return failure();
1461 unsigned int operandNumber = operand->getOperandNumber();
1462 auto extractOp =
1463 operand->get().getDefiningOp<vector::ExtractStridedSliceOp>();
1464 auto distributedType =
1465 cast<VectorType>(warpOp.getResult(operandNumber).getType());
1466 // Distributed type must be 2D or higher.
1467 // TODO: Support 1D distributed types.
1468 if (distributedType.getRank() < 2)
1469 return rewriter.notifyMatchFailure(
1470 extractOp, "result vector type must be 2D or higher");
1471
1472 // Find the distributed dimension. There should be exactly one.
1473 auto yieldedType = cast<VectorType>(operand->get().getType());
1474 int64_t distributedDim = getDistributedDim(yieldedType, distributedType);
1475 assert(distributedDim != -1 && "could not find distributed dimension");
1476
1477 int64_t numOfExtractedDims =
1478 static_cast<int64_t>(extractOp.getSizes().size());
1479 // If the distributed dim is included in the extracted dims, then we make
1480 // sure distributed dim is fully extracted. If distributed dim is not
1481 // included in extracted dims, it is guaranteed to be fully extracted (i.e.
1482 // distributed dim comes after all the extracted dims)
1483 // TODO: Partial extraction from distributed dimension require cross lane
1484 // communication.
1485 if (distributedDim < numOfExtractedDims) {
1486 int64_t distributedDimOffset =
1487 llvm::cast<IntegerAttr>(extractOp.getOffsets()[distributedDim])
1488 .getInt();
1489 int64_t distributedDimSize =
1490 llvm::cast<IntegerAttr>(extractOp.getSizes()[distributedDim])
1491 .getInt();
1492 if (distributedDimOffset != 0 ||
1493 distributedDimSize != yieldedType.getDimSize(distributedDim))
1494 return rewriter.notifyMatchFailure(
1495 extractOp, "distributed dimension must be fully extracted");
1496 }
1497 SmallVector<int64_t> newDistributedShape(
1498 extractOp.getSourceVectorType().getShape());
1499 newDistributedShape[distributedDim] =
1500 distributedType.getDimSize(distributedDim);
1501 auto newDistributedType =
1502 VectorType::get(newDistributedShape, distributedType.getElementType());
1503 SmallVector<size_t> newRetIndices;
1504 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
1505 rewriter, warpOp, {extractOp.getSource()}, {newDistributedType},
1506 newRetIndices);
1507 rewriter.setInsertionPointAfter(newWarpOp);
1508 SmallVector<Attribute> distributedSizes = llvm::map_to_vector(
1509 extractOp.getSizes(), [](Attribute attr) { return attr; });
1510 // Update the distributed sizes to match the distributed type.
1511 if (distributedDim < static_cast<int64_t>(distributedSizes.size()))
1512 distributedSizes[distributedDim] = rewriter.getI64IntegerAttr(
1513 distributedType.getDimSize(distributedDim));
1514
1515 // Create a new extract strided slice op that extracts from the
1516 // distributed vector.
1517 Value distributedVec = newWarpOp->getResult(newRetIndices[0]);
1518 Value newExtract = vector::ExtractStridedSliceOp::create(
1519 rewriter, extractOp.getLoc(), distributedType, distributedVec,
1520 extractOp.getOffsets(),
1521 ArrayAttr::get(rewriter.getContext(), distributedSizes),
1522 extractOp.getStrides());
1523 rewriter.replaceAllUsesWith(newWarpOp->getResult(operandNumber),
1524 newExtract);
1525 return success();
1526 }
1527};
1528
1529/// Pattern to move out vector.extract of single element vector. Those don't
1530/// need to be distributed and can just be propagated outside of the region.
1531struct WarpOpExtract : public WarpDistributionPattern {
1532 using Base::Base;
1533 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
1534 PatternRewriter &rewriter) const override {
1535 OpOperand *operand =
1536 getWarpResult(warpOp, llvm::IsaPred<vector::ExtractOp>);
1537 if (!operand)
1538 return failure();
1539 unsigned int operandNumber = operand->getOperandNumber();
1540 auto extractOp = operand->get().getDefiningOp<vector::ExtractOp>();
1541 VectorType extractSrcType = extractOp.getSourceVectorType();
1542 Location loc = extractOp.getLoc();
1543
1544 // For 1-d or 0-d source cases, we rely on WarpOpExtractScalar pattern.
1545 if (extractSrcType.getRank() <= 1) {
1546 return failure();
1547 }
1548
1549 // All following cases are 2d or higher dimensional source vectors.
1550
1551 if (warpOp.getResult(operandNumber).getType() == operand->get().getType()) {
1552 // There is no distribution, this is a broadcast. Simply move the extract
1553 // out of the warp op.
1554 // TODO: This could be optimized. E.g., in case of a scalar result, let
1555 // one lane extract and shuffle the result to all other lanes (same as
1556 // the 1d case).
1557 SmallVector<size_t> newRetIndices;
1558 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
1559 rewriter, warpOp, {extractOp.getSource()},
1560 {extractOp.getSourceVectorType()}, newRetIndices);
1561 rewriter.setInsertionPointAfter(newWarpOp);
1562 Value distributedVec = newWarpOp->getResult(newRetIndices[0]);
1563 // Extract from distributed vector.
1564 Value newExtract = vector::ExtractOp::create(
1565 rewriter, loc, distributedVec, extractOp.getMixedPosition());
1566 rewriter.replaceAllUsesWith(newWarpOp->getResult(operandNumber),
1567 newExtract);
1568 return success();
1569 }
1570
1571 // Find the distributed dimension. There should be exactly one.
1572 auto distributedType =
1573 cast<VectorType>(warpOp.getResult(operandNumber).getType());
1574 auto yieldedType = cast<VectorType>(operand->get().getType());
1575 int64_t distributedDim = getDistributedDim(yieldedType, distributedType);
1576 assert(distributedDim != -1 && "could not find distributed dimension");
1577 (void)distributedDim;
1578
1579 // Yield source vector from warp op.
1580 SmallVector<int64_t> newDistributedShape(extractSrcType.getShape());
1581 for (int i = 0; i < distributedType.getRank(); ++i)
1582 newDistributedShape[i + extractOp.getNumIndices()] =
1583 distributedType.getDimSize(i);
1584 auto newDistributedType =
1585 VectorType::get(newDistributedShape, distributedType.getElementType());
1586 SmallVector<size_t> newRetIndices;
1587 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
1588 rewriter, warpOp, {extractOp.getSource()}, {newDistributedType},
1589 newRetIndices);
1590 rewriter.setInsertionPointAfter(newWarpOp);
1591 Value distributedVec = newWarpOp->getResult(newRetIndices[0]);
1592 // Extract from distributed vector.
1593 Value newExtract = vector::ExtractOp::create(rewriter, loc, distributedVec,
1594 extractOp.getMixedPosition());
1595 rewriter.replaceAllUsesWith(newWarpOp->getResult(operandNumber),
1596 newExtract);
1597 return success();
1598 }
1599};
1600
1601/// Pattern to move out vector.extract with a scalar result.
1602/// Only supports 1-D and 0-D sources for now.
1603struct WarpOpExtractScalar : public WarpDistributionPattern {
1604 WarpOpExtractScalar(MLIRContext *ctx, WarpShuffleFromIdxFn fn,
1605 PatternBenefit b = 1)
1606 : WarpDistributionPattern(ctx, b), warpShuffleFromIdxFn(std::move(fn)) {}
1607 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
1608 PatternRewriter &rewriter) const override {
1609 OpOperand *operand =
1610 getWarpResult(warpOp, llvm::IsaPred<vector::ExtractOp>);
1611 if (!operand)
1612 return failure();
1613 unsigned int operandNumber = operand->getOperandNumber();
1614 auto extractOp = operand->get().getDefiningOp<vector::ExtractOp>();
1615 VectorType extractSrcType = extractOp.getSourceVectorType();
1616 // Only supports 1-D or 0-D sources for now.
1617 if (extractSrcType.getRank() > 1) {
1618 return rewriter.notifyMatchFailure(
1619 extractOp, "only 0-D or 1-D source supported for now");
1620 }
1621 // TODO: Supported shuffle types should be parameterizable, similar to
1622 // `WarpShuffleFromIdxFn`.
1623 if (!extractSrcType.getElementType().isF32() &&
1624 !extractSrcType.getElementType().isInteger(32))
1625 return rewriter.notifyMatchFailure(
1626 extractOp, "only f32/i32 element types are supported");
1627 bool is0dOrVec1Extract = extractSrcType.getNumElements() == 1;
1628 Type elType = extractSrcType.getElementType();
1629 VectorType distributedVecType;
1630 if (!is0dOrVec1Extract) {
1631 assert(extractSrcType.getRank() == 1 &&
1632 "expected that extract src rank is 0 or 1");
1633 if (extractSrcType.getShape()[0] % warpOp.getWarpSize() != 0)
1634 return failure();
1635 int64_t elementsPerLane =
1636 extractSrcType.getShape()[0] / warpOp.getWarpSize();
1637 distributedVecType = VectorType::get({elementsPerLane}, elType);
1638 } else {
1639 distributedVecType = extractSrcType;
1640 }
1641 // Yield source vector and position (if present) from warp op.
1642 SmallVector<Value> additionalResults{extractOp.getSource()};
1643 SmallVector<Type> additionalResultTypes{distributedVecType};
1644 additionalResults.append(
1645 SmallVector<Value>(extractOp.getDynamicPosition()));
1646 additionalResultTypes.append(
1647 SmallVector<Type>(extractOp.getDynamicPosition().getTypes()));
1648
1649 Location loc = extractOp.getLoc();
1650 SmallVector<size_t> newRetIndices;
1651 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
1652 rewriter, warpOp, additionalResults, additionalResultTypes,
1653 newRetIndices);
1654 rewriter.setInsertionPointAfter(newWarpOp);
1655 Value distributedVec = newWarpOp->getResult(newRetIndices[0]);
1656
1657 // 0d extract: The new warp op broadcasts the source vector to all lanes.
1658 // All lanes extract the scalar.
1659 if (is0dOrVec1Extract) {
1660 Value newExtract;
1661 SmallVector<int64_t> indices(extractSrcType.getRank(), 0);
1662 newExtract =
1663 vector::ExtractOp::create(rewriter, loc, distributedVec, indices);
1664 rewriter.replaceAllUsesWith(newWarpOp->getResult(operandNumber),
1665 newExtract);
1666 return success();
1667 }
1668
1669 int64_t staticPos = extractOp.getStaticPosition()[0];
1670 OpFoldResult pos = ShapedType::isDynamic(staticPos)
1671 ? (newWarpOp->getResult(newRetIndices[1]))
1672 : OpFoldResult(rewriter.getIndexAttr(staticPos));
1673 // 1d extract: Distribute the source vector. One lane extracts and shuffles
1674 // the value to all other lanes.
1675 int64_t elementsPerLane = distributedVecType.getShape()[0];
1676 AffineExpr sym0 = getAffineSymbolExpr(0, rewriter.getContext());
1677 // tid of extracting thread: pos / elementsPerLane
1678 Value broadcastFromTid = affine::makeComposedAffineApply(
1679 rewriter, loc, sym0.ceilDiv(elementsPerLane), pos);
1680 // Extract at position: pos % elementsPerLane
1681 Value newPos =
1682 elementsPerLane == 1
1683 ? arith::ConstantIndexOp::create(rewriter, loc, 0).getResult()
1684 : affine::makeComposedAffineApply(rewriter, loc,
1685 sym0 % elementsPerLane, pos);
1686 Value extracted =
1687 vector::ExtractOp::create(rewriter, loc, distributedVec, newPos);
1688
1689 // Shuffle the extracted value to all lanes.
1690 Value shuffled = warpShuffleFromIdxFn(
1691 loc, rewriter, extracted, broadcastFromTid, newWarpOp.getWarpSize());
1692 rewriter.replaceAllUsesWith(newWarpOp->getResult(operandNumber), shuffled);
1693 return success();
1694 }
1695
1696private:
1697 WarpShuffleFromIdxFn warpShuffleFromIdxFn;
1698};
1699
1700/// Pattern to move out vector.insert with a scalar input.
1701/// Only supports 1-D and 0-D destinations for now.
1702struct WarpOpInsertScalar : public WarpDistributionPattern {
1703 using Base::Base;
1704 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
1705 PatternRewriter &rewriter) const override {
1706 OpOperand *operand = getWarpResult(warpOp, llvm::IsaPred<vector::InsertOp>);
1707 if (!operand)
1708 return failure();
1709 unsigned int operandNumber = operand->getOperandNumber();
1710 auto insertOp = operand->get().getDefiningOp<vector::InsertOp>();
1711 VectorType vecType = insertOp.getDestVectorType();
1712 VectorType distrType =
1713 cast<VectorType>(warpOp.getResult(operandNumber).getType());
1714
1715 // Only supports 1-D or 0-D destinations for now.
1716 if (vecType.getRank() > 1) {
1717 return rewriter.notifyMatchFailure(
1718 insertOp, "only 0-D or 1-D source supported for now");
1719 }
1720
1721 // Yield destination vector, source scalar and position from warp op.
1722 SmallVector<Value> additionalResults{insertOp.getDest(),
1723 insertOp.getValueToStore()};
1724 SmallVector<Type> additionalResultTypes{
1725 distrType, insertOp.getValueToStore().getType()};
1726 additionalResults.append(SmallVector<Value>(insertOp.getDynamicPosition()));
1727 additionalResultTypes.append(
1728 SmallVector<Type>(insertOp.getDynamicPosition().getTypes()));
1729
1730 Location loc = insertOp.getLoc();
1731 SmallVector<size_t> newRetIndices;
1732 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
1733 rewriter, warpOp, additionalResults, additionalResultTypes,
1734 newRetIndices);
1735 rewriter.setInsertionPointAfter(newWarpOp);
1736 Value distributedVec = newWarpOp->getResult(newRetIndices[0]);
1737 Value newSource = newWarpOp->getResult(newRetIndices[1]);
1738 rewriter.setInsertionPointAfter(newWarpOp);
1739
1740 OpFoldResult pos;
1741 if (vecType.getRank() != 0) {
1742 int64_t staticPos = insertOp.getStaticPosition()[0];
1743 pos = ShapedType::isDynamic(staticPos)
1744 ? (newWarpOp->getResult(newRetIndices[2]))
1745 : OpFoldResult(rewriter.getIndexAttr(staticPos));
1746 }
1747
1748 // This condition is always true for 0-d vectors.
1749 if (vecType == distrType) {
1750 Value newInsert;
1751 SmallVector<OpFoldResult> indices;
1752 if (pos) {
1753 indices.push_back(pos);
1754 }
1755 newInsert = vector::InsertOp::create(rewriter, loc, newSource,
1756 distributedVec, indices);
1757 // Broadcast: Simply move the vector.insert op out.
1758 rewriter.replaceAllUsesWith(newWarpOp->getResult(operandNumber),
1759 newInsert);
1760 return success();
1761 }
1762
1763 // This is a distribution. Only one lane should insert.
1764 int64_t elementsPerLane = distrType.getShape()[0];
1765 AffineExpr sym0 = getAffineSymbolExpr(0, rewriter.getContext());
1766 // tid of extracting thread: pos / elementsPerLane
1767 Value insertingLane = affine::makeComposedAffineApply(
1768 rewriter, loc, sym0.ceilDiv(elementsPerLane), pos);
1769 // Insert position: pos % elementsPerLane
1770 OpFoldResult newPos = affine::makeComposedFoldedAffineApply(
1771 rewriter, loc, sym0 % elementsPerLane, pos);
1772 Value isInsertingLane =
1773 arith::CmpIOp::create(rewriter, loc, arith::CmpIPredicate::eq,
1774 newWarpOp.getLaneid(), insertingLane);
1775 Value newResult =
1776 scf::IfOp::create(
1777 rewriter, loc, isInsertingLane,
1778 /*thenBuilder=*/
1779 [&](OpBuilder &builder, Location loc) {
1780 Value newInsert = vector::InsertOp::create(
1781 builder, loc, newSource, distributedVec, newPos);
1782 scf::YieldOp::create(builder, loc, newInsert);
1783 },
1784 /*elseBuilder=*/
1785 [&](OpBuilder &builder, Location loc) {
1786 scf::YieldOp::create(builder, loc, distributedVec);
1787 })
1788 .getResult(0);
1789 rewriter.replaceAllUsesWith(newWarpOp->getResult(operandNumber), newResult);
1790 return success();
1791 }
1792};
1793
1794struct WarpOpInsert : public WarpDistributionPattern {
1795 using Base::Base;
1796 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
1797 PatternRewriter &rewriter) const override {
1798 OpOperand *operand = getWarpResult(warpOp, llvm::IsaPred<vector::InsertOp>);
1799 if (!operand)
1800 return failure();
1801 unsigned int operandNumber = operand->getOperandNumber();
1802 auto insertOp = operand->get().getDefiningOp<vector::InsertOp>();
1803 Location loc = insertOp.getLoc();
1804
1805 // For 1-d or 0-d destination cases, we rely on WarpOpInsertScalar pattern.
1806 if (insertOp.getDestVectorType().getRank() <= 1) {
1807 return failure();
1808 }
1809
1810 // All following cases are 2d or higher dimensional source vectors.
1811
1812 if (warpOp.getResult(operandNumber).getType() == operand->get().getType()) {
1813 // There is no distribution, this is a broadcast. Simply move the insert
1814 // out of the warp op.
1815 SmallVector<size_t> newRetIndices;
1816 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
1817 rewriter, warpOp, {insertOp.getValueToStore(), insertOp.getDest()},
1818 {insertOp.getValueToStoreType(), insertOp.getDestVectorType()},
1819 newRetIndices);
1820 rewriter.setInsertionPointAfter(newWarpOp);
1821 Value distributedSrc = newWarpOp->getResult(newRetIndices[0]);
1822 Value distributedDest = newWarpOp->getResult(newRetIndices[1]);
1823 Value newResult = vector::InsertOp::create(rewriter, loc, distributedSrc,
1824 distributedDest,
1825 insertOp.getMixedPosition());
1826 rewriter.replaceAllUsesWith(newWarpOp->getResult(operandNumber),
1827 newResult);
1828 return success();
1829 }
1830
1831 // Find the distributed dimension. There should be exactly one.
1832 auto distrDestType =
1833 cast<VectorType>(warpOp.getResult(operandNumber).getType());
1834 auto yieldedType = cast<VectorType>(operand->get().getType());
1835 int64_t distrDestDim = -1;
1836 for (int64_t i = 0; i < yieldedType.getRank(); ++i) {
1837 if (distrDestType.getDimSize(i) != yieldedType.getDimSize(i)) {
1838 // Keep this assert here in case WarpExecuteOnLane0Op gets extended to
1839 // support distributing multiple dimensions in the future.
1840 assert(distrDestDim == -1 && "found multiple distributed dims");
1841 distrDestDim = i;
1842 }
1843 }
1844 assert(distrDestDim != -1 && "could not find distributed dimension");
1845
1846 // Compute the distributed source vector type.
1847 VectorType srcVecType = cast<VectorType>(insertOp.getValueToStoreType());
1848 SmallVector<int64_t> distrSrcShape(srcVecType.getShape());
1849 // E.g.: vector.insert %s, %d [2] : vector<96xf32> into vector<128x96xf32>
1850 // Case 1: distrDestDim = 1 (dim of size 96). In that case, each lane will
1851 // insert a smaller vector<3xf32>.
1852 // Case 2: distrDestDim = 0 (dim of size 128) => distrSrcDim = -1. In that
1853 // case, one lane will insert the source vector<96xf32>. The other
1854 // lanes will not do anything.
1855 int64_t distrSrcDim = distrDestDim - insertOp.getNumIndices();
1856 if (distrSrcDim >= 0)
1857 distrSrcShape[distrSrcDim] = distrDestType.getDimSize(distrDestDim);
1858 auto distrSrcType =
1859 VectorType::get(distrSrcShape, distrDestType.getElementType());
1860
1861 // Yield source and dest vectors from warp op.
1862 SmallVector<size_t> newRetIndices;
1863 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
1864 rewriter, warpOp, {insertOp.getValueToStore(), insertOp.getDest()},
1865 {distrSrcType, distrDestType}, newRetIndices);
1866 rewriter.setInsertionPointAfter(newWarpOp);
1867 Value distributedSrc = newWarpOp->getResult(newRetIndices[0]);
1868 Value distributedDest = newWarpOp->getResult(newRetIndices[1]);
1869
1870 // Insert into the distributed vector.
1871 Value newResult;
1872 if (distrSrcDim >= 0) {
1873 // Every lane inserts a small piece.
1874 newResult = vector::InsertOp::create(rewriter, loc, distributedSrc,
1875 distributedDest,
1876 insertOp.getMixedPosition());
1877 } else {
1878 // One lane inserts the entire source vector.
1879 int64_t elementsPerLane = distrDestType.getDimSize(distrDestDim);
1880 SmallVector<OpFoldResult> pos = insertOp.getMixedPosition();
1881 SmallVector<int64_t> newPos = getAsIntegers(pos);
1882 // tid of inserting lane: pos / elementsPerLane
1883 Value insertingLane = arith::ConstantIndexOp::create(
1884 rewriter, loc, newPos[distrDestDim] / elementsPerLane);
1885 Value isInsertingLane =
1886 arith::CmpIOp::create(rewriter, loc, arith::CmpIPredicate::eq,
1887 newWarpOp.getLaneid(), insertingLane);
1888 // Insert position: pos % elementsPerLane
1889 newPos[distrDestDim] %= elementsPerLane;
1890 auto insertingBuilder = [&](OpBuilder &builder, Location loc) {
1891 Value newInsert = vector::InsertOp::create(builder, loc, distributedSrc,
1892 distributedDest, newPos);
1893 scf::YieldOp::create(builder, loc, newInsert);
1894 };
1895 auto nonInsertingBuilder = [&](OpBuilder &builder, Location loc) {
1896 scf::YieldOp::create(builder, loc, distributedDest);
1897 };
1898 newResult = scf::IfOp::create(rewriter, loc, isInsertingLane,
1899 /*thenBuilder=*/insertingBuilder,
1900 /*elseBuilder=*/nonInsertingBuilder)
1901 .getResult(0);
1902 }
1903
1904 rewriter.replaceAllUsesWith(newWarpOp->getResult(operandNumber), newResult);
1905 return success();
1906 }
1907};
1908
1909/// Sink scf.if out of WarpExecuteOnLane0Op. This can be done only if
1910/// the scf.if is the last operation in the region so that it doesn't
1911/// change the order of execution. This creates a new scf.if after the
1912/// WarpExecuteOnLane0Op. Each branch of the new scf.if is enclosed in
1913/// the "inner" WarpExecuteOnLane0Op. Example:
1914/// ```
1915/// gpu.warp_execute_on_lane_0(%laneid)[32] {
1916/// %payload = ... : vector<32xindex>
1917/// scf.if %pred {
1918/// vector.store %payload, %buffer[%idx] : memref<128xindex>,
1919/// vector<32xindex>
1920/// }
1921/// gpu.yield
1922/// }
1923/// ```
1924/// %r = gpu.warp_execute_on_lane_0(%laneid)[32] {
1925/// %payload = ... : vector<32xindex>
1926/// gpu.yield %payload : vector<32xindex>
1927/// }
1928/// scf.if %pred {
1929/// gpu.warp_execute_on_lane_0(%laneid)[32] args(%r : vector<1xindex>) {
1930/// ^bb0(%arg1: vector<32xindex>):
1931/// vector.store %arg1, %buffer[%idx] : memref<128xindex>, vector<32xindex>
1932/// }
1933/// }
1934/// ```
1935struct WarpOpScfIfOp : public WarpDistributionPattern {
1936 WarpOpScfIfOp(MLIRContext *ctx, DistributionMapFn fn, PatternBenefit b = 1)
1937 : WarpDistributionPattern(ctx, b), distributionMapFn(std::move(fn)) {}
1938 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
1939 PatternRewriter &rewriter) const override {
1940 gpu::YieldOp warpOpYield = warpOp.getTerminator();
1941 // Only pick up `IfOp` if it is the last op in the region.
1942 Operation *lastNode = warpOpYield->getPrevNode();
1943 auto ifOp = dyn_cast_or_null<scf::IfOp>(lastNode);
1944 if (!ifOp)
1945 return failure();
1946
1947 // The current `WarpOp` can yield two types of values:
1948 // 1. Not results of `IfOp`:
1949 // Preserve them in the new `WarpOp`.
1950 // Collect their yield index to remap the usages.
1951 // 2. Results of `IfOp`:
1952 // They are not part of the new `WarpOp` results.
1953 // Map current warp's yield operand index to `IfOp` result idx.
1954 SmallVector<Value> nonIfYieldValues;
1955 SmallVector<unsigned> nonIfYieldIndices;
1956 llvm::SmallDenseMap<unsigned, unsigned> ifResultMapping;
1957 llvm::SmallDenseMap<unsigned, VectorType> ifResultDistTypes;
1958 for (OpOperand &yieldOperand : warpOpYield->getOpOperands()) {
1959 const unsigned yieldOperandIdx = yieldOperand.getOperandNumber();
1960 if (yieldOperand.get().getDefiningOp() != ifOp.getOperation()) {
1961 nonIfYieldValues.push_back(yieldOperand.get());
1962 nonIfYieldIndices.push_back(yieldOperandIdx);
1963 continue;
1964 }
1965 OpResult ifResult = cast<OpResult>(yieldOperand.get());
1966 const unsigned ifResultIdx = ifResult.getResultNumber();
1967 ifResultMapping[yieldOperandIdx] = ifResultIdx;
1968 // If this `ifOp` result is vector type and it is yielded by the
1969 // `WarpOp`, we keep track the distributed type for this result.
1970 if (!isa<VectorType>(ifResult.getType()))
1971 continue;
1972 VectorType distType =
1973 cast<VectorType>(warpOp.getResult(yieldOperandIdx).getType());
1974 ifResultDistTypes[ifResultIdx] = distType;
1975 }
1976
1977 // Collect `WarpOp`-defined values used in `ifOp`, the new warp op returns
1978 // them
1979 auto [escapingValuesThen, escapingValueInputTypesThen,
1980 escapingValueDistTypesThen] =
1981 getInnerRegionEscapingValues(warpOp, ifOp.getThenRegion(),
1982 distributionMapFn);
1983 auto [escapingValuesElse, escapingValueInputTypesElse,
1984 escapingValueDistTypesElse] =
1985 getInnerRegionEscapingValues(warpOp, ifOp.getElseRegion(),
1986 distributionMapFn);
1987 if (llvm::is_contained(escapingValueDistTypesThen, Type{}) ||
1988 llvm::is_contained(escapingValueDistTypesElse, Type{}))
1989 return failure();
1990
1991 // The new `WarpOp` groups yields values in following order:
1992 // 1. Branch condition
1993 // 2. Escaping values then branch
1994 // 3. Escaping values else branch
1995 // 4. All non-`ifOp` yielded values.
1996 SmallVector<Value> newWarpOpYieldValues{ifOp.getCondition()};
1997 newWarpOpYieldValues.append(escapingValuesThen.begin(),
1998 escapingValuesThen.end());
1999 newWarpOpYieldValues.append(escapingValuesElse.begin(),
2000 escapingValuesElse.end());
2001 SmallVector<Type> newWarpOpDistTypes{ifOp.getCondition().getType()};
2002 newWarpOpDistTypes.append(escapingValueDistTypesThen.begin(),
2003 escapingValueDistTypesThen.end());
2004 newWarpOpDistTypes.append(escapingValueDistTypesElse.begin(),
2005 escapingValueDistTypesElse.end());
2006
2007 for (auto [idx, val] :
2008 llvm::zip_equal(nonIfYieldIndices, nonIfYieldValues)) {
2009 newWarpOpYieldValues.push_back(val);
2010 newWarpOpDistTypes.push_back(warpOp.getResult(idx).getType());
2011 }
2012 // Replace the old `WarpOp` with the new one that has additional yield
2013 // values and types.
2014 SmallVector<size_t> newIndices;
2015 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
2016 rewriter, warpOp, newWarpOpYieldValues, newWarpOpDistTypes, newIndices);
2017 // `ifOp` returns the result of the inner warp op.
2018 SmallVector<Type> newIfOpDistResTypes;
2019 for (auto [i, res] : llvm::enumerate(ifOp.getResults())) {
2020 Type distType = cast<Value>(res).getType();
2021 if (auto vecType = dyn_cast<VectorType>(distType)) {
2022 AffineMap map = distributionMapFn(cast<Value>(res));
2023 // Fallback to affine map if the dist result was not previously recorded
2024 distType = ifResultDistTypes.count(i)
2025 ? ifResultDistTypes[i]
2026 : getDistributedType(
2027 vecType, map,
2028 map.isEmpty() ? 1 : newWarpOp.getWarpSize());
2029 }
2030 newIfOpDistResTypes.push_back(distType);
2031 }
2032 // Create a new `IfOp` outside the new `WarpOp` region.
2033 OpBuilder::InsertionGuard g(rewriter);
2034 rewriter.setInsertionPointAfter(newWarpOp);
2035 auto newIfOp = scf::IfOp::create(
2036 rewriter, ifOp.getLoc(), newIfOpDistResTypes,
2037 newWarpOp.getResult(newIndices[0]), static_cast<bool>(ifOp.thenBlock()),
2038 static_cast<bool>(ifOp.elseBlock()));
2039 auto encloseRegionInWarpOp =
2040 [&](Block *oldIfBranch, Block *newIfBranch,
2041 llvm::SmallSetVector<Value, 32> &escapingValues,
2042 SmallVector<Type> &escapingValueInputTypes,
2043 size_t warpResRangeStart) {
2044 OpBuilder::InsertionGuard g(rewriter);
2045 if (!newIfBranch)
2046 return;
2047 rewriter.setInsertionPointToStart(newIfBranch);
2048 llvm::SmallDenseMap<Value, int64_t> escapeValToBlockArgIndex;
2049 SmallVector<Value> innerWarpInputVals;
2050 SmallVector<Type> innerWarpInputTypes;
2051 for (size_t i = 0; i < escapingValues.size();
2052 ++i, ++warpResRangeStart) {
2053 innerWarpInputVals.push_back(
2054 newWarpOp.getResult(newIndices[warpResRangeStart]));
2055 escapeValToBlockArgIndex[escapingValues[i]] =
2056 innerWarpInputTypes.size();
2057 innerWarpInputTypes.push_back(escapingValueInputTypes[i]);
2058 }
2059 auto innerWarp = WarpExecuteOnLane0Op::create(
2060 rewriter, newWarpOp.getLoc(), newIfOp.getResultTypes(),
2061 newWarpOp.getLaneid(), newWarpOp.getWarpSize(),
2062 innerWarpInputVals, innerWarpInputTypes);
2063
2064 Region &warpRegion = innerWarp.getWarpRegion();
2065 rewriter.eraseBlock(innerWarp.getBody());
2066 rewriter.inlineRegionBefore(*oldIfBranch->getParent(), warpRegion,
2067 warpRegion.end());
2068 innerWarp.getWarpRegion().addArguments(
2069 innerWarpInputTypes,
2070 SmallVector<Location>(innerWarpInputTypes.size(), ifOp.getLoc()));
2071
2072 SmallVector<Value> yieldOperands;
2073 for (Value operand : oldIfBranch->getTerminator()->getOperands())
2074 yieldOperands.push_back(operand);
2075 rewriter.eraseOp(oldIfBranch->getTerminator());
2076
2077 rewriter.setInsertionPointToEnd(innerWarp.getBody());
2078 gpu::YieldOp::create(rewriter, innerWarp.getLoc(), yieldOperands);
2079 rewriter.setInsertionPointAfter(innerWarp);
2080 scf::YieldOp::create(rewriter, ifOp.getLoc(), innerWarp.getResults());
2081
2082 // Update any users of escaping values that were forwarded to the
2083 // inner `WarpOp`. These values are arguments of the inner `WarpOp`.
2084 innerWarp.walk([&](Operation *op) {
2085 SmallVector<std::pair<unsigned, Value>> replacements;
2086 for (OpOperand &operand : op->getOpOperands()) {
2087 auto it = escapeValToBlockArgIndex.find(operand.get());
2088 if (it == escapeValToBlockArgIndex.end())
2089 continue;
2090 replacements.emplace_back(
2091 operand.getOperandNumber(),
2092 innerWarp.getBodyRegion().getArgument(it->second));
2093 }
2094 if (!replacements.empty()) {
2095 rewriter.modifyOpInPlace(op, [&]() {
2096 for (auto [idx, newVal] : replacements)
2097 op->setOperand(idx, newVal);
2098 });
2099 }
2100 });
2101 mlir::vector::moveScalarUniformCode(innerWarp);
2102 };
2103 encloseRegionInWarpOp(&ifOp.getThenRegion().front(),
2104 &newIfOp.getThenRegion().front(), escapingValuesThen,
2105 escapingValueInputTypesThen, 1);
2106 if (!ifOp.getElseRegion().empty())
2107 encloseRegionInWarpOp(&ifOp.getElseRegion().front(),
2108 &newIfOp.getElseRegion().front(),
2109 escapingValuesElse, escapingValueInputTypesElse,
2110 1 + escapingValuesThen.size());
2111 // Update the users of `<- WarpOp.yield <- IfOp.yield` to use the new `IfOp`
2112 // result.
2113 for (auto [origIdx, newIdx] : ifResultMapping)
2114 rewriter.replaceAllUsesExcept(newWarpOp.getResult(origIdx),
2115 newIfOp.getResult(newIdx), newIfOp);
2116
2117 // The original `ifOp` was left inside `newWarpOp` with empty then/else
2118 // regions (their blocks were moved into the inner WarpOps).
2119 // Clear remaining uses and erase it to restore IR validity. Directly
2120 // update newWarpOp's yield operands instead of using replaceAllUsesWith,
2121 // to avoid triggering notifyOperandReplaced on the now-invalid ifOp.
2122 {
2123 OpBuilder::InsertionGuard guard(rewriter);
2124 rewriter.setInsertionPoint(ifOp);
2125 Operation *yield = newWarpOp.getTerminator();
2126 rewriter.modifyOpInPlace(yield, [&]() {
2127 for (auto [origIdx, ifResultIdx] : ifResultMapping) {
2128 Value poison = ub::PoisonOp::create(
2129 rewriter, ifOp.getLoc(), ifOp.getResult(ifResultIdx).getType());
2130 yield->setOperand(origIdx, poison);
2131 }
2132 });
2133 rewriter.eraseOp(ifOp);
2134 }
2135
2136 return success();
2137 }
2138
2139private:
2140 DistributionMapFn distributionMapFn;
2141};
2142
2143/// Sink scf.for region out of WarpExecuteOnLane0Op. This can be done only if
2144/// the scf.ForOp is the last operation in the region so that it doesn't
2145/// change the order of execution. This creates a new scf.for region after the
2146/// WarpExecuteOnLane0Op. The new scf.for region will contain a new
2147/// WarpExecuteOnLane0Op region. Example:
2148/// ```
2149/// %w = gpu.warp_execute_on_lane_0(%laneid) -> (vector<4xf32>) {
2150/// ...
2151/// %v1 = scf.for %arg3 = %c0 to %c128 step %c1 iter_args(%arg4 = %v)
2152/// -> (vector<128xf32>) {
2153/// ...
2154/// scf.yield %r : vector<128xf32>
2155/// }
2156/// gpu.yield %v1 : vector<128xf32>
2157/// }
2158/// ```
2159/// To:
2160/// %w0 = gpu.warp_execute_on_lane_0(%arg0) -> (vector<4xf32>) {
2161/// ...
2162/// gpu.yield %v : vector<128xf32>
2163/// }
2164/// %w = scf.for %arg3 = %c0 to %c128 step %c1 iter_args(%varg = %q0)
2165/// -> (vector<4xf32>) {
2166/// %iw = gpu.warp_execute_on_lane_0(%laneid)
2167/// args(%varg : vector<4xf32>) -> (vector<4xf32>) {
2168/// ^bb0(%arg: vector<128xf32>):
2169/// ...
2170/// gpu.yield %ir : vector<128xf32>
2171/// }
2172/// scf.yield %iw : vector<4xf32>
2173/// }
2174/// ```
2175struct WarpOpScfForOp : public WarpDistributionPattern {
2176
2177 WarpOpScfForOp(MLIRContext *ctx, DistributionMapFn fn, PatternBenefit b = 1)
2178 : WarpDistributionPattern(ctx, b), distributionMapFn(std::move(fn)) {}
2179 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
2180 PatternRewriter &rewriter) const override {
2181 gpu::YieldOp warpOpYield = warpOp.getTerminator();
2182 // Only pick up `ForOp` if it is the last op in the region.
2183 Operation *lastNode = warpOpYield->getPrevNode();
2184 auto forOp = dyn_cast_or_null<scf::ForOp>(lastNode);
2185 if (!forOp)
2186 return failure();
2187 // Collect Values that come from the `WarpOp` but are outside the `ForOp`.
2188 // Those Values need to be returned by the new warp op.
2189 auto [escapingValues, escapingValueInputTypes, escapingValueDistTypes] =
2190 getInnerRegionEscapingValues(warpOp, forOp.getBodyRegion(),
2191 distributionMapFn);
2192 if (llvm::is_contained(escapingValueDistTypes, Type{}))
2193 return failure();
2194 // `WarpOp` can yield two types of values:
2195 // 1. Values that are not results of the `ForOp`:
2196 // These values must also be yielded by the new `WarpOp`. Also, we need
2197 // to record the index mapping for these values to replace them later.
2198 // 2. Values that are results of the `ForOp`:
2199 // In this case, we record the index mapping between the `WarpOp` result
2200 // index and matching `ForOp` result index.
2201 // Additionally, we keep track of the distributed types for all `ForOp`
2202 // vector results.
2203 SmallVector<Value> nonForYieldedValues;
2204 SmallVector<unsigned> nonForResultIndices;
2205 llvm::SmallDenseMap<unsigned, unsigned> forResultMapping;
2206 llvm::SmallDenseMap<unsigned, VectorType> forResultDistTypes;
2207 llvm::SmallBitVector forResultsMapped(forOp.getNumResults());
2208 for (OpOperand &yieldOperand : warpOpYield->getOpOperands()) {
2209 // Yielded value is not a result of the forOp.
2210 if (yieldOperand.get().getDefiningOp() != forOp.getOperation()) {
2211 nonForYieldedValues.push_back(yieldOperand.get());
2212 nonForResultIndices.push_back(yieldOperand.getOperandNumber());
2213 continue;
2214 }
2215 OpResult forResult = cast<OpResult>(yieldOperand.get());
2216 unsigned int forResultNumber = forResult.getResultNumber();
2217 forResultMapping[yieldOperand.getOperandNumber()] = forResultNumber;
2218 forResultsMapped.set(forResultNumber);
2219 // If this `ForOp` result is vector type and it is yielded by the
2220 // `WarpOp`, we keep track the distributed type for this result.
2221 if (!isa<VectorType>(forResult.getType()))
2222 continue;
2223 VectorType distType = cast<VectorType>(
2224 warpOp.getResult(yieldOperand.getOperandNumber()).getType());
2225 forResultDistTypes[forResultNumber] = distType;
2226 }
2227
2228 // Newly created `WarpOp` will yield values in following order:
2229 // 1. Loop bounds.
2230 // 2. All init args of the `ForOp`.
2231 // 3. All escaping values.
2232 // 4. All non-`ForOp` yielded values.
2233 SmallVector<Value> newWarpOpYieldValues;
2234 SmallVector<Type> newWarpOpDistTypes;
2235 newWarpOpYieldValues.insert(
2236 newWarpOpYieldValues.end(),
2237 {forOp.getLowerBound(), forOp.getUpperBound(), forOp.getStep()});
2238 newWarpOpDistTypes.insert(newWarpOpDistTypes.end(),
2239 {forOp.getLowerBound().getType(),
2240 forOp.getUpperBound().getType(),
2241 forOp.getStep().getType()});
2242 for (auto [i, initArg] : llvm::enumerate(forOp.getInitArgs())) {
2243 newWarpOpYieldValues.push_back(initArg);
2244 // Compute the distributed type for this init arg.
2245 Type distType = initArg.getType();
2246 if (auto vecType = dyn_cast<VectorType>(distType)) {
2247 // If the `ForOp` result corresponds to this init arg is already yielded
2248 // we can get the distributed type from `forResultDistTypes` map.
2249 // Otherwise, we compute it using distributionMapFn.
2250 AffineMap map = distributionMapFn(initArg);
2251 distType =
2252 forResultDistTypes.count(i)
2253 ? forResultDistTypes[i]
2254 : getDistributedType(vecType, map,
2255 map.isEmpty() ? 1 : warpOp.getWarpSize());
2256 }
2257 newWarpOpDistTypes.push_back(distType);
2258 }
2259 // Insert escaping values and their distributed types.
2260 newWarpOpYieldValues.insert(newWarpOpYieldValues.end(),
2261 escapingValues.begin(), escapingValues.end());
2262 newWarpOpDistTypes.insert(newWarpOpDistTypes.end(),
2263 escapingValueDistTypes.begin(),
2264 escapingValueDistTypes.end());
2265 // Next, we insert all non-`ForOp` yielded values and their distributed
2266 // types.
2267 for (auto [i, v] :
2268 llvm::zip_equal(nonForResultIndices, nonForYieldedValues)) {
2269 newWarpOpYieldValues.push_back(v);
2270 newWarpOpDistTypes.push_back(warpOp.getResult(i).getType());
2271 }
2272 // Create the new `WarpOp` with the updated yield values and types.
2273 SmallVector<size_t> newIndices;
2274 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
2275 rewriter, warpOp, newWarpOpYieldValues, newWarpOpDistTypes, newIndices);
2276
2277 // Next, we create a new `ForOp` with the init args yielded by the new
2278 // `WarpOp`.
2279 const unsigned initArgsStartIdx = 3; // After loop bounds.
2280 const unsigned escapingValuesStartIdx =
2281 initArgsStartIdx +
2282 forOp.getInitArgs().size(); // `ForOp` init args are positioned before
2283 // escaping values in the new `WarpOp`.
2284 SmallVector<Value> newForOpOperands;
2285 for (size_t i = initArgsStartIdx; i < escapingValuesStartIdx; ++i)
2286 newForOpOperands.push_back(newWarpOp.getResult(newIndices[i]));
2287
2288 // Create a new `ForOp` outside the new `WarpOp` region.
2289 OpBuilder::InsertionGuard g(rewriter);
2290 rewriter.setInsertionPointAfter(newWarpOp);
2291 auto newForOp = scf::ForOp::create(
2292 rewriter, forOp.getLoc(),
2293 /**LowerBound=**/ newWarpOp.getResult(newIndices[0]),
2294 /**UpperBound=**/ newWarpOp.getResult(newIndices[1]),
2295 /**Step=**/ newWarpOp.getResult(newIndices[2]), newForOpOperands,
2296 /*bodyBuilder=*/nullptr, forOp.getUnsignedCmp());
2297 // Next, we insert a new `WarpOp` (called inner `WarpOp`) inside the
2298 // newly created `ForOp`. This `WarpOp` will contain all ops that were
2299 // contained within the original `ForOp` body.
2300 rewriter.setInsertionPointToStart(newForOp.getBody());
2301
2302 SmallVector<Value> innerWarpInput(newForOp.getRegionIterArgs().begin(),
2303 newForOp.getRegionIterArgs().end());
2304 SmallVector<Type> innerWarpInputType(forOp.getResultTypes().begin(),
2305 forOp.getResultTypes().end());
2306 // Escaping values are forwarded to the inner `WarpOp` as its (additional)
2307 // arguments. We keep track of the mapping between these values and their
2308 // argument index in the inner `WarpOp` (to replace users later).
2309 llvm::SmallDenseMap<Value, int64_t> argIndexMapping;
2310 for (size_t i = escapingValuesStartIdx;
2311 i < escapingValuesStartIdx + escapingValues.size(); ++i) {
2312 innerWarpInput.push_back(newWarpOp.getResult(newIndices[i]));
2313 argIndexMapping[escapingValues[i - escapingValuesStartIdx]] =
2314 innerWarpInputType.size();
2315 innerWarpInputType.push_back(
2316 escapingValueInputTypes[i - escapingValuesStartIdx]);
2317 }
2318 // Create the inner `WarpOp` with the new input values and types.
2319 auto innerWarp = WarpExecuteOnLane0Op::create(
2320 rewriter, newWarpOp.getLoc(), newForOp.getResultTypes(),
2321 newWarpOp.getLaneid(), newWarpOp.getWarpSize(), innerWarpInput,
2322 innerWarpInputType);
2323
2324 // Inline the `ForOp` body into the inner `WarpOp` body.
2325 SmallVector<Value> argMapping;
2326 argMapping.push_back(newForOp.getInductionVar());
2327 for (Value args : innerWarp.getBody()->getArguments())
2328 argMapping.push_back(args);
2329
2330 argMapping.resize(forOp.getBody()->getNumArguments());
2331 SmallVector<Value> yieldOperands;
2332 for (Value operand : forOp.getBody()->getTerminator()->getOperands()) {
2333 if (BlockArgument blockArg = dyn_cast<BlockArgument>(operand);
2334 blockArg && blockArg.getOwner() == forOp.getBody()) {
2335 yieldOperands.push_back(argMapping[blockArg.getArgNumber()]);
2336 continue;
2337 }
2338 yieldOperands.push_back(operand);
2339 }
2340
2341 rewriter.eraseOp(forOp.getBody()->getTerminator());
2342 rewriter.mergeBlocks(forOp.getBody(), innerWarp.getBody(), argMapping);
2343
2344 // Insert a gpu `YieldOp` at the end of the inner `WarpOp` body that yields
2345 // original `ForOp` results.
2346 rewriter.setInsertionPointToEnd(innerWarp.getBody());
2347 gpu::YieldOp::create(rewriter, innerWarp.getLoc(), yieldOperands);
2348 rewriter.setInsertionPointAfter(innerWarp);
2349 // Insert a scf.yield op at the end of the new `ForOp` body that yields
2350 // the inner `WarpOp` results.
2351 if (!innerWarp.getResults().empty())
2352 scf::YieldOp::create(rewriter, forOp.getLoc(), innerWarp.getResults());
2353
2354 // Update the users of the new `WarpOp` results that were coming from the
2355 // original `ForOp` to the corresponding new `ForOp` result.
2356 for (auto [origIdx, newIdx] : forResultMapping)
2357 rewriter.replaceAllUsesExcept(newWarpOp.getResult(origIdx),
2358 newForOp.getResult(newIdx), newForOp);
2359
2360 // The original `ForOp` was left inside `newWarpOp` with an empty body
2361 // region (its body block was moved into `innerWarp` by `mergeBlocks`).
2362 // Clear remaining uses and erase it to restore IR validity.
2363 for (OpResult result : forOp.getResults()) {
2364 if (forResultsMapped.test(result.getResultNumber()))
2365 rewriter.replaceAllUsesWith(
2366 result, forOp.getInitArgs()[result.getResultNumber()]);
2367 }
2368 rewriter.eraseOp(forOp);
2369
2370 // Update any users of escaping values that were forwarded to the
2371 // inner `WarpOp`. These values are now arguments of the inner `WarpOp`.
2372 newForOp.walk([&](Operation *op) {
2373 SmallVector<std::pair<unsigned, Value>> replacements;
2374 for (OpOperand &operand : op->getOpOperands()) {
2375 auto it = argIndexMapping.find(operand.get());
2376 if (it == argIndexMapping.end())
2377 continue;
2378 replacements.emplace_back(
2379 operand.getOperandNumber(),
2380 innerWarp.getBodyRegion().getArgument(it->second));
2381 }
2382 if (!replacements.empty()) {
2383 rewriter.modifyOpInPlace(op, [&]() {
2384 for (auto [idx, newVal] : replacements)
2385 op->setOperand(idx, newVal);
2386 });
2387 }
2388 });
2389
2390 // Finally, hoist out any now uniform code from the inner `WarpOp`.
2391 mlir::vector::moveScalarUniformCode(innerWarp);
2392 return success();
2393 }
2394
2395private:
2396 DistributionMapFn distributionMapFn;
2397};
2398
2399/// A pattern that extracts vector.reduction ops from a WarpExecuteOnLane0Op.
2400/// The vector is reduced in parallel. Currently limited to vector size
2401/// matching the warpOp size. E.g.:
2402/// ```
2403/// %r = gpu.warp_execute_on_lane_0(%laneid)[32] -> (f32) {
2404/// %0 = "some_def"() : () -> (vector<32xf32>)
2405/// %1 = vector.reduction "add", %0 : vector<32xf32> into f32
2406/// gpu.yield %1 : f32
2407/// }
2408/// ```
2409/// is lowered to:
2410/// ```
2411/// %0 = gpu.warp_execute_on_lane_0(%laneid)[32] -> (vector<1xf32>) {
2412/// %1 = "some_def"() : () -> (vector<32xf32>)
2413/// gpu.yield %1 : vector<32xf32>
2414/// }
2415/// %a = vector.extract %0[0] : f32 from vector<1xf32>
2416/// %r = ("warp.reduction %a")
2417/// ```
2418struct WarpOpReduction : public WarpDistributionPattern {
2419 WarpOpReduction(MLIRContext *context,
2420 DistributedReductionFn distributedReductionFn,
2421 PatternBenefit benefit = 1)
2422 : WarpDistributionPattern(context, benefit),
2423 distributedReductionFn(std::move(distributedReductionFn)) {}
2424
2425 LogicalResult matchAndRewrite(WarpExecuteOnLane0Op warpOp,
2426 PatternRewriter &rewriter) const override {
2427 OpOperand *yieldOperand =
2428 getWarpResult(warpOp, llvm::IsaPred<vector::ReductionOp>);
2429 if (!yieldOperand)
2430 return failure();
2431
2432 auto reductionOp =
2433 cast<vector::ReductionOp>(yieldOperand->get().getDefiningOp());
2434 auto vectorType = cast<VectorType>(reductionOp.getVector().getType());
2435 // Only rank 1 vectors supported.
2436 if (vectorType.getRank() != 1)
2437 return rewriter.notifyMatchFailure(
2438 warpOp, "Only rank 1 reductions can be distributed.");
2439 if (vectorType.isScalable())
2440 return rewriter.notifyMatchFailure(
2441 warpOp, "Scalable reductions cannot be distributed.");
2442 // Only warp_size-sized vectors supported.
2443 if (vectorType.getShape()[0] % warpOp.getWarpSize() != 0)
2444 return rewriter.notifyMatchFailure(
2445 warpOp, "Reduction vector dimension must match was size.");
2446 if (!reductionOp.getType().isIntOrFloat())
2447 return rewriter.notifyMatchFailure(
2448 warpOp, "Reduction distribution currently only supports floats and "
2449 "integer types.");
2450
2451 int64_t numElements = vectorType.getShape()[0] / warpOp.getWarpSize();
2452 // Return vector that will be reduced from the WarpExecuteOnLane0Op.
2453 unsigned operandIndex = yieldOperand->getOperandNumber();
2454 SmallVector<Value> yieldValues = {reductionOp.getVector()};
2455 SmallVector<Type> retTypes = {
2456 VectorType::get({numElements}, reductionOp.getType())};
2457 if (reductionOp.getAcc()) {
2458 yieldValues.push_back(reductionOp.getAcc());
2459 retTypes.push_back(reductionOp.getAcc().getType());
2460 }
2461 SmallVector<size_t> newRetIndices;
2462 WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndAppendReturns(
2463 rewriter, warpOp, yieldValues, retTypes, newRetIndices);
2464 rewriter.setInsertionPointAfter(newWarpOp);
2465
2466 // Obtain data to reduce for a single lane.
2467 Value laneValVec = newWarpOp.getResult(newRetIndices[0]);
2468 // Distribute and reduce across threads.
2469 Value fullReduce =
2470 distributedReductionFn(reductionOp.getLoc(), rewriter, laneValVec,
2471 reductionOp.getKind(), newWarpOp.getWarpSize());
2472 if (reductionOp.getAcc()) {
2473 fullReduce = vector::makeArithReduction(
2474 rewriter, reductionOp.getLoc(), reductionOp.getKind(), fullReduce,
2475 newWarpOp.getResult(newRetIndices[1]));
2476 }
2477 rewriter.replaceAllUsesWith(newWarpOp.getResult(operandIndex), fullReduce);
2478 return success();
2479 }
2480
2481private:
2482 DistributedReductionFn distributedReductionFn;
2483};
2484} // namespace
2485
2487 RewritePatternSet &patterns,
2489 patterns.add<WarpOpToScfIfPattern>(patterns.getContext(), options, benefit);
2490}
2491
2492void mlir::vector::populateDistributeTransferWriteOpPatterns(
2493 RewritePatternSet &patterns, const DistributionMapFn &distributionMapFn,
2494 unsigned maxNumElementsToExtract, PatternBenefit benefit) {
2495 patterns.add<WarpOpTransferWrite>(patterns.getContext(), distributionMapFn,
2496 maxNumElementsToExtract, benefit);
2497}
2498
2499void mlir::vector::populatePropagateWarpVectorDistributionPatterns(
2500 RewritePatternSet &patterns, const DistributionMapFn &distributionMapFn,
2501 const WarpShuffleFromIdxFn &warpShuffleFromIdxFn, PatternBenefit benefit,
2502 PatternBenefit readBenefit) {
2503 patterns.add<WarpOpTransferRead>(patterns.getContext(), readBenefit);
2504 patterns.add<WarpOpElementwise, WarpOpDeadResult, WarpOpBroadcast,
2505 WarpOpShapeCast, WarpOpExtract, WarpOpForwardOperand,
2506 WarpOpConstant, WarpOpInsertScalar, WarpOpInsert,
2507 WarpOpCreateMask<vector::CreateMaskOp>,
2508 WarpOpCreateMask<vector::ConstantMaskOp>,
2509 WarpOpExtractStridedSlice, WarpOpInsertStridedSlice, WarpOpStep>(
2510 patterns.getContext(), benefit);
2511 patterns.add<WarpOpExtractScalar>(patterns.getContext(), warpShuffleFromIdxFn,
2512 benefit);
2513 patterns.add<WarpOpScfForOp>(patterns.getContext(), distributionMapFn,
2514 benefit);
2515 patterns.add<WarpOpScfIfOp>(patterns.getContext(), distributionMapFn,
2516 benefit);
2517}
2518
2519void mlir::vector::populateDistributeReduction(
2520 RewritePatternSet &patterns,
2521 const DistributedReductionFn &distributedReductionFn,
2522 PatternBenefit benefit) {
2523 patterns.add<WarpOpReduction>(patterns.getContext(), distributedReductionFn,
2524 benefit);
2525}
2526
2527/// Helper to know if an op can be hoisted out of the region.
2528static bool canBeHoisted(Operation *op,
2529 function_ref<bool(Value)> definedOutside) {
2530 return llvm::all_of(op->getOperands(), definedOutside) &&
2531 isMemoryEffectFree(op) && op->getNumRegions() == 0;
2532}
2533
2534void mlir::vector::moveScalarUniformCode(WarpExecuteOnLane0Op warpOp) {
2535 Block *body = warpOp.getBody();
2536
2537 // Keep track of the ops we want to hoist.
2538 llvm::SmallSetVector<Operation *, 8> opsToMove;
2539
2540 // Helper to check if a value is or will be defined outside of the region.
2541 auto isDefinedOutsideOfBody = [&](Value value) {
2542 auto *definingOp = value.getDefiningOp();
2543 return (definingOp && opsToMove.count(definingOp)) ||
2544 warpOp.isDefinedOutsideOfRegion(value);
2545 };
2546
2547 // Do not use walk here, as we do not want to go into nested regions and hoist
2548 // operations from there.
2549 for (auto &op : body->without_terminator()) {
2550 bool hasVectorResult = llvm::any_of(op.getResults(), [](Value result) {
2551 return isa<VectorType>(result.getType());
2552 });
2553 if (!hasVectorResult && canBeHoisted(&op, isDefinedOutsideOfBody))
2554 opsToMove.insert(&op);
2555 }
2556
2557 // Move all the ops marked as uniform outside of the region.
2558 for (Operation *op : opsToMove)
2559 op->moveBefore(warpOp);
2560}
return success()
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
static llvm::ManagedStatic< PassManagerOptions > options
static AffineMap calculateImplicitMap(VectorType sequentialType, VectorType distributedType)
Currently the distribution map is implicit based on the vector shape.
static Value tryReuseArgBuffer(SmallVectorImpl< std::pair< Value, bool > > &buffers, Type targetType, Location loc, RewriterBase &rewriter)
Reuse an args buffer from buffers to transit a value of targetType, or return null if none fits.
static Operation * cloneOpWithOperandsAndTypes(RewriterBase &rewriter, Location loc, Operation *op, ArrayRef< Value > operands, ArrayRef< Type > resultTypes)
static bool isBlockInCFGLoop(Block *block)
Return true if block is part of a CFG cycle, which LoopLikeOpInterface does not cover.
static int getDistributedDim(VectorType sequentialType, VectorType distributedType)
Given a sequential and distributed vector type, returns the distributed dimension.
static bool canBeHoisted(Operation *op, function_ref< bool(Value)> definedOutside)
Helper to know if an op can be hoisted out of the region.
static bool isInCFGLoop(Block *block)
Return true if block or any of its ancestor blocks is part of a CFG cycle, which LoopLikeOpInterface ...
AffineExpr ceilDiv(uint64_t v) const
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition AffineMap.h:46
unsigned getDimPosition(unsigned idx) const
Extracts the position of the dimensional expression at the given result, when the caller knows it is ...
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
bool isEmpty() const
Returns true if this affine map is an empty map, i.e., () -> ().
ArrayRef< AffineExpr > getResults() const
unsigned getNumResults() const
AffineMap compose(AffineMap map) const
Returns the AffineMap resulting from composing this with map.
bool isIdentity() const
Returns true if this affine map is an identity affine map.
Block represents an ordered list of Operations.
Definition Block.h:34
BlockArgument getArgument(unsigned i)
Definition Block.h:154
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
Definition Block.cpp:27
SuccessorRange getSuccessors()
Definition Block.h:280
Operation * getTerminator()
Get the terminator operation of this block.
Definition Block.cpp:249
iterator_range< iterator > without_terminator()
Return an iterator range over the operation within this block excluding the terminator operation at t...
Definition Block.h:222
Operation * getParentOp()
Returns the closest surrounding operation that contains this block.
Definition Block.cpp:31
IntegerAttr getIndexAttr(int64_t value)
Definition Builders.cpp:116
AffineExpr getAffineConstantExpr(int64_t constant)
Definition Builders.cpp:381
IntegerAttr getI64IntegerAttr(int64_t value)
Definition Builders.cpp:120
MLIRContext * getContext() const
Definition Builders.h:56
IndexType getIndexType()
Definition Builders.cpp:59
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
IRValueT get() const
Return the current value being used by this operand.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
Definition Builders.cpp:581
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition Builders.h:434
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition Builders.h:401
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Definition Builders.h:439
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition Builders.cpp:466
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
Definition Builders.h:415
This class represents an operand of an operation.
Definition Value.h:256
unsigned getOperandNumber() const
Return which operand this is in the OpOperand list of the Operation.
Definition Value.cpp:231
unsigned getResultNumber() const
Returns the number of this result.
Definition Value.h:468
StringRef getStringRef() const
Return the name of this operation. This always succeeds.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
void setOperand(unsigned idx, Value value)
Definition Operation.h:378
bool hasOneUse()
Returns true if this operation has exactly one use.
Definition Operation.h:903
Block * getBlock()
Returns the operation block that contains this operation.
Definition Operation.h:230
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:434
unsigned getNumRegions()
Returns the number of regions held by this operation.
Definition Operation.h:728
MutableArrayRef< OpOperand > getOpOperands()
Definition Operation.h:410
unsigned getNumOperands()
Definition Operation.h:373
Attribute getPropertiesAsAttribute()
Return the properties converted to an attribute.
OperationName getName()
The name of an operation is the key identifier for it.
Definition Operation.h:115
DictionaryAttr getDiscardableAttrDictionary()
Return all of the discardable attributes on this operation as a DictionaryAttr.
Definition Operation.h:555
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition Operation.h:405
void moveBefore(Operation *existingOp)
Unlink this operation from its current block and insert it right before existingOp which may be in th...
result_range getResults()
Definition Operation.h:442
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
bool empty()
Definition Region.h:60
iterator end()
Definition Region.h:56
Operation * getParentOp()
Return the parent operation this region is attached to.
Definition Region.h:198
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
virtual void eraseBlock(Block *block)
This method erases all operations in a block.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
virtual void finalizeOpModification(Operation *op)
This method is used to signal the end of an in-place modification of the given operation.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
void replaceAllUsesExcept(Value from, Value to, Operation *exceptedUser)
Find uses of from and replace them with to except if the user is exceptedUser.
void mergeBlocks(Block *source, Block *dest, ValueRange argValues={})
Inline the operations of block 'source' into the end of block 'dest'.
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
void inlineRegionBefore(Region &region, Region &parent, Region::iterator before)
Move the blocks that belong to "region" before the given position in another region "parent".
virtual void replaceAllUsesWith(Value from, Value to)
Find uses of from and replace them with to.
virtual void startOpModification(Operation *op)
This method is used to notify the rewriter that an in-place operation modification is about to happen...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
Location getLoc() const
Return the location of this value.
Definition Value.cpp:24
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition Value.cpp:18
Region * getParentRegion()
Return the Region in which this Value is defined.
Definition Value.cpp:39
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
Definition ArithOps.cpp:453
bool hasElementwiseMappableTraits(Operation *op)
Together, Elementwise, Scalarizable, Vectorizable, and Tensorizable provide an easy way for scalar op...
AffineApplyOp makeComposedAffineApply(OpBuilder &b, Location loc, AffineMap map, ArrayRef< OpFoldResult > operands, bool composeAffineMin=false)
Returns a composed AffineApplyOp by composing map and operands with other AffineApplyOps supplying th...
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
Value makeArithReduction(OpBuilder &b, Location loc, CombiningKind kind, Value v1, Value acc, arith::FastMathFlagsAttr fastmath=nullptr, Value mask=nullptr)
Returns the result value of reducing two scalar/vector values with the corresponding arith operation.
std::function< AffineMap(Value)> DistributionMapFn
BroadcastableToResult isBroadcastableTo(Type srcType, VectorType dstVectorType, std::pair< VectorDim, VectorDim > *mismatchingDims=nullptr)
Return whether srcType can be broadcast to dstVectorType under the semantics of the vector....
void populateWarpExecuteOnLane0OpToScfForPattern(RewritePatternSet &patterns, const WarpExecuteOnLane0LoweringOptions &options, PatternBenefit benefit=1)
SmallVector< int64_t > getAsIntegers(ArrayRef< Value > values)
Returns the integer numbers in values.
Include the generated interface declarations.
void bindDims(MLIRContext *ctx, AffineExprTy &...exprs)
Bind a list of AffineExpr references to DimExpr at positions: [0 .
Definition AffineExpr.h:313
bool isMemoryEffectFree(Operation *op)
Returns true if the given operation is free of memory effects.
bool isOpTriviallyDead(Operation *op)
Return true if the given operation is unused, and has no side effects on memory that prevent erasing.
void bindSymbols(MLIRContext *ctx, AffineExprTy &...exprs)
Bind a list of AffineExpr references to SymbolExpr at positions: [0 .
Definition AffineExpr.h:327
AffineMap compressUnusedDims(AffineMap map)
Drop the dims that are not used.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:123
AffineExpr getAffineDimExpr(unsigned position, MLIRContext *context)
These free functions allow clients of the API to not use classes in detail.
void visitUsedValuesDefinedAbove(Region &region, Region &limit, function_ref< void(OpOperand *)> callback)
Calls callback for each use of a value within region or its descendants that was defined at the ances...
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155
AffineExpr getAffineSymbolExpr(unsigned position, MLIRContext *context)
This represents an operation in an abstracted form, suitable for use with the builder APIs.
WarpExecuteOnLane0Op moveRegionToNewWarpOpAndAppendReturns(RewriterBase &rewriter, WarpExecuteOnLane0Op warpOp, ValueRange newYieldedValues, TypeRange newReturnTypes, SmallVector< size_t > &indices) const
Helper to create a new WarpExecuteOnLane0Op region with extra outputs.
bool delinearizeLaneId(OpBuilder &builder, Location loc, ArrayRef< int64_t > originalShape, ArrayRef< int64_t > distributedShape, int64_t warpSize, Value laneId, SmallVectorImpl< Value > &delinearizedIds) const
Delinearize the given laneId into multiple dimensions, where each dimension's size is determined by o...
WarpExecuteOnLane0Op moveRegionToNewWarpOpAndReplaceReturns(RewriterBase &rewriter, WarpExecuteOnLane0Op warpOp, ValueRange newYieldedValues, TypeRange newReturnTypes) const
Helper to create a new WarpExecuteOnLane0Op with different signature.
virtual LogicalResult matchAndRewrite(WarpExecuteOnLane0Op op, PatternRewriter &rewriter) const override=0
OpOperand * getWarpResult(WarpExecuteOnLane0Op warpOp, llvm::function_ref< bool(Operation *)> fn) const
Return a value yielded by warpOp which statifies the filter lamdba condition and is not dead.