11#include "llvm/ADT/STLExtras.h"
12#include "llvm/Support/ErrorOr.h"
13#include "llvm/Support/FileSystem.h"
14#include "llvm/Support/MemoryBuffer.h"
15#include "llvm/Support/Path.h"
16#include "llvm/Support/Program.h"
17#include "llvm/Support/raw_ostream.h"
27 if (StringRef(field.name).equals_insensitive(fieldName)) {
36llvm::SmallVector<std::string> getRegDBDirs() {
37 llvm::SmallVector<std::string> dirs;
38 if (
const char *installDir = std::getenv(
"MLIR_AIE_INSTALL_DIR")) {
39 llvm::SmallString<256> dir(installDir);
40 llvm::sys::path::append(dir,
"lib",
"regdb");
41 dirs.push_back(std::string(dir));
44 std::string mainExecutable = llvm::sys::fs::getMainExecutable(
45 nullptr,
reinterpret_cast<void *
>(&getRegDBDirs));
46 if (!mainExecutable.empty()) {
47 llvm::SmallString<256> dir(mainExecutable);
48 llvm::sys::path::remove_filename(dir);
49 llvm::sys::path::append(dir,
"..",
"lib",
"regdb");
50 llvm::sys::path::remove_dots(dir,
true);
51 dirs.push_back(std::string(dir));
57std::unique_ptr<RegisterDatabase> RegisterDatabase::load(StringRef registerFile,
64 for (
const std::string &dir : getRegDBDirs()) {
65 llvm::SmallString<256> registerPath(dir);
66 llvm::sys::path::append(registerPath, registerFile);
67 llvm::SmallString<256> eventPath(dir);
68 llvm::sys::path::append(eventPath,
"events_database.json");
69 if (!llvm::sys::fs::exists(registerPath) ||
70 !llvm::sys::fs::exists(eventPath))
74 std::string diagnostics;
75 llvm::raw_string_ostream diag(diagnostics);
76 auto db = std::unique_ptr<RegisterDatabase>(
new RegisterDatabase());
77 if (db->loadFromJSON(registerPath, eventPath, archKey, diag)) {
78 llvm::errs() << diag.str();
79 db->buildOffsetIndex();
82 failures += diag.str();
86 llvm::errs() <<
"Failed to locate AIE register database resources. "
87 <<
"Set MLIR_AIE_INSTALL_DIR to the "
88 <<
"MLIR-AIE installation path.\n";
90 llvm::errs() << failures;
95 return load(
"aie_registers_aie2.json",
"aie2");
99 return load(
"aie_registers_aie4.json",
"aie4");
102bool RegisterDatabase::loadFromJSON(StringRef registerPath, StringRef eventPath,
103 StringRef archKey, raw_ostream &diag) {
105 auto registerBuf = MemoryBuffer::getFile(registerPath);
107 diag <<
"Failed to load register database: " << registerPath <<
"\n";
111 auto registerJSON = json::parse(registerBuf.get()->getBuffer());
113 diag <<
"Failed to parse register JSON\n";
118 auto *root = registerJSON->getAsObject();
122 auto *modules = root->getObject(
"modules");
127 for (
const auto &modulePair : *modules) {
128 StringRef moduleName = modulePair.first;
129 auto *moduleObj = modulePair.second.getAsObject();
133 auto *registers = moduleObj->getArray(
"registers");
138 for (
const auto ®Val : *registers) {
139 auto *regObj = regVal.getAsObject();
144 regInfo.
module = moduleName.str();
147 if (
auto name = regObj->getString(
"name"))
148 regInfo.
name = name->str();
149 if (
auto offset = regObj->getString(
"offset")) {
151 StringRef offsetStr = *offset;
153 if (offsetStr.starts_with(
"0x") || offsetStr.starts_with(
"0X"))
154 offsetStr = offsetStr.drop_front(2);
157 if (offsetStr.getAsInteger(16, offsetVal)) {
158 diag <<
"Warning: Failed to parse offset '" << *offset
159 <<
"' for register " << regInfo.
name <<
"\n";
161 regInfo.
offset =
static_cast<uint32_t
>(offsetVal);
164 if (
auto width = regObj->getInteger(
"width"))
165 regInfo.
width =
static_cast<uint32_t
>(*width);
166 if (
auto type = regObj->getString(
"type"))
167 regInfo.
type = type->str();
168 if (
auto reset = regObj->getString(
"reset"))
169 regInfo.
reset = reset->str();
170 if (
auto desc = regObj->getString(
"description"))
174 if (
auto *fields = regObj->getArray(
"bit_fields")) {
175 for (
const auto &fieldVal : *fields) {
176 auto *fieldObj = fieldVal.getAsObject();
180 BitFieldInfo fieldInfo;
181 if (
auto name = fieldObj->getString(
"name"))
182 fieldInfo.
name = name->str();
183 if (
auto *bitRange = fieldObj->getArray(
"bit_range")) {
184 if (bitRange->size() == 2) {
185 if (
auto start = (*bitRange)[0].getAsInteger())
186 fieldInfo.
bit_start =
static_cast<uint32_t
>(*start);
187 if (
auto end = (*bitRange)[1].getAsInteger())
188 fieldInfo.
bit_end =
static_cast<uint32_t
>(*end);
191 if (
auto type = fieldObj->getString(
"type"))
192 fieldInfo.
type = type->str();
193 if (
auto reset = fieldObj->getString(
"reset"))
194 fieldInfo.
reset = reset->str();
195 if (
auto desc = fieldObj->getString(
"description"))
204 std::string key = moduleName.lower() +
"::" + regInfo.
name;
205 llvm::transform(key, key.begin(), ::tolower);
206 registers_[key] = regInfo;
211 auto eventBuf = MemoryBuffer::getFile(eventPath);
213 diag <<
"Failed to load event database: " << eventPath <<
"\n";
217 auto eventJSON = json::parse(eventBuf.get()->getBuffer());
219 diag <<
"Failed to parse event JSON\n";
224 auto *eventRoot = eventJSON->getAsObject();
228 auto *arch = eventRoot->getObject(archKey);
230 diag <<
"Failed to find '" << archKey
231 <<
"' architecture in event database\n";
235 auto *eventModules = arch->getObject(
"modules");
240 for (
const auto &modulePair : *eventModules) {
241 StringRef moduleName = modulePair.first;
242 auto *events = modulePair.second.getAsArray();
247 for (
const auto &eventVal : *events) {
248 auto *eventObj = eventVal.getAsObject();
253 eventInfo.
module = moduleName.str();
255 if (
auto name = eventObj->getString(
"name"))
256 eventInfo.
name = name->str();
257 if (
auto number = eventObj->getInteger(
"number"))
258 eventInfo.
number =
static_cast<uint32_t
>(*number);
261 std::string key = moduleName.str() +
"::" + eventInfo.
name;
262 llvm::transform(key, key.begin(), ::tolower);
263 events_[key] = eventInfo;
271 StringRef module)
const {
272 std::string key =
module.str() + "::" + name.str();
273 llvm::transform(key, key.begin(), ::tolower);
274 auto it = registers_.find(key);
275 return it != registers_.end() ? &it->second :
nullptr;
278void RegisterDatabase::buildOffsetIndex() {
279 registersByOffset_.clear();
280 for (
const auto &entry : registers_) {
282 std::string moduleKey = info.
module;
283 llvm::transform(moduleKey, moduleKey.begin(), ::tolower);
284 registersByOffset_[moduleKey][info.
offset] = &info;
290 StringRef module)
const {
291 std::string moduleKey =
module.lower();
292 auto modIt = registersByOffset_.find(moduleKey);
293 if (modIt == registersByOffset_.end())
295 auto offIt = modIt->second.find(offset);
296 if (offIt == modIt->second.end())
298 return offIt->second;
302 StringRef module)
const {
303 std::string key =
module.str() + "::" + name.str();
304 llvm::transform(key, key.begin(), ::tolower);
305 auto it = events_.find(key);
306 if (it != events_.end()) {
307 return it->second.number;
312std::optional<uint32_t>
314 uint32_t value)
const {
320 uint64_t maxValue = width >= 32 ? std::numeric_limits<uint32_t>::max()
321 : ((1ULL << width) - 1ULL);
323 if (value > maxValue)
326 value &=
static_cast<uint32_t
>(maxValue);
329 uint64_t encoded = (
static_cast<uint64_t
>(value) << field.
bit_start);
330 return static_cast<uint32_t
>(encoded);
std::optional< uint32_t > encodeFieldValue(const BitFieldInfo &field, uint32_t value) const
Encode a value for a specific bitfield.
static std::unique_ptr< RegisterDatabase > loadAIE2()
Load database for AIE2 architecture.
static std::unique_ptr< RegisterDatabase > loadAIE4()
Load database for AIE4 architecture.
const RegisterInfo * lookupRegister(llvm::StringRef name, llvm::StringRef module) const
Lookup register by name and module.
std::optional< uint32_t > lookupEvent(llvm::StringRef name, llvm::StringRef module) const
Lookup event by name and module.
const RegisterInfo * lookupRegisterByOffset(uint32_t offset, llvm::StringRef module) const
Lookup register by tile-relative offset and module name.
Bit field information for a register.
uint32_t getWidth() const
const BitFieldInfo * getField(llvm::StringRef fieldName) const
Lookup a bit field by name, case-insensitively.
std::vector< BitFieldInfo > bit_fields