MLIR 23.0.0git
ForallToFor.cpp
Go to the documentation of this file.
1//===- ForallToFor.cpp - scf.forall to scf.for loop conversion ------------===//
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// Transforms SCF.ForallOp's into SCF.ForOp's.
10//
11//===----------------------------------------------------------------------===//
12
14
18
19namespace mlir {
20#define GEN_PASS_DEF_SCFFORALLTOFORLOOP
21#include "mlir/Dialect/SCF/Transforms/Passes.h.inc"
22} // namespace mlir
23
24using namespace mlir;
25using scf::LoopNest;
26
27LogicalResult
28mlir::scf::forallToForLoop(RewriterBase &rewriter, scf::ForallOp forallOp,
30 OpBuilder::InsertionGuard guard(rewriter);
31 rewriter.setInsertionPoint(forallOp);
32
33 if (!forallOp.getOutputs().empty()) {
34 forallOp.emitWarning()
35 << "skipping scf.forall with outputs, currently not supported";
36 return success();
37 }
38
39 Location loc = forallOp.getLoc();
40 SmallVector<Value> lbs = forallOp.getLowerBound(rewriter);
41 SmallVector<Value> ubs = forallOp.getUpperBound(rewriter);
42 SmallVector<Value> steps = forallOp.getStep(rewriter);
43 LoopNest loopNest = scf::buildLoopNest(rewriter, loc, lbs, ubs, steps);
44
45 SmallVector<Value> ivs = llvm::map_to_vector(
46 loopNest.loops, [](scf::ForOp loop) { return loop.getInductionVar(); });
47
48 Block *innermostBlock = loopNest.loops.back().getBody();
49 rewriter.eraseOp(forallOp.getBody()->getTerminator());
50 rewriter.inlineBlockBefore(forallOp.getBody(), innermostBlock,
51 innermostBlock->getTerminator()->getIterator(),
52 ivs);
53 rewriter.eraseOp(forallOp);
54
55 if (results) {
56 llvm::move(loopNest.loops, std::back_inserter(*results));
57 }
58
59 return success();
60}
61
62namespace {
63struct ForallToForLoop : public impl::SCFForallToForLoopBase<ForallToForLoop> {
64 void runOnOperation() override {
65 Operation *parentOp = getOperation();
66 IRRewriter rewriter(parentOp->getContext());
67
68 parentOp->walk([&](scf::ForallOp forallOp) {
69 if (failed(scf::forallToForLoop(rewriter, forallOp))) {
70 return signalPassFailure();
71 }
72 });
73 }
74};
75} // namespace
76
77std::unique_ptr<Pass> mlir::createForallToForLoopPass() {
78 return std::make_unique<ForallToForLoop>();
79}
return success()
Block represents an ordered list of Operations.
Definition Block.h:33
Operation & back()
Definition Block.h:162
Operation * getTerminator()
Get the terminator operation of this block.
Definition Block.cpp:249
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
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition Builders.h:398
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
MLIRContext * getContext()
Return the context this operation is associated with.
Definition Operation.h:216
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
virtual void inlineBlockBefore(Block *source, Block *dest, Block::iterator before, ValueRange argValues={})
Inline the operations of block 'source' into block 'dest' before the given position.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:573
LogicalResult forallToForLoop(RewriterBase &rewriter, ForallOp forallOp, SmallVectorImpl< Operation * > *results=nullptr)
Try converting scf.forall into a set of nested scf.for loops.
LoopNest buildLoopNest(OpBuilder &builder, Location loc, ValueRange lbs, ValueRange ubs, ValueRange steps, ValueRange iterArgs, function_ref< ValueVector(OpBuilder &, Location, ValueRange, ValueRange)> bodyBuilder=nullptr)
Creates a perfect nest of "for" loops, i.e.
Definition SCF.cpp:777
Include the generated interface declarations.
std::unique_ptr< Pass > createForallToForLoopPass()
Creates a pass that converts SCF forall loops to SCF for loops.