MLIR-AIE 1.4.6.dev
AIETargetCDODirect.cpp
Go to the documentation of this file.
1//===- AIETargetCDODirect.cpp -----------------------------------*- C++ -*-===//
2//
3// Copyright (C) 2023 Advanced Micro Devices, Inc.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
8#include "aie/Targets/AIERT.h"
10extern "C" {
11#include "cdo-driver/cdo_driver.h"
12}
13
17
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"
23
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/Twine.h"
27#include "llvm/Support/Debug.h"
28
29#include <algorithm>
30#include <cassert>
31#include <filesystem>
32#include <functional>
33#include <string>
34#include <vector>
35
36#ifndef NDEBUG
37#define XAIE_DEBUG
38#endif
39
40extern "C" {
41#include "aie_codegen.h"
42}
43
44#define DEBUG_TYPE "aie-generate-cdo"
45
46using namespace mlir;
47using namespace xilinx;
48using namespace xilinx::AIE;
49
50static void initializeCDOGenerator(byte_ordering endianness, bool cdoDebug) {
51 // Enables AXI-MM prints for configs being added in CDO
52 if (cdoDebug)
53 EnAXIdebug();
54 setEndianness(endianness);
55};
56
57static LogicalResult
58generateCDOBinary(const StringRef outputPath,
59 const std::function<LogicalResult()> &cb) {
60 startCDOFileStream(outputPath.str().c_str());
61 FileHeader();
62 // Never generate a completely empty CDO file. If the file only contains a
63 // header, then bootgen flags it as invalid.
64 insertNoOpCommand(4);
65 if (failed(cb()))
66 return failure();
67 configureHeader();
68 endCurrentCDOFileStream();
69 return success();
70}
71
72static LogicalResult generateCDOBinariesSeparately(AIERTControl &ctl,
73 const StringRef workDirPath,
74 DeviceOp &targetOp,
75 bool aieSim,
76 bool enableCores) {
77 auto ps = std::filesystem::path::preferred_separator;
78
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")
82 .str(),
83 [&ctl, &targetOp, &workDirPath, &aieSim] {
84 return ctl.addAieElfs(targetOp, workDirPath,
85 aieSim);
86 })))
87 return failure();
88
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")
93 .str(),
94 [&ctl, &targetOp] { return ctl.addInitConfig(targetOp); })))
95 return failure();
96
97 LLVM_DEBUG(llvm::dbgs() << "Generating aie_cdo_enable.bin");
98 if (enableCores &&
99 failed(generateCDOBinary(
100 (llvm::Twine(workDirPath) + std::string(1, ps) +
101 targetOp.getSymName() + "_aie_cdo_enable.bin")
102 .str(),
103 [&ctl, &targetOp] { return ctl.addCoreEnable(targetOp); })))
104 return failure();
105
106 return success();
107}
108
109static LogicalResult generateCDOUnified(AIERTControl &ctl,
110 const StringRef workDirPath,
111 DeviceOp &targetOp, bool aieSim,
112 bool enableCores) {
113 auto ps = std::filesystem::path::preferred_separator;
114
115 return generateCDOBinary(
116 (llvm::Twine(workDirPath) + std::string(1, ps) + targetOp.getSymName() +
117 "_aie_cdo.bin")
118 .str(),
119 [&ctl, &targetOp, &workDirPath, &aieSim, &enableCores] {
120 if (!targetOp.getOps<CoreOp>().empty() &&
121 failed(ctl.addAieElfs(targetOp, workDirPath, aieSim)))
122 return failure();
123 if (failed(ctl.addInitConfig(targetOp)))
124 return failure();
125 if (enableCores && !targetOp.getOps<CoreOp>().empty() &&
126 failed(ctl.addCoreEnable(targetOp)))
127 return failure();
128 return success();
129 });
130}
131
132static LogicalResult
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) {
137
138 DeviceOp targetOp = AIE::DeviceOp::getForSymbolInModuleOrError(m, deviceName);
139 if (!targetOp) {
140 return failure();
141 }
142 const AIETargetModel &targetModel =
143 (const AIETargetModel &)targetOp.getTargetModel();
144
145 // things like XAIE_MEM_TILE_ROW_START and the missing
146 // shim dma on tile (0,0) are hard-coded assumptions about NPU...
147 assert(targetModel.hasProperty(AIETargetModel::IsNPU) &&
148 "Only NPU currently supported");
149
150 AIERTControl ctl(targetModel);
151 if (failed(ctl.setIOBackend(aieSim, xaieDebug)))
152 return failure();
153 initializeCDOGenerator(endianness, cdoDebug);
154
155 auto result = [&]() {
156 if (emitUnified) {
157 return generateCDOUnified(ctl, workDirPath, targetOp, aieSim,
158 enableCores);
159 }
160 return generateCDOBinariesSeparately(ctl, workDirPath, targetOp, aieSim,
161 enableCores);
162 }();
163 return result;
164}
165
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,
174 enableCores);
175}
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)
Definition AIERT.cpp:298