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((llvm::Twine(workDirPath) + std::string(1, ps) +
91 targetOp.getSymName() + "_aie_cdo_init.bin")
92 .str(),
93 [&ctl, &targetOp] {
94 if (failed(ctl.addInitConfig(targetOp)))
95 return failure();
96 return ctl.addDmaActivation(targetOp);
97 })))
98 return failure();
99
100 LLVM_DEBUG(llvm::dbgs() << "Generating aie_cdo_enable.bin");
101 if (enableCores &&
102 failed(generateCDOBinary(
103 (llvm::Twine(workDirPath) + std::string(1, ps) +
104 targetOp.getSymName() + "_aie_cdo_enable.bin")
105 .str(),
106 [&ctl, &targetOp] { return ctl.addCoreEnable(targetOp); })))
107 return failure();
108
109 return success();
110}
111
112static LogicalResult generateCDOUnified(AIERTControl &ctl,
113 const StringRef workDirPath,
114 DeviceOp &targetOp, bool aieSim,
115 bool enableCores) {
116 auto ps = std::filesystem::path::preferred_separator;
117
118 return generateCDOBinary(
119 (llvm::Twine(workDirPath) + std::string(1, ps) + targetOp.getSymName() +
120 "_aie_cdo.bin")
121 .str(),
122 [&ctl, &targetOp, &workDirPath, &aieSim, &enableCores] {
123 if (!targetOp.getOps<CoreOp>().empty() &&
124 failed(ctl.addAieElfs(targetOp, workDirPath, aieSim)))
125 return failure();
126 if (failed(ctl.addInitConfig(targetOp)) ||
127 failed(ctl.addDmaActivation(targetOp)))
128 return failure();
129 if (enableCores && !targetOp.getOps<CoreOp>().empty() &&
130 failed(ctl.addCoreEnable(targetOp)))
131 return failure();
132 return success();
133 });
134}
135
136static LogicalResult
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) {
141
142 DeviceOp targetOp = AIE::DeviceOp::getForSymbolInModuleOrError(m, deviceName);
143 if (!targetOp) {
144 return failure();
145 }
146 const AIETargetModel &targetModel =
147 (const AIETargetModel &)targetOp.getTargetModel();
148
149 // things like XAIE_MEM_TILE_ROW_START and the missing
150 // shim dma on tile (0,0) are hard-coded assumptions about NPU...
151 assert(targetModel.hasProperty(AIETargetModel::IsNPU) &&
152 "Only NPU currently supported");
153 if (failed(AIERTControl::isSupported(targetOp, targetModel)))
154 return failure();
155
156 AIERTControl ctl(targetModel);
157 if (failed(ctl.setIOBackend(aieSim, xaieDebug)))
158 return failure();
159 initializeCDOGenerator(endianness, cdoDebug);
160
161 auto result = [&]() {
162 if (emitUnified) {
163 return generateCDOUnified(ctl, workDirPath, targetOp, aieSim,
164 enableCores);
165 }
166 return generateCDOBinariesSeparately(ctl, workDirPath, targetOp, aieSim,
167 enableCores);
168 }();
169 return result;
170}
171
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,
180 enableCores);
181}
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:341
static mlir::LogicalResult isSupported(mlir::Operation *op, const AIETargetModel &tm)
Diagnose, on op, a target this aie-rt lowering cannot configure.
Definition AIERT.cpp:269