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

Michael Buch via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 17 02:27:34 PDT 2026


================
@@ -23,6 +24,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.
+static bool decodeVirtualRegisterName(uint64_t DwarfRegNum,
+                                      SmallString<32> &Out) {
+  if (DwarfRegNum == 0)
+    return false;
+
+  SmallString<32> Tmp;
+  for (uint64_t V = DwarfRegNum; V; V >>= 8)
+    Tmp.push_back(static_cast<char>(V & 0xFF));
+
+  // Encoding builds the uint64_t as (result << 8) | byte per character (MSB of
+  // the value is the first character). Unpacking with >>= 8 yields bytes LSB
+  // first, so reverse to restore the original register name (e.g. %rd2).
+  std::reverse(Tmp.begin(), Tmp.end());
+
+  if (Tmp.empty() || 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;
+}
+
+/// \p DecodedScratch must stay alive as long as the returned \p StringRef is
+/// used (it points into \p DecodedScratch when the virtual-register decode
+/// path is taken).
+static StringRef resolveRegName(
+    uint64_t DwarfRegNum, bool IsEH,
+    const std::function<StringRef(uint64_t, bool)> &GetNameForDWARFReg,
----------------
Michael137 wrote:

Nit: could make this an `llvm::function_ref`

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


More information about the llvm-commits mailing list