11#include "cdo-driver/cdo_driver.h"
18#include "mlir/IR/Block.h"
19#include "mlir/IR/BuiltinOps.h"
20#include "mlir/IR/Operation.h"
21#include "mlir/Support/LLVM.h"
22#include "mlir/Support/LogicalResult.h"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/Twine.h"
27#include "llvm/Support/Debug.h"
41#include "aie_codegen.h"
44#define DEBUG_TYPE "aie-generate-cdo"
50static void initializeCDOGenerator(byte_ordering endianness,
bool cdoDebug) {
54 setEndianness(endianness);
58generateCDOBinary(
const StringRef outputPath,
59 const std::function<LogicalResult()> &cb) {
60 startCDOFileStream(outputPath.str().c_str());
68 endCurrentCDOFileStream();
72static LogicalResult generateCDOBinariesSeparately(
AIERTControl &ctl,
73 const StringRef workDirPath,
77 auto ps = std::filesystem::path::preferred_separator;
79 LLVM_DEBUG(llvm::dbgs() <<
"Generating aie_cdo_elfs.bin");
80 if (failed(generateCDOBinary((llvm::Twine(workDirPath) + std::string(1, ps) +
81 targetOp.getSymName() +
"_aie_cdo_elfs.bin")
83 [&ctl, &targetOp, &workDirPath, &aieSim] {
84 return ctl.addAieElfs(targetOp, workDirPath,
89 LLVM_DEBUG(llvm::dbgs() <<
"Generating aie_cdo_init.bin");
90 if (failed(generateCDOBinary(
91 (llvm::Twine(workDirPath) + std::string(1, ps) +
92 targetOp.getSymName() +
"_aie_cdo_init.bin")
94 [&ctl, &targetOp] { return ctl.addInitConfig(targetOp); })))
97 LLVM_DEBUG(llvm::dbgs() <<
"Generating aie_cdo_enable.bin");
99 failed(generateCDOBinary(
100 (llvm::Twine(workDirPath) + std::string(1, ps) +
101 targetOp.getSymName() +
"_aie_cdo_enable.bin")
103 [&ctl, &targetOp] { return ctl.addCoreEnable(targetOp); })))
109static LogicalResult generateCDOUnified(
AIERTControl &ctl,
110 const StringRef workDirPath,
111 DeviceOp &targetOp,
bool aieSim,
113 auto ps = std::filesystem::path::preferred_separator;
115 return generateCDOBinary(
116 (llvm::Twine(workDirPath) + std::string(1, ps) + targetOp.getSymName() +
119 [&ctl, &targetOp, &workDirPath, &aieSim, &enableCores] {
120 if (!targetOp.getOps<CoreOp>().empty() &&
121 failed(ctl.addAieElfs(targetOp, workDirPath, aieSim)))
123 if (failed(ctl.addInitConfig(targetOp)))
125 if (enableCores && !targetOp.getOps<CoreOp>().empty() &&
126 failed(ctl.addCoreEnable(targetOp)))
133translateToCDODirect(ModuleOp m, llvm::StringRef workDirPath,
134 llvm::StringRef deviceName, byte_ordering endianness,
135 bool emitUnified,
bool cdoDebug,
bool aieSim,
136 bool xaieDebug,
bool enableCores) {
138 DeviceOp targetOp = AIE::DeviceOp::getForSymbolInModuleOrError(m, deviceName);
148 "Only NPU currently supported");
153 initializeCDOGenerator(endianness, cdoDebug);
155 auto result = [&]() {
157 return generateCDOUnified(ctl, workDirPath, targetOp, aieSim,
160 return generateCDOBinariesSeparately(ctl, workDirPath, targetOp, aieSim,
167 ModuleOp m, llvm::StringRef workDirPath, llvm::StringRef deviceName,
168 bool bigEndian,
bool emitUnified,
bool cdoDebug,
bool aieSim,
169 bool xaieDebug,
bool enableCores) {
170 byte_ordering endianness =
171 bigEndian ? byte_ordering::Big_Endian : byte_ordering::Little_Endian;
172 return translateToCDODirect(m, workDirPath, deviceName, endianness,
173 emitUnified, cdoDebug, aieSim, xaieDebug,
bool hasProperty(ModelProperty Prop) const
mlir::LogicalResult AIETranslateToCDODirect(mlir::ModuleOp m, llvm::StringRef workDirPath, llvm::StringRef deviceName, bool bigEndian=false, bool emitUnified=false, bool cdoDebug=false, bool aieSim=false, bool xaieDebug=false, bool enableCores=true)
mlir::LogicalResult setIOBackend(bool aieSim, bool xaieDebug)