1//===- ParallelForToNestedFors.cpp - scf.parallel to nested scf.for ops --===//
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.ParallelOp to nested scf.for ops.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Dialect/SCF/IR/SCF.h"
14#include "mlir/Dialect/SCF/Transforms/Passes.h"
15#include "mlir/Dialect/SCF/Transforms/Transforms.h"
16#include "mlir/IR/PatternMatch.h"
17#include "llvm/Support/Debug.h"
18
19namespace mlir {
20#define GEN_PASS_DEF_SCFPARALLELFORTONESTEDFORS
21#include "mlir/Dialect/SCF/Transforms/Passes.h.inc"
22} // namespace mlir
23
24#define DEBUG_TYPE "parallel-for-to-nested-fors"
25using namespace mlir;
26
27FailureOr<scf::LoopNest>
28mlir::scf::parallelForToNestedFors(RewriterBase &rewriter,
29 scf::ParallelOp parallelOp) {
30
31 if (!parallelOp.getResults().empty())
32 return rewriter.notifyMatchFailure(
33 arg&: parallelOp, msg: "Currently scf.parallel to scf.for conversion doesn't "
34 "support scf.parallel with results.");
35
36 rewriter.setInsertionPoint(parallelOp);
37
38 Location loc = parallelOp.getLoc();
39 SmallVector<Value> lowerBounds = parallelOp.getLowerBound();
40 SmallVector<Value> upperBounds = parallelOp.getUpperBound();
41 SmallVector<Value> steps = parallelOp.getStep();
42
43 assert(lowerBounds.size() == upperBounds.size() &&
44 lowerBounds.size() == steps.size() &&
45 "Mismatched parallel loop bounds");
46
47 scf::LoopNest loopNest =
48 scf::buildLoopNest(builder&: rewriter, loc, lbs: lowerBounds, ubs: upperBounds, steps);
49 for (scf::ForOp forOp : loopNest.loops)
50 forOp.setUnsignedCmp(parallelOp.getUnsignedCmp());
51
52 SmallVector<Value> newInductionVars = llvm::map_to_vector(
53 C&: loopNest.loops, F: [](scf::ForOp forOp) { return forOp.getInductionVar(); });
54 Block *linearizedBody = loopNest.loops.back().getBody();
55 Block *parallelBody = parallelOp.getBody();
56 rewriter.eraseOp(op: parallelBody->getTerminator());
57 rewriter.inlineBlockBefore(source: parallelBody, op: linearizedBody->getTerminator(),
58 argValues: newInductionVars);
59 rewriter.eraseOp(op: parallelOp);
60 return loopNest;
61}
62
63namespace {
64struct ParallelForToNestedFors final
65 : public impl::SCFParallelForToNestedForsBase<ParallelForToNestedFors> {
66 void runOnOperation() override {
67 Operation *parentOp = getOperation();
68 IRRewriter rewriter(parentOp->getContext());
69
70 parentOp->walk(
71 callback: [&](scf::ParallelOp parallelOp) {
72 if (failed(Result: scf::parallelForToNestedFors(rewriter, parallelOp))) {
73 LLVM_DEBUG(
74 llvm::dbgs()
75 << "Failed to convert scf.parallel to nested scf.for ops for:\n"
76 << parallelOp << "\n");
77 return WalkResult::advance();
78 }
79 return WalkResult::advance();
80 });
81 }
82};
83} // namespace
84
85std::unique_ptr<Pass> mlir::createParallelForToNestedForsPass() {
86 return std::make_unique<ParallelForToNestedFors>();
87}
88