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((llvm::Twine(workDirPath) + std::string(1, ps) +
91 targetOp.getSymName() +
"_aie_cdo_init.bin")
94 if (failed(ctl.addInitConfig(targetOp)))
96 return ctl.addDmaActivation(targetOp);
100 LLVM_DEBUG(llvm::dbgs() <<
"Generating aie_cdo_enable.bin");
102 failed(generateCDOBinary(
103 (llvm::Twine(workDirPath) + std::string(1, ps) +
104 targetOp.getSymName() +
"_aie_cdo_enable.bin")
106 [&ctl, &targetOp] { return ctl.addCoreEnable(targetOp); })))
112static LogicalResult generateCDOUnified(
AIERTControl &ctl,
113 const StringRef workDirPath,
114 DeviceOp &targetOp,
bool aieSim,
116 auto ps = std::filesystem::path::preferred_separator;
118 return generateCDOBinary(
119 (llvm::Twine(workDirPath) + std::string(1, ps) + targetOp.getSymName() +
122 [&ctl, &targetOp, &workDirPath, &aieSim, &enableCores] {
123 if (!targetOp.getOps<CoreOp>().empty() &&
124 failed(ctl.addAieElfs(targetOp, workDirPath, aieSim)))
126 if (failed(ctl.addInitConfig(targetOp)) ||
127 failed(ctl.addDmaActivation(targetOp)))
129 if (enableCores && !targetOp.getOps<CoreOp>().empty() &&
130 failed(ctl.addCoreEnable(targetOp)))
137translateToCDODirect(ModuleOp m, llvm::StringRef workDirPath,
138 llvm::StringRef deviceName, byte_ordering endianness,
139 bool emitUnified,
bool cdoDebug,
bool aieSim,
140 bool xaieDebug,
bool enableCores) {
142 DeviceOp targetOp = AIE::DeviceOp::getForSymbolInModuleOrError(m, deviceName);
152 "Only NPU currently supported");
159 initializeCDOGenerator(endianness, cdoDebug);
161 auto result = [&]() {
163 return generateCDOUnified(ctl, workDirPath, targetOp, aieSim,
166 return generateCDOBinariesSeparately(ctl, workDirPath, targetOp, aieSim,
173 ModuleOp m, llvm::StringRef workDirPath, llvm::StringRef deviceName,
174 bool bigEndian,
bool emitUnified,
bool cdoDebug,
bool aieSim,
175 bool xaieDebug,
bool enableCores) {
176 byte_ordering endianness =
177 bigEndian ? byte_ordering::Big_Endian : byte_ordering::Little_Endian;
178 return translateToCDODirect(m, workDirPath, deviceName, endianness,
179 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)
static mlir::LogicalResult isSupported(mlir::Operation *op, const AIETargetModel &tm)
Diagnose, on op, a target this aie-rt lowering cannot configure.