MLIR 22.0.0git
SCFToGPU.cpp
Go to the documentation of this file.
1//===- SCFToGPU.cpp - Convert an affine loop nest to a GPU kernel ---------===//
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//
9// This implements a straightforward conversion of an loop nest into a GPU
10// kernel. The caller is expected to guarantee that the conversion is correct
11// or to further transform the kernel to ensure correctness.
12//
13//===----------------------------------------------------------------------===//
14
16
25#include "mlir/IR/AffineExpr.h"
26#include "mlir/IR/Builders.h"
27#include "mlir/IR/IRMapping.h"
31#include "llvm/ADT/DenseSet.h"
32#include "llvm/Support/DebugLog.h"
33#include <optional>
34
35#define DEBUG_TYPE "loops-to-gpu"
36
37using namespace mlir;
38using namespace mlir::affine;
39using namespace mlir::scf;
40
41// Name of internal attribute to mark visited operations during conversion.
42//
43// NOTE: The conversion originally used the following legality criteria:
44// `!parallelOp->hasAttr(gpu::getMappingAttrName())`
45// But the provided pattern might reject some cases based on more detailed
46// analysis of the `mapping` attribute.
47// To avoid dialect conversion failure due to non-converted illegal operation
48// we use this extra Unit attribute as a marker, that the operation was checked
49// by the pattern and is should be considered as legal in the following legality
50// checks. The `finalizeParallelLoopToGPUConversion` function performs clean up
51// of this extra attributes ans is supposed to be called after the dialect
52// conversion.
53//
54// TODO: Implement a cleaner solution, factoring out the "matching" logic
55// from the pattern and its callees into a separate function that can be called
56// from both the pattern and the op legality check.
57static constexpr StringLiteral kVisitedAttrName = "SCFToGPU_visited";
58
59// Extract an indexed value from KernelDim3.
60static Value getDim3Value(const gpu::KernelDim3 &dim3, unsigned pos) {
61 switch (pos) {
62 case 0:
63 return dim3.x;
64 case 1:
65 return dim3.y;
66 case 2:
67 return dim3.z;
68 default:
69 llvm_unreachable("dim3 position out of bounds");
70 }
71 return nullptr;
72}
73
74// Get the lower bound-related operands of a loop operation.
76 return forOp.getLowerBoundOperands();
77}
78
79// Get the upper bound-related operands of a loop operation.
81 return forOp.getUpperBoundOperands();
82}
83
84// Get a Value that corresponds to the loop step. If the step is an attribute,
85// materialize a corresponding constant using builder.
86static Value getOrCreateStep(AffineForOp forOp, OpBuilder &builder) {
87 return arith::ConstantIndexOp::create(builder, forOp.getLoc(),
88 forOp.getStepAsInt());
89}
90
91// Get a Value for the loop lower bound. If the value requires computation,
92// materialize the instructions using builder.
93static Value getOrEmitLowerBound(AffineForOp forOp, OpBuilder &builder) {
94 return lowerAffineLowerBound(forOp, builder);
95}
96
97// Get a Value for the loop upper bound. If the value requires computation,
98// materialize the instructions using builder.
99static Value getOrEmitUpperBound(AffineForOp forOp, OpBuilder &builder) {
100 return lowerAffineUpperBound(forOp, builder);
101}
102
103// Check the structure of the loop nest:
104// - there are enough loops to map to numDims;
105// - the loops are perfectly nested;
106// - the loop bounds can be computed above the outermost loop.
107// This roughly corresponds to the "matcher" part of the pattern-based
108// rewriting infrastructure.
109static LogicalResult checkAffineLoopNestMappableImpl(AffineForOp forOp,
110 unsigned numDims) {
111 Region &limit = forOp.getRegion();
112 for (unsigned i = 0, e = numDims; i < e; ++i) {
113 Operation *nested = &forOp.getBody()->front();
114 if (!areValuesDefinedAbove(getLowerBoundOperands(forOp), limit) ||
116 return forOp.emitError(
117 "loops with bounds depending on other mapped loops "
118 "are not supported");
119
120 // The innermost loop can have an arbitrary body, skip the perfect nesting
121 // check for it.
122 if (i == e - 1)
123 break;
124
125 auto begin = forOp.getBody()->begin(), end = forOp.getBody()->end();
126 if (forOp.getBody()->empty() || std::next(begin, 2) != end)
127 return forOp.emitError("expected perfectly nested loops in the body");
128
129 if (!(forOp = dyn_cast<AffineForOp>(nested)))
130 return nested->emitError("expected a nested loop");
131 }
132 return success();
133}
134
135static LogicalResult checkAffineLoopNestMappable(AffineForOp forOp,
136 unsigned numBlockDims,
137 unsigned numThreadDims) {
138 if (numBlockDims < 1 || numThreadDims < 1) {
139 LDBG() << "nothing to map";
140 return success();
141 }
142
143 if (numBlockDims > 3) {
144 return forOp.emitError("cannot map to more than 3 block dimensions");
145 }
146 if (numThreadDims > 3) {
147 return forOp.emitError("cannot map to more than 3 thread dimensions");
148 }
149 return checkAffineLoopNestMappableImpl(forOp, numBlockDims + numThreadDims);
150}
151
152namespace {
153// Helper structure that holds common state of the loop to GPU kernel
154// conversion.
155struct AffineLoopToGpuConverter {
156 std::optional<AffineForOp> collectBounds(AffineForOp forOp,
157 unsigned numLoops);
158
159 void createLaunch(AffineForOp rootForOp, AffineForOp innermostForOp,
160 unsigned numBlockDims, unsigned numThreadDims);
161
162 // Ranges of the loops mapped to blocks or threads.
163 SmallVector<Value, 6> dims;
164 // Lower bounds of the loops mapped to blocks or threads.
165 SmallVector<Value, 6> lbs;
166 // Induction variables of the loops mapped to blocks or threads.
167 SmallVector<Value, 6> ivs;
168 // Steps of the loops mapped to blocks or threads.
169 SmallVector<Value, 6> steps;
170};
171} // namespace
172
173// Collect ranges, bounds, steps and induction variables in preparation for
174// mapping a loop nest of depth "numLoops" rooted at "forOp" to a GPU kernel.
175// This may fail if the IR for computing loop bounds cannot be constructed, for
176// example if an affine loop uses semi-affine maps. Return the last loop to be
177// mapped on success, std::nullopt on failure.
178std::optional<AffineForOp>
179AffineLoopToGpuConverter::collectBounds(AffineForOp forOp, unsigned numLoops) {
180 OpBuilder builder(forOp.getOperation());
181 dims.reserve(numLoops);
182 lbs.reserve(numLoops);
183 ivs.reserve(numLoops);
184 steps.reserve(numLoops);
185 AffineForOp currentLoop = forOp;
186 for (unsigned i = 0; i < numLoops; ++i) {
187 Value lowerBound = getOrEmitLowerBound(currentLoop, builder);
188 Value upperBound = getOrEmitUpperBound(currentLoop, builder);
189 if (!lowerBound || !upperBound) {
190 return std::nullopt;
191 }
192
193 Value range = arith::SubIOp::create(builder, currentLoop.getLoc(),
194 upperBound, lowerBound);
195 Value step = getOrCreateStep(currentLoop, builder);
196 if (getConstantIntValue(step) != static_cast<int64_t>(1))
197 range = arith::CeilDivSIOp::create(builder, currentLoop.getLoc(), range,
198 step);
199 dims.push_back(range);
200
201 lbs.push_back(lowerBound);
202 ivs.push_back(currentLoop.getInductionVar());
203 steps.push_back(step);
204
205 if (i != numLoops - 1)
206 currentLoop = cast<AffineForOp>(&currentLoop.getBody()->front());
207 }
208 return currentLoop;
209}
210
211// Replace the rooted at "rootForOp" with a GPU launch operation. This expects
212// "innermostForOp" to point to the last loop to be transformed to the kernel,
213// and to have (numBlockDims + numThreadDims) perfectly nested loops between
214// "rootForOp" and "innermostForOp".
215void AffineLoopToGpuConverter::createLaunch(AffineForOp rootForOp,
216 AffineForOp innermostForOp,
217 unsigned numBlockDims,
218 unsigned numThreadDims) {
219 OpBuilder builder(rootForOp.getOperation());
220 // Prepare the grid and block sizes for the launch operation. If there is
221 // no loop mapped to a specific dimension, use constant "1" as its size.
222 Value constOne =
223 (numBlockDims < 3 || numThreadDims < 3)
224 ? arith::ConstantIndexOp::create(builder, rootForOp.getLoc(), 1)
225 : nullptr;
226 Value gridSizeX = numBlockDims > 0 ? dims[0] : constOne;
227 Value gridSizeY = numBlockDims > 1 ? dims[1] : constOne;
228 Value gridSizeZ = numBlockDims > 2 ? dims[2] : constOne;
229 Value blockSizeX = numThreadDims > 0 ? dims[numBlockDims] : constOne;
230 Value blockSizeY = numThreadDims > 1 ? dims[numBlockDims + 1] : constOne;
231 Value blockSizeZ = numThreadDims > 2 ? dims[numBlockDims + 2] : constOne;
232
233 // Create a launch op and move the body region of the innermost loop to the
234 // launch op.
235 auto launchOp =
236 gpu::LaunchOp::create(builder, rootForOp.getLoc(), gridSizeX, gridSizeY,
237 gridSizeZ, blockSizeX, blockSizeY, blockSizeZ);
238
239 // Replace the loop terminator (loops contain only a single block) with the
240 // gpu terminator and move the operations from the loop body block to the gpu
241 // launch body block. Do not move the entire block because of the difference
242 // in block arguments.
243 Operation &terminator = innermostForOp.getBody()->back();
244 Location terminatorLoc = terminator.getLoc();
245 terminator.erase();
246 builder.setInsertionPointToEnd(innermostForOp.getBody());
247 gpu::TerminatorOp::create(builder, terminatorLoc, TypeRange());
248 launchOp.getBody().front().getOperations().splice(
249 launchOp.getBody().front().begin(),
250 innermostForOp.getBody()->getOperations());
251
252 // Remap the loop iterators to use block/thread identifiers instead. Loops
253 // may iterate from LB with step S whereas GPU thread/block ids always iterate
254 // from 0 to N with step 1. Therefore, loop induction variables are replaced
255 // with (gpu-thread/block-id * S) + LB.
256 builder.setInsertionPointToStart(&launchOp.getBody().front());
257 auto *lbArgumentIt = lbs.begin();
258 auto *stepArgumentIt = steps.begin();
259 for (const auto &en : llvm::enumerate(ivs)) {
260 Value id =
261 en.index() < numBlockDims
262 ? getDim3Value(launchOp.getBlockIds(), en.index())
263 : getDim3Value(launchOp.getThreadIds(), en.index() - numBlockDims);
264 Value step = steps[en.index()];
265 if (getConstantIntValue(step) != static_cast<int64_t>(1))
266 id = arith::MulIOp::create(builder, rootForOp.getLoc(), step, id);
267
268 Value ivReplacement =
269 arith::AddIOp::create(builder, rootForOp.getLoc(), *lbArgumentIt, id);
270 en.value().replaceAllUsesWith(ivReplacement);
271 std::advance(lbArgumentIt, 1);
272 std::advance(stepArgumentIt, 1);
273 }
274
275 // We are done and can erase the original outermost loop.
276 rootForOp.erase();
277}
278
279// Generic loop to GPU kernel conversion function.
280static LogicalResult convertAffineLoopNestToGPULaunch(AffineForOp forOp,
281 unsigned numBlockDims,
282 unsigned numThreadDims) {
283 if (failed(checkAffineLoopNestMappable(forOp, numBlockDims, numThreadDims)))
284 return failure();
285
286 AffineLoopToGpuConverter converter;
287 auto maybeInnerLoop =
288 converter.collectBounds(forOp, numBlockDims + numThreadDims);
289 if (!maybeInnerLoop)
290 return failure();
291 converter.createLaunch(forOp, *maybeInnerLoop, numBlockDims, numThreadDims);
292
293 return success();
294}
295
296LogicalResult mlir::convertAffineLoopNestToGPULaunch(AffineForOp forOp,
297 unsigned numBlockDims,
298 unsigned numThreadDims) {
299 return ::convertAffineLoopNestToGPULaunch(forOp, numBlockDims, numThreadDims);
300}
301
302namespace {
303struct ParallelToGpuLaunchLowering : public OpRewritePattern<ParallelOp> {
304 using OpRewritePattern<ParallelOp>::OpRewritePattern;
305
306 LogicalResult matchAndRewrite(ParallelOp parallelOp,
307 PatternRewriter &rewriter) const override;
308};
309} // namespace
310
311/// Tries to derive a static upper bound from the defining operation of
312/// `upperBound`.
314 PatternRewriter &rewriter) {
315 if (auto op = upperBound.getDefiningOp<arith::ConstantIndexOp>()) {
316 return op;
317 }
318
319 if (auto minOp = upperBound.getDefiningOp<AffineMinOp>()) {
320 for (const AffineExpr &result : minOp.getMap().getResults()) {
321 if (auto constExpr = dyn_cast<AffineConstantExpr>(result)) {
322 return arith::ConstantIndexOp::create(rewriter, minOp.getLoc(),
323 constExpr.getValue());
324 }
325 }
326 }
327
328 if (auto minOp = upperBound.getDefiningOp<arith::MinSIOp>()) {
329 for (Value operand : {minOp.getLhs(), minOp.getRhs()}) {
330 if (auto staticBound = deriveStaticUpperBound(operand, rewriter))
331 return staticBound;
332 }
333 }
334
335 if (auto multiplyOp = upperBound.getDefiningOp<arith::MulIOp>()) {
336 if (auto lhs = deriveStaticUpperBound(multiplyOp.getOperand(0), rewriter)
338 if (auto rhs = deriveStaticUpperBound(multiplyOp.getOperand(1), rewriter)
340 // Assumptions about the upper bound of minimum computations no longer
341 // work if multiplied by mixed signs, so abort in this case.
342 if ((lhs.value() < 0) != (rhs.value() < 0))
343 return {};
344
345 return arith::ConstantIndexOp::create(rewriter, multiplyOp.getLoc(),
346 lhs.value() * rhs.value());
347 }
348 }
349
350 return {};
351}
352
353static bool isMappedToProcessor(gpu::Processor processor) {
354 return processor != gpu::Processor::Sequential;
355}
356
357static unsigned getLaunchOpArgumentNum(gpu::Processor processor) {
358 switch (processor) {
359 case gpu::Processor::BlockX:
360 return 0;
361 case gpu::Processor::BlockY:
362 return 1;
363 case gpu::Processor::BlockZ:
364 return 2;
365 case gpu::Processor::ThreadX:
366 return 3;
367 case gpu::Processor::ThreadY:
368 return 4;
369 case gpu::Processor::ThreadZ:
370 return 5;
371 default:;
372 }
373 llvm_unreachable(
374 "invalid processor type while retrieving launch op argument number");
375}
376
377/// Modifies the current transformation state to capture the effect of the given
378/// `scf.parallel` operation on index substitutions and the operations to be
379/// inserted.
380/// Specifically, if a dimension of a parallel loop is mapped to a hardware id,
381/// this function will
382/// - compute the loop index based on the hardware id and affine map from the
383/// mapping and update `cloningMap` to substitute all uses.
384/// - derive a new upper bound for the hardware id and augment the provided
385/// `gpu.launch operation` accordingly.
386/// - if the upper bound is imprecise, insert a conditional in the `gpu.launch`
387/// and update the rewriter to insert into the conditional's body.
388/// If the dimension is mapped to sequential,
389/// - insert a for loop into the body and update the rewriter to insert into
390/// the for loop's body.
391/// - update the `cloningMap` to replace uses of the index with the index of
392/// the new for loop.
393/// In either case,
394/// - append the instructions from the loops body to worklist, in reverse order.
395/// To note the end of the current scope in case a loop or conditional was
396/// inserted, a sentinel (the `gpu.launch` operation) is inserted into the
397/// worklist. This signals the processor of the worklist to pop the rewriter
398/// one scope-level up.
399static LogicalResult processParallelLoop(
400 ParallelOp parallelOp, gpu::LaunchOp launchOp, IRMapping &cloningMap,
403 // TODO: Verify that this is a valid GPU mapping.
404 // processor ids: 0-2 block [x/y/z], 3-5 -> thread [x/y/z], 6-> sequential
405 ArrayAttr mapping =
406 parallelOp->getAttrOfType<ArrayAttr>(gpu::getMappingAttrName());
407
408 // TODO: Support multiple reductions.
409 if (!mapping || parallelOp.getNumResults() > 1)
410 return failure();
411
412 Location loc = parallelOp.getLoc();
413
414 auto launchIndependent = [&launchOp](Value val) {
415 return val.getParentRegion()->isAncestor(launchOp->getParentRegion());
416 };
417
418 auto ensureLaunchIndependent = [&rewriter,
419 launchIndependent](Value val) -> Value {
420 if (launchIndependent(val))
421 return val;
422 if (std::optional<int64_t> constOp = getConstantIntValue(val))
423 return arith::ConstantIndexOp::create(rewriter, val.getLoc(),
424 constOp.value());
425 return {};
426 };
427
428 for (auto config : llvm::zip(
429 mapping, parallelOp.getInductionVars(), parallelOp.getLowerBound(),
430 parallelOp.getUpperBound(), parallelOp.getStep())) {
431 Attribute mappingAttribute;
432 Value iv, lowerBound, upperBound, step;
433 std::tie(mappingAttribute, iv, lowerBound, upperBound, step) = config;
434 auto annotation =
435 dyn_cast<gpu::ParallelLoopDimMappingAttr>(mappingAttribute);
436 if (!annotation)
437 return parallelOp.emitOpError()
438 << "expected mapping attribute for lowering to GPU";
439 Value newIndex;
440 gpu::Processor processor = annotation.getProcessor();
441
442 if (isMappedToProcessor(processor)) {
443 // Use the corresponding thread/grid index as replacement for the loop iv.
444 Value operand =
445 launchOp.getBody().getArgument(getLaunchOpArgumentNum(processor));
446 // Take the indexmap and add the lower bound and step computations in.
447 // This computes operand * step + lowerBound.
448 // Use an affine map here so that it composes nicely with the provided
449 // annotation.
450 AffineMap lowerAndStep = AffineMap::get(
451 1, 2,
452 rewriter.getAffineDimExpr(0) * rewriter.getAffineSymbolExpr(0) +
453 rewriter.getAffineSymbolExpr(1));
454 // Map through cloningMap first so we use values valid at the launch
455 // scope, then ensure they are launch-independent (or cloned constants).
456 Value mappedStep = cloningMap.lookupOrDefault(step);
457 Value mappedLowerBound = cloningMap.lookupOrDefault(lowerBound);
458
459 mappedStep = ensureLaunchIndependent(mappedStep);
460 mappedLowerBound = ensureLaunchIndependent(mappedLowerBound);
461
462 // If either cannot be made available above the launch, fail gracefully.
463 if (!mappedStep || !mappedLowerBound) {
464 return rewriter.notifyMatchFailure(
465 parallelOp, "lower bound / step must be constant or defined above "
466 "the gpu.launch");
467 }
468
469 newIndex = AffineApplyOp::create(
470 rewriter, loc, annotation.getMap().compose(lowerAndStep),
471 ValueRange{operand, mappedStep, mappedLowerBound});
472 // If there was also a bound, insert that, too.
473 // TODO: Check that we do not assign bounds twice.
474 if (annotation.getBound()) {
475 // We pass as the single operand to the bound-map the number of
476 // iterations, which is (upperBound - lowerBound) ceilDiv step. To
477 // support inner loops with dynamic upper bounds (as generated by e.g.
478 // tiling), try to derive a max for the bounds. If the used bound for
479 // the hardware id is imprecise, wrap the contained code into a
480 // conditional. If the lower-bound is constant or defined before the
481 // launch, we can use it in the launch bounds. Otherwise fail.
482 if (!launchIndependent(lowerBound) &&
483 !getConstantIntValue(lowerBound).has_value())
484 return failure();
485 // The step must also be constant or defined outside of the loop nest.
486 if (!launchIndependent(step) && !getConstantIntValue(step).has_value())
487 return failure();
488 // If the upper-bound is constant or defined before the launch, we can
489 // use it in the launch bounds directly. Otherwise try derive a bound.
490 bool boundIsPrecise = launchIndependent(upperBound) ||
491 getConstantIntValue(upperBound).has_value();
492 {
493 PatternRewriter::InsertionGuard guard(rewriter);
494 rewriter.setInsertionPoint(launchOp);
495 if (!boundIsPrecise) {
496 upperBound = deriveStaticUpperBound(upperBound, rewriter);
497 if (!upperBound) {
498 return rewriter.notifyMatchFailure(
499 parallelOp,
500 "cannot derive loop-invariant upper bound for number of"
501 "iterations");
502 }
503 }
504 // Compute the number of iterations needed. We compute this as an
505 // affine expression ceilDiv (upperBound - lowerBound) step. We use
506 // affine.apply here so that it composes nicely with the provided map.
507 AffineMap stepMap = AffineMap::get(
508 1, 2,
509 ((rewriter.getAffineDimExpr(0) - rewriter.getAffineSymbolExpr(0))
510 .ceilDiv(rewriter.getAffineSymbolExpr(1))));
511 Value launchBound = AffineApplyOp::create(
512 rewriter, loc, annotation.getBound().compose(stepMap),
514 ensureLaunchIndependent(
515 cloningMap.lookupOrDefault(upperBound)),
516 ensureLaunchIndependent(
517 cloningMap.lookupOrDefault(lowerBound)),
518 ensureLaunchIndependent(cloningMap.lookupOrDefault(step))});
519 // todo(herhut,ravishankarm): Update the behavior of setMappingAttr
520 // when this condition is relaxed.
521 if (!bounds.try_emplace(processor, launchBound).second) {
522 return rewriter.notifyMatchFailure(
523 parallelOp, "cannot redefine the bound for processor " +
524 Twine(static_cast<int64_t>(processor)));
525 }
526 }
527 if (!boundIsPrecise) {
528 // We are using an approximation, create a surrounding conditional.
529 Value originalBound = std::get<3>(config);
530 arith::CmpIOp pred = arith::CmpIOp::create(
531 rewriter, loc, arith::CmpIPredicate::slt, newIndex,
532 cloningMap.lookupOrDefault(originalBound));
533 scf::IfOp ifOp = scf::IfOp::create(rewriter, loc, pred, false);
534 rewriter.setInsertionPointToStart(&ifOp.getThenRegion().front());
535 // Put a sentinel into the worklist so we know when to pop out of the
536 // if body again. We use the launchOp here, as that cannot be part of
537 // the bodies instruction.
538 worklist.push_back(launchOp.getOperation());
539 }
540 }
541 } else {
542 // Create a sequential for loop.
543 auto loopOp = scf::ForOp::create(rewriter, loc,
544 cloningMap.lookupOrDefault(lowerBound),
545 cloningMap.lookupOrDefault(upperBound),
546 cloningMap.lookupOrDefault(step));
547 newIndex = loopOp.getInductionVar();
548 rewriter.setInsertionPointToStart(loopOp.getBody());
549 // Put a sentinel into the worklist so we know when to pop out of the loop
550 // body again. We use the launchOp here, as that cannot be part of the
551 // bodies instruction.
552 worklist.push_back(launchOp.getOperation());
553 }
554 cloningMap.map(iv, newIndex);
555 }
556
557 // Propagate custom user defined optional attributes, that can be used at
558 // later stage, such as extension data for GPU kernel dispatch
559 for (const auto &namedAttr : parallelOp->getAttrs()) {
560 if (namedAttr.getName() == gpu::getMappingAttrName() ||
561 namedAttr.getName() == ParallelOp::getOperandSegmentSizeAttr())
562 continue;
563 launchOp->setAttr(namedAttr.getName(), namedAttr.getValue());
564 }
565
566 Block *body = parallelOp.getBody();
567 worklist.reserve(worklist.size() + body->getOperations().size());
568 // Include scf.reduce terminator if exists and has an operand.
569 if (auto terminator = body->getTerminator();
570 isa<scf::ReduceOp>(terminator) && terminator->getOperands().size() == 1) {
571 worklist.push_back(terminator);
572 }
573 for (Operation &op : llvm::reverse(body->without_terminator()))
574 worklist.push_back(&op);
575 return success();
576}
577
578/// Lower a `scf.parallel` operation into a corresponding `gpu.launch`
579/// operation.
580///
581/// This essentially transforms a loop nest into a corresponding SIMT function.
582/// The conversion is driven by mapping annotations on the `scf.parallel`
583/// operations. The mapping is provided via a `DictionaryAttribute` named
584/// `mapping`, which has three entries:
585/// - processor: the hardware id to map to. 0-2 are block dimensions, 3-5 are
586/// thread dimensions and 6 is sequential.
587/// - map : An affine map that is used to pre-process hardware ids before
588/// substitution.
589/// - bound : An affine map that is used to compute the bound of the hardware
590/// id based on an upper bound of the number of iterations.
591/// If the `scf.parallel` contains nested `scf.parallel` operations, those
592/// need to be annotated, as well. Structurally, the transformation works by
593/// splicing all operations from nested `scf.parallel` operations into a single
594/// sequence. Indices mapped to hardware ids are substituted with those ids,
595/// wheras sequential mappings result in a sequential for-loop. To have more
596/// flexibility when mapping code to hardware ids, the transform supports two
597/// affine maps. The first `map` is used to compute the actual index for
598/// substitution from the hardware id. The second `bound` is used to compute the
599/// launch dimension for the hardware id from the number of iterations the
600/// mapped loop is performing. Note that the number of iterations might be
601/// imprecise if the corresponding loop-bounds are loop-dependent. In such case,
602/// the hardware id might iterate over additional indices. The transformation
603/// caters for this by predicating the created sequence of instructions on
604/// the actual loop bound. This only works if an static upper bound for the
605/// dynamic loop bound can be derived, currently via analyzing `affine.min`
606/// operations.
607LogicalResult
608ParallelToGpuLaunchLowering::matchAndRewrite(ParallelOp parallelOp,
609 PatternRewriter &rewriter) const {
610 // Mark the operation as visited for recursive legality check.
611 parallelOp->setAttr(kVisitedAttrName, rewriter.getUnitAttr());
612
613 // We can only transform starting at the outer-most loop. Launches inside of
614 // parallel loops are not supported.
615 if (auto parentLoop = parallelOp->getParentOfType<ParallelOp>())
616 return failure();
617 // Create a launch operation. We start with bound one for all grid/block
618 // sizes. Those will be refined later as we discover them from mappings.
619 Location loc = parallelOp.getLoc();
620 Value constantOne = arith::ConstantIndexOp::create(rewriter, loc, 1);
621 gpu::LaunchOp launchOp =
622 gpu::LaunchOp::create(rewriter, loc, constantOne, constantOne,
623 constantOne, constantOne, constantOne, constantOne);
624 rewriter.setInsertionPointToEnd(&launchOp.getBody().front());
625 gpu::TerminatorOp::create(rewriter, loc);
626 rewriter.setInsertionPointToStart(&launchOp.getBody().front());
627
628 IRMapping cloningMap;
629 llvm::DenseMap<gpu::Processor, Value> launchBounds;
630 SmallVector<Operation *, 16> worklist;
631 if (failed(processParallelLoop(parallelOp, launchOp, cloningMap, worklist,
632 launchBounds, rewriter)))
633 return failure();
634
635 // Whether we have seen any side-effects. Reset when leaving an inner scope.
636 bool seenSideeffects = false;
637 // Whether we have left a nesting scope (and hence are no longer innermost).
638 bool leftNestingScope = false;
639 LocalAliasAnalysis aliasAnalysis;
640 llvm::DenseSet<Value> writtenBuffer;
641 while (!worklist.empty()) {
642 Operation *op = worklist.pop_back_val();
643 // Now walk over the body and clone it.
644 // TODO: This is only correct if there either is no further scf.parallel
645 // nested or this code has side-effect but the memory buffer is not
646 // alias to inner loop access buffer. Otherwise we might need
647 // predication.
648 if (auto nestedParallel = dyn_cast<ParallelOp>(op)) {
649 // Before entering a nested scope, make sure there have been no
650 // sideeffects until now or the nested operations do not access the
651 // buffer written by outer scope.
652 if (seenSideeffects) {
653 WalkResult walkRes = nestedParallel.walk([&](Operation *nestedOp) {
654 if (isMemoryEffectFree(nestedOp))
655 return WalkResult::advance();
656
657 auto memEffectInterface = dyn_cast<MemoryEffectOpInterface>(nestedOp);
658 if (!memEffectInterface)
659 return WalkResult::advance();
660
661 SmallVector<MemoryEffects::EffectInstance> effects;
662 memEffectInterface.getEffects(effects);
663 for (const MemoryEffects::EffectInstance &effect : effects) {
664 if (isa<MemoryEffects::Read>(effect.getEffect()) ||
665 isa<MemoryEffects::Write>(effect.getEffect())) {
666 Value baseBuffer = effect.getValue();
667 if (!baseBuffer)
668 return WalkResult::interrupt();
669 for (Value val : writtenBuffer) {
670 if (aliasAnalysis.alias(baseBuffer, val) !=
672 return WalkResult::interrupt();
673 }
674 }
675 }
676 }
677 return WalkResult::advance();
678 });
679 if (walkRes.wasInterrupted())
680 return failure();
681 }
682 // A nested scf.parallel needs insertion of code to compute indices.
683 // Insert that now. This will also update the worklist with the loops
684 // body.
685 if (failed(processParallelLoop(nestedParallel, launchOp, cloningMap,
686 worklist, launchBounds, rewriter)))
687 return failure();
688 } else if (op == launchOp.getOperation()) {
689 // Found our sentinel value. We have finished the operations from one
690 // nesting level, pop one level back up.
691 auto *parent = rewriter.getInsertionPoint()->getParentOp();
692 rewriter.setInsertionPointAfter(parent);
693 leftNestingScope = true;
694 seenSideeffects = false;
695 writtenBuffer.clear();
696 } else if (auto reduceOp = dyn_cast<scf::ReduceOp>(op)) {
697 // Convert scf.reduction op
698 auto parentLoop = op->getParentOfType<ParallelOp>();
699 if (!parentLoop || op->getOperands().size() != 1)
700 return failure();
701 auto operand = op->getOperands().front();
702 auto newValue = cloningMap.lookupOrNull(operand);
703 if (!newValue || !operand.getType().isSignlessIntOrFloat())
704 return failure();
705 // Ensure reduction region is isolated from above.
706 llvm::SetVector<Value> externalValues;
707 getUsedValuesDefinedAbove(reduceOp.getRegion(0), externalValues);
708 if (externalValues.size())
709 return failure();
710 // Replace by gpu.all_reduce.
711 auto gpuRedOp = gpu::AllReduceOp::create(rewriter, loc, newValue);
712 cloningMap.map(parentLoop->getResult(0), gpuRedOp.getResult());
713 // Copy region.
714 rewriter.inlineRegionBefore(reduceOp.getRegion(0), gpuRedOp.getRegion(),
715 gpuRedOp.getRegion().begin());
716 // Replace src.reduce.return with gpu.yield.
717 auto scfReturn = gpuRedOp.getRegion().front().getTerminator();
718 auto ip = rewriter.saveInsertionPoint();
719 rewriter.setInsertionPointToEnd(&gpuRedOp.getRegion().front());
720 rewriter.replaceOpWithNewOp<gpu::YieldOp>(
721 scfReturn, scfReturn->getOperands().front());
722 rewriter.restoreInsertionPoint(ip);
723 } else {
724 // Otherwise we copy it over.
725 Operation *clone = rewriter.clone(*op, cloningMap);
726 cloningMap.map(op->getResults(), clone->getResults());
727 // Check for side effects.
729 // Record the buffer accessed by the operations with write effects.
730 if (auto memEffectInterface =
731 dyn_cast<MemoryEffectOpInterface>(clone)) {
732 SmallVector<MemoryEffects::EffectInstance> effects;
733 memEffectInterface.getEffects(effects);
734 for (const MemoryEffects::EffectInstance &effect : effects) {
735 if (isa<MemoryEffects::Write>(effect.getEffect())) {
736 Value writtenBase = effect.getValue();
737 // Conservatively return failure if we cannot find the written
738 // address.
739 if (!writtenBase)
740 return failure();
741 writtenBuffer.insert(writtenBase);
742 }
743 }
744 }
745 }
746 // TODO: Handle region side effects properly.
747 seenSideeffects |=
749 // If we are no longer in the innermost scope, sideeffects are disallowed.
750 if (seenSideeffects && leftNestingScope)
751 return failure();
752 }
753 }
754
755 // Now that we succeeded creating the launch operation, also update the
756 // bounds.
757 for (auto bound : launchBounds)
758 launchOp.setOperand(getLaunchOpArgumentNum(std::get<0>(bound)),
759 std::get<1>(bound));
760
761 rewriter.eraseOp(parallelOp);
762 return success();
763}
764
766 patterns.add<ParallelToGpuLaunchLowering>(patterns.getContext());
767}
768
770 target.addLegalDialect<memref::MemRefDialect>();
771 target.addDynamicallyLegalOp<scf::ParallelOp>([](scf::ParallelOp parallelOp) {
772 return !parallelOp->hasAttr(gpu::getMappingAttrName()) ||
773 parallelOp->hasAttr(kVisitedAttrName);
774 });
775}
776
778 op->walk([](scf::ParallelOp parallelOp) {
779 parallelOp->removeAttr(kVisitedAttrName);
780 });
781}
return success()
lhs
ArrayAttr()
static LogicalResult checkAffineLoopNestMappableImpl(AffineForOp forOp, unsigned numDims)
Definition SCFToGPU.cpp:109
static Value getOrEmitUpperBound(AffineForOp forOp, OpBuilder &builder)
Definition SCFToGPU.cpp:99
static Value getDim3Value(const gpu::KernelDim3 &dim3, unsigned pos)
Definition SCFToGPU.cpp:60
static LogicalResult processParallelLoop(ParallelOp parallelOp, gpu::LaunchOp launchOp, IRMapping &cloningMap, SmallVectorImpl< Operation * > &worklist, DenseMap< gpu::Processor, Value > &bounds, PatternRewriter &rewriter)
Modifies the current transformation state to capture the effect of the given scf.parallel operation o...
Definition SCFToGPU.cpp:399
static bool isMappedToProcessor(gpu::Processor processor)
Definition SCFToGPU.cpp:353
static Operation::operand_range getLowerBoundOperands(AffineForOp forOp)
Definition SCFToGPU.cpp:75
static Value getOrCreateStep(AffineForOp forOp, OpBuilder &builder)
Definition SCFToGPU.cpp:86
static Value getOrEmitLowerBound(AffineForOp forOp, OpBuilder &builder)
Definition SCFToGPU.cpp:93
static Value deriveStaticUpperBound(Value upperBound, PatternRewriter &rewriter)
Tries to derive a static upper bound from the defining operation of upperBound.
Definition SCFToGPU.cpp:313
static unsigned getLaunchOpArgumentNum(gpu::Processor processor)
Definition SCFToGPU.cpp:357
static constexpr StringLiteral kVisitedAttrName
Definition SCFToGPU.cpp:57
static Operation::operand_range getUpperBoundOperands(AffineForOp forOp)
Definition SCFToGPU.cpp:80
static LogicalResult checkAffineLoopNestMappable(AffineForOp forOp, unsigned numBlockDims, unsigned numThreadDims)
Definition SCFToGPU.cpp:135
Base type for affine expression.
Definition AffineExpr.h:68
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition AffineMap.h:46
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
@ NoAlias
The two locations do not alias at all.
Attributes are known-constant values of operations.
Definition Attributes.h:25
Block represents an ordered list of Operations.
Definition Block.h:33
OpListType & getOperations()
Definition Block.h:147
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
UnitAttr getUnitAttr()
Definition Builders.cpp:98
AffineExpr getAffineSymbolExpr(unsigned position)
Definition Builders.cpp:368
AffineExpr getAffineDimExpr(unsigned position)
Definition Builders.cpp:364
This is a utility class for mapping one set of IR entities to another.
Definition IRMapping.h:26
auto lookupOrDefault(T from) const
Lookup a mapped value within the map.
Definition IRMapping.h:65
void map(Value from, Value to)
Inserts a new mapping for 'from' to 'to'.
Definition IRMapping.h:30
auto lookupOrNull(T from) const
Lookup a mapped value within the map.
Definition IRMapping.h:58
AliasResult alias(Value lhs, Value rhs)
Given two values, return their aliasing behavior.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
RAII guard to reset the insertion point of the builder when destroyed.
Definition Builders.h:348
This class helps build Operations.
Definition Builders.h:207
InsertPoint saveInsertionPoint() const
Return a saved insertion point.
Definition Builders.h:385
Block::iterator getInsertionPoint() const
Returns the current insertion point of the builder.
Definition Builders.h:445
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:562
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition Builders.h:431
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition Builders.h:398
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Definition Builders.h:436
void restoreInsertionPoint(InsertPoint ip)
Restore the insert point to a previously saved point.
Definition Builders.h:390
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
Definition Builders.h:412
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
unsigned getNumRegions()
Returns the number of regions held by this operation.
Definition Operation.h:674
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:223
OperandRange operand_range
Definition Operation.h:371
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition Operation.h:238
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition Operation.h:378
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
Definition Operation.h:797
result_range getResults()
Definition Operation.h:415
void erase()
Remove this operation from its parent block and delete it.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
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 inlineRegionBefore(Region &region, Region &parent, Region::iterator before)
Move the blocks that belong to "region" before the given position in another region "parent".
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:387
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition Value.cpp:18
static WalkResult advance()
Definition WalkResult.h:47
bool wasInterrupted() const
Returns true if the walk was interrupted.
Definition WalkResult.h:51
static WalkResult interrupt()
Definition WalkResult.h:46
Specialization of arith.constant op that returns an integer of index type.
Definition Arith.h:113
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
Definition ArithOps.cpp:359
SideEffects::EffectInstance< Effect > EffectInstance
StringRef getMappingAttrName()
Name of the mapping attribute produced by loop mappers.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:573
Value constantOne(OpBuilder &builder, Location loc, Type tp)
Generates a 1-valued constant of the given type.
Include the generated interface declarations.
void finalizeParallelLoopToGPUConversion(Operation *op)
Clean up after applyPartialConversion/applyFullConversion call.
Definition SCFToGPU.cpp:777
void populateParallelLoopToGPUPatterns(RewritePatternSet &patterns)
Adds the conversion pattern from scf.parallel to gpu.launch to the provided pattern list.
Definition SCFToGPU.cpp:765
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
const FrozenRewritePatternSet GreedyRewriteConfig config
LogicalResult convertAffineLoopNestToGPULaunch(affine::AffineForOp forOp, unsigned numBlockDims, unsigned numThreadDims)
Convert a perfect affine loop nest with the outermost loop identified by forOp into a gpu::Launch ope...
bool isMemoryEffectFree(Operation *op)
Returns true if the given operation is free of memory effects.
Value lowerAffineUpperBound(affine::AffineForOp op, OpBuilder &builder)
Emit code that computes the upper bound of the given affine loop using standard arithmetic operations...
const FrozenRewritePatternSet & patterns
void getUsedValuesDefinedAbove(Region &region, Region &limit, SetVector< Value > &values)
Fill values with a list of values defined at the ancestors of the limit region and used within region...
Operation * clone(OpBuilder &b, Operation *op, TypeRange newResultTypes, ValueRange newOperands)
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:126
bool areValuesDefinedAbove(Range values, Region &limit)
Check if all values in the provided range are defined above the limit region.
Definition RegionUtils.h:26
void configureParallelLoopToGPULegality(ConversionTarget &target)
Configures the rewrite target such that only scf.parallel operations that are not rewritten by the pr...
Definition SCFToGPU.cpp:769
Value lowerAffineLowerBound(affine::AffineForOp op, OpBuilder &builder)
Emit code that computes the lower bound of the given affine loop using standard arithmetic operations...
Utility class for the GPU dialect to represent triples of Values accessible through ....
Definition GPUDialect.h:39