12#include "mlir/Pass/Pass.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/ADT/StringRef.h"
25#define GEN_PASS_DEF_AIECERTVERIFY
26#include "aie/Dialect/AIEX/Transforms/AIEXPasses.h.inc"
32#define DEBUG_TYPE "cert-verify"
38static LogicalResult checkJobSlots(AIE::DeviceOp device) {
39 LogicalResult result = success();
40 device.walk([&](AIEX::CertPageOp page) {
41 llvm::DenseMap<unsigned, AIEX::CertJobOp> slots;
42 for (
auto job : page.getBody().front().getOps<AIEX::CertJobOp>()) {
43 unsigned slot = job.getJobId() % AIEX::CertJobOp::NumSlots;
44 auto [it, inserted] = slots.try_emplace(slot, job);
46 job.emitError() <<
"job ID " << job.getJobId()
47 <<
" collides with job ID " << it->second.getJobId()
48 <<
" modulo " << AIEX::CertJobOp::NumSlots
49 <<
" on the same page";
59static int getUcGroupId(Operation *op) {
60 auto group = op->getParentOfType<AIEX::CertAttachToGroupOp>();
61 return group ? group.getGroupID() : 0;
67static LogicalResult checkLocalBarrierColocation(AIE::DeviceOp device) {
68 LogicalResult result = success();
70 std::map<std::pair<int, int>, Operation *> firstScope;
72 [&](AIEX::CertLocalBarrierOp barrier) {
73 int uc = getUcGroupId(barrier);
74 int id =
static_cast<int>(barrier.getLocalBarrierId());
75 Operation *scope = barrier->getParentOfType<AIEX::CertPageOp>();
77 scope = barrier->getParentOfType<AIEX::CertJobOp>();
78 auto key = std::make_pair(uc,
id);
79 auto it = firstScope.find(key);
80 if (it == firstScope.end()) {
81 firstScope[key] = scope;
84 if (it->second != scope) {
86 <<
"cert.local_barrier "
87 << stringifyCertLocalBarrierId(barrier.getLocalBarrierId())
89 <<
" must be on the same page as its other participants";
100static LogicalResult checkWaitTctsUniqueness(AIE::DeviceOp device) {
101 LogicalResult result = success();
103 std::map<std::tuple<Operation *, int, int>, Operation *> seen;
104 device.walk([&](AIEX::CertWaitTCTSOp wait) {
105 int tile = wait.getTileId();
106 int channel = wait.getChannelId();
107 Operation *job = wait->getParentOfType<AIEX::CertJobOp>();
108 Operation *scope = wait->getParentOfType<AIEX::CertPageOp>();
111 auto key = std::make_tuple(scope, tile, channel);
112 auto it = seen.find(key);
113 if (it == seen.end()) {
118 if (it->second == job)
120 wait.emitError() <<
"more than one job on this cert.page waits on tile "
121 << tile <<
" channel " << channel <<
" in uC "
122 << getUcGroupId(wait)
123 <<
"; at most one job per page may wait_tcts on a given "
130static std::set<int> computePresentUcs(AIE::DeviceOp device) {
131 std::set<int> present;
132 device.walk([&](AIEX::CertAttachToGroupOp group) {
133 present.insert(group.getGroupID());
135 if (!device.getBody()->getOps<AIEX::CertPageOp>().empty() ||
136 !device.getBody()->getOps<AIEX::CertJobOp>().empty() ||
137 !device.getBody()->getOps<AIEX::CertSectionOp>().empty())
144static LogicalResult checkPreemptConsistency(AIE::DeviceOp device) {
145 std::map<int, std::multiset<int>> preemptIds;
146 std::map<int, AIEX::CertAttachToGroupOp> repAttach;
147 std::set<int> present;
150 device.walk([&](AIEX::CertAttachToGroupOp group) {
151 int uc = group.getGroupID();
153 if (!repAttach.count(uc))
154 repAttach[uc] = group;
158 if (!device.getBody()->getOps<AIEX::CertPageOp>().empty() ||
159 !device.getBody()->getOps<AIEX::CertJobOp>().empty() ||
160 !device.getBody()->getOps<AIEX::CertSectionOp>().empty())
164 device.walk([&](AIEX::CertPreemptOp preempt) {
165 int uc = getUcGroupId(preempt);
167 preemptIds[uc].insert(preempt.getId());
170 if (present.size() < 2)
173 int ref = *present.begin();
174 const std::multiset<int> &refSet = preemptIds[ref];
175 LogicalResult result = success();
176 for (
int uc : present) {
179 if (preemptIds[uc] != refSet) {
180 Operation *loc = repAttach.count(uc) ? repAttach[uc].getOperation()
181 : device.getOperation();
183 <<
"cert.preempt ids on uC " << uc <<
" differ from uC " << ref
184 <<
"; all uCs must have the same preempt ids (G-preempt)";
195static LogicalResult checkGroupIds(AIE::DeviceOp device) {
198 LogicalResult result = success();
199 auto checkId = [&](Operation *op, int64_t id, llvm::StringRef what) {
200 if (id < 0 || id >= numUcs) {
201 op->emitError() << what <<
" " <<
id <<
" is not a valid uC id [0, "
206 device.walk([&](AIEX::CertAttachToGroupOp g) {
207 checkId(g, g.getGroupID(),
"cert.attach_to_group group id");
210 device.walk([&](AIEX::CertPageOp p) {
211 if (
auto pl = p.getPlacementAttr())
212 checkId(p, pl.getInt(),
"cert.page placement");
223static LogicalResult checkRemoteBarrierParties(AIE::DeviceOp device) {
224 std::set<int> present = computePresentUcs(device);
225 LogicalResult result = success();
227 std::map<AIEX::CertRemoteBarrierId, SmallVector<AIEX::CertRemoteBarrierOp, 4>>
229 device.walk([&](AIEX::CertRemoteBarrierOp b) {
230 byId[b.getRemoteBarrierId()].push_back(b);
233 for (
auto &entry : byId) {
234 StringRef
id = stringifyCertRemoteBarrierId(entry.first);
235 SmallVector<AIEX::CertRemoteBarrierOp, 4> &occs = entry.second;
238 uint32_t mask = occs.front().getPartyMask();
239 bool masksAgree = llvm::all_of(occs, [&](AIEX::CertRemoteBarrierOp b) {
240 return b.getPartyMask() == mask;
244 b.emitError() <<
"cert.remote_barrier " <<
id
245 <<
" occurrences disagree on party_mask";
251 std::map<int, int> occPerUc;
252 for (
auto b : occs) {
253 int own = getUcGroupId(b);
255 if (own >= 0 && own < 32 && !((mask >> own) & 1u)) {
256 b.emitError() <<
"cert.remote_barrier " <<
id <<
" on uC " << own
257 <<
" excludes its own uC from party_mask";
261 for (
auto &[uc, count] : occPerUc)
263 occs.front().emitError()
264 <<
"cert.remote_barrier " <<
id <<
" has " << count
265 <<
" occurrences on uC " << uc
266 <<
" (at most one participant per uC per rendezvous)";
271 for (
int bit = 0; bit < 32; ++bit) {
272 if (!((mask >> bit) & 1u))
274 if (!present.count(bit)) {
275 occs.front().emitError()
276 <<
"cert.remote_barrier " <<
id <<
" party_mask references uC "
277 << bit <<
" which is not present in the design";
279 }
else if (!occPerUc.count(bit)) {
280 occs.front().emitError()
281 <<
"cert.remote_barrier " <<
id <<
" party_mask includes uC " << bit
282 <<
" but that uC has no matching remote_barrier(" <<
id
283 <<
") to rendezvous with";
292static std::optional<uint32_t> registerWrittenBy(Operation &op) {
293 if (
auto o = dyn_cast<AIEX::CertUcDmaWriteDesOp>(op))
294 return o.getWaitHandle();
295 if (
auto o = dyn_cast<AIEX::CertRead32Op>(op))
297 if (
auto o = dyn_cast<AIEX::CertRead32DOp>(op))
305static LogicalResult checkUcDmaWaitHandles(AIE::DeviceOp device) {
306 LogicalResult result = success();
307 device.walk([&](AIEX::CertJobOp job) {
308 llvm::SmallDenseMap<uint32_t, Operation *> lastWrite;
309 for (Operation &o : job.getBody().front()) {
310 if (
auto wait = dyn_cast<AIEX::CertWaitUcDmaOp>(o)) {
311 uint32_t r = wait.getWaitHandle();
312 auto it = lastWrite.find(r);
314 wait.emitError() <<
"cert.wait_uc_dma handle r" << r
315 <<
" is a shared register; the handle must be a "
316 "local register r0..r7 written by a "
317 "cert.uc_dma_write_des in the same job";
319 }
else if (it == lastWrite.end()) {
320 wait.emitError() <<
"cert.wait_uc_dma waits on r" << r
321 <<
", which no earlier cert.uc_dma_write_des in "
324 }
else if (!isa<AIEX::CertUcDmaWriteDesOp>(it->second)) {
325 wait.emitError() <<
"cert.wait_uc_dma waits on r" << r
326 <<
", but its last write is a '"
327 << it->second->getName().getStringRef()
328 <<
"', which replaced the handle";
332 if (
auto r = registerWrittenBy(o))
339struct AIECertVerifyPass
340 : xilinx::AIEX::impl::AIECertVerifyBase<AIECertVerifyPass> {
341 void runOnOperation()
override {
342 AIE::DeviceOp device = getOperation();
343 if (failed(checkJobSlots(device)))
345 if (failed(checkGroupIds(device)))
347 if (failed(checkLocalBarrierColocation(device)))
349 if (failed(checkWaitTctsUniqueness(device)))
351 if (failed(checkPreemptConsistency(device)))
353 if (failed(checkRemoteBarrierParties(device)))
355 if (failed(checkUcDmaWaitHandles(device)))
363 return std::make_unique<AIECertVerifyPass>();
virtual uint32_t getNumControllers() const
Total number of microcontrollers (uC) in the device.
std::unique_ptr< mlir::OperationPass< AIE::DeviceOp > > createAIECertVerifyPass()