MLIR-AIE 1.4.5.dev
AIERegisterDatabase.h
Go to the documentation of this file.
1//===- AIERegisterDatabase.h ------------------------------------*- C++ -*-===//
2//
3// Copyright (C) 2025 Advanced Micro Devices, Inc.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7// Register and event database for AIE trace configuration
8//===----------------------------------------------------------------------===//
9
10#ifndef AIE_REGISTER_DATABASE_H
11#define AIE_REGISTER_DATABASE_H
12
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/StringMap.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/Support/JSON.h"
17
18#include <memory>
19#include <optional>
20#include <string>
21#include <vector>
22
23namespace xilinx {
24namespace AIE {
25
26/// Bit field information for a register
28 std::string name;
29 uint32_t bit_start; // LSB position
30 uint32_t bit_end; // MSB position
31 std::string type;
32 std::string reset;
33 std::string description;
34
35 uint32_t getWidth() const { return bit_end - bit_start + 1; }
36};
37
38/// Register information
40 std::string name;
41 uint32_t offset;
42 std::string module;
43 uint32_t width;
44 std::string type;
45 std::string reset;
46 std::string description;
47 std::vector<BitFieldInfo> bit_fields;
48
49 const BitFieldInfo *getField(llvm::StringRef fieldName) const;
50};
51
52/// Event information
53struct EventInfo {
54 std::string name;
55 uint32_t number;
56 std::string module; // core, memory, pl, mem_tile
57};
58
59/// Register and event database for a specific architecture
60class RegisterDatabase {
61public:
62 /// Load database for AIE2 architecture
63 static std::unique_ptr<RegisterDatabase> loadAIE2();
64
65 /// Lookup register by name and module
66 const RegisterInfo *lookupRegister(llvm::StringRef name,
67 llvm::StringRef module) const;
68
69 /// Lookup register by tile-relative offset and module name. Used by the
70 /// transaction-binary locmap to decorate each transaction word with its
71 /// semantic register name. Module names are the JSON keys in
72 /// aie_registers_aie2.json: "core", "memory", "memory_tile", "shim".
73 /// Returns nullptr if no register matches.
74 const RegisterInfo *lookupRegisterByOffset(uint32_t offset,
75 llvm::StringRef module) const;
76
77 /// Lookup event by name and module
78 std::optional<uint32_t> lookupEvent(llvm::StringRef name,
79 llvm::StringRef module) const;
80
81 /// Encode a value for a specific bitfield.
82 /// Return nullopt if value does not fit in the field's bit width.
83 std::optional<uint32_t> encodeFieldValue(const BitFieldInfo &field,
84 uint32_t value) const;
85
86private:
87 RegisterDatabase() = default;
88
89 bool loadFromJSON(llvm::StringRef registerPath, llvm::StringRef eventPath);
90
91 // Build the offset-keyed reverse index from `registers_`. Called once at
92 // load time after `loadFromJSON` finishes populating `registers_`.
93 void buildOffsetIndex();
94
95 llvm::StringMap<RegisterInfo> registers_;
96 llvm::StringMap<EventInfo> events_;
97 // (lowercased module name) -> (offset -> *RegisterInfo). Pointers into
98 // entries owned by `registers_`; safe because `registers_` is never
99 // mutated after construction.
100 llvm::StringMap<llvm::DenseMap<uint32_t, const RegisterInfo *>>
101 registersByOffset_;
102};
103
104} // namespace AIE
105} // namespace xilinx
106
107#endif // AIE_REGISTER_DATABASE_H
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.
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.
const BitFieldInfo * getField(llvm::StringRef fieldName) const
std::vector< BitFieldInfo > bit_fields