[llvm] [llvm-dwarfdump] Decode the virtual register names from the dwarf register numbers (PR #192353)

Scott Linder via llvm-commits llvm-commits at lists.llvm.org
Thu May 7 13:08:52 PDT 2026


================
@@ -23,6 +25,56 @@ namespace llvm {
 typedef DWARFExpression::Operation Op;
 typedef Op::Description Desc;
 
+/// Some backends (e.g. NVPTX) encode virtual register names as the DWARF
+/// register number: the ASCII bytes of the name are concatenated into a
+/// uint64_t (see NVPTXRegisterInfo::encodeRegisterForDwarf). When the object
+/// file is not the target backend, MCRegisterInfo cannot map these numbers, so
+/// recover the string for dumping.
+/// Returns true if the register name was decoded successfully, false otherwise.
+static bool decodeVirtualRegisterName(uint64_t DwarfRegNum,
+                                      SmallString<8> &Out) {
+  if (DwarfRegNum == 0)
+    return false;
+
+  uint64_t DwarfRegNumBE =
+      support::endian::byte_swap<uint64_t>(DwarfRegNum, endianness::big);
+  const char *Data = reinterpret_cast<const char *>(&DwarfRegNumBE);
+  const char *Begin = std::find_if(Data, Data + sizeof(DwarfRegNumBE),
+                                   [](char c) { return c != '\0'; });
+  SmallString<8> Tmp(Begin, Data + sizeof(DwarfRegNumBE));
+
+  if (Tmp.size() < 2)
+    return false;
+
+  if (!llvm::isAlnum(Tmp[0]) && Tmp[0] != '%')
+    return false;
+
+  for (size_t I = 1; I < Tmp.size(); ++I)
+    if (!llvm::isAlnum(Tmp[I]))
+      return false;
+
+  Out = Tmp;
+  return true;
+}
+
+/// Resolves a DWARF register number to a display name: first via \p
+/// GetNameForDWARFReg (MC register names), otherwise try decoding
+/// ASCII-encoded virtual register names (NVPTX-specific).
+/// Returns empty if neither applies.
+static std::string resolveRegName(
+    uint64_t DwarfRegNum, bool IsEH,
+    const std::function<StringRef(uint64_t, bool)> &GetNameForDWARFReg) {
+  if (GetNameForDWARFReg) {
+    StringRef R = GetNameForDWARFReg(DwarfRegNum, IsEH);
+    if (!R.empty())
+      return std::string(R);
+  }
+  SmallString<8> Decoded;
+  if (decodeVirtualRegisterName(DwarfRegNum, Decoded))
+    return std::string(Decoded);
+  return {};
----------------
slinder1 wrote:

nit: in general the codebase seems to prefer `""` here instead of calling the default constructor with the unified syntax

https://github.com/llvm/llvm-project/pull/192353


More information about the llvm-commits mailing list