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 output <<
" ctx->XAieConfig->AieGen = " << device <<
";\n";
474 output <<
" ctx->XAieConfig->BaseAddr = 0x20000000000;\n";
475 output <<
" ctx->XAieConfig->ColShift = " << targetModel.
getColumnShift()
477 output <<
" ctx->XAieConfig->RowShift = " << targetModel.
getRowShift()
479 output <<
" ctx->XAieConfig->NumRows = " << targetModel.
rows() <<
";\n";
480 output <<
" ctx->XAieConfig->NumCols = " << targetModel.
columns() <<
";\n";
481 output <<
" ctx->XAieConfig->ShimRowNum = 0;\n";
482 output <<
" ctx->XAieConfig->MemTileRowStart = 1;\n";
483 output <<
" ctx->XAieConfig->MemTileNumRows = "
485 output <<
" // ctx->XAieConfig->ReservedRowStart = "
486 "XAIE_RES_TILE_ROW_START;\n";
488 <<
" // ctx->XAieConfig->ReservedNumRows = XAIE_RES_TILE_NUM_ROWS;\n";
489 output <<
" ctx->XAieConfig->AieTileRowStart = "
491 output <<
" ctx->XAieConfig->AieTileNumRows = "
493 output <<
" memset(ctx->XAieDevInst, 0, sizeof(XAie_DevInst));\n";
494 output <<
" return ctx;\n";
503 output <<
"int mlir_aie_initialize_buffers(" << ctx_p <<
");\n";
504 output <<
"int mlir_aie_configure_cores(" << ctx_p <<
") {\n";
506 for (
auto tileOp : targetOp.getOps<TileOp>()) {
507 int col = tileOp.colIndex();
508 int row = tileOp.rowIndex();
509 if (tileOp.isShimTile() || tileOp.isMemTile()) {
513 output <<
"__mlir_aie_try(XAie_CoreReset(" << deviceInstRef <<
", "
515 output <<
"__mlir_aie_try(XAie_CoreDisable(" << deviceInstRef <<
", "
519 output <<
"for (int l = 0; l < " << numLocks <<
"; ++l)\n"
520 <<
" __mlir_aie_try(XAie_LockRelease(" << deviceInstRef <<
", "
521 <<
tileLocStr(col, row) <<
", XAie_LockInit(l, 0x0), 0));\n";
522 if (
auto coreOp = tileOp.getCoreOp()) {
523 if (coreOp.isEmpty() && coreOp.getElfFile() == std::nullopt) {
528 std::string fileName;
529 if (
auto fileAttr = coreOp.getElfFile()) {
530 fileName = fileAttr.value().str();
532 return coreOp.emitOpError()
533 <<
"Expected lowered ELF file to be given as attribute "
534 "`elf_file` for this core. Compile cores first.";
537 <<
"AieRC RC = XAie_LoadElf(" << deviceInstRef <<
", "
539 <<
"(const char*)\"" << fileName <<
"\",0);\n";
540 output <<
"if (RC != XAIE_OK)\n"
541 <<
" __mlir_aie_verbose(fprintf(stderr, \"Failed to load elf "
542 "for Core[%d,%d], ret is %d\\n\", "
543 << std::to_string(col) <<
", " << std::to_string(row)
545 <<
"assert(RC == XAIE_OK);\n"
553 output <<
" int rc = mlir_aie_initialize_buffers(ctx);\n";
554 output <<
" if (rc != XAIE_OK)\n";
555 output <<
" return rc;\n";
557 output <<
"return XAIE_OK;\n";
558 output <<
"} // mlir_aie_configure_cores\n\n";
563 output <<
"int mlir_aie_start_cores(" << ctx_p <<
") {\n";
565 for (
auto tileOp : targetOp.getOps<TileOp>()) {
566 int col = tileOp.colIndex();
567 int row = tileOp.rowIndex();
568 if (!tileOp.isShimTile() && !tileOp.isMemTile()) {
569 output <<
"__mlir_aie_try(XAie_CoreUnreset(" << deviceInstRef <<
", "
571 output <<
"__mlir_aie_try(XAie_CoreEnable(" << deviceInstRef <<
", "
575 output <<
"return XAIE_OK;\n";
576 output <<
"} // mlir_aie_start_cores\n\n";
581 output <<
"int mlir_aie_configure_dmas(" << ctx_p <<
") {\n";
583 for (
auto memOp : targetOp.getOps<MemOp>()) {
584 DenseMap<Block *, int> blockMap;
588 for (
auto &block : memOp.getBody()) {
589 auto bdOps = block.getOps<DMABDOp>();
592 std::optional<int32_t>
id = (*bdOps.begin()).getBdId();
593 blockMap[&block] =
id.has_value() ? *id : bdNum;
596 auto result = generateDMAConfig(memOp, output, targetModel, blockMap);
600 for (
auto memOp : targetOp.getOps<MemTileDMAOp>()) {
601 DenseMap<Block *, int> blockMap;
603 DenseMap<Block *, int> channelMap;
605 for (
auto &block : memOp.getBody()) {
606 for (
auto op : block.getOps<DMAStartOp>()) {
607 int chNum = op.getChannelIndex();
608 channelMap[&block] = chNum;
609 auto *dest = op.getDest();
611 channelMap[dest] = chNum;
612 if (dest->getSuccessors().empty())
614 dest = dest->getSuccessors()[0];
615 if (channelMap.count(dest))
624 for (
auto &block : memOp.getBody()) {
625 auto bdOps = block.getOps<DMABDOp>();
628 assert(channelMap.count(&block));
629 if (std::optional<int32_t>
id = (*bdOps.begin()).getBdId())
630 blockMap[&block] = *id;
631 else if (channelMap[&block] & 1)
632 blockMap[&block] = oddBdNum++;
634 blockMap[&block] = evenBdNum++;
636 auto result = generateDMAConfig(memOp, output, targetModel, blockMap);
641 output <<
"return XAIE_OK;\n";
642 output <<
"} // mlir_aie_configure_dmas\n\n";
644 for (
auto op : targetOp.getOps<ExternalBufferOp>()) {
646 output <<
"static u64 _mlir_aie_external_" << op.name().getValue()
648 output <<
"static bool _mlir_aie_external_set_" << op.name().getValue()
651 output <<
"void mlir_aie_external_set_addr_" << op.name().getValue()
652 <<
"(" << ctx_p <<
", u64 VA) {\n"
653 <<
" u64 device_address = mlir_aie_get_device_address(ctx, (void "
655 <<
" _mlir_aie_external_set_" << op.name().getValue()
657 <<
" _mlir_aie_external_" << op.name().getValue()
658 <<
" = device_address;\n"
665 for (
auto op : targetOp.getOps<ShimDMAOp>()) {
666 int col = op.colIndex();
667 int row = op.rowIndex();
669 DenseMap<Block *, int> blockMap;
673 for (
auto &block : op.getBody()) {
674 auto bdOps = block.getOps<DMABDOp>();
677 std::optional<int32_t>
id = (*bdOps.begin()).getBdId();
678 int slot =
id.has_value() ? *id : bdNum;
679 blockMap[&block] = slot;
681 for (
auto op : block.getOps<DMABDOp>()) {
682 offset = op.getOffsetInBytes();
683 auto buffer = cast<ExternalBufferOp>(op.getBuffer().getDefiningOp());
685 output <<
"u64 mlir_aie_external_get_addr_myBuffer_" << col << row
686 <<
"_" << slot <<
"(void) {\n"
687 <<
" assert(_mlir_aie_external_set_"
688 << buffer.name().getValue() <<
");\n"
689 <<
" return _mlir_aie_external_" << buffer.name().getValue()
690 <<
" + " << llvm::utohexstr(offset) <<
";\n"
698 output <<
"int mlir_aie_configure_shimdma_" << col << row <<
"(" << ctx_p
700 auto result = generateDMAConfig(op, output, targetModel, blockMap);
703 output <<
"return XAIE_OK;\n";
704 output <<
"} // mlir_aie_configure_shimdma\n\n";
710 output <<
"int mlir_aie_initialize_locks(" << ctx_p <<
") {\n";
712 targetOp.walk<WalkOrder::PreOrder>([&](LockOp lock) {
713 TileOp tile = lock.getTileOp();
714 int col = tile.colIndex();
715 int row = tile.rowIndex();
716 int lockID = lock.getLockIDValue();
717 auto init = lock.getInit();
719 output <<
"__mlir_aie_try(XAie_LockSetValue(" << deviceInstRef <<
", "
721 <<
"XAie_LockInit(" << lockID <<
", " << *init <<
")));\n";
723 output <<
"return XAIE_OK;\n";
724 output <<
"} // mlir_aie_initialize_locks\n";
734 output <<
"int mlir_aie_initialize_buffers(" << ctx_p <<
") {\n";
735 auto initBuffersResult = targetOp.walk<WalkOrder::PreOrder>([&](BufferOp
737 auto initialValue = buffer.getInitialValue();
739 return WalkResult::advance();
740 auto denseInit = llvm::dyn_cast<DenseElementsAttr>(*initialValue);
742 return WalkResult::advance();
745 return WalkResult(buffer.emitOpError(
746 "buffer op type not supported for initialization"));
747 if (!buffer.getAddress())
748 return WalkResult(buffer.emitOpError(
749 "buffer has no address; run --aie-assign-buffer-addresses first"));
750 TileOp tile = buffer.getTileOp();
753 std::string bufName(buffer.name().getValue());
755 output <<
"static const unsigned char " << bufName <<
"_init[] = {";
756 for (
size_t i = 0; i < bytes->size(); ++i)
757 output << (i ?
"," :
"") << (unsigned)(
unsigned char)(*bytes)[i];
759 output <<
"__mlir_aie_try(XAie_DataMemBlockWrite(" << deviceInstRef <<
", "
760 <<
tileLocStr(tile.colIndex(), tile.rowIndex()) <<
", "
761 <<
"0x" << llvm::utohexstr(*buffer.getAddress()) <<
", "
762 <<
"(void *)" << bufName <<
"_init, " << bytes->size() <<
"));\n";
764 return WalkResult::advance();
766 if (initBuffersResult.wasInterrupted())
768 output <<
"return XAIE_OK;\n";
769 output <<
"} // mlir_aie_initialize_buffers\n";
774 output <<
"int mlir_aie_configure_switchboxes(" << ctx_p <<
") {\n";
775 output <<
" int x, y;\n";
778 for (
auto switchboxOp : targetOp.getOps<SwitchboxOp>()) {
779 Region &r = switchboxOp.getConnections();
780 Block &b = r.front();
781 bool isEmpty = b.getOps<ConnectOp>().empty() &&
782 b.getOps<MasterSetOp>().empty() &&
783 b.getOps<PacketRulesOp>().empty();
784 bool isParam =
false;
786 if (isa<TileOp>(switchboxOp.getTile().getDefiningOp())) {
787 int col = switchboxOp.colIndex();
788 int row = switchboxOp.rowIndex();
790 output <<
"// Core Stream Switch column " << col <<
" row " << row
792 output <<
"x = " << col <<
";\n";
793 output <<
"y = " << row <<
";\n";
797 for (
auto connectOp : b.getOps<ConnectOp>())
798 output <<
"__mlir_aie_try(XAie_StrmConnCctEnable(" << deviceInstRef
800 << wireBundleToPortType(connectOp.getSourceBundle()) <<
", "
801 << connectOp.sourceIndex() <<
", "
802 << wireBundleToPortType(connectOp.getDestBundle()) <<
", "
803 << connectOp.destIndex() <<
"));\n";
805 for (
auto connectOp : b.getOps<MasterSetOp>()) {
810 for (
auto val : connectOp.getAmsels()) {
811 AMSelOp amsel = cast<AMSelOp>(val.getDefiningOp());
812 arbiter = amsel.arbiterIndex();
813 int msel = amsel.getMselValue();
816 bool isdma = (connectOp.getDestBundle() == WireBundle::DMA);
818 output <<
"__mlir_aie_try(XAie_StrmPktSwMstrPortEnable(" << deviceInstRef
820 << wireBundleToPortType(connectOp.getDestBundle()) <<
", "
821 << connectOp.destIndex() <<
", "
822 <<
"/* drop_header */ "
823 << (isdma ?
"XAIE_SS_PKT_DROP_HEADER"
824 :
"XAIE_SS_PKT_DONOT_DROP_HEADER")
826 <<
"/* arbiter */ " << arbiter <<
", "
828 <<
"0x" << llvm::utohexstr(mask) <<
"));\n";
831 for (
auto connectOp : b.getOps<PacketRulesOp>()) {
833 Block &block = connectOp.getRules().front();
834 for (
auto slotOp : block.getOps<PacketRuleOp>()) {
835 AMSelOp amselOp = cast<AMSelOp>(slotOp.getAmsel().getDefiningOp());
836 int arbiter = amselOp.arbiterIndex();
837 int msel = amselOp.getMselValue();
838 output <<
"__mlir_aie_try(XAie_StrmPktSwSlavePortEnable("
839 << deviceInstRef <<
", " <<
tileLocStr(
"x",
"y") <<
", "
840 << wireBundleToPortType(connectOp.getSourceBundle()) <<
", "
841 << connectOp.sourceIndex() <<
"));\n";
844 output <<
"__mlir_aie_try(XAie_StrmPktSwSlaveSlotEnable("
845 << deviceInstRef <<
", " <<
tileLocStr(
"x",
"y") <<
", "
846 << wireBundleToPortType(connectOp.getSourceBundle()) <<
", "
847 << connectOp.sourceIndex() <<
", "
848 <<
"/* slot */ " << slot <<
", "
849 <<
"/* packet */ " <<
packetStr(slotOp.valueInt(), 0)
852 <<
"0x" << llvm::utohexstr(slotOp.maskInt()) <<
", "
853 <<
"/* msel */ " << msel <<
", "
854 <<
"/* arbiter */ " << arbiter <<
"));\n";
864 for (
auto op : targetOp.getOps<ShimMuxOp>()) {
865 Region &r = op.getConnections();
866 Block &b = r.front();
867 bool isEmpty = b.getOps<ConnectOp>().empty();
869 if (isa<TileOp>(op.getTile().getDefiningOp())) {
870 int col = op.colIndex();
871 int row = op.rowIndex();
873 output <<
"// ShimMux column " << col <<
" row " << row <<
"\n";
874 output <<
"// NOTE ShimMux always connects from the south as "
875 <<
"directions are defined relative to the tile stream "
877 output <<
"x = " << col <<
";\n";
878 output <<
"y = " << row <<
";\n";
882 for (
auto connectOp : b.getOps<ConnectOp>()) {
884 if (connectOp.getSourceBundle() == WireBundle::DMA ||
885 connectOp.getDestBundle() == WireBundle::DMA) {
886 if (connectOp.getSourceBundle() == WireBundle::North)
888 output <<
"__mlir_aie_try(XAie_EnableAieToShimDmaStrmPort("
889 << deviceInstRef <<
", " <<
tileLocStr(
"x",
"y") <<
", "
890 << connectOp.sourceIndex() <<
"));\n";
891 else if (connectOp.getDestBundle() == WireBundle::North)
893 output <<
"__mlir_aie_try(XAie_EnableShimDmaToAieStrmPort("
894 << deviceInstRef <<
", " <<
tileLocStr(
"x",
"y") <<
", "
895 << connectOp.destIndex() <<
"));\n";
898 else if (connectOp.getSourceBundle() == WireBundle::PLIO ||
899 connectOp.getDestBundle() == WireBundle::PLIO) {
900 if (connectOp.getSourceBundle() == WireBundle::North) {
902 output <<
"__mlir_aie_try(XAie_AieToPlIntfEnable(" << deviceInstRef
904 << connectOp.destIndex() <<
", PLIF_WIDTH_64));\n";
905 }
else if (connectOp.getDestBundle() == WireBundle::North) {
907 output <<
"__mlir_aie_try(XAie_PlToAieIntfEnable(" << deviceInstRef
909 << connectOp.destIndex() <<
", PLIF_WIDTH_64));\n";
914 output <<
"return XAIE_OK;\n";
915 output <<
"} // mlir_aie_configure_switchboxes\n\n";
920 output <<
"int mlir_aie_configure_cascade(" << ctx_p <<
") {\n";
921 for (
auto configOp : targetOp.getOps<ConfigureCascadeOp>()) {
922 TileOp tile = cast<TileOp>(configOp.getTile().getDefiningOp());
923 int col = tile.colIndex();
924 int row = tile.rowIndex();
925 output <<
"XAie_CoreConfigAccumulatorControl(" << deviceInstRef <<
", "
926 <<
"XAie_TileLoc(" << col <<
", " << row <<
"), "
927 << stringifyCascadeDir(configOp.getInputDir()).upper() <<
", "
928 << stringifyCascadeDir(configOp.getOutputDir()).upper() <<
");\n";
930 output <<
"return XAIE_OK;\n";
931 output <<
"} // mlir_aie_configure_cascade\n\n";
936 for (
auto tile : tiles) {
937 Operation *tileOp = tile.second;
938 TileID coord = cast<TileOp>(tileOp).getTileID();
943 auto bufferAccessor = [&](BufferOp buf) {
946 std::string bufName(buf.name().getValue());
947 Type t = buf.getType();
950 if (
auto memrefType = llvm::dyn_cast<MemRefType>(t)) {
951 et = memrefType.getElementType();
952 if (et.isInteger(32))
957 output <<
"// buffer " << bufName <<
" with unsupported type " << t
963 output <<
"// buffer " << bufName <<
" with unsupported type " << t
967 assert(buf.getAddress().has_value() &&
"buffer must have address");
968 output <<
"const int " << bufName
969 <<
"_offset = " << buf.getAddress().value() <<
";\n";
970 output << typestr <<
" mlir_aie_read_buffer_" << bufName <<
"(" << ctx_p
971 <<
", int index) {\n";
972 output <<
"u32 value; auto rc = XAie_DataMemRdWord(" << deviceInstRef
973 <<
", " << loc <<
", " << bufName
974 <<
"_offset + (index*4), &value);\n";
975 if (et.isInteger(32))
976 output <<
" return value;\n";
977 else if (et.isF32()) {
978 output <<
" union caster { int32_t i; float f; };\n";
979 output <<
" caster c; c.i = value;\n";
980 output <<
" return c.f;\n";
983 output <<
"int mlir_aie_write_buffer_" << bufName <<
"(" << ctx_p
984 <<
", int index, " << typestr <<
" value) {\n";
985 if (et.isInteger(32))
986 output <<
" int32_t int_value = value;\n";
987 else if (et.isF32()) {
988 output <<
" union caster { int32_t i; float f; };\n";
989 output <<
" caster c; c.f = value;\n";
990 output <<
" int32_t int_value = c.i;\n";
992 output <<
"AieRC rc = XAie_DataMemWrWord(" << deviceInstRef <<
", "
993 << loc <<
", " << bufName <<
"_offset + (index*4), int_value);\n";
994 output <<
"return rc;\n";
999 for (
auto buf : buffers[tileOp])
1000 bufferAccessor(buf);
1003 auto lockAccessor = [&](LockOp lock) {
1004 int col = lock.colIndex();
1005 int row = lock.rowIndex();
1006 if (!lock.hasName())
1008 std::string lockName(lock.name().getValue());
1009 output <<
"int mlir_aie_acquire_" << lockName <<
"(" << ctx_p
1010 <<
", int value, int timeout) {\n";
1011 output <<
" const int id = " << lock.getLockIDValue() <<
";\n";
1012 output <<
" return XAie_LockAcquire(" << deviceInstRef <<
", "
1013 <<
tileLocStr(col, row) <<
", " << tileLockStr(
"id",
"value")
1016 output <<
"int mlir_aie_release_" << lockName <<
"(" << ctx_p
1017 <<
", int value, int timeout) {\n";
1018 output <<
" const int id = " << lock.getLockIDValue() <<
";\n";
1019 output <<
" return XAie_LockRelease(" << deviceInstRef <<
", "
1020 <<
tileLocStr(col, row) <<
", " << tileLockStr(
"id",
"value")
1025 targetOp.walk<WalkOrder::PreOrder>([&](LockOp lock) { lockAccessor(lock); });
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.
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)