MLIR-AIE 1.4.6.dev
AIEDedupCtrlPacketMulticast.cpp
Go to the documentation of this file.
1//===- AIEDedupCtrlPacketMulticast.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 "CtrlPacketRuns.h"
12#include "mlir/Pass/Pass.h"
13
14#include <map>
15
16namespace xilinx::AIEX {
17#define GEN_PASS_DEF_AIEDEDUPCTRLPACKETMULTICAST
18#include "aie/Dialect/AIEX/Transforms/AIEXPasses.h.inc"
19} // namespace xilinx::AIEX
20
21using namespace mlir;
22using namespace xilinx;
23using namespace xilinx::AIEX;
24
25namespace {
26
27struct AIEDedupCtrlPacketMulticastPass
28 : xilinx::AIEX::impl::AIEDedupCtrlPacketMulticastBase<
29 AIEDedupCtrlPacketMulticastPass> {
30 void runOnOperation() override {
31 ModuleOp module = getOperation();
32 auto result = module.walk([&](AIE::RuntimeSequenceOp sequence) {
33 return sequence.walk([&](Block *block) -> WalkResult {
34 for (PacketInterval interval : packetIntervals(*block)) {
35 if ((!interval.ordinary && !interval.independent) ||
36 !interval.device ||
37 llvm::any_of(interval.packets, [](NpuControlPacketOp packet) {
38 return packet.getMulticastId().has_value();
39 }))
40 continue;
41 auto groups = controlPacketMulticasts(module, interval.device);
42 if (failed(groups))
43 return WalkResult::interrupt();
44 if (groups->empty())
45 continue;
46 std::map<AIE::TileID, SmallVector<NpuControlPacketOp>> byTile;
47 for (NpuControlPacketOp packet : interval.packets)
48 byTile[packet.getTileFromAddr()].push_back(packet);
49 OpBuilder builder(interval.packets.front());
50 auto anchor = ControlPacketBarrierOp::create(
51 builder, interval.packets.front().getLoc(),
52 builder.getUnitAttr());
53 for (auto [id, group] : llvm::enumerate(*groups)) {
54 SmallVector<SmallVector<NpuControlPacketOp> *> copies;
55 for (auto tile : group.tiles.getAsRange<AIE::TileAttr>())
56 copies.push_back(&byTile[tile.getTileID()]);
57 if (copies.size() < 2)
58 continue;
59 size_t length = 0;
60 for (NpuControlPacketOp packet : *copies.front()) {
61 if (packet.getOpcode() != 0 || !packet.getData())
62 break;
63 if (llvm::any_of(llvm::drop_begin(copies), [&](auto *other) {
64 if (length >= other->size())
65 return true;
66 NpuControlPacketOp candidate = (*other)[length];
67 return candidate.getOpcode() != 0 ||
68 packet.getLocalAddress() !=
69 candidate.getLocalAddress() ||
70 packet.getDataAttr() != candidate.getDataAttr() ||
71 packet.getStreamId() != candidate.getStreamId() ||
72 packet.getLength() != candidate.getLength();
73 }))
74 break;
75 ++length;
76 }
77 for (size_t index = 0; index < length; ++index) {
78 NpuControlPacketOp packet = (*copies.front())[index];
79 packet.setMulticastId(id);
80 packet->moveBefore(anchor);
81 for (auto *other : llvm::drop_begin(copies))
82 (*other)[index].erase();
83 }
84 for (auto *copy : copies)
85 copy->erase(copy->begin(), copy->begin() + length);
86 }
87 anchor.erase();
88 }
89 return WalkResult::advance();
90 });
91 });
92 if (result.wasInterrupted())
93 signalPassFailure();
94 }
95};
96
97} // namespace
98
99std::unique_ptr<OperationPass<ModuleOp>>
101 return std::make_unique<AIEDedupCtrlPacketMulticastPass>();
102}
mlir::FailureOr< llvm::SmallVector< ControlPacketMulticast > > controlPacketMulticasts(mlir::ModuleOp module, mlir::FlatSymbolRefAttr reference)
std::unique_ptr< mlir::OperationPass< mlir::ModuleOp > > createAIEDedupCtrlPacketMulticastPass()