|
| 1 | +//===- AIEObjectFifoUnroll.cpp ----------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Copyright (C) 2026 Advanced Micro Devices, Inc. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | +// |
| 6 | +//===----------------------------------------------------------------------===// |
| 7 | + |
| 8 | +#include "aie/Dialect/AIE/IR/AIEDialect.h" |
| 9 | +#include "aie/Dialect/AIE/Transforms/AIEPasses.h" |
| 10 | + |
| 11 | +#include "mlir/Dialect/SCF/IR/SCF.h" |
| 12 | +#include "mlir/Dialect/SCF/Utils/Utils.h" |
| 13 | +#include "mlir/Pass/Pass.h" |
| 14 | + |
| 15 | +namespace xilinx::AIE { |
| 16 | +#define GEN_PASS_DEF_AIEOBJECTFIFOUNROLL |
| 17 | +#include "aie/Dialect/AIE/Transforms/AIEPasses.h.inc" |
| 18 | +} // namespace xilinx::AIE |
| 19 | + |
| 20 | +#define DEBUG_TYPE "aie-objectFifo-unroll" |
| 21 | + |
| 22 | +using namespace mlir; |
| 23 | +using namespace xilinx; |
| 24 | +using namespace xilinx::AIE; |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +// The unroll factor for a loop is the value of the `aie.unroll_hint` attribute |
| 29 | +// attached by AIEObjectFifoStatefulTransform (the least common multiple of the |
| 30 | +// depths of the objectFifos accessed within the loop). Loops without the hint |
| 31 | +// carry no objectFifo access and are not unrolled (factor 1). |
| 32 | +static int64_t unrollFactorForLoop(scf::ForOp forOp) { |
| 33 | + if (auto hint = |
| 34 | + forOp->getAttrOfType<IntegerAttr>(kObjectFifoUnrollHintAttrName)) |
| 35 | + return hint.getInt(); |
| 36 | + return 1; |
| 37 | +} |
| 38 | + |
| 39 | +// Statically known trip count of a loop, or nullopt if it cannot be computed. |
| 40 | +static std::optional<int64_t> staticTripCount(scf::ForOp forOp) { |
| 41 | + if (forOp.getSingleLowerBound() && forOp.getSingleUpperBound() && |
| 42 | + forOp.getSingleStep()) |
| 43 | + if (std::optional<llvm::APInt> tc = forOp.getStaticTripCount()) |
| 44 | + return tc->getSExtValue(); |
| 45 | + return std::nullopt; |
| 46 | +} |
| 47 | + |
| 48 | +// True if the loop carries an objectFifo access, i.e. it was annotated with the |
| 49 | +// unroll hint by the objectFifo stateful transform. |
| 50 | +static bool loopHasObjectFifoOp(scf::ForOp forOp) { |
| 51 | + return forOp->hasAttr(kObjectFifoUnrollHintAttrName); |
| 52 | +} |
| 53 | + |
| 54 | +struct AIEObjectFifoUnrollPass |
| 55 | + : xilinx::AIE::impl::AIEObjectFifoUnrollBase<AIEObjectFifoUnrollPass> { |
| 56 | + void getDependentDialects(DialectRegistry ®istry) const override { |
| 57 | + registry.insert<AIEDialect>(); |
| 58 | + registry.insert<scf::SCFDialect>(); |
| 59 | + } |
| 60 | + |
| 61 | + void runOnOperation() override { |
| 62 | + DeviceOp device = getOperation(); |
| 63 | + |
| 64 | + for (auto coreOp : device.getOps<CoreOp>()) { |
| 65 | + // Collect every scf.for loop that carries an objectFifo access (i.e. was |
| 66 | + // annotated with the unroll hint). Ancestor loops are annotated too, so |
| 67 | + // they are naturally included. |
| 68 | + SmallVector<scf::ForOp> loops; |
| 69 | + coreOp.walk([&](scf::ForOp forOp) { |
| 70 | + if (loopHasObjectFifoOp(forOp)) |
| 71 | + loops.push_back(forOp); |
| 72 | + }); |
| 73 | + |
| 74 | + // Operation::walk uses post-order traversal by default, so a nested loop |
| 75 | + // is visited before its enclosing loop; iterating the list in order thus |
| 76 | + // processes the innermost loops first. Unrolling innermost loops first |
| 77 | + // avoids invalidating references to inner loops when an outer loop (which |
| 78 | + // duplicates its nested loops) is unrolled. |
| 79 | + for (scf::ForOp forOp : loops) { |
| 80 | + int64_t unrollFactor = unrollFactorForLoop(forOp); |
| 81 | + if (unrollFactor <= 1) |
| 82 | + continue; |
| 83 | + |
| 84 | + std::optional<int64_t> trip = staticTripCount(forOp); |
| 85 | + // When the loop performs fewer iterations than a full rotation of the |
| 86 | + // objectFifos, unroll it completely: every iteration must map to an |
| 87 | + // explicit buffer/lock slot. |
| 88 | + if (trip && *trip <= unrollFactor) { |
| 89 | + if (failed(mlir::loopUnrollFull(forOp))) { |
| 90 | + forOp.emitOpError() |
| 91 | + << "failed to fully unroll objectFifo loop (trip count " |
| 92 | + << *trip << ")"; |
| 93 | + return signalPassFailure(); |
| 94 | + } |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + // Otherwise unroll by the rotation period. loopUnrollByFactor peels a |
| 99 | + // cleanup/epilogue loop for the remaining iterations when the trip |
| 100 | + // count is not an exact multiple of the factor. |
| 101 | + FailureOr<mlir::UnrolledLoopInfo> info = |
| 102 | + mlir::loopUnrollByFactor(forOp, static_cast<uint64_t>(unrollFactor)); |
| 103 | + if (failed(info)) { |
| 104 | + forOp.emitOpError() |
| 105 | + << "failed to unroll objectFifo loop by factor " << unrollFactor; |
| 106 | + return signalPassFailure(); |
| 107 | + } |
| 108 | + |
| 109 | + // The epilogue runs the remaining (< factor) iterations. Fully unroll |
| 110 | + // it as well so that each of those iterations maps to an explicit |
| 111 | + // buffer/lock rotation slot. This is best-effort: an epilogue with a |
| 112 | + // non-constant trip count cannot be fully unrolled and is left rolled. |
| 113 | + if (info->epilogueLoopOp) { |
| 114 | + scf::ForOp epilogue = *info->epilogueLoopOp; |
| 115 | + if (loopHasObjectFifoOp(epilogue)) |
| 116 | + (void)mlir::loopUnrollFull(epilogue); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Drop any lingering unroll hints so they do not leak into the output. |
| 121 | + coreOp.walk([&](scf::ForOp forOp) { |
| 122 | + forOp->removeAttr(kObjectFifoUnrollHintAttrName); |
| 123 | + }); |
| 124 | + } |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +} // namespace |
| 129 | + |
| 130 | +std::unique_ptr<mlir::OperationPass<xilinx::AIE::DeviceOp>> |
| 131 | +xilinx::AIE::createAIEObjectFifoUnrollPass() { |
| 132 | + return std::make_unique<AIEObjectFifoUnrollPass>(); |
| 133 | +} |
0 commit comments