14#include "mlir/Dialect/Utils/StaticValueUtils.h"
15#include "mlir/IR/Attributes.h"
16#include "mlir/IR/IRMapping.h"
17#include "mlir/Pass/Pass.h"
18#include "mlir/Tools/mlir-translate/MlirTranslateMain.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/IR/Module.h"
31static const char *xaie_cpp_file_header = R
"code(
32// This file was auto-generated by aiecc --aie-generate-xaie.
35#define __mlir_aie_verbose(x) x
37#define __mlir_aie_verbose(x)
40// The following is a wrapper for the common "if(call() != 0) return 1" pattern.
41// Use this only in functions that return int. If the call this wrapper is used
42// on does not succeed, the expanded code will exit out of the function
43// containing this macro with an error code.
44#define __mlir_aie_try(x) do { \
46 if(ret != XAIE_OK) { \
51static XAie_DmaDimDesc *__mlir_aie_alloc_dim_desc(size_t ndims) {
52 XAie_DmaDimDesc *ret = NULL;
53 ret = (XAie_DmaDimDesc *)calloc(sizeof(XAie_DmaDimDesc), ndims);
55 __mlir_aie_verbose(fprintf(stderr, "Allocating DmaDimDesc failed.\n"));
62static std::string tileLockStr(StringRef
id, StringRef val) {
64 llvm::raw_string_ostream rss(str);
66 rss <<
"XAie_LockInit(" <<
id <<
"," << val <<
")";
72static std::string wireBundleToPortType(WireBundle bundle) {
74 case WireBundle::PLIO:
76 case WireBundle::TileControl:
79 return stringifyWireBundle(bundle).upper();
86template <
typename OpType>
87static mlir::LogicalResult generateDMAConfig(OpType memOp, raw_ostream &output,
89 DenseMap<Block *, int> blockMap) {
90 StringRef enable =
"XAIE_ENABLE";
91 StringRef disable =
"XAIE_DISABLE";
92 StringRef deviceInstRef =
"ctx->XAieDevInst";
94 int col = memOp.colIndex();
95 int row = memOp.rowIndex();
99 llvm::SetVector<Block *> blockVector =
102 llvm::SmallPtrSet<Block *, 8> unchainedBlocks =
105 for (
auto *block : blockVector) {
106 bool foundBdPacket =
false;
109 bool foundBd =
false;
113 int elementWidthInBytes = 0;
115 int iterSize = 0, iterStride = 0, iterCurr = 0;
116 uint32_t axcache = 0;
117 std::optional<int> oooId;
118 ArrayRef<BDDimLayoutAttr> dims;
120 SmallVector<BDDimLayoutAttr> dimsStorage;
124 for (
auto op : block->getOps<DMABDPACKETOp>()) {
125 foundBdPacket =
true;
126 packetType = op.getPacketType();
127 packetID = op.getPacketID();
129 for (
auto op : block->getOps<DMABDOp>()) {
131 if (
auto packetInfo = op.getPacket()) {
132 foundBdPacket =
true;
133 packetType = packetInfo->getPktType();
134 packetID = packetInfo->assignedId();
137 std::optional<int32_t> bufferAddr = op.getBufferOp().getAddress();
138 assert(bufferAddr &&
"buffer must have address assigned");
139 BaseAddrA = *bufferAddr;
140 int bufferCol = op.getBufferOp().getTileOp().colIndex();
141 int bufferRow = op.getBufferOp().getTileOp().rowIndex();
146 col, row, bufferCol, bufferRow);
148 BaseAddrA += addrOffset.value();
152 lenA = op.getLenInBytes();
153 offsetA = op.getOffsetInBytes();
154 elementWidthInBytes = op.getBufferElementTypeWidthInBytes();
155 if (
auto iter = op.getIteration()) {
156 iterSize = iter->getSize();
157 iterStride = iter->getStride();
158 iterCurr = iter->getCurrent();
160 axcache = op.getAxcacheOrDefault();
161 oooId = op.getOutOfOrderId();
162 if (!op.getMixedSizes().empty()) {
164 for (mlir::OpFoldResult s : op.getMixedSizes())
165 if (!mlir::getConstantIntValue(s))
166 return op->emitOpError(
167 "runtime-valued BD size/stride is not supported on the static "
168 "XAIE lowering path; use compile-time constant sizes");
169 for (mlir::OpFoldResult s : op.getMixedStrides())
170 if (!mlir::getConstantIntValue(s))
171 return op->emitOpError(
172 "runtime-valued BD size/stride is not supported on the static "
173 "XAIE lowering path; use compile-time constant sizes");
174 std::optional<SmallVector<BDDimLayoutAttr>> folded =
175 op.getConstantDimensions();
177 return op->emitOpError(
"internal error folding BD dimensions");
178 dimsStorage = std::move(*folded);
186 return memOp.emitOpError(
"DMA contains at least one multi-dimensional "
187 "buffer descriptor. This is currently only "
188 "supported for AIE-ML and later devices.");
191 if (foundBd && lenA > std::numeric_limits<uint32_t>::max()) {
192 return memOp.emitOpError(
"buffer descriptor length in bytes (")
193 << lenA <<
") does not fit in 32 bits";
196 int acqValue = 0, relValue = 0;
197 bool hasAcq =
false, hasRel =
false;
198 int acqLockID = 0, relLockID = 0;
199 for (
auto op : block->getOps<UseLockOp>()) {
200 LockOp lock = cast<LockOp>(op.getLock().getDefiningOp());
201 int lockCol = lock.colIndex();
202 int lockRow = lock.rowIndex();
203 int lockID = lock.getLockIDValue();
209 lockID += lockOffset.value();
212 if (op.acquire() || op.acquireGE()) {
215 auto value = op.getConstantValue();
220 acqValue = -acqValue;
221 }
else if (op.release()) {
224 auto value = op.getConstantValue();
230 return op.emitOpError(
"unsupported lock action");
234 int bdNum = blockMap[block];
239 output <<
"XAie_DmaDesc " <<
tileDMAInstStr(col, row, bdNum) <<
";\n";
240 output <<
"__mlir_aie_try(XAie_DmaDescInit(" << deviceInstRef <<
", "
243 if (hasAcq || hasRel) {
244 output <<
"__mlir_aie_try(XAie_DmaSetLock("
246 <<
"XAie_LockInit(" << acqLockID <<
"," << acqValue <<
"),"
247 <<
"XAie_LockInit(" << relLockID <<
"," << relValue <<
")));\n";
250 <<
".LockDesc.LockAcqEn = " << disable <<
";\n";
253 <<
".LockDesc.LockRelEn = " << disable <<
";\n";
258 output <<
"__mlir_aie_try(XAie_DmaSetAddrLen("
260 <<
"mlir_aie_external_get_addr_myBuffer_" << col << row <<
"_"
262 <<
" /* len */ " << lenA <<
"));\n";
263 output <<
"__mlir_aie_try(XAie_DmaSetAxi("
266 <<
"/* burstlen */ 4, "
268 <<
"/* Cache */ " << axcache <<
", "
269 <<
"/* Secure */ " << enable <<
"));\n";
271 if ((BaseAddrA + offsetA) % 4)
272 return memOp.emitError(
"bd address must be 4B (32b) aligned");
273 output <<
"__mlir_aie_try(XAie_DmaSetAddrLen("
275 <<
"0x" << llvm::utohexstr(BaseAddrA + offsetA) <<
", "
276 <<
" /* len */ " << lenA <<
"));\n";
280 BaseAddrA, offsetA, lenA,
281 elementWidthInBytes,
"1");
283 if (iterSize > 1 && iterStride > 0) {
284 int64_t strideInBytes =
285 static_cast<int64_t
>(iterStride) * elementWidthInBytes;
286 if (strideInBytes % 4)
287 return memOp.emitError(
"iteration_stride must result in a stride "
288 "aligned to 32-bit words");
289 int stepInWords =
static_cast<int>(strideInBytes / 4);
290 output <<
"__mlir_aie_try(XAie_DmaSetBdIteration("
292 <<
", " << iterSize <<
", " << iterCurr <<
"));\n";
295 if (block->getNumSuccessors() > 0) {
296 Block *nextBlock = block->getSuccessors()[0];
299 int enableNextBd = 1;
300 if (!nextBlock->getOps<EndOp>().empty())
302 if (unchainedBlocks.contains(block))
305 int nextBdNum = blockMap[nextBlock];
306 output <<
"__mlir_aie_try(XAie_DmaSetNextBd("
308 <<
" /* nextbd */ " << nextBdNum <<
", "
309 <<
" /* enableNextBd */ " << enableNextBd <<
"));\n";
313 output <<
"__mlir_aie_try(XAie_DmaSetPkt("
315 <<
packetStr(packetID, packetType) <<
"));\n";
317 if (oooId.has_value()) {
318 output <<
"__mlir_aie_try(XAie_DmaSetOutofOrderBdId("
322 output <<
"__mlir_aie_try(XAie_DmaEnableBd("
324 output <<
"__mlir_aie_try(XAie_DmaWriteBd(" << deviceInstRef <<
", "
327 <<
" /* bd */ " << bdNum <<
"));\n";
331 for (
auto *block : blockVector) {
332 for (
auto op : block->getOps<DMAStartOp>()) {
333 int bdNum = blockMap[op.getDest()];
334 StringRef dmaDir = stringifyDMAChannelDir(op.getChannelDir());
335 int chNum = op.getChannelIndex();
337 if (target_model.getTargetArch() == AIEArch::AIE1) {
338 output <<
"__mlir_aie_try(XAie_DmaChannelPushBdToQueue("
339 << deviceInstRef <<
", " <<
tileLocStr(col, row) <<
", "
340 <<
"/* ChNum */" << chNum
343 <<
"/* dmaDir */ DMA_" << dmaDir <<
", "
344 <<
"/* BdNum */" << bdNum <<
"));\n";
345 }
else if (op.getOutOfOrder()) {
348 int repeatCount = op.getRepeatCount() + 1;
350 std::string oooSuffix = std::to_string(col) +
"_" +
351 std::to_string(row) +
"_" +
352 std::to_string(chNum);
354 output <<
"XAie_DmaChannelDesc ooo_desc_" << oooSuffix <<
";\n";
355 output <<
"__mlir_aie_try(XAie_DmaChannelDescInit(" << deviceInstRef
356 <<
", &ooo_desc_" << oooSuffix <<
", " <<
tileLocStr(col, row)
358 output <<
"__mlir_aie_try(XAie_DmaChannelEnOutofOrder(&ooo_desc_"
359 << oooSuffix <<
", " << enable <<
"));\n";
360 output <<
"__mlir_aie_try(XAie_DmaWriteChannel(" << deviceInstRef
361 <<
", &ooo_desc_" << oooSuffix <<
", " <<
tileLocStr(col, row)
362 <<
", /* ChNum */" << chNum <<
", /* dmaDir */ DMA_" << dmaDir
364 output <<
"XAie_DmaDeclareQueueConfig(ooo_queue_" << oooSuffix
365 <<
", /* StartBd */ 0, /* Repeat */ " << repeatCount
366 <<
", /* EnToken */ " << disable <<
", /* EnOutOfOrder */ "
368 output <<
"__mlir_aie_try(XAie_DmaChannelSetStartQueueGeneric("
369 << deviceInstRef <<
", " <<
tileLocStr(col, row)
370 <<
", /* ChNum */" << chNum <<
", /* dmaDir */ DMA_" << dmaDir
371 <<
", &ooo_queue_" << oooSuffix <<
"));\n";
376 int repeatCount = op.getRepeatCount() + 1;
377 output <<
"__mlir_aie_try(XAie_DmaChannelSetStartQueue("
378 << deviceInstRef <<
", " <<
tileLocStr(col, row) <<
", "
379 <<
"/* ChNum */" << chNum
382 <<
"/* dmaDir */ DMA_" << dmaDir <<
", "
383 <<
"/* BdNum */" << bdNum <<
", "
384 <<
"/* Repeat */ " << repeatCount <<
", "
389 output <<
"__mlir_aie_try(XAie_DmaChannelEnable(" << deviceInstRef <<
", "
391 <<
"/* ChNum */ " << chNum
394 <<
"/* dmaDir */ DMA_" << dmaDir <<
"));\n";
402 llvm::StringRef deviceName) {
403 StringRef ctx_p =
"aie_libxaie_ctx_t* ctx";
404 StringRef deviceInstRef =
"ctx->XAieDevInst";
406 DenseMap<TileID, Operation *> tiles;
407 DenseMap<Operation *, SmallVector<BufferOp, 4>> buffers;
409 DeviceOp targetOp = AIE::DeviceOp::getForSymbolInModule(module, deviceName);
411 return module.emitOpError("expected AIE.device operation at toplevel");
414 for (CoreOp core : targetOp.getOps<CoreOp>()) {
415 bool configuresDMA = core.getProcessorBus();
416 core.walk([&](Operation *op) {
417 configuresDMA |= isa<DmaPushQueueOp, DmaPollForQueueSpaceOp>(op);
420 return core.emitOpError(
"core-issued configuration is not supported by "
421 "the XAIEV2 backend");
423 WalkResult corePushes = targetOp.walk([&](DMAStartOp start) {
424 if (!start.getCorePushes())
425 return WalkResult::advance();
426 start.emitOpError(
"core-issued configuration is not supported by "
427 "the XAIEV2 backend");
428 return WalkResult::interrupt();
430 if (corePushes.wasInterrupted())
432 const auto &targetModel = targetOp.getTargetModel();
440 output << xaie_cpp_file_header;
441 output <<
"aie_libxaie_ctx_t* mlir_aie_init_libxaie() {\n";
442 output <<
" aie_libxaie_ctx_t *ctx = new aie_libxaie_ctx_t;\n";
443 output <<
" if (!ctx)\n";
444 output <<
" return 0;\n";
445 output <<
" ctx->XAieConfig = new XAie_Config;\n";
446 output <<
" if (!ctx->XAieConfig)\n";
447 output <<
" return 0;\n";
448 output <<
" ctx->XAieDevInst = new XAie_DevInst;\n";
449 output <<
" if (!ctx->XAieDevInst)\n";
450 output <<
" return 0;\n";
452 std::string AIE1_device(
"XAIE_DEV_GEN_AIE");
453 std::string AIE2_device(
"XAIE_DEV_GEN_AIEML");
454 std::string AIE2p_device(
"XAIE_DEV_GEN_AIE2P");
458 device = AIE1_device;
461 device = AIE2_device;
464 device = AIE2p_device;
466 case AIEArch::AIE2ps:
470 return targetOp.emitOpError(
471 "--aie-generate-xaie is not supported for the AIE2PS architecture");
473 return targetOp.emitOpError(
474 "--aie-generate-xaie is not supported for the AIE4 architecture");
476 output <<
" ctx->XAieConfig->AieGen = " << device <<
";\n";
477 output <<
" ctx->XAieConfig->BaseAddr = 0x20000000000;\n";
478 output <<
" ctx->XAieConfig->ColShift = " << targetModel.
getColumnShift()
480 output <<
" ctx->XAieConfig->RowShift = " << targetModel.
getRowShift()
482 output <<
" ctx->XAieConfig->NumRows = " << targetModel.
rows() <<
";\n";
483 output <<
" ctx->XAieConfig->NumCols = " << targetModel.
columns() <<
";\n";
484 output <<
" ctx->XAieConfig->ShimRowNum = 0;\n";
485 output <<
" ctx->XAieConfig->MemTileRowStart = 1;\n";
486 output <<
" ctx->XAieConfig->MemTileNumRows = "
488 output <<
" // ctx->XAieConfig->ReservedRowStart = "
489 "XAIE_RES_TILE_ROW_START;\n";
491 <<
" // ctx->XAieConfig->ReservedNumRows = XAIE_RES_TILE_NUM_ROWS;\n";
492 output <<
" ctx->XAieConfig->AieTileRowStart = "
494 output <<
" ctx->XAieConfig->AieTileNumRows = "
496 output <<
" memset(ctx->XAieDevInst, 0, sizeof(XAie_DevInst));\n";
497 output <<
" return ctx;\n";
506 output <<
"int mlir_aie_initialize_buffers(" << ctx_p <<
");\n";
507 output <<
"int mlir_aie_configure_cores(" << ctx_p <<
") {\n";
509 for (
auto tileOp : targetOp.getOps<TileOp>()) {
510 int col = tileOp.colIndex();
511 int row = tileOp.rowIndex();
512 if (tileOp.isShimTile() || tileOp.isMemTile()) {
516 output <<
"__mlir_aie_try(XAie_CoreReset(" << deviceInstRef <<
", "
518 output <<
"__mlir_aie_try(XAie_CoreDisable(" << deviceInstRef <<
", "
522 output <<
"for (int l = 0; l < " << numLocks <<
"; ++l)\n"
523 <<
" __mlir_aie_try(XAie_LockRelease(" << deviceInstRef <<
", "
524 <<
tileLocStr(col, row) <<
", XAie_LockInit(l, 0x0), 0));\n";
525 if (
auto coreOp = tileOp.getCoreOp()) {
526 if (coreOp.isEmpty() && coreOp.getElfFile() == std::nullopt) {
531 std::string fileName;
532 if (
auto fileAttr = coreOp.getElfFile()) {
533 fileName = fileAttr.value().str();
535 return coreOp.emitOpError()
536 <<
"Expected lowered ELF file to be given as attribute "
537 "`elf_file` for this core. Compile cores first.";
540 <<
"AieRC RC = XAie_LoadElf(" << deviceInstRef <<
", "
542 <<
"(const char*)\"" << fileName <<
"\",0);\n";
543 output <<
"if (RC != XAIE_OK)\n"
544 <<
" __mlir_aie_verbose(fprintf(stderr, \"Failed to load elf "
545 "for Core[%d,%d], ret is %d\\n\", "
546 << std::to_string(col) <<
", " << std::to_string(row)
548 <<
"assert(RC == XAIE_OK);\n"
556 output <<
" int rc = mlir_aie_initialize_buffers(ctx);\n";
557 output <<
" if (rc != XAIE_OK)\n";
558 output <<
" return rc;\n";
560 output <<
"return XAIE_OK;\n";
561 output <<
"} // mlir_aie_configure_cores\n\n";
566 output <<
"int mlir_aie_start_cores(" << ctx_p <<
") {\n";
568 for (
auto tileOp : targetOp.getOps<TileOp>()) {
569 int col = tileOp.colIndex();
570 int row = tileOp.rowIndex();
571 if (!tileOp.isShimTile() && !tileOp.isMemTile()) {
572 output <<
"__mlir_aie_try(XAie_CoreUnreset(" << deviceInstRef <<
", "
574 output <<
"__mlir_aie_try(XAie_CoreEnable(" << deviceInstRef <<
", "
578 output <<
"return XAIE_OK;\n";
579 output <<
"} // mlir_aie_start_cores\n\n";
584 output <<
"int mlir_aie_configure_dmas(" << ctx_p <<
") {\n";
586 for (
auto memOp : targetOp.getOps<MemOp>()) {
587 DenseMap<Block *, int> blockMap;
591 for (
auto &block : memOp.getBody()) {
592 auto bdOps = block.getOps<DMABDOp>();
595 std::optional<int32_t>
id = (*bdOps.begin()).getBdId();
596 blockMap[&block] =
id.has_value() ? *id : bdNum;
599 auto result = generateDMAConfig(memOp, output, targetModel, blockMap);
603 for (
auto memOp : targetOp.getOps<MemTileDMAOp>()) {
604 DenseMap<Block *, int> blockMap;
606 DenseMap<Block *, int> channelMap;
608 for (
auto &block : memOp.getBody()) {
609 for (
auto op : block.getOps<DMAStartOp>()) {
610 int chNum = op.getChannelIndex();
611 channelMap[&block] = chNum;
612 auto *dest = op.getDest();
614 channelMap[dest] = chNum;
615 if (dest->getSuccessors().empty())
617 dest = dest->getSuccessors()[0];
618 if (channelMap.count(dest))
627 for (
auto &block : memOp.getBody()) {
628 auto bdOps = block.getOps<DMABDOp>();
631 assert(channelMap.count(&block));
632 if (std::optional<int32_t>
id = (*bdOps.begin()).getBdId())
633 blockMap[&block] = *id;
634 else if (channelMap[&block] & 1)
635 blockMap[&block] = oddBdNum++;
637 blockMap[&block] = evenBdNum++;
639 auto result = generateDMAConfig(memOp, output, targetModel, blockMap);
644 output <<
"return XAIE_OK;\n";
645 output <<
"} // mlir_aie_configure_dmas\n\n";
647 for (
auto op : targetOp.getOps<ExternalBufferOp>()) {
649 output <<
"static u64 _mlir_aie_external_" << op.name().getValue()
651 output <<
"static bool _mlir_aie_external_set_" << op.name().getValue()
654 output <<
"void mlir_aie_external_set_addr_" << op.name().getValue()
655 <<
"(" << ctx_p <<
", u64 VA) {\n"
656 <<
" u64 device_address = mlir_aie_get_device_address(ctx, (void "
658 <<
" _mlir_aie_external_set_" << op.name().getValue()
660 <<
" _mlir_aie_external_" << op.name().getValue()
661 <<
" = device_address;\n"
668 for (
auto op : targetOp.getOps<ShimDMAOp>()) {
669 int col = op.colIndex();
670 int row = op.rowIndex();
672 DenseMap<Block *, int> blockMap;
676 for (
auto &block : op.getBody()) {
677 auto bdOps = block.getOps<DMABDOp>();
680 std::optional<int32_t>
id = (*bdOps.begin()).getBdId();
681 int slot =
id.has_value() ? *id : bdNum;
682 blockMap[&block] = slot;
684 for (
auto op : block.getOps<DMABDOp>()) {
685 offset = op.getOffsetInBytes();
686 auto buffer = cast<ExternalBufferOp>(op.getBuffer().getDefiningOp());
688 output <<
"u64 mlir_aie_external_get_addr_myBuffer_" << col << row
689 <<
"_" << slot <<
"(void) {\n"
690 <<
" assert(_mlir_aie_external_set_"
691 << buffer.name().getValue() <<
");\n"
692 <<
" return _mlir_aie_external_" << buffer.name().getValue()
693 <<
" + " << llvm::utohexstr(offset) <<
";\n"
701 output <<
"int mlir_aie_configure_shimdma_" << col << row <<
"(" << ctx_p
703 auto result = generateDMAConfig(op, output, targetModel, blockMap);
706 output <<
"return XAIE_OK;\n";
707 output <<
"} // mlir_aie_configure_shimdma\n\n";
713 output <<
"int mlir_aie_initialize_locks(" << ctx_p <<
") {\n";
715 targetOp.walk<WalkOrder::PreOrder>([&](LockOp lock) {
716 TileOp tile = lock.getTileOp();
717 int col = tile.colIndex();
718 int row = tile.rowIndex();
719 int lockID = lock.getLockIDValue();
720 auto init = lock.getInit();
722 output <<
"__mlir_aie_try(XAie_LockSetValue(" << deviceInstRef <<
", "
724 <<
"XAie_LockInit(" << lockID <<
", " << *init <<
")));\n";
726 output <<
"return XAIE_OK;\n";
727 output <<
"} // mlir_aie_initialize_locks\n";
737 output <<
"int mlir_aie_initialize_buffers(" << ctx_p <<
") {\n";
738 auto initBuffersResult = targetOp.walk<WalkOrder::PreOrder>([&](BufferOp
740 auto initialValue = buffer.getInitialValue();
742 return WalkResult::advance();
743 auto denseInit = llvm::dyn_cast<DenseElementsAttr>(*initialValue);
745 return WalkResult::advance();
748 return WalkResult(buffer.emitOpError(
749 "buffer op type not supported for initialization"));
750 if (!buffer.getAddress())
751 return WalkResult(buffer.emitOpError(
752 "buffer has no address; run --aie-assign-buffer-addresses first"));
753 TileOp tile = buffer.getTileOp();
756 std::string bufName(buffer.name().getValue());
758 output <<
"static const unsigned char " << bufName <<
"_init[] = {";
759 for (
size_t i = 0; i < bytes->size(); ++i)
760 output << (i ?
"," :
"") << (unsigned)(
unsigned char)(*bytes)[i];
762 output <<
"__mlir_aie_try(XAie_DataMemBlockWrite(" << deviceInstRef <<
", "
763 <<
tileLocStr(tile.colIndex(), tile.rowIndex()) <<
", "
764 <<
"0x" << llvm::utohexstr(*buffer.getAddress()) <<
", "
765 <<
"(void *)" << bufName <<
"_init, " << bytes->size() <<
"));\n";
767 return WalkResult::advance();
769 if (initBuffersResult.wasInterrupted())
771 output <<
"return XAIE_OK;\n";
772 output <<
"} // mlir_aie_initialize_buffers\n";
777 output <<
"int mlir_aie_configure_switchboxes(" << ctx_p <<
") {\n";
778 output <<
" int x, y;\n";
781 for (
auto switchboxOp : targetOp.getOps<SwitchboxOp>()) {
782 Region &r = switchboxOp.getConnections();
783 Block &b = r.front();
784 bool isEmpty = b.getOps<ConnectOp>().empty() &&
785 b.getOps<MasterSetOp>().empty() &&
786 b.getOps<PacketRulesOp>().empty();
787 bool isParam =
false;
789 if (isa<TileOp>(switchboxOp.getTile().getDefiningOp())) {
790 int col = switchboxOp.colIndex();
791 int row = switchboxOp.rowIndex();
793 output <<
"// Core Stream Switch column " << col <<
" row " << row
795 output <<
"x = " << col <<
";\n";
796 output <<
"y = " << row <<
";\n";
800 for (
auto connectOp : b.getOps<ConnectOp>())
801 output <<
"__mlir_aie_try(XAie_StrmConnCctEnable(" << deviceInstRef
803 << wireBundleToPortType(connectOp.getSourceBundle()) <<
", "
804 << connectOp.sourceIndex() <<
", "
805 << wireBundleToPortType(connectOp.getDestBundle()) <<
", "
806 << connectOp.destIndex() <<
"));\n";
808 for (
auto connectOp : b.getOps<MasterSetOp>()) {
813 for (
auto val : connectOp.getAmsels()) {
814 AMSelOp amsel = cast<AMSelOp>(val.getDefiningOp());
815 arbiter = amsel.arbiterIndex();
816 int msel = amsel.getMselValue();
819 bool isdma = (connectOp.getDestBundle() == WireBundle::DMA);
821 output <<
"__mlir_aie_try(XAie_StrmPktSwMstrPortEnable(" << deviceInstRef
823 << wireBundleToPortType(connectOp.getDestBundle()) <<
", "
824 << connectOp.destIndex() <<
", "
825 <<
"/* drop_header */ "
826 << (isdma ?
"XAIE_SS_PKT_DROP_HEADER"
827 :
"XAIE_SS_PKT_DONOT_DROP_HEADER")
829 <<
"/* arbiter */ " << arbiter <<
", "
831 <<
"0x" << llvm::utohexstr(mask) <<
"));\n";
834 for (
auto connectOp : b.getOps<PacketRulesOp>()) {
836 Block &block = connectOp.getRules().front();
837 for (
auto slotOp : block.getOps<PacketRuleOp>()) {
838 AMSelOp amselOp = cast<AMSelOp>(slotOp.getAmsel().getDefiningOp());
839 int arbiter = amselOp.arbiterIndex();
840 int msel = amselOp.getMselValue();
841 output <<
"__mlir_aie_try(XAie_StrmPktSwSlavePortEnable("
842 << deviceInstRef <<
", " <<
tileLocStr(
"x",
"y") <<
", "
843 << wireBundleToPortType(connectOp.getSourceBundle()) <<
", "
844 << connectOp.sourceIndex() <<
"));\n";
847 output <<
"__mlir_aie_try(XAie_StrmPktSwSlaveSlotEnable("
848 << deviceInstRef <<
", " <<
tileLocStr(
"x",
"y") <<
", "
849 << wireBundleToPortType(connectOp.getSourceBundle()) <<
", "
850 << connectOp.sourceIndex() <<
", "
851 <<
"/* slot */ " << slot <<
", "
852 <<
"/* packet */ " <<
packetStr(slotOp.valueInt(), 0)
855 <<
"0x" << llvm::utohexstr(slotOp.maskInt()) <<
", "
856 <<
"/* msel */ " << msel <<
", "
857 <<
"/* arbiter */ " << arbiter <<
"));\n";
867 for (
auto op : targetOp.getOps<ShimMuxOp>()) {
868 Region &r = op.getConnections();
869 Block &b = r.front();
870 bool isEmpty = b.getOps<ConnectOp>().empty();
872 if (isa<TileOp>(op.getTile().getDefiningOp())) {
873 int col = op.colIndex();
874 int row = op.rowIndex();
876 output <<
"// ShimMux column " << col <<
" row " << row <<
"\n";
877 output <<
"// NOTE ShimMux always connects from the south as "
878 <<
"directions are defined relative to the tile stream "
880 output <<
"x = " << col <<
";\n";
881 output <<
"y = " << row <<
";\n";
885 for (
auto connectOp : b.getOps<ConnectOp>()) {
887 if (connectOp.getSourceBundle() == WireBundle::DMA ||
888 connectOp.getDestBundle() == WireBundle::DMA) {
889 if (connectOp.getSourceBundle() == WireBundle::North)
891 output <<
"__mlir_aie_try(XAie_EnableAieToShimDmaStrmPort("
892 << deviceInstRef <<
", " <<
tileLocStr(
"x",
"y") <<
", "
893 << connectOp.sourceIndex() <<
"));\n";
894 else if (connectOp.getDestBundle() == WireBundle::North)
896 output <<
"__mlir_aie_try(XAie_EnableShimDmaToAieStrmPort("
897 << deviceInstRef <<
", " <<
tileLocStr(
"x",
"y") <<
", "
898 << connectOp.destIndex() <<
"));\n";
901 else if (connectOp.getSourceBundle() == WireBundle::PLIO ||
902 connectOp.getDestBundle() == WireBundle::PLIO) {
903 if (connectOp.getSourceBundle() == WireBundle::North) {
905 output <<
"__mlir_aie_try(XAie_AieToPlIntfEnable(" << deviceInstRef
907 << connectOp.destIndex() <<
", PLIF_WIDTH_64));\n";
908 }
else if (connectOp.getDestBundle() == WireBundle::North) {
910 output <<
"__mlir_aie_try(XAie_PlToAieIntfEnable(" << deviceInstRef
912 << connectOp.destIndex() <<
", PLIF_WIDTH_64));\n";
917 output <<
"return XAIE_OK;\n";
918 output <<
"} // mlir_aie_configure_switchboxes\n\n";
923 output <<
"int mlir_aie_configure_cascade(" << ctx_p <<
") {\n";
924 for (
auto configOp : targetOp.getOps<ConfigureCascadeOp>()) {
925 TileOp tile = cast<TileOp>(configOp.getTile().getDefiningOp());
926 int col = tile.colIndex();
927 int row = tile.rowIndex();
928 output <<
"XAie_CoreConfigAccumulatorControl(" << deviceInstRef <<
", "
929 <<
"XAie_TileLoc(" << col <<
", " << row <<
"), "
930 << stringifyCascadeDir(configOp.getInputDir()).upper() <<
", "
931 << stringifyCascadeDir(configOp.getOutputDir()).upper() <<
");\n";
933 output <<
"return XAIE_OK;\n";
934 output <<
"} // mlir_aie_configure_cascade\n\n";
939 for (
auto tile : tiles) {
940 Operation *tileOp = tile.second;
941 TileID coord = cast<TileOp>(tileOp).getTileID();
946 auto bufferAccessor = [&](BufferOp buf) {
949 std::string bufName(buf.name().getValue());
950 Type t = buf.getType();
953 if (
auto memrefType = llvm::dyn_cast<MemRefType>(t)) {
954 et = memrefType.getElementType();
955 if (et.isInteger(32))
960 output <<
"// buffer " << bufName <<
" with unsupported type " << t
966 output <<
"// buffer " << bufName <<
" with unsupported type " << t
970 assert(buf.getAddress().has_value() &&
"buffer must have address");
971 output <<
"const int " << bufName
972 <<
"_offset = " << buf.getAddress().value() <<
";\n";
973 output << typestr <<
" mlir_aie_read_buffer_" << bufName <<
"(" << ctx_p
974 <<
", int index) {\n";
975 output <<
"u32 value; auto rc = XAie_DataMemRdWord(" << deviceInstRef
976 <<
", " << loc <<
", " << bufName
977 <<
"_offset + (index*4), &value);\n";
978 if (et.isInteger(32))
979 output <<
" return value;\n";
980 else if (et.isF32()) {
981 output <<
" union caster { int32_t i; float f; };\n";
982 output <<
" caster c; c.i = value;\n";
983 output <<
" return c.f;\n";
986 output <<
"int mlir_aie_write_buffer_" << bufName <<
"(" << ctx_p
987 <<
", int index, " << typestr <<
" value) {\n";
988 if (et.isInteger(32))
989 output <<
" int32_t int_value = value;\n";
990 else if (et.isF32()) {
991 output <<
" union caster { int32_t i; float f; };\n";
992 output <<
" caster c; c.f = value;\n";
993 output <<
" int32_t int_value = c.i;\n";
995 output <<
"AieRC rc = XAie_DataMemWrWord(" << deviceInstRef <<
", "
996 << loc <<
", " << bufName <<
"_offset + (index*4), int_value);\n";
997 output <<
"return rc;\n";
1002 for (
auto buf : buffers[tileOp])
1003 bufferAccessor(buf);
1006 auto lockAccessor = [&](LockOp lock) {
1007 int col = lock.colIndex();
1008 int row = lock.rowIndex();
1009 if (!lock.hasName())
1011 std::string lockName(lock.name().getValue());
1012 output <<
"int mlir_aie_acquire_" << lockName <<
"(" << ctx_p
1013 <<
", int value, int timeout) {\n";
1014 output <<
" const int id = " << lock.getLockIDValue() <<
";\n";
1015 output <<
" return XAie_LockAcquire(" << deviceInstRef <<
", "
1016 <<
tileLocStr(col, row) <<
", " << tileLockStr(
"id",
"value")
1019 output <<
"int mlir_aie_release_" << lockName <<
"(" << ctx_p
1020 <<
", int value, int timeout) {\n";
1021 output <<
" const int id = " << lock.getLockIDValue() <<
";\n";
1022 output <<
" return XAie_LockRelease(" << deviceInstRef <<
", "
1023 <<
tileLocStr(col, row) <<
", " << tileLockStr(
"id",
"value")
1028 targetOp.walk<WalkOrder::PreOrder>([&](LockOp lock) { lockAccessor(lock); });
virtual std::optional< uint32_t > getMemLocalBaseAddress(int localCol, int localRow, int memCol, int memRow) const
Return the memory base address (or offset) in the local tile when accessing a neighbor's memory or an...
virtual AIEArch getTargetArch() const =0
Return the target architecture.
bool isMemTile(int col, int row) const
Return true if the given tile is a Mem tile.
virtual std::optional< uint32_t > getLockLocalBaseIndex(int localCol, int localRow, int lockCol, int lockRow) const
Return the lock base index (or offset) in the local tile when accessing a neighbor's lock or an empty...
virtual uint32_t getNumLocks(AIETileType tileType) const =0
Return the number of lock objects for a given tile type.
virtual int rows() const =0
Return the number of rows in the device.
bool isShimNOCTile(int col, int row) const
Return true if the given tile is a ShimNOC tile.
@ UsesMultiDimensionalBDs
virtual uint32_t getColumnShift() const =0
bool hasProperty(ModelProperty Prop) const
virtual int columns() const =0
Return the number of columns in the device.
virtual uint32_t getNumMemTileRows() const =0
virtual uint32_t getRowShift() const =0
void collectTiles(DeviceOp &device, llvm::DenseMap< TileID, mlir::Operation * > &tiles)
void collectBuffers(DeviceOp &device, llvm::DenseMap< mlir::Operation *, llvm::SmallVector< BufferOp, 4 > > &buffers)
std::string tileDMAInstStr(llvm::StringRef col, llvm::StringRef row, llvm::StringRef bdNum)
void generateXAieDmaSetMultiDimAddr(llvm::raw_ostream &output, int ndims, llvm::ArrayRef< BDDimLayoutAttr > dims, int col, int row, int bdNum, int baseAddrA, int64_t offsetA, uint32_t lenA, int elementWidthInBytes, const char *errorRet)
struct TileID { friend std::ostream &operator<<(std::ostream &os, const TileID &s) { os<< "TileID("<< s.col<< ", "<< s.row<< ")"; return os; } friend std::string to_string(const TileID &s) { std::ostringstream ss; ss<< s; return ss.str(); } friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const TileID &s) { os<< to_string(s); return os; } inline bool operator<(const TileID &rhs) const { return std::tie(col, row)< std::tie(rhs.col, rhs.row); } bool operator==(const TileID &rhs) const { return std::tie(col, row)==std::tie(rhs.col, rhs.row); } bool operator!=(const TileID &rhs) const { return !(*this==rhs);} int col, row;} TileID
const AIETargetModel & getTargetModel(mlir::Operation *op)
mlir::LogicalResult verifyDMAChannelsResolved(DeviceOp device)
std::optional< std::vector< char > > denseAttrToBytes(mlir::DenseElementsAttr denseInit)
A buffer's initial_value as the bytes to write to its tile memory, in element order.
std::string tileDMAInstRefStr(llvm::StringRef col, llvm::StringRef row, llvm::StringRef bdNum)
llvm::SmallPtrSet< mlir::Block *, 8 > collectUnchainedBlocks(const llvm::SetVector< mlir::Block * > &blockVector)
Collect BD blocks that require use_next_bd=0 during configuration.
llvm::SetVector< mlir::Block * > getOrderedChainOfBlocks(mlir::Region *region)
mlir::LogicalResult AIETranslateToXAIEV2(mlir::ModuleOp module, llvm::raw_ostream &output, llvm::StringRef deviceName="")
std::string tileLocStr(llvm::StringRef col, llvm::StringRef row)
std::string packetStr(llvm::StringRef id, llvm::StringRef type)