1//=== TestParallelLoopUnrolling.cpp - loop unrolling test pass ===//
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 file implements a pass to unroll loops by a specified unroll factor.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Dialect/Arith/IR/Arith.h"
14#include "mlir/Dialect/SCF/IR/SCF.h"
15#include "mlir/Dialect/SCF/Utils/Utils.h"
16#include "mlir/IR/Builders.h"
17#include "mlir/Pass/Pass.h"
18#include "llvm/ADT/STLExtras.h"
19
20using namespace mlir;
21
22namespace {
23
24static unsigned getNestingDepth(Operation *op) {
25 Operation *currOp = op;
26 unsigned depth = 0;
27 while ((currOp = currOp->getParentOp())) {
28 if (isa<scf::ParallelOp>(Val: currOp))
29 depth++;
30 }
31 return depth;
32}
33
34struct TestParallelLoopUnrollingPass
35 : public PassWrapper<TestParallelLoopUnrollingPass, OperationPass<>> {
36 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestParallelLoopUnrollingPass)
37
38 StringRef getArgument() const final { return "test-parallel-loop-unrolling"; }
39 StringRef getDescription() const final {
40 return "Tests parallel loop unrolling transformation";
41 }
42 TestParallelLoopUnrollingPass() = default;
43 TestParallelLoopUnrollingPass(const TestParallelLoopUnrollingPass &) {}
44
45 void getDependentDialects(DialectRegistry &registry) const override {
46 registry.insert<arith::ArithDialect>();
47 }
48
49 void runOnOperation() override {
50 if (unrollFactors.empty()) {
51 emitError(loc: UnknownLoc::get(context: &getContext()),
52 message: "missing `unroll-factors` pass option");
53 signalPassFailure();
54 return;
55 }
56 if (llvm::is_contained(Range&: unrollFactors, Element: 0)) {
57 emitError(loc: UnknownLoc::get(context: &getContext()),
58 message: "unroll factors must be non-zero");
59 signalPassFailure();
60 return;
61 }
62
63 SmallVector<scf::ParallelOp, 4> loops;
64 getOperation()->walk(callback: [&](scf::ParallelOp parLoop) {
65 if (getNestingDepth(op: parLoop) == loopDepth)
66 loops.push_back(Elt: parLoop);
67 });
68 auto annotateFn = [this](unsigned i, Operation *op, OpBuilder b) {
69 if (annotateLoop) {
70 op->setDiscardableAttr(name: "unrolled_iteration", value: b.getUI32IntegerAttr(value: i));
71 }
72 };
73 PatternRewriter rewriter(getOperation()->getContext());
74 for (auto loop : loops) {
75 (void)parallelLoopUnrollByFactors(op: loop, unrollFactors, rewriter,
76 annotateFn);
77 }
78 }
79
80 ListOption<uint64_t> unrollFactors{
81 *this, "unroll-factors",
82 llvm::cl::desc(
83 "Unroll factors for each parallel loop dim. If fewer factors than "
84 "loop dims are provided, they are applied to the inner dims.")};
85 Option<unsigned> loopDepth{*this, "loop-depth", llvm::cl::desc("Loop depth."),
86 llvm::cl::init(Val: 0)};
87 Option<bool> annotateLoop{*this, "annotate",
88 llvm::cl::desc("Annotate unrolled iterations."),
89 llvm::cl::init(Val: false)};
90};
91} // namespace
92
93namespace mlir {
94namespace test {
95void registerTestParallelLoopUnrollingPass() {
96 PassRegistration<TestParallelLoopUnrollingPass>();
97}
98} // namespace test
99} // namespace mlir
100