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

Vyom Sharma via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 4 07:54:18 PDT 2024


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

Based on issue #101819

>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] [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();
 }



More information about the llvm-commits mailing list