MLIR 22.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 Location loc = forallOp.getLoc();
34 SmallVector<Value> lbs = forallOp.getLowerBound(rewriter);
35 SmallVector<Value> ubs = forallOp.getUpperBound(rewriter);
36 SmallVector<Value> steps = forallOp.getStep(rewriter);
37 LoopNest loopNest = scf::buildLoopNest(rewriter, loc, lbs, ubs, steps);
38
39 SmallVector<Value> ivs = llvm::map_to_vector(
40 loopNest.loops, [](scf::ForOp loop) { return loop.getInductionVar(); });
41
42 Block *innermostBlock = loopNest.loops.back().getBody();
43 rewriter.eraseOp(forallOp.getBody()->getTerminator());
44 rewriter.inlineBlockBefore(forallOp.getBody(), innermostBlock,
45 innermostBlock->getTerminator()->getIterator(),
46 ivs);
47 rewriter.eraseOp(forallOp);
48
49 if (results) {
50 llvm::move(loopNest.loops, std::back_inserter(*results));
51 }
52
53 return success();
54}
55
56namespace {
57struct ForallToForLoop : public impl::SCFForallToForLoopBase<ForallToForLoop> {
58 void runOnOperation() override {
59 Operation *parentOp = getOperation();
60 IRRewriter rewriter(parentOp->getContext());
61
62 parentOp->walk([&](scf::ForallOp forallOp) {
63 if (failed(scf::forallToForLoop(rewriter, forallOp))) {
64 return signalPassFailure();
65 }
66 });
67 }
68};
69} // namespace
70
71std::unique_ptr<Pass> mlir::createForallToForLoopPass() {
72 return std::make_unique<ForallToForLoop>();
73}
return success()
Block represents an ordered list of Operations.
Definition Block.h:33
Operation & back()
Definition Block.h:152
Operation * getTerminator()
Get the terminator operation of this block.
Definition Block.cpp:244
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:561
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:837
Include the generated interface declarations.
std::unique_ptr< Pass > createForallToForLoopPass()
Creates a pass that converts SCF forall loops to SCF for loops.