| 1 | //===-------- TestLoopUnrolling.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 | |
| 19 | using namespace mlir; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | static unsigned getNestingDepth(Operation *op) { |
| 24 | Operation *currOp = op; |
| 25 | unsigned depth = 0; |
| 26 | while ((currOp = currOp->getParentOp())) { |
| 27 | if (isa<scf::ForOp>(Val: currOp)) |
| 28 | depth++; |
| 29 | } |
| 30 | return depth; |
| 31 | } |
| 32 | |
| 33 | struct TestLoopUnrollingPass |
| 34 | : public PassWrapper<TestLoopUnrollingPass, OperationPass<>> { |
| 35 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestLoopUnrollingPass) |
| 36 | |
| 37 | StringRef getArgument() const final { return "test-loop-unrolling" ; } |
| 38 | StringRef getDescription() const final { |
| 39 | return "Tests loop unrolling transformation" ; |
| 40 | } |
| 41 | TestLoopUnrollingPass() = default; |
| 42 | TestLoopUnrollingPass(const TestLoopUnrollingPass &) {} |
| 43 | explicit TestLoopUnrollingPass(uint64_t unrollFactorParam, |
| 44 | unsigned loopDepthParam, |
| 45 | bool annotateLoopParam, |
| 46 | bool promoteSingleIterationParam) { |
| 47 | unrollFactor = unrollFactorParam; |
| 48 | loopDepth = loopDepthParam; |
| 49 | annotateLoop = annotateLoopParam; |
| 50 | promoteSingleIteration = promoteSingleIterationParam; |
| 51 | } |
| 52 | |
| 53 | void getDependentDialects(DialectRegistry ®istry) const override { |
| 54 | registry.insert<arith::ArithDialect>(); |
| 55 | } |
| 56 | |
| 57 | void runOnOperation() override { |
| 58 | if (!(unrollFactor.getValue() > 0 || unrollFactor.getValue() == -1)) { |
| 59 | emitError(loc: UnknownLoc::get(context: &getContext()), |
| 60 | message: "Invalid option: 'unroll-factor' should be greater than 0 or " |
| 61 | "equal to -1" ); |
| 62 | return signalPassFailure(); |
| 63 | } |
| 64 | SmallVector<scf::ForOp, 4> loops; |
| 65 | getOperation()->walk(callback: [&](scf::ForOp forOp) { |
| 66 | if (getNestingDepth(op: forOp) == loopDepth) |
| 67 | loops.push_back(Elt: forOp); |
| 68 | }); |
| 69 | auto annotateFn = [this](unsigned i, Operation *op, OpBuilder b) { |
| 70 | if (annotateLoop) { |
| 71 | op->setDiscardableAttr(name: "unrolled_iteration" , value: b.getUI32IntegerAttr(value: i)); |
| 72 | } |
| 73 | }; |
| 74 | for (auto loop : loops) { |
| 75 | if (unrollFactor.getValue() == -1) |
| 76 | (void)loopUnrollFull(forOp: loop); |
| 77 | else |
| 78 | (void)loopUnrollByFactor(forOp: loop, unrollFactor, annotateFn, |
| 79 | shouldPromoteIfSingleIteration: promoteSingleIteration); |
| 80 | } |
| 81 | } |
| 82 | Option<int64_t> unrollFactor{ |
| 83 | *this, "unroll-factor" , |
| 84 | llvm::cl::desc( |
| 85 | "Loop unroll factor, set it to -1, and it will fully unroll." ), |
| 86 | llvm::cl::init(Val: 1)}; |
| 87 | Option<bool> annotateLoop{*this, "annotate" , |
| 88 | llvm::cl::desc("Annotate unrolled iterations." ), |
| 89 | llvm::cl::init(Val: false)}; |
| 90 | Option<bool> unrollUpToFactor{*this, "unroll-up-to-factor" , |
| 91 | llvm::cl::desc("Loop unroll up to factor." ), |
| 92 | llvm::cl::init(Val: false)}; |
| 93 | Option<unsigned> loopDepth{*this, "loop-depth" , llvm::cl::desc("Loop depth." ), |
| 94 | llvm::cl::init(Val: 0)}; |
| 95 | Option<bool> promoteSingleIteration{ |
| 96 | *this, "promote-single-iteration" , |
| 97 | llvm::cl::desc("Promote single-iteration loops after unrolling." ), |
| 98 | llvm::cl::init(Val: true)}; |
| 99 | }; |
| 100 | } // namespace |
| 101 | |
| 102 | namespace mlir { |
| 103 | namespace test { |
| 104 | void registerTestLoopUnrollingPass() { |
| 105 | PassRegistration<TestLoopUnrollingPass>(); |
| 106 | } |
| 107 | } // namespace test |
| 108 | } // namespace mlir |
| 109 | |