12#include "mlir/IR/OpImplementation.h"
13#include "mlir/IR/SymbolTable.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringRef.h"
25std::string TraceEventAttr::getEventName()
const {
26 if (
auto strAttr = llvm::dyn_cast<StringAttr>(getValue())) {
28 StringRef strValue = strAttr.getValue();
29 size_t pos = strValue.find(
"::");
30 if (pos != StringRef::npos) {
31 return strValue.substr(pos + 2).str();
33 return strAttr.getValue().str();
40bool TraceEventAttr::isStringAttr()
const {
41 return llvm::isa<StringAttr>(getValue());
44std::optional<int64_t> TraceEventAttr::getEnumValue()
const {
45 if (
auto intAttr = llvm::dyn_cast<IntegerAttr>(getValue())) {
46 return intAttr.getInt();
56static bool isValidEventForTile(TileLike tile, Attribute eventAttr,
59 bool isShimTile = tile.isShimTile();
60 bool isMemTile = tile.isMemTile();
61 bool isCoreTile = tile.isCoreTile();
65 Attribute innerAttr = eventAttr;
66 if (
auto traceEvent = llvm::dyn_cast<TraceEventAttr>(eventAttr)) {
67 innerAttr = traceEvent.getValue();
78 if (
auto strAttr = llvm::dyn_cast<StringAttr>(innerAttr)) {
79 StringRef val = strAttr.getValue();
80 size_t sep = val.find(
"::");
81 if (sep == StringRef::npos)
84 StringRef prefix = val.substr(0, sep);
90 return prefix ==
"CoreEventAIE" || prefix ==
"MemEventAIE";
92 return prefix ==
"CoreEventAIE2" || prefix ==
"MemEventAIE2";
94 return prefix ==
"CoreEventAIE2P" || prefix ==
"MemEventAIE2P";
96 return prefix ==
"CoreEventAIE4" || prefix ==
"MemEventAIE4";
104 return prefix ==
"MemTileEventAIE2";
106 return prefix ==
"MemTileEventAIE2P";
108 return prefix ==
"MemTileEventAIE4";
116 return prefix ==
"ShimTileEventAIE";
118 return prefix ==
"ShimTileEventAIE2";
120 return prefix ==
"ShimTileEventAIE2P";
122 return prefix ==
"ShimTileEventAIE4";
137void TraceOp::getAsmResultNames(
138 function_ref<
void(Value, StringRef)> setNameFn) {
142LogicalResult TraceOp::verify() {
145 for (
auto &op : getBody().getOps()) {
146 if (isa<TraceEventOp>(op)) {
152 if (eventCount > 8) {
153 return emitOpError(
"trace unit supports maximum 8 events, got ")
158 if (!llvm::dyn_cast_or_null<TileLike>(getTile().getDefiningOp())) {
159 return emitOpError(
"tile operand must be a TileOp or LogicalTileOp");
163 llvm::DenseSet<uint32_t> comboSlots;
164 llvm::DenseSet<uint32_t> edgeSlots;
166 for (
auto &op : getBody().getOps()) {
167 if (
auto comboOp = dyn_cast<TraceComboEventOp>(op)) {
168 uint32_t slot = comboOp.getSlot();
169 if (!comboSlots.insert(slot).second) {
170 return comboOp.emitOpError(
"combo event slot ")
171 << slot <<
" already in use in this trace";
173 }
else if (
auto edgeOp = dyn_cast<TraceEdgeEventOp>(op)) {
174 uint32_t slot = edgeOp.getSlot();
175 if (!edgeSlots.insert(slot).second) {
176 return edgeOp.emitOpError(
"edge detection slot ")
177 << slot <<
" already in use in this trace";
191 MLIRContext *ctx = parser.getContext();
194 std::string strValue;
195 if (succeeded(parser.parseOptionalString(&strValue))) {
196 result = StringAttr::get(ctx, strValue);
201 StringRef enumTypeName;
202 llvm::SMLoc loc = parser.getCurrentLocation();
204 if (failed(parser.parseKeyword(&enumTypeName))) {
205 return parser.emitError(loc,
"expected string or enum event");
208 if (failed(parser.parseColon()) || failed(parser.parseColon())) {
209 return parser.emitError(loc,
"expected '::' after enum type name");
213 if (failed(parser.parseKeyword(&caseName))) {
214 return parser.emitError(parser.getCurrentLocation(),
215 "expected enum case name");
223 struct EnumValidator {
225 std::function<bool(StringRef)> symbolizer;
229 static const EnumValidator validators[] = {
232 [](StringRef s) {
return symbolizeCoreEventAIE2(s).has_value(); }},
235 auto result = symbolizeMemEventAIE2(s);
236 return result.has_value();
240 auto result = symbolizeMemTileEventAIE2(s);
241 return result.has_value();
243 {
"ShimTileEventAIE2",
245 auto result = symbolizeShimTileEventAIE2(s);
246 return result.has_value();
251 auto result = symbolizeCoreEventAIE(s);
252 return result.has_value();
256 auto result = symbolizeMemEventAIE(s);
257 return result.has_value();
261 auto result = symbolizeShimTileEventAIE(s);
262 return result.has_value();
267 auto result = symbolizeCoreEventAIE2P(s);
268 return result.has_value();
272 auto result = symbolizeMemEventAIE2P(s);
273 return result.has_value();
275 {
"MemTileEventAIE2P",
277 auto result = symbolizeMemTileEventAIE2P(s);
278 return result.has_value();
280 {
"ShimTileEventAIE2P",
282 auto result = symbolizeShimTileEventAIE2P(s);
283 return result.has_value();
287 [](StringRef s) {
return symbolizeCoreEventAIE4(s).has_value(); }},
289 [](StringRef s) {
return symbolizeMemEventAIE4(s).has_value(); }},
291 [](StringRef s) {
return symbolizeMemTileEventAIE4(s).has_value(); }},
292 {
"ShimTileEventAIE4",
293 [](StringRef s) {
return symbolizeShimTileEventAIE4(s).has_value(); }},
297 for (
const auto &validator : validators) {
298 if (enumTypeName != validator.name)
300 if (!validator.symbolizer(caseName))
301 return parser.emitError(loc,
"unknown ")
302 << enumTypeName <<
" value: " << caseName;
303 result = StringAttr::get(ctx, enumTypeName +
"::" + caseName);
307 return parser.emitError(loc,
"unknown event enum type: ") << enumTypeName;
311 if (
auto traceAttr = llvm::dyn_cast<TraceEventAttr>(attr)) {
315 if (
auto strAttr = llvm::dyn_cast<StringAttr>(attr)) {
317 if (strAttr.getValue().contains(
"::")) {
318 printer << strAttr.getValue();
321 printer <<
"\"" << strAttr.getValue() <<
"\"";
323 if (
auto intAttr = llvm::dyn_cast<IntegerAttr>(attr)) {
324 printer << intAttr.getInt();
333LogicalResult TraceEventOp::verify() {
335 auto eventAttr = getEvent();
338 std::string eventName = eventAttr.getEventName();
339 if (eventName.empty()) {
340 return emitOpError(
"event name cannot be empty");
344 auto trace = (*this)->getParentOfType<TraceOp>();
346 return emitOpError(
"must be nested in aie.trace");
349 auto tileLike = dyn_cast<TileLike>(trace.getTile().getDefiningOp());
351 return emitOpError(
"trace tile must be a TileOp or LogicalTileOp");
354 auto device = trace->getParentOfType<DeviceOp>();
355 const auto &targetModel = device.getTargetModel();
357 if (!isValidEventForTile(tileLike, eventAttr.getValue(), targetModel)) {
358 std::string tileTypeStr;
359 if (tileLike.isCoreTile())
360 tileTypeStr =
"core tile";
361 else if (tileLike.isMemTile())
362 tileTypeStr =
"mem tile";
364 tileTypeStr =
"shim tile";
367 std::string fullEventName;
368 if (
auto strAttr = llvm::dyn_cast<StringAttr>(eventAttr.getValue()))
369 fullEventName = strAttr.getValue().str();
371 fullEventName = eventName;
373 auto col = tileLike.tryGetCol();
374 auto row = tileLike.tryGetRow();
375 auto fmtCoord = [](std::optional<int> v) -> std::string {
376 return v ? std::to_string(*v) :
"?";
378 return emitOpError(
"event '")
379 << fullEventName <<
"' is not valid for " << tileTypeStr <<
" ("
380 << stringifyAIEArch(targetModel.getTargetArch()) <<
") at ("
381 << fmtCoord(col) <<
", " << fmtCoord(row) <<
")";
391LogicalResult TracePacketOp::verify() {
395 if (
auto id = getId()) {
396 if (*id < 1 || *id > 31)
397 return emitOpError(
"packet ID must be in range [1, 31], got ") << *id;
406LogicalResult TracePortOp::verify() {
408 auto trace = (*this)->getParentOfType<TraceOp>();
410 return emitOpError(
"must be nested in aie.trace");
413 auto tileLike = dyn_cast<TileLike>(trace.getTile().getDefiningOp());
415 return emitOpError(
"trace tile must be a TileOp or LogicalTileOp");
419 auto device = trace->getParentOfType<DeviceOp>();
420 const auto &targetModel = device.getTargetModel();
425 return emitOpError(
"direction must be S2MM (master) or MM2S (slave)");
426 bool isMaster = (getDirection() == DMAChannelDir::S2MM);
429 auto col = tileLike.tryGetCol();
430 auto row = tileLike.tryGetRow();
433 .getStreamSwitchPortIndex(*col, *row, getPort(), getChannel(),
436 return emitOpError(
"invalid stream switch port configuration for tile (")
437 << *col <<
", " << *row <<
")";
442 for (
auto &op : trace.getBody().getOps()) {
443 if (
auto otherPort = dyn_cast<TracePortOp>(op)) {
444 if (otherPort != *
this && otherPort.getSlot() == getSlot()) {
445 return emitOpError(
"duplicate port slot ")
446 << getSlot() <<
" in trace " << trace.getSymName();
458LogicalResult TraceStartEventOp::verify() {
460 bool hasBroadcast = getBroadcast().has_value();
461 bool hasEvent = getEvent().has_value();
463 if (!hasBroadcast && !hasEvent) {
464 return emitOpError(
"must specify either broadcast or event");
467 if (hasBroadcast && hasEvent) {
468 return emitOpError(
"cannot specify both broadcast and event");
478LogicalResult TraceStopEventOp::verify() {
480 bool hasBroadcast = getBroadcast().has_value();
481 bool hasEvent = getEvent().has_value();
483 if (!hasBroadcast && !hasEvent) {
484 return emitOpError(
"must specify either broadcast or event");
487 if (hasBroadcast && hasEvent) {
488 return emitOpError(
"cannot specify both broadcast and event");
498void TraceStartEventOp::print(OpAsmPrinter &p) {
499 if (
auto broadcast = getBroadcast()) {
500 p <<
" broadcast = " << broadcast.value();
502 if (
auto event = getEvent()) {
507 p.printOptionalAttrDict((*this)->getAttrs(),
508 {
"broadcast",
"event"});
511ParseResult TraceStartEventOp::parse(OpAsmParser &parser,
512 OperationState &result) {
514 if (succeeded(parser.parseOptionalKeyword(
"broadcast"))) {
515 IntegerAttr broadcast;
516 if (parser.parseEqual() ||
517 parser.parseAttribute(broadcast, parser.getBuilder().getI32Type(),
518 "broadcast", result.attributes))
523 if (succeeded(parser.parseOptionalKeyword(
"event"))) {
524 if (parser.parseEqual() || parser.parseLess())
527 Attribute innerValue;
531 auto traceEvent = TraceEventAttr::get(parser.getContext(), innerValue);
532 result.attributes.set(
"event", traceEvent);
534 if (parser.parseGreater())
538 if (parser.parseOptionalAttrDict(result.attributes))
544void TraceStopEventOp::print(OpAsmPrinter &p) {
545 if (
auto broadcast = getBroadcast()) {
546 p <<
" broadcast = " << broadcast.value();
548 if (
auto event = getEvent()) {
553 p.printOptionalAttrDict((*this)->getAttrs(),
554 {
"broadcast",
"event"});
557ParseResult TraceStopEventOp::parse(OpAsmParser &parser,
558 OperationState &result) {
560 if (succeeded(parser.parseOptionalKeyword(
"broadcast"))) {
561 IntegerAttr broadcast;
562 if (parser.parseEqual() ||
563 parser.parseAttribute(broadcast, parser.getBuilder().getI32Type(),
564 "broadcast", result.attributes))
569 if (succeeded(parser.parseOptionalKeyword(
"event"))) {
570 if (parser.parseEqual() || parser.parseLess())
573 Attribute innerValue;
577 auto traceEvent = TraceEventAttr::get(parser.getContext(), innerValue);
578 result.attributes.set(
"event", traceEvent);
580 if (parser.parseGreater())
584 if (parser.parseOptionalAttrDict(result.attributes))
594LogicalResult TraceComboEventOp::verify() {
595 uint32_t slot = getSlot();
599 return emitOpError(
"combo event slot must be 0, 1, or 2, got ") << slot;
603 auto trace = (*this)->getParentOfType<TraceOp>();
605 return emitOpError(
"must be nested in aie.trace");
608 auto tileLike = dyn_cast<TileLike>(trace.getTile().getDefiningOp());
610 return emitOpError(
"trace tile must be a TileOp or LogicalTileOp");
614 auto device = trace->getParentOfType<DeviceOp>();
615 const auto &targetModel = device.getTargetModel();
617 if (!isValidEventForTile(tileLike, getEventA().getValue(), targetModel)) {
618 return emitOpError(
"eventA is not valid for this tile type");
620 if (!isValidEventForTile(tileLike, getEventB().getValue(), targetModel)) {
621 return emitOpError(
"eventB is not valid for this tile type");
625 std::string eventAName = getEventA().getEventName();
626 std::string eventBName = getEventB().getEventName();
630 if (eventAName.find(
"COMBO_EVENT") != std::string::npos ||
631 eventBName.find(
"COMBO_EVENT") != std::string::npos) {
632 return emitOpError(
"combo slot 0 should use regular events, not "
633 "COMBO_EVENT_* (uses eventA/B)");
635 }
else if (slot == 1) {
637 if (eventAName.find(
"COMBO_EVENT") != std::string::npos ||
638 eventBName.find(
"COMBO_EVENT") != std::string::npos) {
639 return emitOpError(
"combo slot 1 should use regular events, not "
640 "COMBO_EVENT_* (uses eventC/D)");
642 }
else if (slot == 2) {
644 if (eventAName !=
"COMBO_EVENT_0") {
645 return emitOpError(
"combo slot 2 first event must be COMBO_EVENT_0 "
646 "(hierarchical), got ")
649 if (eventBName !=
"COMBO_EVENT_1") {
650 return emitOpError(
"combo slot 2 second event must be COMBO_EVENT_1 "
651 "(hierarchical), got ")
663LogicalResult TraceEdgeEventOp::verify() {
664 uint32_t slot = getSlot();
668 return emitOpError(
"edge detection slot must be 0 or 1, got ") << slot;
672 auto trace = (*this)->getParentOfType<TraceOp>();
674 return emitOpError(
"must be nested in aie.trace");
677 auto tileLike = dyn_cast<TileLike>(trace.getTile().getDefiningOp());
679 return emitOpError(
"trace tile must be a TileOp or LogicalTileOp");
683 auto device = trace->getParentOfType<DeviceOp>();
684 const auto &targetModel = device.getTargetModel();
686 if (!isValidEventForTile(tileLike, getEvent().getValue(), targetModel)) {
687 return emitOpError(
"event is not valid for this tile type");
691 std::string eventName = getEvent().getEventName();
692 if (eventName.find(
"EDGE_DETECTION_EVENT") != std::string::npos) {
693 return emitOpError(
"edge detection source should be a regular event, not "
694 "another EDGE_DETECTION_EVENT");
696 if (eventName.find(
"COMBO_EVENT") != std::string::npos) {
697 return emitOpError(
"edge detection source should be a regular event, not "
698 "a COMBO_EVENT (combo events can be used but may have "
699 "unexpected behavior)");
705void TraceEdgeEventOp::print(OpAsmPrinter &p) {
706 p <<
"<" << getSlot() <<
"> event = <";
708 p <<
"> trigger = " << getTrigger();
709 p.printOptionalAttrDict((*this)->getAttrs(),
710 {
"slot",
"event",
"trigger"});
713ParseResult TraceEdgeEventOp::parse(OpAsmParser &parser,
714 OperationState &result) {
716 Attribute eventValue;
717 EdgeTriggerAttr trigger;
719 if (parser.parseLess() ||
720 parser.parseAttribute(slot, parser.getBuilder().getI32Type(),
"slot",
721 result.attributes) ||
722 parser.parseGreater() || parser.parseKeyword(
"event") ||
723 parser.parseEqual() || parser.parseLess())
730 auto event = TraceEventAttr::get(parser.getContext(), eventValue);
731 result.attributes.set(
"event", event);
733 if (parser.parseGreater() || parser.parseKeyword(
"trigger") ||
738 StringRef triggerStr;
739 if (failed(parser.parseKeyword(&triggerStr)))
742 auto triggerEnum = symbolizeEdgeTrigger(triggerStr);
744 return parser.emitError(parser.getCurrentLocation(),
745 "unknown edge trigger: ")
748 trigger = EdgeTriggerAttr::get(parser.getContext(), *triggerEnum);
749 result.attributes.set(
"trigger", trigger);
751 if (parser.parseOptionalAttrDict(result.attributes))
761LogicalResult TraceHostConfigOp::verify() {
764 if (getReuseOutputBuffer()) {
765 if (getRouting() != TraceShimRouting::Single) {
766 return emitOpError(
"reuse_output_buffer (append trace after last tensor) "
767 "only works with single shim destination strategy "
773 if (getBufferSize() <= 0) {
774 return emitOpError(
"buffer_size must be positive");
778 if (getEgressShimCol() < 0) {
779 return emitOpError(
"egress_shim_col must be >= 0");
789LogicalResult TraceStartConfigOp::verify() {
792 auto symbolName = getTraceConfig();
793 if (symbolName.empty()) {
794 return emitOpError(
"trace config symbol name cannot be empty");
virtual AIEArch getTargetArch() const =0
Return the target architecture.
mlir::ParseResult parseTraceEvent(mlir::AsmParser &parser, mlir::Attribute &result)
void printTraceEventEnum(mlir::AsmPrinter &printer, mlir::Attribute attr)
WireBundle getDMAChannelBundle(DMAChannelDir dir)
The stream-switch bundle the channels of dir use.