MLIR-AIE 1.4.6.dev
AIETargetSimulationFiles.cpp
Go to the documentation of this file.
1//===- AIETargetSimulationFiles.cpp -----------------------------*- C++ -*-===//
2//
3// Copyright (C) 2022 Advanced Micro Devices, Inc.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
10
11#include "mlir/IR/Attributes.h"
12#include "mlir/Target/LLVMIR/Import.h"
13#include "mlir/Tools/mlir-translate/MlirTranslateMain.h"
14
15#include "llvm/IR/Module.h"
16
17using namespace xilinx;
18using namespace AIE;
19
20mlir::LogicalResult AIE::AIETranslateSCSimConfig(mlir::ModuleOp module,
21 llvm::raw_ostream &output,
22 llvm::StringRef deviceName) {
23 DeviceOp targetOp =
24 AIE::DeviceOp::getForSymbolInModuleOrError(module, deviceName);
25 if (!targetOp) {
26 return mlir::failure();
27 }
28 AIEArch arch = targetOp.getTargetModel().getTargetArch();
29 if (arch == AIEArch::AIE4)
30 return targetOp.emitOpError("simulation files are not supported for the "
31 "AIE4 architecture");
32
33 if (arch == AIEArch::AIE2p) {
34 output << "{\n"
35 << " \"SimulationConfig\": {\n"
36 << " \"device_json\": {\n"
37 << " \"directory\": \"data/aie2p/devices\",\n"
38 << " \"file\": \"aie2p_8x4_device.json\"\n"
39 << " },\n"
40 << " \"phy_device_file\": \"aie2p_8x4_device\",\n"
41 << " \"aiearch\": \"aie2p\",\n"
42 << " \"aie_freq\": 1000000000.0,\n"
43 << " \"use_real_noc\": 1,\n"
44 << " \"evaluate_fifo_depth\": 0,\n"
45 << " \"noc_ip_block\": {\n"
46 << " \"lib_path\": \"./sim/noc/liblnoc_tlm.so\",\n"
47 << " \"traffic_file\": \"./sim/noc/noc_traffic.nts\",\n"
48 << " \"config_file\": \"./sim/noc/noc_soln.ncr\"\n"
49 << " },\n"
50 << " \"pl_ip_block\": [\n"
51 << " {\n"
52 << " \"name\": \"ps_ps_main\",\n"
53 << " \"ip\": \"ps\",\n"
54 << " \"lib_path\": \"ps/ps.so\",\n"
55 << " \"pl_freq\": 362500000.0,\n"
56 << " \"axi_mm\": [\n"
57 << " {\n"
58 << " \"port_name\": \"ps_axi\",\n"
59 << " \"direction\": \"ps_to_gm\",\n"
60 << " \"noc_endpoint\": \"NOC_NMU128_X0Y5\",\n"
61 << " \"bus_width\": 0\n"
62 << " }\n"
63 << " ],\n"
64 << " \"event_bus\": []\n"
65 << " }\n"
66 << " ]\n"
67 << " }\n"
68 << "}\n";
69 } else if (arch == AIEArch::AIE2) {
70 output
71 << "{\n"
72 << " \"SimulationConfig\": {\n"
73 << " \"device_json\": {\n"
74 << " \"directory\": \"data/aie_ml/devices\",\n"
75 << " \"file\": \"VC2802.json\"\n"
76 << " },\n"
77 << " \"phy_device_file\": \"xcve2802-vsvh1760-2LP-e-S-es1\",\n"
78 << " \"aiearch\": \"aie2\",\n"
79 << " \"aie_freq\": 1050000000.0,\n"
80 << " \"use_real_noc\": 1,\n"
81 << " \"evaluate_fifo_depth\": 0,\n"
82 << " \"noc_ip_block\": {\n"
83 << " \"lib_path\": \"./sim/noc/liblnoc_tlm.so\",\n"
84 << " \"traffic_file\": \"./sim/noc/noc_traffic.nts\",\n"
85 << " \"config_file\": \"./sim/noc/noc_soln.ncr\"\n"
86 << " },\n"
87 << " \"pl_ip_block\": [\n"
88 << " {\n"
89 << " \"name\": \"ps_ps_main\",\n"
90 << " \"ip\": \"ps\",\n"
91 << " \"lib_path\": \"ps/ps.so\",\n"
92 << " \"pl_freq\": 362500000.0,\n"
93 << " \"axi_mm\": [\n"
94 << " {\n"
95 << " \"port_name\": \"ps_axi\",\n"
96 << " \"direction\": \"ps_to_gm\",\n"
97 << " \"noc_endpoint\": \"NOC_NMU128_X0Y5\",\n"
98 << " \"bus_width\": 0\n"
99 << " }\n"
100 << " ],\n"
101 << " \"event_bus\": []\n"
102 << " }\n"
103 << " ]\n"
104 << " }\n"
105 << "}\n";
106 } else if (arch == AIEArch::AIE1) { // AIEArch::AIE1
107 output << "{\n"
108 << " \"SimulationConfig\": {\n"
109 << " \"device_json\": {\n"
110 << " \"directory\": \"data/devices\",\n"
111 << " \"file\": \"VC1902.json\"\n"
112 << " },\n"
113 << " \"phy_device_file\": \"xcvc1902-vsva2197-2MP-e-S\",\n"
114 << " \"aiearch\": \"aie\",\n"
115 << " \"aie_freq\": 1250000000.0,\n"
116 << " \"use_real_noc\": 1,\n"
117 << " \"evaluate_fifo_depth\": 0,\n"
118 << " \"noc_ip_block\": {\n"
119 << " \"lib_path\": \"./Work/noc/liblnoc_tlm.so\",\n"
120 << " \"traffic_file\": \"./Work/noc/noc_traffic.nts\",\n"
121 << " \"config_file\": \"./Work/noc/noc_soln.ncr\"\n"
122 << " },\n"
123 << " \"pl_ip_block\": [\n"
124 << " {\n"
125 << " \"name\": \"ps_ps_main\",\n"
126 << " \"ip\": \"ps\",\n"
127 << " \"lib_path\": \"ps/ps.so\",\n"
128 << " \"pl_freq\": 312500000.0,\n"
129 << " \"axi_mm\": [\n"
130 << " {\n"
131 << " \"port_name\": \"ps_axi\",\n"
132 << " \"direction\": \"ps_to_gm\",\n"
133 << " \"noc_endpoint\": \"NOC_NMU128_X0Y5\",\n"
134 << " \"bus_width\": 0\n"
135 << " }\n"
136 << " ],\n"
137 << " \"event_bus\": []\n"
138 << " }\n"
139 << " ]\n"
140 << " }\n"
141 << "}\n";
142 }
143 return mlir::success();
144}
145
146/* Generates a aieshim_solution.aiesol file which is necessary to run aiesim.
147Sample invocation:
148aie-translate --aie-mlir-to-shim-solution ./aie.mlir >
149./Work/arch/aieshim_solution.aiesol
150
151NOTE: to correctly enable all tiles used for routing, the aie-opt routing pass
152must be called first. So, a more practical invocation: aie-opt
153--aie-create-pathfinder-flows ./aie.mlir | aie-translate --aie-mlir-to-shim >
154./Work/arch/aieshim_solution.aiesol
155*/
156mlir::LogicalResult AIE::AIETranslateShimSolution(mlir::ModuleOp module,
157 llvm::raw_ostream &output,
158 llvm::StringRef deviceName) {
159 DeviceOp targetOp =
160 AIE::DeviceOp::getForSymbolInModuleOrError(module, deviceName);
161 if (!targetOp) {
162 return mlir::failure();
163 }
164 if (failed(verifyDMAChannelsResolved(targetOp)))
165 return mlir::failure();
166 if (targetOp.getTargetModel().getTargetArch() == AIEArch::AIE4)
167 return targetOp.emitOpError("simulation files are not supported for the "
168 "AIE4 architecture");
169
170 // Generate boilerplate header
171 output << "{\n";
172 output << " \"Placement\": [\n";
173
174 int shim_MM2S_count = 0;
175
176 // For each DMAStartOp in shims, generate a "LogicalInstance" section
177 auto all_shim_ops = targetOp.getOps<ShimDMAOp>();
178 for (ShimDMAOp shimOp : all_shim_ops) {
179 for (DMAStartOp startOp : shimOp.getOps<DMAStartOp>()) {
180 // For aiesimulator to run, PortName must start at 00 and increase
181 if (startOp.getChannelDir() == DMAChannelDir::MM2S) {
182 if (shim_MM2S_count > 0)
183 output << ",\n";
184
185 std::string port_name = "";
186 port_name.append("M");
187 port_name.append(shim_MM2S_count < 10 ? "0" : ""); // padding zero
188 port_name.append(std::to_string(shim_MM2S_count++));
189 port_name.append("_AXI");
190 // TODO: How to tell if PortName should be AXI or AXIS?
191
192 // Generate a Logical Instance line
193 output << " {\n"
194 << R"( "LogicalInstance" : { "InstanceName" : )"
195 << R"("aie_engine_0", "PortName" : ")" << port_name << "\"},\n";
196
197 std::string col = std::to_string(shimOp.colIndex());
198 int ch = startOp.getChannelIndex();
199 std::string channel = std::to_string(ch);
200 // "name" field appears to be arbitrary, but we try to be descriptive
201 std::string physical_name = "";
202 physical_name.append("AIE_NOC_X").append(col).append("Y0_AIE_NOC_");
203 physical_name.append("M_AXI_ch").append(channel);
204
205 // Generate a Physical Instance line
206 output << R"( "PhysicalInstance" : [{ "name" : ")" << physical_name
207 << R"(", "column" : )" << col << ", \"channel\" : " << channel
208 << " }],\n"
209 << " \"IsSoft\" : true\n }";
210 }
211 }
212 }
213
214 output << "\n ]\n";
215 output << "}\n";
216
217 return mlir::success();
218}
219
220mlir::LogicalResult AIE::AIETranslateGraphXPE(mlir::ModuleOp module,
221 llvm::raw_ostream &output,
222 llvm::StringRef deviceName) {
223 /* Generates a .xpe file which is necessary to run aiesim.
224 .xpe is a power report file, but has information on which AIE tiles are used.
225 Sample invocation:
226 aie-translate --aie-mlir-to-xpe ./aie.mlir > ./Work/reports/graph.xpe
227
228 NOTE: to correctly enable all tiles used for routing, the aie-opt routing pass
229 must be called first. So, a more practical invocation: aie-opt
230 --aie-create-pathfinder-flows ./aie.mlir | aie-translate --aie-mlir-to-xpe >
231 ./Work/reports/graph.xpe
232 */
233
234 DeviceOp targetOp =
235 AIE::DeviceOp::getForSymbolInModuleOrError(module, deviceName);
236 if (!targetOp) {
237 return mlir::failure();
238 }
239 AIEArch arch = targetOp.getTargetModel().getTargetArch();
240 if (arch == AIEArch::AIE4)
241 return targetOp.emitOpError("simulation files are not supported for the "
242 "AIE4 architecture");
243
244 // Generate boilerplate header
245 // TODO: date and version should probably not be hardcoded
246 output << "<?xml version=\"1.0\"?>"
247 << "\n";
248 output << "<POWERDATA data=\"AI-Engine Compiler\" dataVersion=\"2022.2\" "
249 "design=\"graph\" date=\"2023\">\n";
250 if ((arch == AIEArch::AIE2) || (arch == AIEArch::AIE2p)) {
251 output
252 // AIE2 xcve2802
253 << " <DEVICE part=\"xcve2802\" grade=\"extended\" package=\"vsvh1760\" "
254 "speed=\"-2LP\" process=\"typical\" vid=\"No\"></DEVICE>\n";
255 // AIE2 - xcve2302
256 // << " <DEVICE part=\"xcve2302\" grade=\"extended\" package=\"sfva784\" "
257 // "speed=\"-1MP\" process=\"typical\" vid=\"No\"></DEVICE>\n";
258 } else { // AIEArch::AIE1
259 output
260 << " <DEVICE part=\"xcvc1902\" grade=\"extended\" package=\"vsva2197\" "
261 "speed=\"-2MP\" process=\"typical\" vid=\"No\"></DEVICE>\n";
262 }
263 output << " <AIE status=\"COMPILER_OUTPUT\">\n";
264
265 // Generate design specific info on tiles within the mlir module
266 auto module_tile_ops = targetOp.getOps<TileOp>();
267 int num_tiles = std::distance(module_tile_ops.begin(), module_tile_ops.end());
268 // TODO: clk_freq only 1150 for AIE2
269 if ((arch == AIEArch::AIE2) || (arch == AIEArch::AIE2p)) {
270 output << R"( <AIE_MODULE name="graph" num_tiles=")"
271 << std::to_string(num_tiles) << "\" clk_freq=\"1150\">\n";
272 } else {
273 output << R"( <AIE_MODULE name="graph" num_tiles=")"
274 << std::to_string(num_tiles) << "\" clk_freq=\"1250\">\n";
275 }
276
277 // Get all CoreOps into a convenient map which can then be referenced by
278 // coordinates
279 std::map<std::pair<int, int>, std::vector<CoreOp>> coreMap;
280 for (CoreOp coreOp : targetOp.getOps<CoreOp>())
281 coreMap[std::make_pair(coreOp.colIndex(), coreOp.rowIndex())].push_back(
282 coreOp);
283
284 // For each TileOp in the module, generate a <TILE> section
285 int kernel_count = 0;
286 for (TileOp tileOp : module_tile_ops) {
287 int col = tileOp.colIndex();
288 int row = tileOp.rowIndex();
289
290 // NOTE: row == 0 assumes shim always row 0
291 if (tileOp.isShimNOCorPLTile() || tileOp.isMemTile())
292 continue; // Skip shim and mem tiles (handled below)
293
294 if ((arch == AIEArch::AIE2) || (arch == AIEArch::AIE2p)) {
295
296 output << " <TILE name=\"CR(" <<
297 // CR coordinates ignores shim, and 2 mem rows hence row-3
298 // AIE2 - xcve2802
299 std::to_string(col) << ","
300 << std::to_string(
301 row - targetOp.getTargetModel().getNumMemTileRows() - 1)
302 << ")\" "
303 // CR coordinates ignores shim and 1 mem row, hence row-2
304 // AIE2 - xcve2302
305 // std::to_string(col) << "," << std::to_string(row - 2) << ")\" "
306 << R"(type="int16" int_core_load="1.0" fp_core_load="0" )"
307 << "mem_banks=\"0\" mem_rw_rate=\"0.2\" stream_util=\"0.0\" "
308 "coordinates=\""
309 <<
310 // TODO: where does number of mem_banks come from?? Hardcoded to 0 for
311 // now
312 // TODO: does stream_util matter for sim?
313 std::to_string(col) << ","
314 << std::to_string(row -
315 targetOp.getTargetModel().getNumMemTileRows())
316 << "\">\n";
317
318 } else {
319 output << " <TILE name=\"CR(" <<
320 // CR coordinates ignores shim, hence row-1
321 std::to_string(col) << "," << std::to_string(row - 1) << ")\" "
322 << R"(type="int16" int_core_load="1.0" fp_core_load="0" )"
323 << "mem_banks=\"0\" mem_rw_rate=\"0.2\" stream_util=\"0.0\" "
324 "coordinates=\""
325 <<
326 // TODO: where does number of mem_banks come from?? Hardcoded to 0 for
327 // now
328 // TODO: does stream_util matter for sim?
329 std::to_string(col) << "," << std::to_string(row) << "\">\n";
330 }
331
332 // If the TileOp has associated Kernels, generate <KERNEL> sections
333 auto coreOps_in_tile =
334 coreMap[std::make_pair(tileOp.colIndex(), tileOp.rowIndex())];
335 for (auto coreOp : coreOps_in_tile) {
336 (void)(coreOp); // get around unused variable warning for coreOp
337 output << " <KERNEL name=\"i" << std::to_string(kernel_count++)
338 << "\" "
339 << "int_core_load=\"" << std::to_string(1 / coreOps_in_tile.size())
340 << "\" fp_core_load=\"0\"></KERNEL>\n";
341 }
342 output << " </TILE>\n";
343 }
344
345 // For each ShimOp in the module, generate a <SHIM> section
346 for (ShimDMAOp shimOp : targetOp.getOps<ShimDMAOp>()) {
347 if ((arch == AIEArch::AIE2) || (arch == AIEArch::AIE2p)) {
348 const auto *noc_label = (targetOp.getTargetModel().isShimNOCTile(
349 shimOp.colIndex(), shimOp.rowIndex()))
350 ? "AIE_PL_NOC_SIM"
351 : "AIE_PL_SHIM";
352 output << " <SHIM name=\"SHIM(" << std::to_string(shimOp.colIndex())
353 << ", " << std::to_string(shimOp.rowIndex())
354 << ")\" "
355 // TODO: stream_util can be 0 for aiesim purposes?
356 "type=\""
357 << noc_label << R"(" stream_util="0" num_pl_streams="0" )" <<
358 // TODO: how to get num_aximm_connections from mlir?
359 R"(num_aximm_connections="1" coordinates=")"
360 << std::to_string(shimOp.colIndex()) << ","
361 << std::to_string(shimOp.rowIndex()) << "\" "
362 << "></SHIM>\n";
363 } else {
364 output << " <SHIM name=\"SHIM(" << std::to_string(shimOp.colIndex())
365 << ", " << std::to_string(shimOp.rowIndex()) << ")\" " <<
366 // TODO: stream_util can be 0 for aiesim purposes?
367 R"(type="AIE_PL_NOC_SHIM" stream_util="0" num_pl_streams="0"
368 )" <<
369 // TODO: how to get num_aximm_connections from mlir?
370 R"(num_aximm_connections="1" coordinates=")"
371 << std::to_string(shimOp.colIndex()) << ","
372 << std::to_string(shimOp.rowIndex()) << "\" "
373 << "></SHIM>\n";
374 }
375 }
376
377 // For each memTile
378 if ((arch == AIEArch::AIE2) || (arch == AIEArch::AIE2p)) {
379 for (TileOp tileOp : module_tile_ops) {
380 int col = tileOp.colIndex();
381 int row = tileOp.rowIndex();
382
383 // NOTE: row == 0 assumes shim always row 0
384 if (row == 0 ||
385 row > static_cast<int>(targetOp.getTargetModel().getNumMemTileRows()))
386 continue; // Skip regular tiles (handled above)
387
388 output << " <MEM name=\"MEM(" << std::to_string(col) << ", "
389 << std::to_string(row - 1)
390 << ")\" "
391 // TODO: stream_util can be 0 for aiesim purposes?
392 "type=\"AIE_MEM\" mem_banks=\"0\" mme_rw_rate=\"0.1\" "
393 << R"(stream_util="0.1" coordinates=")" << std::to_string(col)
394 << "," << std::to_string(row - 1) << "\" "
395 << "></MEM>\n";
396 }
397 }
398
399 output << " </AIE_MODULE>\n";
400 output << " </AIE>\n";
401 output << "</POWERDATA>\n";
402
403 return mlir::success();
404}
mlir::LogicalResult AIETranslateSCSimConfig(mlir::ModuleOp module, llvm::raw_ostream &output, llvm::StringRef deviceName="")
mlir::LogicalResult AIETranslateShimSolution(mlir::ModuleOp module, llvm::raw_ostream &, llvm::StringRef deviceName="")
mlir::LogicalResult AIETranslateGraphXPE(mlir::ModuleOp module, llvm::raw_ostream &, llvm::StringRef)
mlir::LogicalResult verifyDMAChannelsResolved(DeviceOp device)