MLIR-AIE 1.4.6.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 /// Lookup a bit field by name, case-insensitively.
50 const BitFieldInfo *getField(llvm::StringRef fieldName) const;
51};
52
53/// Event information
54struct EventInfo {
55 std::string name;
56 uint32_t number;
57 std::string module; // core, memory, pl, mem_tile
58};
59
60/// Register and event database for a specific architecture
61class RegisterDatabase {
62public:
63 /// Load database for AIE2 architecture
64 static std::unique_ptr<RegisterDatabase> loadAIE2();
65
66 /// Load database for AIE4 architecture
67 static std::unique_ptr<RegisterDatabase> loadAIE4();
68
69 /// Lookup register by name and module
70 const RegisterInfo *lookupRegister(llvm::StringRef name,
71 llvm::StringRef module) const;
72
73 /// Lookup register by tile-relative offset and module name. Used by the
74 /// transaction-binary locmap to decorate each transaction word with its
75 /// semantic register name. Module names are the JSON keys in
76 /// aie_registers_<arch>.json: "core", "memory", "memory_tile", "shim".
77 /// Returns nullptr if no register matches.
78 const RegisterInfo *lookupRegisterByOffset(uint32_t offset,
79 llvm::StringRef module) const;
80
81 /// Lookup event by name and module
82 std::optional<uint32_t> lookupEvent(llvm::StringRef name,
83 llvm::StringRef module) const;
84
85 /// Encode a value for a specific bitfield.
86 /// Return nullopt if value does not fit in the field's bit width.
87 std::optional<uint32_t> encodeFieldValue(const BitFieldInfo &field,
88 uint32_t value) const;
89
90private:
91 RegisterDatabase() = default;
92
93 /// Load `registerFile` and the `archKey` section of the event database from
94 /// the register database directory.
95 static std::unique_ptr<RegisterDatabase> load(llvm::StringRef registerFile,
96 llvm::StringRef archKey);
97
98 bool loadFromJSON(llvm::StringRef registerPath, llvm::StringRef eventPath,
99 llvm::StringRef archKey, llvm::raw_ostream &diag);
100
101 // Build the offset-keyed reverse index from `registers_`. Called once at
102 // load time after `loadFromJSON` finishes populating `registers_`.
103 void buildOffsetIndex();
104
105 llvm::StringMap<RegisterInfo> registers_;
106 llvm::StringMap<EventInfo> events_;
107 // (lowercased module name) -> (offset -> *RegisterInfo). Pointers into
108 // entries owned by `registers_`; safe because `registers_` is never
109 // mutated after construction.
110 llvm::StringMap<llvm::DenseMap<uint32_t, const RegisterInfo *>>
111 registersByOffset_;
112};
113
114} // namespace AIE
115} // namespace xilinx
116
117#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.
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.
const BitFieldInfo * getField(llvm::StringRef fieldName) const
Lookup a bit field by name, case-insensitively.
std::vector< BitFieldInfo > bit_fields