25#include "mlir/Conversion/ArithToEmitC/ArithToEmitC.h"
26#include "mlir/Conversion/SCFToEmitC/SCFToEmitC.h"
27#include "mlir/Dialect/Arith/IR/Arith.h"
28#include "mlir/Dialect/Arith/Transforms/Passes.h"
29#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
30#include "mlir/Dialect/EmitC/IR/EmitC.h"
31#include "mlir/Dialect/EmitC/Transforms/TypeConversions.h"
32#include "mlir/Dialect/MemRef/IR/MemRef.h"
33#include "mlir/Dialect/SCF/IR/SCF.h"
34#include "mlir/IR/Builders.h"
35#include "mlir/IR/BuiltinOps.h"
36#include "mlir/Interfaces/SideEffectInterfaces.h"
37#include "mlir/Pass/Pass.h"
38#include "mlir/Transforms/DialectConversion.h"
39#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
40#include "mlir/Transforms/RegionUtils.h"
42#include "llvm/ADT/StringSet.h"
43#include "llvm/ADT/TypeSwitch.h"
44#include "llvm/Support/Format.h"
49#define GEN_PASS_DECL_CONVERTAIEXTOEMITC
50#define GEN_PASS_DEF_CONVERTAIEXTOEMITC
51#include "aie/Conversion/Passes.h.inc"
61static std::string refuse(StringRef why,
bool inFormat) {
64 if (ch ==
'\n' || ch ==
'\r')
66 else if (ch ==
'"' || ch ==
'\\')
68 else if (ch ==
'{' && inFormat)
73 return "return aie_runtime::txn_refused(\"" + lit +
"\");";
81constexpr llvm::StringLiteral kTxnVecType =
"std::vector<uint32_t>";
83emitc::OpaqueType getU32Type(MLIRContext *ctx) {
84 return emitc::OpaqueType::get(ctx,
"uint32_t");
90template <
typename PoolOp>
91std::string bdPoolName(PoolOp op) {
92 return "bd_pool_" + std::to_string(op.getColumn()) +
"_" +
93 std::to_string(op.getRow()) +
"_" + std::to_string(op.getBdBegin()) +
94 "_" + std::to_string(op.getBdEnd());
99Value u32Literal(OpBuilder &b, Location loc, uint32_t val) {
100 return emitc::ConstantOp::create(
101 b, loc, getU32Type(b.getContext()),
102 emitc::OpaqueAttr::get(b.getContext(), std::to_string(val) +
"u"));
108void emitTxnCall(OpBuilder &b, Location loc, StringRef fn, Value txnVec,
110 SmallVector<Value> callArgs;
111 callArgs.push_back(txnVec);
112 callArgs.append(
args.begin(),
args.end());
113 emitc::CallOpaqueOp::create(b, loc, TypeRange{}, (
"aie_runtime::" + fn).str(),
119struct DeviceResolved {
120 llvm::DenseMap<Operation *, uint32_t> absoluteAddr;
121 llvm::DenseMap<Operation *, DenseIntElementsAttr> blockWriteData;
124class AIEXToEmitCConverter {
126 AIEXToEmitCConverter(emitc::FuncOp funcOp, Value txnVec,
127 const DeviceResolved &resolved, Value opCountVar,
128 bool foldDDRAddrOffset)
129 : funcOp(funcOp), txnVec(txnVec), resolved(resolved),
130 opCountVar(opCountVar), foldDDRAddrOffset(foldDDRAddrOffset) {}
136 std::optional<uint32_t> run() {
143 funcOp.walk([&](Operation *op) {
144 if (isa<memref::MemRefDialect>(op->getDialect()) &&
145 isOpTriviallyDead(op)) {
152 SmallVector<Operation *> consumed;
153 convertBlockRecursive(funcOp.getBlocks().front(), count, consumed);
162 for (Operation *op : llvm::reverse(consumed))
164 IRRewriter rewriter(funcOp.getContext());
165 (void)runRegionDCE(rewriter, funcOp.getFunctionBody());
173 void convertBlockRecursive(Block &block, uint32_t &count,
174 SmallVector<Operation *> &consumed) {
175 SmallVector<Operation *> work;
176 for (Operation &op : block)
178 for (Operation *op : work) {
185 if (isa<emitc::EmitCDialect, arith::ArithDialect>(op->getDialect()))
187 if (isa<scf::SCFDialect>(op->getDialect())) {
188 for (Region &r : op->getRegions())
190 convertBlockRecursive(b, count, consumed);
193 convertOne(op, count);
194 if (!isa<memref::GetGlobalOp>(op))
195 consumed.push_back(op);
204 void countOp(OpBuilder &b, Location loc, uint32_t &count) {
207 emitc::VerbatimOp::create(b, loc,
"++{};", ValueRange{opCountVar});
214 void convertOne(Operation *op, uint32_t &count) {
216 Location loc = op->getLoc();
218 llvm::TypeSwitch<Operation *, void>(op)
219 .Case<AIEX::NpuWrite32Op>([&](
auto w) {
223 Value addrV = runtimeOrResolvedAddr(b, loc, w, w.getAddress());
225 return fail(w,
"cannot convert a symbolic/unresolved write32 "
226 "address to the C++ TXN target");
227 emitTxnCall(b, loc,
"txn_append_write32", txnVec,
228 {addrV, w.getValue()});
229 countOp(b, loc, count);
231 .Case<AIEX::NpuMaskWrite32Op>([&](
auto mw) {
232 Value addrV = runtimeOrResolvedAddr(b, loc, mw, mw.getAddress());
234 return fail(mw,
"cannot convert a symbolic/unresolved maskwrite32 "
235 "address to the C++ TXN target");
236 emitTxnCall(b, loc,
"txn_append_maskwrite32", txnVec,
237 {addrV, mw.getValue(), mw.getMask()});
238 countOp(b, loc, count);
240 .Case<AIEX::NpuMaskPollOp>([&](
auto mp) {
241 Value addrV = runtimeOrResolvedAddr(b, loc, mp, mp.getAddress());
243 return fail(mp,
"cannot convert a symbolic/unresolved maskpoll "
244 "address to the C++ TXN target");
245 emitTxnCall(b, loc,
"txn_append_maskpoll32", txnVec,
246 {addrV, mp.getValue(), mp.getMask()});
247 countOp(b, loc, count);
249 .Case<AIEX::NpuSyncOp>([&](
auto s) {
250 emitTxnCall(b, loc,
"txn_append_sync", txnVec,
251 {s.getColumn(), s.getRow(), s.getDirection(),
252 s.getChannel(), s.getColumnNum(), s.getRowNum()});
253 countOp(b, loc, count);
255 .Case<AIEX::NpuLoadPdiOp>([&](
auto pdi) {
256 Value address = emitc::ConstantOp::create(
257 b, loc, emitc::OpaqueType::get(b.getContext(),
"uint64_t"),
258 emitc::OpaqueAttr::get(b.getContext(),
259 std::to_string(pdi.getAddress()) +
"ULL"));
260 emitTxnCall(b, loc,
"txn_append_loadpdi", txnVec,
261 {u32Literal(b, loc, pdi.getId()),
262 u32Literal(b, loc, pdi.getSize()), address});
263 countOp(b, loc, count);
265 .Case<AIEX::NpuPreemptOp>([&](
auto preempt) {
266 emitTxnCall(b, loc,
"txn_append_preempt", txnVec,
267 {u32Literal(b, loc, preempt.getLevel())});
268 countOp(b, loc, count);
270 .Case<AIEX::NpuAddressPatchOp>([&](
auto ap) {
273 Value addrV = ap.getAddrVal() ? ap.getAddrVal()
274 : u32Literal(b, loc, ap.getAddr());
275 std::optional<uint32_t> argIdx = ap.getArgIdx();
277 return fail(ap,
"address_patch still names its host buffer by SSA "
278 "value; run -aie-resolve-address-patch-buffers");
279 Value idxV = emitc::ConstantOp::create(
280 b, loc, emitc::OpaqueType::get(b.getContext(),
"int32_t"),
281 emitc::OpaqueAttr::get(b.getContext(), std::to_string(*argIdx)));
285 Value foldV = emitc::ConstantOp::create(
286 b, loc, emitc::OpaqueType::get(b.getContext(),
"bool"),
287 emitc::OpaqueAttr::get(b.getContext(),
288 foldDDRAddrOffset ?
"true" :
"false"));
289 Value argPlus = ap.getArgPlus();
292 if (argPlus.getType().isInteger(32))
293 argPlus = emitc::CastOp::create(b, loc, getU32Type(b.getContext()),
295 emitTxnCall(b, loc,
"txn_append_arg_patch", txnVec,
296 {addrV, idxV, argPlus, foldV});
297 countOp(b, loc, count);
299 .Case<AIEX::NpuBlockWriteOp>([&](
auto bw) {
300 convertBlockWrite(b, loc, bw);
301 countOp(b, loc, count);
303 .Case<AIEX::NpuBlockWriteValuesOp>([&](
auto bw) {
304 convertBlockWriteValues(b, loc, bw);
305 countOp(b, loc, count);
307 .Case<cf::AssertOp>([&](
auto a) {
311 emitc::VerbatimOp::create(
312 b, loc,
"if (!({})) " + refuse(a.getMsg(),
true),
313 ValueRange{a.getArg()});
315 .Case<AIEX::DMABdPoolPopOp>([&](AIEX::DMABdPoolPopOp pop) {
316 if (std::optional<uint32_t>
id = pop.getId()) {
317 emitc::VerbatimOp::create(b, loc,
318 "aie_runtime::bd_pool_reserve(" +
319 bdPoolName(pop) +
", " +
320 std::to_string(*
id) +
");");
321 pop.getBdId().replaceAllUsesWith(emitc::LiteralOp::create(
322 b, loc, pop.getBdId().getType(), std::to_string(*
id)));
329 std::string var =
"bd_" + std::to_string(nextPoolVar++);
330 emitc::VerbatimOp::create(
332 "uint32_t " + var +
"; if (!aie_runtime::bd_pool_pop(" +
333 bdPoolName(pop) +
", " + var +
")) " +
334 refuse(
"no free buffer descriptor on tile (" +
335 std::to_string(pop.getColumn()) +
", " +
336 std::to_string(pop.getRow()) +
337 "): the sequence keeps more transfers in flight "
338 "than the tile has descriptors",
341 emitc::LiteralOp::create(b, loc, pop.getBdId().getType(), var);
342 pop.getBdId().replaceAllUsesWith(ref);
344 .Case<AIEX::DMABdPoolPushOp>([&](AIEX::DMABdPoolPushOp push) {
346 emitc::CallOpaqueOp::create(
347 b, loc, TypeRange{},
"aie_runtime::bd_pool_push",
348 ValueRange{poolRef(b, loc, bdPoolName(push)), push.getBdId()});
352 .Case<AIEX::NpuCreateScratchpadOp, AIEX::NpuUpdateFromScratchpadOp>(
354 fail(sp,
"needs the scratchpad of a full-ELF context, which the "
355 "C++ TXN target's per-call streams run without; use a "
356 "runtime sequence argument instead of a scratchpad "
361 .Case<memref::GetGlobalOp>([&](
auto) {})
362 .Default([&](Operation *unsupported) {
363 if (isa<scf::SCFDialect>(unsupported->getDialect()))
365 "control flow in runtime sequences is not yet supported by "
366 "the C++ TXN target");
368 fail(unsupported,
"is not supported by the C++ TXN target");
376 Value resolvedAddr(OpBuilder &b, Location loc, Operation *clone) {
377 auto it = resolved.absoluteAddr.find(clone);
378 if (it == resolved.absoluteAddr.end())
380 return u32Literal(b, loc, it->second);
387 Value runtimeOrResolvedAddr(OpBuilder &b, Location loc, Operation *clone,
389 if (Value lit = resolvedAddr(b, loc, clone))
391 Operation *def = addrOperand ? addrOperand.getDefiningOp() :
nullptr;
392 if (def && !isa<arith::ConstantOp>(def))
398 void fail(Operation *op,
const llvm::Twine &msg) {
399 op->emitOpError(msg);
405 Value poolRef(OpBuilder &b, Location loc, StringRef name) {
406 return emitc::LiteralOp::create(
407 b, loc, emitc::OpaqueType::get(b.getContext(),
"aie_runtime::BdPool"),
411 void convertBlockWrite(OpBuilder &b, Location loc, AIEX::NpuBlockWriteOp bw) {
412 auto addrIt = resolved.absoluteAddr.find(bw);
413 auto dataIt = resolved.blockWriteData.find(bw);
414 if (addrIt == resolved.absoluteAddr.end() ||
415 dataIt == resolved.blockWriteData.end() || !dataIt->second) {
416 bw.emitOpError(
"cannot convert blockwrite without a constant address and "
417 "data to the C++ TXN target");
421 uint32_t addr = addrIt->second;
422 DenseIntElementsAttr data = dataIt->second;
426 MLIRContext *ctx = b.getContext();
427 int64_t n = data.size();
429 emitc::ArrayType::get(ctx, SmallVector<int64_t>{n}, getU32Type(ctx));
430 std::string init =
"{";
431 llvm::raw_string_ostream ss(init);
433 for (APInt word : data.getValues<APInt>()) {
436 ss << llvm::format(
"0x%08xu",
static_cast<unsigned>(word.getZExtValue()));
440 auto arrVar = emitc::VariableOp::create(b, loc, arrTy,
441 emitc::OpaqueAttr::get(ctx, init));
443 uint32_t colVal = 0, rowVal = 0;
444 if (
auto col = bw.getColumn(), row = bw.getRow(); col && row) {
448 emitTxnCall(b, loc,
"txn_append_blockwrite", txnVec,
449 {u32Literal(b, loc, addr), arrVar.getResult(),
450 u32Literal(b, loc,
static_cast<uint32_t
>(n)),
451 u32Literal(b, loc, colVal), u32Literal(b, loc, rowVal)});
459 void convertBlockWriteValues(OpBuilder &b, Location loc,
460 AIEX::NpuBlockWriteValuesOp bw) {
461 MLIRContext *ctx = b.getContext();
462 Value addrV = runtimeOrResolvedAddr(b, loc, bw, bw.getAddress());
464 fail(bw,
"cannot convert a symbolic/unresolved blockwrite_values address "
465 "to the C++ TXN target");
468 ValueRange words = bw.getValues();
469 int64_t n =
static_cast<int64_t
>(words.size());
474 emitc::ArrayType::get(ctx, SmallVector<int64_t>{n}, getU32Type(ctx));
475 auto arrVar = emitc::VariableOp::create(b, loc, arrTy,
476 emitc::OpaqueAttr::get(ctx,
"{}"));
482 for (int64_t i = 0; i < n; i++)
483 emitc::VerbatimOp::create(b, loc,
"{}[" + std::to_string(i) +
"] = {};",
484 ValueRange{arrVar.getResult(), words[i]});
487 emitTxnCall(b, loc,
"txn_append_blockwrite", txnVec,
488 {addrV, arrVar.getResult(),
489 u32Literal(b, loc,
static_cast<uint32_t
>(n)),
490 u32Literal(b, loc, 0), u32Literal(b, loc, 0)});
493 emitc::FuncOp funcOp;
495 const DeviceResolved &resolved;
501 unsigned nextPoolVar = 0;
502 bool foldDDRAddrOffset;
505struct ConvertAIEXToEmitCPass
506 : xilinx::impl::ConvertAIEXToEmitCBase<ConvertAIEXToEmitCPass> {
507 ConvertAIEXToEmitCPass() =
default;
510 explicit ConvertAIEXToEmitCPass(
const ConvertAIEXToEmitCOptions &options)
511 : ConvertAIEXToEmitCBase(options) {}
513 void runOnOperation()
override {
514 ModuleOp moduleOp = getOperation();
515 MLIRContext *ctx = &getContext();
517 return signalPassFailure();
520 AIE::RuntimeSequenceOp seq;
521 AIE::DeviceOp device;
523 SmallVector<SeqInfo> sequences;
524 moduleOp.walk([&](AIE::RuntimeSequenceOp seq) {
525 sequences.push_back({seq, seq->getParentOfType<AIE::DeviceOp>()});
527 if (emitDispatchShim && sequences.size() != 1) {
528 moduleOp.emitOpError()
529 <<
"emit-dispatch-shim needs exactly one aie.runtime_sequence to "
532 return signalPassFailure();
534 if (sequences.empty())
537 OpBuilder builder(ctx);
538 for (
auto &[seqOp, deviceOp] : sequences) {
540 seqOp.emitOpError(
"must be nested inside an aie.device");
541 return signalPassFailure();
543 std::optional<emitc::FuncOp> gen =
544 emitFunction(builder, moduleOp, seqOp, deviceOp);
546 return signalPassFailure();
547 if (emitDispatchShim) {
548 emitc::FuncOp generated = *gen;
549 if (failed(emitDispatchShimFuncs(builder, moduleOp, generated)))
550 return signalPassFailure();
555 SmallVector<Operation *> toErase;
556 for (Operation &op : *moduleOp.getBody())
557 if (!isa<emitc::FuncOp, emitc::IncludeOp>(op))
558 toErase.push_back(&op);
559 for (Operation *op : llvm::reverse(toErase))
565 if (failed(lowerArithAndScfToEmitC(moduleOp)))
566 return signalPassFailure();
568 builder.setInsertionPointToStart(moduleOp.getBody());
569 emitc::IncludeOp::create(builder, moduleOp.getLoc(),
570 "aie/Runtime/TxnEncoding.h",
572 emitc::IncludeOp::create(builder, moduleOp.getLoc(),
"cstdint",
574 emitc::IncludeOp::create(builder, moduleOp.getLoc(),
"vector",
578 emitc::IncludeOp::create(builder, moduleOp.getLoc(),
"optional",
585 LogicalResult lowerArithAndScfToEmitC(ModuleOp moduleOp) {
590 RewritePatternSet expand(moduleOp.getContext());
591 arith::populateCeilFloorDivExpandOpsPatterns(expand);
592 arith::populateExpandMinMaxIPatterns(expand);
595 GreedyRewriteConfig config;
596 config.enableFolding(
false).enableConstantCSE(
false);
597 if (failed(applyPatternsGreedily(moduleOp, std::move(expand), config)))
601 TypeConverter typeConverter;
602 typeConverter.addConversion([](Type t) {
return t; });
603 populateEmitCSizeTTypeConversions(typeConverter);
605 RewritePatternSet patterns(moduleOp.getContext());
606 populateArithToEmitCPatterns(typeConverter, patterns);
607 populateSCFToEmitCConversionPatterns(patterns, typeConverter);
609 ConversionTarget target(*moduleOp.getContext());
610 target.addLegalDialect<emitc::EmitCDialect>();
611 target.addIllegalDialect<arith::ArithDialect>();
612 target.addIllegalDialect<scf::SCFDialect>();
613 return applyPartialConversion(moduleOp, target, std::move(patterns));
620 static std::optional<std::string> cTypeName(Type t) {
621 if (isa<emitc::SizeTType>(t))
622 return std::string(
"size_t");
623 auto intTy = dyn_cast<IntegerType>(t);
624 if (!intTy || !emitc::isSupportedIntegerType(intTy))
626 if (intTy.getWidth() == 1)
627 return std::string(
"bool");
628 return (intTy.isUnsigned() ?
"uint" :
"int") +
629 std::to_string(intTy.getWidth()) +
"_t";
635 LogicalResult emitDispatchShimFuncs(OpBuilder &builder, ModuleOp moduleOp,
637 MLIRContext *ctx = moduleOp.getContext();
638 Location loc = gen.getLoc();
639 TypeRange genParams = gen.getFunctionType().getInputs();
641 SmallVector<std::string> abiTypes;
642 for (Type t : genParams) {
643 std::optional<std::string> name = cTypeName(t);
645 return gen.emitOpError()
646 <<
"runtime-sequence parameter type " << t
647 <<
" has no dispatch ABI spelling; it cannot be passed from the "
648 "host at dispatch time";
649 abiTypes.push_back(*name);
652 builder.setInsertionPointToEnd(moduleOp.getBody());
660 builder.getStrArrayAttr({
"extern \"C\"",
"AIE_DISPATCH_EXPORT"});
665 emitc::PointerType::get(emitc::OpaqueType::get(ctx,
"const char"));
666 auto abiFn = emitc::FuncOp::create(builder, loc,
"dispatch_abi",
667 FunctionType::get(ctx, {}, {charPtrTy}));
668 abiFn.setSpecifiersAttr(externC);
669 OpBuilder ab = OpBuilder::atBlockBegin(abiFn.addEntryBlock());
670 Value abiStr = emitc::LiteralOp::create(
671 ab, loc, charPtrTy,
"\"" + llvm::join(abiTypes,
",") +
"\"");
672 emitc::ReturnOp::create(ab, loc, abiStr);
674 SmallVector<Type> shimParams(genParams);
675 shimParams.push_back(
676 emitc::PointerType::get(emitc::PointerType::get(getU32Type(ctx))));
677 auto i64Ty = emitc::OpaqueType::get(ctx,
"int64_t");
679 emitc::FuncOp::create(builder, loc,
"dispatch_generate",
680 FunctionType::get(ctx, shimParams, {i64Ty}));
681 shimFn.setSpecifiersAttr(externC);
682 Block *shimBlock = shimFn.addEntryBlock();
683 OpBuilder sb = OpBuilder::atBlockBegin(shimBlock);
685 SmallVector<Value> genArgs(shimBlock->getArguments().drop_back());
686 SmallVector<StringRef> holes(genArgs.size(),
"{}");
687 std::string call =
"auto __result = " + gen.getSymName().str() +
"(" +
688 llvm::join(holes,
", ") +
");";
689 emitc::VerbatimOp::create(
690 sb, loc,
"static thread_local " + kTxnVecType.str() +
" __txn;");
691 emitc::VerbatimOp::create(sb, loc,
"aie_runtime::txn_refusal = nullptr;");
692 emitc::VerbatimOp::create(sb, loc, call, genArgs);
693 emitc::VerbatimOp::create(sb, loc,
"if (!__result) return -2;");
694 emitc::VerbatimOp::create(sb, loc,
"__txn = std::move(*__result);");
695 emitc::VerbatimOp::create(sb, loc,
"*{} = __txn.data();",
696 ValueRange{shimBlock->getArguments().back()});
697 emitc::ReturnOp::create(
699 emitc::LiteralOp::create(sb, loc, i64Ty,
700 "static_cast<int64_t>(__txn.size())"));
704 auto whyFn = emitc::FuncOp::create(builder, loc,
"dispatch_last_refusal",
705 FunctionType::get(ctx, {}, {charPtrTy}));
706 whyFn.setSpecifiersAttr(externC);
707 OpBuilder wb = OpBuilder::atBlockBegin(whyFn.addEntryBlock());
708 emitc::ReturnOp::create(
710 emitc::LiteralOp::create(wb, loc, charPtrTy,
711 "aie_runtime::txn_refusal"));
717 static void recordDeviceResolved(Operation *orig, Operation *clone,
718 DeviceResolved &resolved) {
719 if (
auto w = dyn_cast<AIEX::NpuWrite32Op>(orig)) {
720 if (
auto a = w.getAbsoluteAddress())
721 resolved.absoluteAddr[clone] = *a;
722 }
else if (
auto mw = dyn_cast<AIEX::NpuMaskWrite32Op>(orig)) {
723 if (
auto a = mw.getAbsoluteAddress())
724 resolved.absoluteAddr[clone] = *a;
725 }
else if (
auto mp = dyn_cast<AIEX::NpuMaskPollOp>(orig)) {
726 if (
auto a = mp.getAbsoluteAddress())
727 resolved.absoluteAddr[clone] = *a;
728 }
else if (
auto bw = dyn_cast<AIEX::NpuBlockWriteOp>(orig)) {
729 if (
auto a = bw.getAbsoluteAddress())
730 resolved.absoluteAddr[clone] = *a;
731 if (
auto d = bw.getDataWords())
732 resolved.blockWriteData[clone] = d;
733 }
else if (
auto bwv = dyn_cast<AIEX::NpuBlockWriteValuesOp>(orig)) {
736 resolved.absoluteAddr[clone] = *a;
740 FailureOr<emitc::FuncOp> emitFunction(OpBuilder &builder, ModuleOp moduleOp,
741 AIE::RuntimeSequenceOp seqOp,
742 AIE::DeviceOp deviceOp) {
743 Location loc = seqOp.getLoc();
744 Block &entry = seqOp.getBody().front();
749 SmallVector<Type> paramTypes;
751 emitc::OpaqueType::get(moduleOp.getContext(), kTxnVecType);
755 auto txnRetType = emitc::OpaqueType::get(
756 moduleOp.getContext(),
757 (llvm::Twine(
"std::optional<") + kTxnVecType +
">").str());
762 auto sizeT = emitc::SizeTType::get(moduleOp.getContext());
763 auto paramTypeFor = [&](Type t) -> Type {
764 return isa<IndexType>(t) ? Type(sizeT) : t;
766 for (BlockArgument arg : entry.getArguments())
767 if (!isa<BaseMemRefType>(arg.getType()))
768 paramTypes.push_back(paramTypeFor(arg.getType()));
770 builder.setInsertionPointToEnd(moduleOp.getBody());
771 std::string funcName =
"generate_txn_" + deviceOp.getSymName().str() +
"_" +
772 seqOp.getSymName().str();
773 auto funcOp = emitc::FuncOp::create(
774 builder, loc, funcName,
775 FunctionType::get(moduleOp.getContext(), paramTypes,
776 TypeRange{txnRetType}));
777 funcOp.setSpecifiersAttr(builder.getStrArrayAttr({
"inline"}));
778 Block *funcBlock = funcOp.addEntryBlock();
779 OpBuilder fb = OpBuilder::atBlockBegin(funcBlock);
785 emitc::VerbatimOp::create(fb, loc,
"std::vector<uint32_t> txn;");
786 Value txnVec = emitc::LiteralOp::create(fb, loc, txnVecType,
"txn");
787 emitTxnCall(fb, loc,
"txn_init", txnVec, {});
791 llvm::StringSet<> declaredPools;
792 auto declarePool = [&](
auto poolOp) {
793 std::string name = bdPoolName(poolOp);
794 if (!declaredPools.insert(name).second)
796 uint32_t lo = poolOp.getBdBegin(), hi = poolOp.getBdEnd();
797 emitc::VerbatimOp::create(fb, loc,
798 "aie_runtime::BdPool " + name +
799 " = aie_runtime::bd_pool_init_range(" +
800 std::to_string(lo) +
", " +
801 std::to_string(hi) +
");");
803 seqOp.walk([&](Operation *op) {
804 if (
auto pop = dyn_cast<AIEX::DMABdPoolPopOp>(op))
806 else if (
auto push = dyn_cast<AIEX::DMABdPoolPushOp>(op))
813 bool hasControlFlow =
false;
814 seqOp.walk([&](Operation *op) {
815 if (isa<scf::SCFDialect>(op->getDialect()))
816 hasControlFlow =
true;
819 if (hasControlFlow) {
820 emitc::VerbatimOp::create(fb, loc,
"uint32_t __opcount = 0;");
821 opCountVar = emitc::LiteralOp::create(
822 fb, loc, getU32Type(moduleOp.getContext()),
"__opcount");
831 for (BlockArgument arg : entry.getArguments())
832 if (!isa<BaseMemRefType>(arg.getType()))
833 mapping.map(arg, funcBlock->getArgument(p++));
834 DeviceResolved resolved;
835 for (Operation &op : entry.without_terminator()) {
836 fb.clone(op, mapping);
839 op.walk([&](Operation *orig) {
840 Operation *c = mapping.lookupOrNull(orig);
842 recordDeviceResolved(orig, c, resolved);
846 AIEXToEmitCConverter conv(funcOp, txnVec, resolved, opCountVar,
848 std::optional<uint32_t> count = conv.run();
855 const AIE::AIETargetModel &tm = deviceOp.getTargetModel();
856 OpBuilder eb(funcBlock, funcBlock->end());
857 std::string countStr =
858 hasControlFlow ?
"__opcount" : (std::to_string(*count) +
"u");
861 std::string header =
"aie_runtime::txn_prepend_header(txn, " + countStr +
862 ", aie_runtime::txn_device_info(" +
864 std::to_string(tm.
rows()) +
", " +
865 std::to_string(tm.
columns()) +
", " +
867 emitc::VerbatimOp::create(eb, loc, header);
872 Value ret = emitc::LiteralOp::create(eb, loc, txnRetType,
"std::move(txn)");
873 emitc::ReturnOp::create(eb, loc, ret);
880std::unique_ptr<OperationPass<ModuleOp>>
882 return std::make_unique<ConvertAIEXToEmitCPass>();
885std::unique_ptr<OperationPass<ModuleOp>>
887 bool emitDispatchShim) {
888 ConvertAIEXToEmitCOptions options;
889 options.foldDDRAddrOffset = foldDDRAddrOffset;
890 options.emitDispatchShim = emitDispatchShim;
891 return std::make_unique<ConvertAIEXToEmitCPass>(options);
virtual int rows() const =0
Return the number of rows in the device.
virtual int columns() const =0
Return the number of columns in the device.
virtual uint32_t getNumMemTileRows() const =0
std::optional< uint32_t > getConstantIntOperand(mlir::Value v)
uint8_t txnDeviceGen(const AIETargetModel &tm)
Device generation ID written into the TXN header (aie_runtime::TxnDeviceInfo::devGen).
mlir::LogicalResult inlineWriteConfigs(mlir::ModuleOp module)
std::unique_ptr< mlir::OperationPass< mlir::ModuleOp > > createConvertAIEXToEmitCPass()