16#include "mlir/Dialect/Arith/IR/Arith.h"
17#include "mlir/Dialect/MemRef/IR/MemRef.h"
18#include "mlir/Dialect/SCF/IR/SCF.h"
19#include "mlir/Dialect/Utils/StaticValueUtils.h"
20#include "mlir/IR/Matchers.h"
21#include "mlir/Interfaces/DataLayoutInterfaces.h"
22#include "mlir/Pass/Pass.h"
23#include "llvm/ADT/MapVector.h"
24#include "llvm/ADT/SetVector.h"
25#include "llvm/ADT/TypeSwitch.h"
26#include "llvm/Support/CheckedArithmetic.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/MathExtras.h"
40#define DEBUG_TYPE "aie-objectfifo-verify"
43#define GEN_PASS_DEF_AIEOBJECTFIFOVERIFY
44#include "aie/Dialect/AIE/Transforms/AIEPasses.h.inc"
50 SmallVector<Operation *> fillers;
51 SmallVector<Operation *> drainers;
56struct TransferBudget {
57 ObjectFifoDmaEndpointOp endpoint;
62static std::optional<int64_t> getStaticTripCount(scf::ForOp forOp) {
63 if (forOp.getSingleLowerBound() && forOp.getSingleUpperBound() &&
64 forOp.getSingleStep()) {
65 if (std::optional<llvm::APInt> tc = forOp.getStaticTripCount()) {
66 return tc->getSExtValue();
74static bool runsForever(Operation *op) {
75 auto whileOp = dyn_cast<scf::WhileOp>(op);
83static std::optional<int64_t> heldAfter(Block &block, StringRef name,
85 for (Operation &op : block) {
86 if (
auto acquire = dyn_cast<ObjectFifoAcquireOp>(op)) {
87 if (acquire.getObjFifoName() == name)
88 held = std::max<int64_t>(held, acquire.acqNumber());
91 if (
auto release = dyn_cast<ObjectFifoReleaseOp>(op)) {
92 if (release.getObjFifoName() == name)
93 held -= release.relNumber();
98 bool touches = op.getNumRegions() != 0 &&
99 op.walk([&](Operation *inner) {
100 auto acquire = dyn_cast<ObjectFifoAcquireOp>(inner);
101 auto release = dyn_cast<ObjectFifoReleaseOp>(inner);
102 if ((acquire && acquire.getObjFifoName() == name) ||
103 (release && release.getObjFifoName() == name))
104 return WalkResult::interrupt();
105 return WalkResult::advance();
109 auto forOp = dyn_cast<scf::ForOp>(op);
110 std::optional<int64_t> trip =
111 forOp ? getStaticTripCount(forOp) : std::nullopt;
116 std::optional<int64_t> once = heldAfter(*forOp.getBody(), name, held);
119 if (*trip > 1 && heldAfter(*forOp.getBody(), name, *once) != once)
133static std::optional<int64_t> tripCount(scf::ForOp forOp, DeviceOp device) {
134 if (std::optional<int64_t> trip = getStaticTripCount(forOp))
136 std::optional<int64_t> lower = getConstantIntValue(forOp.getLowerBound());
137 std::optional<int64_t> step = getConstantIntValue(forOp.getStep());
138 Value bound = forOp.getUpperBound();
139 if (
auto cast = bound.getDefiningOp<arith::IndexCastOp>())
140 bound = cast.getIn();
141 auto load = bound.getDefiningOp<memref::LoadOp>();
142 if (!lower || !step || *step <= 0 || !load || load.getIndices().size() != 1)
144 std::optional<int64_t> index = getConstantIntValue(load.getIndices().front());
145 auto buffer = load.getMemRef().getDefiningOp<BufferOp>();
148 auto type = buffer ? dyn_cast<MemRefType>(buffer.getType()) : MemRefType();
149 if (!index || !type || !type.getElementType().isInteger(32) ||
150 !type.getLayout().isIdentity() || !buffer.getSymName() ||
151 buffer.getInitialValue() ||
152 llvm::any_of(buffer->getUsers(),
153 [](Operation *user) { return !isa<memref::LoadOp>(user); }))
157 for (Operation *prev = load->getPrevNode(); prev && !wait;
158 prev = prev->getPrevNode())
159 if (
auto use = dyn_cast<UseLockOp>(prev); use && !use.release())
161 std::optional<int64_t> waitsFor =
162 wait ? getConstantIntValue(wait.getValue()) : std::nullopt;
163 LockOp lock = wait ? wait.getLock().getDefiningOp<LockOp>() : LockOp();
164 if (!lock || !wait.acquireGE() || !waitsFor || *waitsFor < 1 ||
166 wait.getBlocking().value_or(LockBlocking::Blocking) !=
167 LockBlocking::Blocking ||
168 lock.getInit().value_or(0) != 0)
170 for (Operation *user : lock->getUsers()) {
171 if (isa<AIEX::SetLockOp>(user))
173 auto use = dyn_cast<UseLockOp>(user);
178 std::optional<int64_t> given = getConstantIntValue(use.getValue());
180 for (Operation *prev = use->getPrevNode(); prev && !before;
181 prev = prev->getPrevNode())
182 if (auto other = dyn_cast<UseLockOp>(prev);
183 other && other.getLock() == use.getLock())
185 std::optional<int64_t> taken = before && !before.release()
186 ? getConstantIntValue(before.getValue())
188 if (!given || !taken || *given > *taken)
192 TileOp tile = buffer.getTileOp();
194 auto reachesTile = [&](FlatSymbolRefAttr named, std::optional<int32_t> col,
195 std::optional<int32_t> row,
196 std::optional<int64_t> address) {
198 return named.getValue() == *buffer.getSymName();
202 col = tm.getColumnFromAddress(static_cast<uint32_t>(*address));
203 row = tm.getRowFromAddress(static_cast<uint32_t>(*address));
205 return *col == tile.getCol() && *row == tile.getRow();
208 std::optional<int64_t> value;
209 for (
auto sequence : device.getOps<RuntimeSequenceOp>()) {
210 bool written = false;
211 WalkResult walk = sequence.walk([&](Operation *op) {
213 llvm::TypeSwitch<Operation *, bool>(op)
214 .Case<AIEX::NpuWrite32Op, AIEX::NpuMaskWrite32Op>([&](auto w) {
215 return reachesTile(w.getBufferAttr(), w.getColumn(), w.getRow(),
216 getConstantIntValue(w.getAddress()));
218 .Case([&](AIEX::NpuBlockWriteOp w) {
219 return reachesTile(w.getBufferAttr(), w.getColumn(), w.getRow(),
222 .Case([&](AIEX::NpuPushQueueOp push) {
223 return reachesTile({}, push.getColumn(), push.getRow(), {});
227 return WalkResult::interrupt();
228 if (auto write = dyn_cast<AIEX::NpuWriteRTPOp>(op)) {
229 if (write.getBuffer() != *buffer.getSymName() ||
230 write.getIndex() != *index)
231 return WalkResult::advance();
233 std::optional<int64_t> word = getConstantIntValue(write.getValue());
234 if (write->getParentOp() != sequence || !word ||
235 (value && *value != *word))
236 return WalkResult::interrupt();
239 return WalkResult::advance();
241 auto set = dyn_cast<AIEX::SetLockOp>(op);
242 if (set && set.getLock() == lock.getResult() && !written)
243 return WalkResult::interrupt();
244 return WalkResult::advance();
246 if (walk.wasInterrupted())
251 return *value > *lower ? (*value - *lower + *step - 1) / *step : 0;
267 TakeRatio(DeviceOp device, StringRef in, StringRef out)
268 : device(device), in(in), out(out) {}
270 std::optional<std::pair<int64_t, int64_t>> of(CoreOp core) {
271 core.walk([&](Operation *op) {
272 if (fifoOf(op) != in && fifoOf(op) != out)
274 for (Operation *parent = op->getParentOp(); parent != core;
275 parent = parent->getParentOp())
276 onUsePath.insert(parent);
279 Region &body = core.getBody();
280 if (!used || !body.hasOneBlock())
283 std::optional<Effect> effect = analyze(body.front(), held);
286 std::optional<Pair> least;
287 for (
const Pair &pair : llvm::concat<const Pair>(effect->low, bounds))
288 if (pair.released > 0 && (!least || pair.taken * least->released <
289 least->taken * pair.released))
291 if (!least || least->taken == 0)
293 int64_t common = std::gcd(least->taken, least->released);
294 return std::make_pair(least->taken / common, least->released / common);
299 int64_t taken, released;
302 SmallVector<Pair> end = {{0, 0}}, low = {{0, 0}};
305 int64_t in = 0, out = 0;
306 bool operator==(
const Held &other)
const {
307 return in == other.in && out == other.out;
310 static constexpr size_t maxPairs = 64;
311 static constexpr int64_t maxPeeledPasses = 4096;
313 static StringRef fifoOf(Operation *op) {
314 if (
auto acquire = dyn_cast<ObjectFifoAcquireOp>(op))
315 return acquire.getObjFifoName();
316 if (
auto release = dyn_cast<ObjectFifoReleaseOp>(op))
317 return release.getObjFifoName();
323 std::optional<SmallVector<Pair>> prune(ArrayRef<Pair> pairs) {
324 SmallVector<Pair> kept;
325 for (
auto [index, pair] : llvm::enumerate(pairs)) {
326 bool dominated =
false;
327 for (
auto [other, by] : llvm::enumerate(pairs))
328 dominated |= other != index && by.taken <= pair.taken &&
329 by.released >= pair.released &&
330 (by.taken != pair.taken || by.released != pair.released ||
333 kept.push_back(pair);
335 if (kept.size() > maxPairs)
340 std::optional<SmallVector<Pair>> sum(ArrayRef<Pair> a, ArrayRef<Pair> b) {
341 SmallVector<Pair> sums;
342 for (
const Pair &x : a)
343 for (
const Pair &y : b) {
345 if (llvm::AddOverflow(x.taken, y.taken, both.taken) ||
346 llvm::AddOverflow(x.released, y.released, both.released))
348 sums.push_back(both);
353 std::optional<SmallVector<Pair>> scale(ArrayRef<Pair> pairs, int64_t n) {
354 SmallVector<Pair> scaled;
355 for (
const Pair &pair : pairs) {
357 if (llvm::MulOverflow(pair.taken, n, times.taken) ||
358 llvm::MulOverflow(pair.released, n, times.released))
360 scaled.push_back(times);
365 std::optional<Effect> then(
const Effect &first,
const Effect &second) {
366 std::optional<SmallVector<Pair>> end = sum(first.end, second.end);
367 std::optional<SmallVector<Pair>> late = sum(first.end, second.low);
370 std::optional<SmallVector<Pair>> low =
371 prune(llvm::to_vector(llvm::concat<const Pair>(first.low, *late)));
374 return Effect{*end, *low};
377 std::optional<Effect> either(
const Effect &a,
const Effect &b) {
378 std::optional<SmallVector<Pair>> end =
379 prune(llvm::to_vector(llvm::concat<const Pair>(a.end, b.end)));
380 std::optional<SmallVector<Pair>> low =
381 prune(llvm::to_vector(llvm::concat<const Pair>(a.low, b.low)));
384 return Effect{*end, *low};
390 std::optional<Effect> repeat(
const Effect &pass, int64_t n) {
391 std::optional<SmallVector<Pair>> end = scale(pass.end, n);
392 std::optional<SmallVector<Pair>> before = scale(pass.end, n - 1);
393 std::optional<SmallVector<Pair>> late =
394 before ? sum(*before, pass.low) : std::nullopt;
397 std::optional<SmallVector<Pair>> low =
398 prune(llvm::to_vector(llvm::concat<const Pair>(pass.low, *late)));
399 std::optional<SmallVector<Pair>> ends = prune(*end);
402 return Effect{*ends, *low};
408 Effect anyRepeat(
const Effect &pass) {
409 llvm::append_range(bounds, pass.end);
411 llvm::append_range(any.low, pass.low);
415 std::optional<Effect> analyze(Block &block, Held &held) {
417 for (Operation &op : block) {
418 std::optional<Effect> step = analyzeOp(op, held);
421 std::optional<Effect> next = then(effect, *step);
429 std::optional<Effect> analyzeOp(Operation &op, Held &held) {
430 StringRef fifo = fifoOf(&op);
431 if (fifo == in || fifo == out) {
432 int64_t &count = fifo == in ? held.in : held.out;
434 if (
auto acquire = dyn_cast<ObjectFifoAcquireOp>(op)) {
435 int64_t wanted = acquire.acqNumber();
437 effect.end = {{std::max<int64_t>(0, wanted - count), 0}};
438 count = std::max(count, wanted);
441 int64_t released = cast<ObjectFifoReleaseOp>(op).relNumber();
442 if (released > count)
446 effect.end = {{0, released}};
447 effect.low.push_back({0, released});
451 if (!onUsePath.contains(&op))
453 if (
auto forOp = dyn_cast<scf::ForOp>(op)) {
454 std::optional<int64_t> trip = tripCount(forOp, device);
457 std::optional<Effect> pass = analyze(*forOp.getBody(), held);
458 if (!pass || !(held == entry))
460 return anyRepeat(*pass);
463 for (int64_t done = 0; done < *trip; ++done) {
465 std::optional<Effect> pass = analyze(*forOp.getBody(), held);
466 std::optional<Effect> rest = pass;
468 if (pass && held == before) {
469 rest = repeat(*pass, *trip - done);
471 rest = anyRepeat(*pass);
473 std::optional<Effect> next = rest ? then(total, *rest) : std::nullopt;
474 if (!next || (!(held == before) && done >= maxPeeledPasses))
482 if (
auto ifOp = dyn_cast<scf::IfOp>(op)) {
483 Held otherwise = held;
484 std::optional<Effect> taken = analyze(ifOp.getThenRegion().front(), held);
485 std::optional<Effect> skipped =
486 ifOp.getElseRegion().empty()
488 : analyze(ifOp.getElseRegion().front(), otherwise);
489 if (!taken || !skipped || !(held == otherwise))
491 return either(*taken, *skipped);
493 if (
auto whileOp = dyn_cast<scf::WhileOp>(op)) {
495 std::optional<Effect> check = analyze(*whileOp.getBeforeBody(), held);
496 Held afterCheck = held;
497 std::optional<Effect> body =
498 check ? analyze(*whileOp.getAfterBody(), held) : std::nullopt;
499 if (!body || !(held == entry))
502 std::optional<Effect> pass = then(*body, *check);
503 return pass ? then(*check, anyRepeat(*pass)) : std::nullopt;
510 llvm::SmallPtrSet<Operation *, 16> onUsePath;
513 SmallVector<Pair> bounds;
516struct AIEObjectFifoVerifyPass
517 :
public xilinx::AIE::impl::AIEObjectFifoVerifyBase<
518 AIEObjectFifoVerifyPass> {
522 std::shared_ptr<PoolLocks> poolLocks;
528 LogicalResult verifyActors(ObjectFifoPoolOp pool,
529 ArrayRef<SegmentActors> actors) {
530 bool external = llvm::any_of(pool.getBufferOps(), [](BufferLike buffer) {
531 return isa<ExternalBufferOp>(buffer.getOperation());
533 std::vector<ObjectFifoSegmentOp> segmentOps = pool.getSegmentOps();
534 for (
auto [index, segment] : llvm::enumerate(actors)) {
535 ObjectFifoSegmentOp segmentOp = segmentOps[index];
536 bool manualFill = segmentOp.getManualFill();
537 bool manualDrain = segmentOp.getManualDrain();
538 if (segment.fillers.size() > 1 ||
539 (manualFill && !segment.fillers.empty())) {
540 return pool.emitOpError(
"segment ")
541 << index <<
" is filled by more than one endpoint";
543 if (segment.drainers.size() > 1 ||
544 (manualDrain && !segment.drainers.empty())) {
545 return pool.emitOpError(
"segment ")
546 << index <<
" is drained by more than one endpoint";
550 bool hostDrains = external && !segment.fillers.empty();
551 bool hostFills = external && !segment.drainers.empty();
552 if (segment.drainers.empty() && !hostDrains && !manualDrain) {
553 return pool.emitOpError(
"segment ") << index <<
" has no drainer";
555 if (segment.fillers.empty() && !pool.getInitValues() && !hostFills &&
557 return pool.emitOpError(
"segment ") << index <<
" has no filler";
564 LogicalResult verifyFlows(DeviceOp device,
565 SymbolTableCollection &symbolTables) {
566 DenseMap<StringRef, int> appearances;
567 for (
auto flow : device.getOps<RouteOp>()) {
568 for (StringRef source : flow.getSourceNames()) {
569 appearances[source]++;
571 for (StringRef dest : flow.getDestinationNames()) {
576 for (
auto endpoint : device.getOps<RouteEndpoint>()) {
578 appearances.lookup(cast<SymbolOpInterface>(*endpoint).getName());
580 return endpoint->emitOpError(
"is not connected by any flow");
582 if (
auto dma = dyn_cast<ObjectFifoDmaEndpointOp>(*endpoint);
583 dma && dma.getDispatch()) {
584 if (failed(verifyDispatch(device, dma, symbolTables)))
589 if (count > 1 && !isa<RouteEndpointOp>(endpoint.getOperation())) {
590 return endpoint->emitOpError(
591 "drives one channel, so at most one flow may name it, but "
593 << count <<
" times";
602 LogicalResult verifyDispatch(DeviceOp device,
603 ObjectFifoDmaEndpointOp endpoint,
604 SymbolTableCollection &symbolTables) {
605 StringRef name = endpoint.getSymName();
606 llvm::SmallSetVector<StringRef, 4> turns;
607 for (StringRef dest :
608 endpoint.getDispatchAttr().getAsValueRange<FlatSymbolRefAttr>())
610 llvm::SmallDenseSet<StringRef, 4> reached;
611 for (
auto flow : device.getOps<RouteOp>()) {
612 if (flow.namesAsDestination(name))
613 return flow.emitOpError(
"names dispatching endpoint @")
614 << name <<
" as a destination";
615 if (!flow.namesAsSource(name))
617 if (flow.getSources().size() != 1 || flow.getDestinations().size() != 1)
618 return flow.emitOpError(
"leaves dispatching endpoint @")
620 <<
" for one destination, so it has one source and "
622 if (!flow.getPacket())
623 return flow.emitOpError(
"leaves dispatching endpoint @")
624 << name <<
" and needs a packet header to tell its turns apart";
625 StringRef dest = *flow.getDestinationNames().begin();
626 if (!turns.contains(dest))
627 return flow.emitOpError(
"reaches @")
628 << dest <<
", which dispatching endpoint @" << name
629 <<
" never takes a turn for";
630 reached.insert(dest);
632 for (StringRef dest : turns)
633 if (!reached.contains(dest))
634 return endpoint.emitOpError(
"dispatches to @")
635 << dest <<
", but no route from it reaches there";
640 int64_t elements = transferElements(endpoint);
641 for (StringRef dest : turns) {
642 auto destEndpoint = symbolTables.lookupSymbolIn<ObjectFifoDmaEndpointOp>(
643 device, StringAttr::get(device.getContext(), dest));
646 int64_t destElements = transferElements(destEndpoint);
647 if (destElements != elements)
648 return endpoint.emitOpError(
"dispatches, so every turn moves ")
649 << elements <<
" elements, but @" << dest <<
" moves "
657 static int64_t transferElements(ObjectFifoDmaEndpointOp endpoint) {
658 int64_t elements = 0;
659 std::optional<ArrayRef<BDDimLayoutArrayAttr>> dimensions =
660 endpoint.getDimensions();
661 for (
auto [index, segment] :
662 llvm::enumerate(endpoint.getSelectedSegments())) {
663 BDDimLayoutArrayAttr dims;
664 if (dimensions && index < dimensions->size()) {
665 dims = (*dimensions)[index];
667 if (dims && !dims.empty()) {
669 for (BDDimLayoutAttr dim : dims) {
670 product *= dim.getSize();
674 elements += segment.getSize();
684 LogicalResult verifyFanIn(DeviceOp device,
685 SymbolTableCollection &symbolTables) {
686 for (
auto flow : device.getOps<RouteOp>()) {
687 if (!flow.isFanIn()) {
690 std::optional<std::pair<StringRef, int64_t>> first;
691 auto check = [&](StringRef name) -> LogicalResult {
692 auto endpoint = symbolTables.lookupSymbolIn<ObjectFifoDmaEndpointOp>(
693 device, StringAttr::get(device.getContext(), name));
697 int64_t elements = transferElements(endpoint);
699 first = {name, elements};
702 if (elements != first->second) {
703 return flow.emitOpError(
"fans in, so every end moves one object of "
704 "the same size, but @")
705 << first->first <<
" moves " << first->second
706 <<
" elements and @" << name <<
" moves " << elements;
710 for (StringRef name : flow.getSourceNames()) {
711 if (failed(check(name))) {
715 for (StringRef name : flow.getDestinationNames()) {
716 if (failed(check(name))) {
729 LogicalResult verifyOverRelease(DeviceOp device) {
730 for (
auto coreOp : device.getOps<CoreOp>()) {
731 WalkResult result = coreOp.walk([&](Operation *loop) {
733 return WalkResult::advance();
735 auto directlyIn = [&](Operation *op) {
738 DenseMap<StringRef, int64_t> acquired;
739 DenseMap<StringRef, int64_t> released;
740 DenseMap<StringRef, Operation *> blame;
741 DenseSet<StringRef> spansNestedLoop;
743 loop->walk([&](ObjectFifoAcquireOp a) {
744 StringRef key = a.getObjFifoName();
745 if (!directlyIn(a)) {
746 spansNestedLoop.insert(key);
748 acquired[key] += a.acqNumber();
749 blame.try_emplace(key, a);
752 loop->walk([&](ObjectFifoReleaseOp r) {
753 StringRef key = r.getObjFifoName();
754 if (!directlyIn(r)) {
755 spansNestedLoop.insert(key);
757 released[key] += r.relNumber();
758 blame.try_emplace(key, r);
762 for (
auto &[key, count] : released) {
763 if (spansNestedLoop.contains(key) || count <= acquired.lookup(key)) {
766 blame.lookup(key)->emitOpError(
767 "cannot release more elements than are already acquired");
768 return WalkResult::interrupt();
770 return WalkResult::advance();
772 if (result.wasInterrupted()) {
789 LogicalResult verifyTransferBudgets(DeviceOp device) {
790 DenseMap<Operation *, TransferBudget> budgets;
791 for (
auto endpoint : device.getOps<ObjectFifoDmaEndpointOp>()) {
792 std::optional<int32_t> iterations = endpoint.getIterCount();
793 ObjectFifoPoolOp pool = endpoint.getPoolOp();
794 if (!iterations || !pool) {
797 budgets[pool] = {endpoint, *iterations,
798 int64_t{*iterations} * pool.getDepth()};
800 if (budgets.empty()) {
804 DenseMap<StringRef, int64_t> released;
805 DenseMap<StringRef, ObjectFifoReleaseOp> blame;
806 DenseSet<StringRef> uncounted;
807 device.walk([&](ObjectFifoReleaseOp release) {
808 StringRef key = release.getObjFifoName();
811 uncounted.insert(key);
814 released[key] += *runs * release.relNumber();
815 blame.try_emplace(key, release);
817 DenseSet<StringRef> acquired;
818 device.walk([&](ObjectFifoAcquireOp acquire) {
819 acquired.insert(acquire.getObjFifoName());
822 for (
auto endpoint : device.getOps<ObjectFifoCoreEndpointOp>()) {
824 auto budget = budgets.find(endpoint.getPoolOp());
825 if (budget == budgets.end() || endpoint.getSegments()) {
828 StringRef name = endpoint.getSymName();
829 int64_t count = released.lookup(name);
830 ObjectFifoDmaEndpointOp dma = budget->second.endpoint;
831 ObjectFifoPoolOp pool = endpoint.getPoolOp();
832 int64_t depth = pool.getDepth();
833 if (count > budget->second.objects) {
834 InFlightDiagnostic diag =
835 blame.lookup(name).emitOpError(
"releases ")
836 << count <<
" objects through @" << name
837 <<
" over the run, but the pool's DMA endpoint stops after "
838 << budget->second.objects <<
" (iterCount "
839 << budget->second.iterations <<
" x depth " << depth <<
")";
841 if (!endpoint.drains() && count <= budget->second.objects + depth)
842 diag <<
", leaving " << count - budget->second.objects
843 <<
" in the pool unsent";
845 diag <<
", so the core would wait forever for the rest";
846 diag.attachNote(dma.getLoc()) <<
"the DMA endpoint is here";
850 if (!acquired.contains(name) || uncounted.contains(name) ||
851 dma.getSegments() || pool.getInitValues() ||
852 dma.drains() == endpoint.drains() || !poolLocks->needsLocks(pool)) {
855 int64_t reachable = endpoint.drains() ? count + depth : count;
856 if (budget->second.objects <= reachable) {
859 InFlightDiagnostic diag =
860 dma.emitOpError(dma.drains() ?
"drains " :
"fills ")
861 << budget->second.objects <<
" objects (iterCount "
862 << budget->second.iterations <<
" x depth " << depth
863 <<
") before it stops, but the core releases exactly " << count
864 <<
" through @" << name;
865 if (endpoint.drains())
866 diag <<
", leaving room for only " << reachable;
867 diag <<
", so the DMA endpoint would wait forever for the rest";
868 diag.attachNote(endpoint.getLoc()) <<
"the core endpoint is here";
880 LogicalResult verifyHeldAtEnd(DeviceOp device,
881 SymbolTableCollection &symbolTables) {
885 llvm::DenseMap<StringRef, SmallVector<RuntimeSequenceOp>> launchesByFifo;
886 std::optional<SmallVector<RuntimeSequenceOp>> allLaunches;
887 auto launchesOf = [&](std::optional<StringRef> fifo)
888 ->
const SmallVector<RuntimeSequenceOp> & {
892 llvm::to_vector(device.getOps<RuntimeSequenceOp>()));
895 auto [it, inserted] = launchesByFifo.try_emplace(*fifo);
897 for (
auto seq : device.getOps<RuntimeSequenceOp>())
899 it->second.push_back(seq);
900 if (it->second.empty())
901 llvm::append_range(it->second, device.getOps<RuntimeSequenceOp>());
905 for (
auto core : device.getOps<CoreOp>()) {
906 Region &body = core.getBody();
907 if (!body.hasOneBlock())
910 core.walk([](scf::WhileOp) {
return WalkResult::interrupt(); })
914 llvm::MapVector<StringRef, ObjectFifoAcquireOp> firstAcquire;
915 core.walk([&](ObjectFifoAcquireOp acquire) {
916 firstAcquire.insert({acquire.getObjFifoName(), acquire});
918 for (
auto &[name, acquire] : firstAcquire) {
919 std::optional<int64_t> held = heldAfter(body.front(), name, 0);
920 if (!held || *held == 0)
923 symbolTables.lookupNearestSymbolFrom<ObjectFifoCoreEndpointOp>(
924 device, acquire.getObjFifoNameAttr());
925 ObjectFifoPoolOp pool = endpoint ? endpoint.getPoolOp() :
nullptr;
926 std::optional<StringRef> fifo =
927 pool ? pool.getFifoName() : std::nullopt;
929 fifo ? poolLocks->rearms().find(*fifo) : poolLocks->rearms().end();
930 bool rearmedEveryLaunch =
931 rearms != poolLocks->rearms().end() &&
932 llvm::all_of(launchesOf(fifo), [&](RuntimeSequenceOp seq) {
933 return llvm::any_of(rearms->second, [&](
auto reset) {
934 return reset->getParentOp() == seq;
937 if (pool && (!poolLocks->needsLocks(pool) || rearmedEveryLaunch))
939 InFlightDiagnostic diag =
940 core.emitOpError(
"ends with ")
941 << *held <<
" acquired from @" << name
942 <<
" never released, so their locks stay taken into the next run "
943 "of this configuration";
944 diag.attachNote(acquire.getLoc()) <<
"acquired here";
959 LogicalResult verifyAdvances(DeviceOp device) {
962 auto repeatsForever = [](Operation *loop) {
963 bool forever =
false;
964 for (Operation *op = loop; !isa<CoreOp>(op); op = op->getParentOp()) {
965 Block *block = op->getBlock();
966 if (block != &op->getParentRegion()->front() ||
968 llvm::make_range(block->begin(), op->getIterator()),
969 [&](Operation &before) { return runsForever(&before); }))
971 auto forOp = dyn_cast<scf::ForOp>(op);
972 auto whileOp = dyn_cast<scf::WhileOp>(op);
973 std::optional<int64_t> trip =
974 forOp ? getStaticTripCount(forOp) : std::nullopt;
975 if ((forOp && (!trip || *trip == 0)) ||
978 forever |= runsForever(op);
979 Operation *parent = op->getParentOp();
980 if (
auto ifOp = dyn_cast<scf::IfOp>(parent)) {
981 bool inThen = op->getParentRegion() == &ifOp.getThenRegion();
982 if (!matchPattern(ifOp.getCondition(), inThen ? m_One() : m_Zero()))
984 }
else if (
auto parentWhile = dyn_cast<scf::WhileOp>(parent)) {
985 if (op->getParentRegion() != &parentWhile.getAfter())
987 }
else if (!isa<scf::ForOp, CoreOp>(parent)) {
994 for (
auto endpoint : device.getOps<ObjectFifoCoreEndpointOp>()) {
995 ObjectFifoPoolOp pool = endpoint.getPoolOp();
996 if (!pool || pool.getInitValues() || endpoint.getSegments() ||
997 !poolLocks->needsLocks(pool))
999 Operation *keepsMoving =
nullptr;
1000 for (
auto dma : device.getOps<ObjectFifoDmaEndpointOp>())
1001 if (dma.getPoolOp() == pool && dma.drains() != endpoint.drains() &&
1002 !dma.getIterCount() && !dma.getSegments())
1004 for (
auto other : device.getOps<ObjectFifoCoreEndpointOp>()) {
1005 if (keepsMoving || other.getPoolOp() != pool ||
1006 other.drains() == endpoint.drains() || other.getSegments())
1008 CoreTurns otherTurns;
1009 std::string otherReason;
1010 if (succeeded(otherTurns.
read({other}, otherReason)) &&
1012 keepsMoving = other;
1019 if (succeeded(turns.
read({endpoint}, reason)) || !turns.
movesNone() ||
1023 dyn_cast_or_null<ObjectFifoAcquireOp>(turns.
periodAcquire());
1026 InFlightDiagnostic diag =
1027 acquire.emitOpError(
"takes from @")
1028 << endpoint.getSymName()
1029 <<
" each time its loop repeats, but the loop releases none of "
1031 if (endpoint.drains())
1032 diag <<
"the core keeps the same objects forever and the end "
1033 "filling the pool stops once it is full";
1035 diag <<
"nothing the core writes there is ever sent";
1036 diag.attachNote(keepsMoving->getLoc()) <<
"the other end is here";
1045 static std::optional<int64_t> hostBytes(RuntimeSequenceOp sequence,
1047 std::optional<SymbolTable::UseRange> uses =
1048 SymbolTable::getSymbolUses(name, &sequence.getBody());
1050 return std::nullopt;
1051 std::optional<int64_t> bytes = 0;
1052 for (
const SymbolTable::SymbolUse &use : *uses) {
1053 auto task = dyn_cast<AIEX::DMAConfigureTaskForOp>(use.getUser());
1054 std::optional<int64_t> runs =
1056 std::optional<int64_t> streamed =
1057 runs ? task.getStaticBytesStreamed() : std::nullopt;
1059 return std::nullopt;
1060 std::optional<int64_t> moved = llvm::checkedMul(*runs, *streamed);
1061 bytes = moved ? llvm::checkedAdd(*bytes, *moved) : std::nullopt;
1063 return std::nullopt;
1070 static int64_t objectBytes(ObjectFifoDmaEndpointOp endpoint) {
1071 auto type = cast<MemRefType>(endpoint.getPoolOp().getElemType());
1072 return transferElements(endpoint) *
1073 static_cast<int64_t
>(DataLayout::closest(endpoint).getTypeSize(
1074 type.getElementType()));
1080 std::optional<int64_t> hostObjects(DeviceOp device,
1081 RuntimeSequenceOp sequence,
1082 ObjectFifoDmaEndpointOp endpoint,
1084 if (endpoint.getIterCount() || endpoint.getDispatch())
1085 return std::nullopt;
1086 StringRef name = endpoint.getSymName();
1087 SmallVector<StringRef> peers;
1088 for (
auto route : device.getOps<RouteOp>()) {
1089 if (endpoint.drains() ? route.namesAsSource(name)
1090 : route.namesAsDestination(name)) {
1091 auto names = endpoint.drains() ? route.getDestinationNames()
1092 : route.getSourceNames();
1093 peers.append(names.begin(), names.end());
1096 if (peers.size() != 1)
1097 return std::nullopt;
1098 Operation *peer = SymbolTable::lookupNearestSymbolFrom(
1099 device, StringAttr::get(device.getContext(), peers.front()));
1101 int64_t sent = objectBytes(endpoint) * endpoint.getRepeat();
1102 int64_t perObject = endpoint.drains() ? sent : objectBytes(endpoint);
1105 if (
auto shim = dyn_cast_or_null<RouteEndpointOp>(peer)) {
1106 std::optional<int64_t> host = hostBytes(sequence, shim.getSymNameAttr());
1108 return std::nullopt;
1110 }
else if (
auto other = dyn_cast_or_null<ObjectFifoDmaEndpointOp>(peer);
1111 other && throughPool && other.drains() != endpoint.drains()) {
1112 ObjectFifoDmaEndpointOp toward;
1113 for (
auto candidate : device.getOps<ObjectFifoDmaEndpointOp>()) {
1114 if (candidate == other || candidate.getPoolOp() != other.getPoolOp() ||
1115 candidate.drains() == other.drains())
1118 return std::nullopt;
1122 return std::nullopt;
1123 std::optional<int64_t> objects =
1124 hostObjects(device, sequence, toward,
false);
1126 return std::nullopt;
1128 *objects * (other.drains() ? objectBytes(other) * other.getRepeat()
1129 : objectBytes(other));
1131 return std::nullopt;
1133 if (perObject == 0 || bytes % perObject)
1134 return std::nullopt;
1135 return bytes / perObject;
1148 LogicalResult verifyDispatchVolumes(DeviceOp device) {
1149 RuntimeSequenceOp sequence;
1150 for (
auto candidate : device.getOps<RuntimeSequenceOp>()) {
1151 auto ops = candidate.getBody()
1152 .getOps<AIEX::SyncScratchpadParametersFromHostOp>();
1153 int64_t count = std::distance(ops.begin(), ops.end());
1157 BoolAttr preamble = candidate.getEmitParameterSyncPreambleAttr();
1158 count = !preamble || preamble.getValue();
1162 if (sequence || count > 1)
1164 sequence = candidate;
1169 for (
auto endpoint : device.getOps<ObjectFifoCoreEndpointOp>()) {
1170 ObjectFifoPoolOp pool = endpoint.getPoolOp();
1171 if (!pool || pool.getInitValues() || endpoint.getSegments() ||
1172 !poolLocks->needsLocks(pool))
1174 ObjectFifoDmaEndpointOp dma;
1176 for (
auto candidate : device.getOps<ObjectFifoDmaEndpointOp>()) {
1177 if (candidate.getPoolOp() != pool ||
1178 candidate.drains() == endpoint.drains())
1183 if (!dma || !single || dma.getSegments())
1188 if (failed(turns.
read({endpoint}, reason)))
1191 std::optional<int64_t> periods = 1;
1192 for (; periods && !isa<CoreOp>(loop) && !runsForever(loop);
1193 loop = loop->getParentOp()) {
1194 auto forOp = dyn_cast<scf::ForOp>(loop);
1195 std::optional<int64_t> trip =
1196 forOp ? getStaticTripCount(forOp) : std::nullopt;
1197 periods = trip && loop->getBlock() == &loop->getParentRegion()->front()
1198 ? llvm::checkedMul(*periods, *trip)
1201 if (!periods || isa<CoreOp>(loop))
1205 auto whileLoop = dyn_cast<scf::WhileOp>(loop);
1206 Block *body = whileLoop ? whileLoop.getAfterBody()
1207 : cast<scf::ForOp>(loop).getBody();
1209 loop->walk([&](AIEX::WaitScratchpadParametersOp) { waits++; });
1211 body->getOps<AIEX::WaitScratchpadParametersOp>().empty())
1213 std::optional<int64_t> perDispatch = llvm::checkedMul(
1214 *periods,
static_cast<int64_t
>(turns.
turns().size()));
1217 std::optional<int64_t> objects = hostObjects(device, sequence, dma,
true);
1218 LLVM_DEBUG(llvm::dbgs()
1219 <<
"dispatch volume of @" << endpoint.getSymName()
1220 <<
": the core moves " << *perDispatch <<
", @"
1221 << sequence.getSymName() <<
" moves "
1222 << (objects ? std::to_string(*objects) :
"an unknown count")
1224 if (!objects || *objects == *perDispatch)
1227 ObjectFifoAcquireOp acquire;
1228 loop->walk([&](ObjectFifoAcquireOp op) {
1229 if (!acquire && op.getObjFifoName() == endpoint.getSymName())
1234 InFlightDiagnostic diag =
1235 acquire.emitOpError()
1236 << (endpoint.drains() ?
"takes " :
"hands over ") << *perDispatch
1237 <<
" objects through @" << endpoint.getSymName()
1238 <<
" for each dispatch, one pass of a loop that waits for each "
1239 "dispatch's parameters, but each run of runtime sequence @"
1240 << sequence.getSymName() <<
" moves " << *objects
1241 <<
", so the core drifts out of step with the dispatches";
1242 diag.attachNote(sequence.getLoc()) <<
"the runtime sequence is here";
1248 using PoolActors = DenseMap<Operation *, SmallVector<SegmentActors>>;
1258 int64_t inBytes, outBytes;
1259 int64_t lagObject = 0, lagIn = 0, lagOut = 0;
1262 static int64_t segmentBytes(ObjectFifoPoolOp pool,
1263 ArrayRef<ObjectFifoSegmentOp> segments) {
1264 int64_t elements = 0;
1265 for (ObjectFifoSegmentOp segment : segments)
1266 elements += segment.getSize();
1267 auto type = cast<MemRefType>(pool.getElemType());
1269 static_cast<int64_t
>(
1270 DataLayout::closest(pool).getTypeSize(type.getElementType()));
1275 static bool passesOn(ObjectFifoDmaEndpointOp dma) {
1276 return !dma.getPadDimensions() && !dma.getDispatch() &&
1277 dma.getRepeat() == 1;
1290 void carries(DeviceOp device, StringAttr from,
const PoolActors &actors,
1291 Reach scale, llvm::SmallPtrSetImpl<Operation *> &onPath,
1292 SmallVectorImpl<Reach> &reaches) {
1294 for (
auto candidate : device.getOps<RouteOp>()) {
1295 if (!candidate.namesAsSource(from.getValue()))
1301 if (!route || route.getSources().size() != 1 || route.getKeepPktHeader())
1303 for (StringRef name : route.getDestinationNames()) {
1304 if (llvm::any_of(device.getOps<RouteOp>(), [&](RouteOp other) {
1305 return other != route && other.namesAsDestination(name);
1308 Operation *dest = SymbolTable::lookupNearestSymbolFrom(
1309 device, StringAttr::get(device.getContext(), name));
1310 if (
auto shim = dyn_cast_or_null<RouteEndpointOp>(dest)) {
1313 if (shim.getBundle() == WireBundle::DMA &&
1314 shim.getTileLike().isShimTile())
1315 reaches.push_back(at);
1318 auto fill = dyn_cast_or_null<ObjectFifoDmaEndpointOp>(dest);
1319 if (!fill || fill.drains() || !passesOn(fill))
1321 ObjectFifoPoolOp pool = fill.getPoolOp();
1322 if (!pool || pool.getInitValues() || pool.getContiguous() ||
1323 !poolLocks->needsLocks(pool) || onPath.contains(pool))
1325 auto it = actors.find(pool);
1326 if (it == actors.end())
1328 std::vector<ObjectFifoSegmentOp> all = pool.getSegmentOps();
1329 std::vector<ObjectFifoSegmentOp> filled = fill.getSelectedSegments();
1330 auto indexOf = [&](ObjectFifoSegmentOp segment) {
1331 return llvm::find(all, segment) - all.begin();
1333 if (filled.empty() || llvm::any_of(filled, [&](
auto segment) {
1334 return it->second[indexOf(segment)].fillers.size() != 1;
1337 llvm::SetVector<Operation *> drainers;
1338 for (
const SegmentActors &segment : it->second)
1339 drainers.insert(segment.drainers.begin(), segment.drainers.end());
1340 onPath.insert(pool);
1341 for (Operation *drainer : drainers) {
1342 auto core = dyn_cast<ObjectFifoCoreEndpointOp>(drainer);
1343 auto dma = dyn_cast<ObjectFifoDmaEndpointOp>(drainer);
1344 if (dma && !passesOn(dma))
1346 std::vector<ObjectFifoSegmentOp> drained =
1347 core ? core.getSelectedSegments() : dma.getSelectedSegments();
1348 if (drained.empty())
1350 bool split = drained.size() == 1 && drained.front() == filled.back();
1351 bool join = filled.size() == 1 && filled.front() == drained.front();
1356 bool laterJoin = dma && filled.size() == 1 && !join &&
1357 llvm::is_contained(drained, filled.front());
1358 if ((drained != filled && !split && !join && !laterJoin) ||
1359 (laterJoin && scale.lagObject))
1361 int64_t in = objectBytes(fill);
1362 int64_t out = core ? segmentBytes(pool, drained) : objectBytes(dma);
1365 if (in <= 0 || out <= 0 ||
1366 llvm::MulOverflow(scale.inBytes, in, next.inBytes) ||
1367 llvm::MulOverflow(scale.outBytes, out, next.outBytes) ||
1369 (llvm::MulOverflow(scale.lagIn, in, next.lagIn) ||
1370 llvm::MulOverflow(scale.lagOut, out, next.lagOut))))
1372 int64_t common = std::gcd(next.inBytes, next.outBytes);
1373 next.inBytes /= common;
1374 next.outBytes /= common;
1376 next = {next.far, next.inBytes, next.outBytes, out, 1, 1};
1377 if (next.lagObject) {
1378 int64_t lagCommon = std::gcd(next.lagIn, next.lagOut);
1379 next.lagIn /= lagCommon;
1380 next.lagOut /= lagCommon;
1383 reaches.push_back(next);
1385 carries(device, dma.getSymNameAttr(), actors, next, onPath, reaches);
1400 void recordConsumption(DeviceOp device,
const PoolActors &actors) {
1401 MLIRContext *ctx = device.getContext();
1403 int64_t in, out, granule;
1407 std::optional<DenseMap<Operation *, unsigned>> rank;
1408 for (
auto input : device.getOps<RouteEndpointOp>()) {
1409 if (input.getBundle() != WireBundle::DMA ||
1410 !input.getTileLike().isShimTile())
1414 llvm::MapVector<Operation *, Share> proofs;
1415 auto prove = [&](RouteEndpointOp output,
const Reach &reach) {
1416 Share share{reach.inBytes, reach.outBytes, 0};
1417 if (reach.lagObject) {
1420 if (llvm::MulOverflow(reach.lagOut, reach.lagObject, whole))
1422 share.granule = whole / std::gcd(whole, reach.lagIn);
1424 auto [it, inserted] = proofs.try_emplace(output, share);
1425 const Share &old = it->second;
1427 if (inserted || llvm::MulOverflow(share.in, old.out, lhs) ||
1428 llvm::MulOverflow(old.in, share.out, rhs))
1430 if ((old.granule && !share.granule) ||
1431 (!old.granule == !share.granule &&
1432 (lhs > rhs || (lhs == rhs && share.granule < old.granule))))
1435 SmallVector<Reach> reaches;
1436 llvm::SmallPtrSet<Operation *, 8> onPath;
1437 carries(device, input.getSymNameAttr(), actors, {nullptr, 1, 1}, onPath,
1441 constexpr int maxCoreHops = 4;
1442 SmallVector<std::pair<Reach, int>> work;
1443 for (
const Reach &reach : reaches)
1444 work.push_back({reach, 0});
1445 while (!work.empty()) {
1446 auto [reach, hops] = work.pop_back_val();
1447 if (
auto output = dyn_cast<RouteEndpointOp>(reach.far)) {
1448 prove(output, reach);
1451 if (hops == maxCoreHops)
1453 auto consumer = cast<ObjectFifoCoreEndpointOp>(reach.far);
1455 for (Operation *user : consumer.getTile().getUsers())
1456 if (
auto candidate = dyn_cast<CoreOp>(user))
1460 int64_t takenBytes =
1461 segmentBytes(consumer.getPoolOp(), consumer.getSelectedSegments());
1462 for (
auto producer : device.getOps<ObjectFifoCoreEndpointOp>()) {
1463 ObjectFifoPoolOp pool = producer.getPoolOp();
1464 if (producer.drains() || producer.getSegments() ||
1465 producer.getTile() != consumer.getTile() || !pool ||
1466 pool.getInitValues() || !poolLocks->needsLocks(pool))
1468 auto it = actors.find(pool);
1469 if (it == actors.end() || it->second.size() != 1 ||
1470 it->second.front().fillers.size() != 1 ||
1471 it->second.front().drainers.size() != 1)
1473 Operation *drainer = it->second.front().drainers.front();
1474 std::optional<std::pair<int64_t, int64_t>> ratio =
1475 TakeRatio(device, consumer.getSymName(), producer.getSymName())
1484 auto dma = dyn_cast<ObjectFifoDmaEndpointOp>(drainer);
1485 int64_t taken, released;
1486 if ((dma && !passesOn(dma)) ||
1487 llvm::MulOverflow(ratio->first, takenBytes, taken) ||
1490 dma ? objectBytes(dma)
1491 : segmentBytes(pool, producer.getSelectedSegments()),
1496 SmallVector<Reach> nexts;
1498 nexts.push_back({drainer, 1, 1, 0, 0, 0});
1500 llvm::SmallPtrSet<Operation *, 8> outPath;
1501 carries(device, dma.getSymNameAttr(), actors, {nullptr, 1, 1},
1504 for (
const Reach &next : nexts) {
1505 if (reach.lagObject && next.lagObject)
1507 Reach composed = next;
1508 if (llvm::MulOverflow(next.inBytes, taken, composed.inBytes) ||
1509 llvm::MulOverflow(composed.inBytes, reach.inBytes,
1510 composed.inBytes) ||
1511 llvm::MulOverflow(next.outBytes, released, composed.outBytes) ||
1512 llvm::MulOverflow(composed.outBytes, reach.outBytes,
1515 int64_t common = std::gcd(composed.inBytes, composed.outBytes);
1516 composed.inBytes /= common;
1517 composed.outBytes /= common;
1520 if (reach.lagObject) {
1521 composed.lagObject = reach.lagObject;
1522 if (llvm::MulOverflow(reach.lagIn, taken, composed.lagIn) ||
1523 llvm::MulOverflow(composed.lagIn, next.inBytes,
1525 llvm::MulOverflow(reach.lagOut, released, composed.lagOut) ||
1526 llvm::MulOverflow(composed.lagOut, next.outBytes,
1529 int64_t lagCommon = std::gcd(composed.lagIn, composed.lagOut);
1530 composed.lagIn /= lagCommon;
1531 composed.lagOut /= lagCommon;
1533 work.push_back({composed, hops + 1});
1537 if (proofs.empty()) {
1538 input.removeConsumedByAttr();
1541 auto ordered = proofs.takeVector();
1542 if (ordered.size() > 1) {
1545 llvm::stable_sort(ordered, [&](
const auto &a,
const auto &b) {
1546 return rank->lookup(a.first) < rank->lookup(b.first);
1549 SmallVector<Attribute> attrs;
1550 for (
auto [output, share] : ordered) {
1551 int64_t common = std::gcd(share.in, share.out);
1552 attrs.push_back(ConsumedByAttr::get(
1554 FlatSymbolRefAttr::get(
1555 cast<RouteEndpointOp>(output).getSymNameAttr()),
1556 share.in / common, share.out / common, share.granule,
1557 FlatSymbolRefAttr()));
1559 input.setConsumedByAttr(ArrayAttr::get(ctx, attrs));
1567 LogicalResult verifyOutOfOrderPools(DeviceOp device) {
1568 auto lookup = [&](FlatSymbolRefAttr name) {
1569 return dyn_cast_or_null<ObjectFifoDmaEndpointOp>(
1570 SymbolTable::lookupNearestSymbolFrom(device, name.getAttr()));
1572 auto objectBytes = [](ObjectFifoPoolOp pool) -> int64_t {
1573 auto type = dyn_cast<MemRefType>(pool.getElemType());
1576 return (type.getNumElements() * type.getElementTypeBitWidth() + 7) / 8;
1578 for (
auto flow : device.getOps<RouteOp>()) {
1579 if (flow.getSources().size() != 1)
1581 ObjectFifoDmaEndpointOp src =
1582 lookup(cast<FlatSymbolRefAttr>(flow.getSources()[0]));
1583 ObjectFifoDmaEndpointOp firstReceiver;
1584 for (
auto destName :
1585 flow.getDestinations().getAsRange<FlatSymbolRefAttr>()) {
1586 ObjectFifoDmaEndpointOp dst = lookup(destName);
1587 if (!dst || !dst.getOutOfOrder())
1590 firstReceiver = dst;
1591 if (firstReceiver.getNumDescriptors() != dst.getNumDescriptors() ||
1592 objectBytes(firstReceiver.getPoolOp()) !=
1593 objectBytes(dst.getPoolOp()))
1594 return flow.emitOpError(
1595 "out-of-order broadcast destinations require matching buffer "
1596 "counts and object sizes");
1597 if (!src || !src.getOutOfOrder())
1599 ObjectFifoPoolOp from = src.getPoolOp(), to = dst.getPoolOp();
1602 if (src.getNumDescriptors() != dst.getNumDescriptors() ||
1603 objectBytes(from) != objectBytes(to))
1604 return src.emitOpError(
"sends out of order from ")
1605 << src.getNumDescriptors() <<
" buffers of "
1606 << objectBytes(from) <<
" bytes into "
1607 << dst.getNumDescriptors() <<
" of " << objectBytes(to)
1608 <<
"; an out-of-order fifo needs the same buffers at both "
1609 "ends, because one descriptor names one whole buffer";
1615 void runOnOperation()
override {
1616 DeviceOp device = getOperation();
1617 poolLocks = std::make_shared<PoolLocks>(device);
1619 DenseMap<Operation *, SmallVector<SegmentActors>> actorsPerPool;
1620 for (
auto pool : device.getOps<ObjectFifoPoolOp>()) {
1621 actorsPerPool[pool].resize(pool.getSegmentOps().size());
1624 auto record = [&](
auto endpoint) {
1625 ObjectFifoPoolOp pool = endpoint.getPoolOp();
1629 auto &actors = actorsPerPool[pool];
1630 std::vector<ObjectFifoSegmentOp> all = pool.getSegmentOps();
1631 for (ObjectFifoSegmentOp segment : endpoint.getSelectedSegments()) {
1632 size_t index = llvm::find(all, segment) - all.begin();
1633 (endpoint.drains() ? actors[index].drainers : actors[index].fillers)
1634 .push_back(endpoint);
1638 for (
auto endpoint : device.getOps<ObjectFifoCoreEndpointOp>()) {
1641 for (
auto endpoint : device.getOps<ObjectFifoDmaEndpointOp>()) {
1645 for (
auto pool : device.getOps<ObjectFifoPoolOp>()) {
1646 if (failed(verifyActors(pool, actorsPerPool[pool]))) {
1647 return signalPassFailure();
1651 SymbolTableCollection symbolTables;
1652 if (failed(verifyFlows(device, symbolTables)) ||
1653 failed(verifyFanIn(device, symbolTables)) ||
1654 failed(verifyOverRelease(device)) ||
1655 failed(verifyTransferBudgets(device)) ||
1656 failed(verifyHeldAtEnd(device, symbolTables)) ||
1657 failed(verifyAdvances(device)) ||
1658 failed(verifyDispatchVolumes(device)) ||
1659 failed(verifyOutOfOrderPools(device))) {
1660 return signalPassFailure();
1662 recordConsumption(device, actorsPerPool);
1668std::unique_ptr<OperationPass<DeviceOp>>
1670 return std::make_unique<AIEObjectFifoVerifyPass>();
mlir::LogicalResult read(llvm::ArrayRef< ObjectFifoCoreEndpointOp > ends, std::string &reason)
Reads the order off the program of the core working through ends, all draining (the channel fills the...
bool movesNone() const
Whether read found a steady state in which the period moves none of the objects: the core keeps what ...
mlir::Operation * periodAcquire() const
The first acquire the period runs, with its branches folded; null when it runs none.
llvm::ArrayRef< unsigned > turns() const
Indices into ends, one per object the channel moves in a period of the core's steady state.
mlir::Operation * periodLoop() const
The loop read took as the core's period, once it found one.
bool namesFifo(Operation *op, StringRef fifo, SymbolTableCollection &symbols)
std::optional< int64_t > staticExecutions(Operation *op)
std::unique_ptr< mlir::OperationPass< DeviceOp > > createAIEObjectFifoVerifyPass()
bool isForeverLoop(mlir::scf::WhileOp whileOp)
Whether whileOp repeats forever: its condition is a constant true, and its condition region does noth...
mlir::Operation * getObjectFifoLoopParent(mlir::Operation *op)
The innermost loop around op that isObjectFifoLoop accepts, or null.
bool isObjectFifoLoop(mlir::Operation *op)
Whether op is a loop that objectFifo lowering unrolls by the rotation period of the objectFifos acces...
llvm::DenseMap< mlir::Operation *, unsigned > rank() const
Rank the nodes by their features and then, a round at a time until no class splits,...
static StructuralGraph of(DeviceOp device)
The ops directly in device but its runtime sequences, each with the ops nested in it,...