//===- LoopUnroll.cpp - Code to perform loop unrolling --------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements loop unrolling. // //===----------------------------------------------------------------------===// #include "mlir/Dialect/Affine/Passes.h" #include "mlir/Dialect/Affine/Analysis/LoopAnalysis.h" #include "mlir/Dialect/Affine/IR/AffineOps.h" #include "mlir/Dialect/Affine/LoopUtils.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/IR/AffineExpr.h" #include "mlir/IR/AffineMap.h" #include "mlir/IR/Builders.h" #include "llvm/ADT/DenseMap.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include <optional> namespace mlir { namespace affine { #define GEN_PASS_DEF_AFFINELOOPUNROLL #include "mlir/Dialect/Affine/Passes.h.inc" } // namespace affine } // namespace mlir #define DEBUG_TYPE … usingnamespacemlir; usingnamespacemlir::affine; namespace { // TODO: this is really a test pass and should be moved out of dialect // transforms. /// Loop unrolling pass. Unrolls all innermost loops unless full unrolling and a /// full unroll threshold was specified, in which case, fully unrolls all loops /// with trip count less than the specified threshold. The latter is for testing /// purposes, especially for testing outer loop unrolling. struct LoopUnroll : public affine::impl::AffineLoopUnrollBase<LoopUnroll> { … }; } // namespace /// Returns true if no other affine.for ops are nested within `op`. static bool isInnermostAffineForOp(AffineForOp op) { … } /// Gathers loops that have no affine.for's nested within. static void gatherInnermostLoops(func::FuncOp f, SmallVectorImpl<AffineForOp> &loops) { … } void LoopUnroll::runOnOperation() { … } /// Unrolls a 'affine.for' op. Returns success if the loop was unrolled, /// failure otherwise. The default unroll factor is 4. LogicalResult LoopUnroll::runOnAffineForOp(AffineForOp forOp) { … } std::unique_ptr<OperationPass<func::FuncOp>> mlir::affine::createLoopUnrollPass( int unrollFactor, bool unrollUpToFactor, bool unrollFull, const std::function<unsigned(AffineForOp)> &getUnrollFactor) { … }