[llvm] [NVPTX] Fixing debug symbols for ptx target emitting (PR #101891)

Vyom Sharma via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 7 02:30:42 PDT 2024


https://github.com/vyom1611 updated https://github.com/llvm/llvm-project/pull/101891

>From 0c1eb6c9cef13b3d02d0b5617722ce1ebcad1465 Mon Sep 17 00:00:00 2001
From: vyom1611 <sharm843 at umn.edu>
Date: Sun, 4 Aug 2024 20:23:07 +0530
Subject: [PATCH 1/3] [NVPTX] Fixing debug symbols for ptx target emitting

---
 .../NVPTX/NVPTXAssignValidGlobalNames.cpp     | 47 +++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp b/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
index 9f5a217795183..b687e6ceba504 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
@@ -18,6 +18,7 @@
 
 #include "NVPTX.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/GlobalVariable.h"
 #include "llvm/IR/LegacyPassManager.h"
@@ -38,6 +39,9 @@ class NVPTXAssignValidGlobalNames : public ModulePass {
 
   /// Clean up the name to remove symbols invalid in PTX.
   std::string cleanUpName(StringRef Name);
+  
+  /// Clean up the debug symbols.
+  void cleanUpDebugSymbols(Module &M);
 };
 }
 
@@ -67,6 +71,9 @@ bool NVPTXAssignValidGlobalNames::runOnModule(Module &M) {
     if (F.hasLocalLinkage())
       F.setName(cleanUpName(F.getName()));
 
+  // Clean up the debug symbols.
+  cleanUpDebugSymbols(M);
+
   return true;
 }
 
@@ -86,6 +93,46 @@ std::string NVPTXAssignValidGlobalNames::cleanUpName(StringRef Name) {
   return ValidNameStream.str();
 }
 
+void NVPTXAssignValidGlobalNames::cleanUpDebugSymbols(Module &M) {
+  LLVMContext &Ctx = M.getContext();
+  
+  for (Function &F : M.functions()) {
+    if (DISubprogram *SP = F.getSubprogram()) {
+      auto CleanedName = cleanUpName(SP->getLinkageName());
+      if (!CleanedName.empty()) {
+        SP->replaceLinkageName(MDString::get(Ctx, CleanedName));
+      }
+    }
+  }
+
+  for (GlobalVariable &GV : M.globals()) {
+    SmallVector<DIGlobalVariableExpression *, 1> GVs;
+    GV.getDebugInfo(GVs);
+    for (auto *GVE : GVs) {
+      DIGlobalVariable *GVMD = GVE->getVariable();
+      auto CleanedName = cleanUpName(GVMD->getLinkageName());
+      if (!CleanedName.empty()) {
+        DIGlobalVariable *NewGVMD = DIGlobalVariable::get(
+          Ctx,
+          GVMD->getScope(),
+          GVMD->getName(),
+          CleanedName, // Use the cleaned name as StringRef
+          GVMD->getFile(),
+          GVMD->getLine(),
+          GVMD->getType(),
+          GVMD->isLocalToUnit(),
+          GVMD->isDefinition(),
+          GVMD->getStaticDataMemberDeclaration(),
+          GVMD->getTemplateParams(),
+          GVMD->getAlignInBits(),
+          GVMD->getAnnotations()
+        );
+        GVMD->replaceAllUsesWith(NewGVMD);
+      }
+    }
+  }
+}
+
 ModulePass *llvm::createNVPTXAssignValidGlobalNamesPass() {
   return new NVPTXAssignValidGlobalNames();
 }

>From 64e683ae02d3c601fc7fc3401873eeb8893c8a1d Mon Sep 17 00:00:00 2001
From: vyom1611 <sharm843 at umn.edu>
Date: Wed, 7 Aug 2024 11:12:25 +0530
Subject: [PATCH 2/3] clang-format linting

---
 .../NVPTX/NVPTXAssignValidGlobalNames.cpp     | 24 +++++++------------
 1 file changed, 8 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp b/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
index b687e6ceba504..06596fab539b5 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
@@ -39,7 +39,7 @@ class NVPTXAssignValidGlobalNames : public ModulePass {
 
   /// Clean up the name to remove symbols invalid in PTX.
   std::string cleanUpName(StringRef Name);
-  
+
   /// Clean up the debug symbols.
   void cleanUpDebugSymbols(Module &M);
 };
@@ -95,7 +95,7 @@ std::string NVPTXAssignValidGlobalNames::cleanUpName(StringRef Name) {
 
 void NVPTXAssignValidGlobalNames::cleanUpDebugSymbols(Module &M) {
   LLVMContext &Ctx = M.getContext();
-  
+
   for (Function &F : M.functions()) {
     if (DISubprogram *SP = F.getSubprogram()) {
       auto CleanedName = cleanUpName(SP->getLinkageName());
@@ -113,20 +113,12 @@ void NVPTXAssignValidGlobalNames::cleanUpDebugSymbols(Module &M) {
       auto CleanedName = cleanUpName(GVMD->getLinkageName());
       if (!CleanedName.empty()) {
         DIGlobalVariable *NewGVMD = DIGlobalVariable::get(
-          Ctx,
-          GVMD->getScope(),
-          GVMD->getName(),
-          CleanedName, // Use the cleaned name as StringRef
-          GVMD->getFile(),
-          GVMD->getLine(),
-          GVMD->getType(),
-          GVMD->isLocalToUnit(),
-          GVMD->isDefinition(),
-          GVMD->getStaticDataMemberDeclaration(),
-          GVMD->getTemplateParams(),
-          GVMD->getAlignInBits(),
-          GVMD->getAnnotations()
-        );
+            Ctx, GVMD->getScope(), GVMD->getName(),
+            CleanedName, // Use the cleaned name as StringRef
+            GVMD->getFile(), GVMD->getLine(), GVMD->getType(),
+            GVMD->isLocalToUnit(), GVMD->isDefinition(),
+            GVMD->getStaticDataMemberDeclaration(), GVMD->getTemplateParams(),
+            GVMD->getAlignInBits(), GVMD->getAnnotations());
         GVMD->replaceAllUsesWith(NewGVMD);
       }
     }

>From be3e9e7ea4640ffce276a4a8bb4f52362f4913d1 Mon Sep 17 00:00:00 2001
From: vyom1611 <sharm843 at umn.edu>
Date: Wed, 7 Aug 2024 15:00:17 +0530
Subject: [PATCH 3/3] [TOOLS][opt-viewer] fixed error for demangler_lock and
 string formatting

---
 llvm/tools/opt-viewer/opt-viewer.py | 2 +-
 llvm/tools/opt-viewer/optrecord.py  | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/llvm/tools/opt-viewer/opt-viewer.py b/llvm/tools/opt-viewer/opt-viewer.py
index c9c7db726f765..2d32d66bbe3ae 100755
--- a/llvm/tools/opt-viewer/opt-viewer.py
+++ b/llvm/tools/opt-viewer/opt-viewer.py
@@ -135,7 +135,7 @@ def render_inline_remarks(self, r, line):
         # Column is the number of characters *including* tabs, keep those and
         # replace everything else with spaces.
         indent = line[: max(r.Column, 1) - 1]
-        indent = re.sub("\S", " ", indent)
+        indent = re.sub(r"\S", " ", indent)
 
         # Create expanded message and link if we have a multiline message.
         lines = r.message.split("\n")
diff --git a/llvm/tools/opt-viewer/optrecord.py b/llvm/tools/opt-viewer/optrecord.py
index 9e2fc7cb553b5..9b20562e803cb 100644
--- a/llvm/tools/opt-viewer/optrecord.py
+++ b/llvm/tools/opt-viewer/optrecord.py
@@ -74,6 +74,8 @@ def set_demangler(cls, demangler):
 
     @classmethod
     def demangle(cls, name):
+        if cls.demangler_proc is None:
+            return name  # Return the original name if demangler is not available
         with cls.demangler_lock:
             cls.demangler_proc.stdin.write((name + "\n").encode("utf-8"))
             cls.demangler_proc.stdin.flush()
@@ -299,7 +301,7 @@ def get_remarks(input_file, filter_=None):
             filter_e = re.compile(filter_)
         for remark in docs:
             remark.canonicalize()
-            # Avoid remarks withoug debug location or if they are duplicated
+            # Avoid remarks without debug location or if they are duplicated
             if not hasattr(remark, "DebugLoc") or remark.key in all_remarks:
                 continue
 



More information about the llvm-commits mailing list