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

Vyom Sharma via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 18 05:30:31 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/4] [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 9f5a217795183b..b687e6ceba5046 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/4] 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 b687e6ceba5046..06596fab539b56 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 0e6deea43ff0b3b8dc265b7fe1418af876d26025 Mon Sep 17 00:00:00 2001
From: vyom1611 <sharm843 at umn.edu>
Date: Sun, 18 Aug 2024 12:01:56 +0530
Subject: [PATCH 3/4] Added small unit test for symbol checking

---
 .../CodeGen/NVPTX/nvptx-debug-symbol-name.ll  | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100644 llvm/test/CodeGen/NVPTX/nvptx-debug-symbol-name.ll

diff --git a/llvm/test/CodeGen/NVPTX/nvptx-debug-symbol-name.ll b/llvm/test/CodeGen/NVPTX/nvptx-debug-symbol-name.ll
new file mode 100644
index 00000000000000..60a0065de9605d
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/nvptx-debug-symbol-name.ll
@@ -0,0 +1,42 @@
+; RUN: llc -mtriple=nvptx64-nvidia-cuda -mcpu=sm_86 < %s | FileCheck %s
+
+; CHECK: .global .align 1 .b8 __func___$__Z10foo_kernelv
+
+; ModuleID = 'test_module'
+source_filename = "test_module.cu"
+target datalayout = "e-i64:64-i128:128-v16:16-v32:32-n16:32:64"
+target triple = "nvptx64-nvidia-cuda"
+
+ at __func__._Z10foo_kernelv = private unnamed_addr constant [11 x i8] c"foo_kernel\00", align 1, !dbg !0
+
+define dso_local void @_Z10foo_kernelv() #0 !dbg !20 {
+entry:
+  call void @_Z6escapePKc(ptr noundef @__func__._Z10foo_kernelv) #2, !dbg !23
+  ret void, !dbg !24
+}
+
+declare dso_local void @_Z6escapePKc(ptr noundef)
+
+attributes #0 = { noinline nounwind optnone "target-cpu"="sm_86" }
+attributes #1 = { nounwind }
+attributes #2 = { nounwind }
+
+!llvm.dbg.cu = !{!14}
+!llvm.ident = !{!18}
+
+!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+!1 = distinct !DIGlobalVariable(scope: null, file: !2, name: "__func__", linkageName: "__func__._Z10foo_kernelv", line: 6, type: !3, isLocal: true, isDefinition: true)
+!2 = !DIFile(filename: "test_module.cu", directory: "/")
+!3 = !DICompositeType(tag: DW_TAG_array_type, baseType: !4, size: 88, elements: !6)
+!4 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !5)
+!5 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
+!6 = !{!7}
+!7 = !DISubrange(count: 11)
+!14 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !2, producer: "clang version 20.0.0git", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !16, nameTableKind: None)
+!16 = !{!0}
+!18 = !{!"clang version 20.0.0git"}
+!20 = distinct !DISubprogram(name: "foo_kernel", linkageName: "_Z10foo_kernelv", scope: !2, file: !2, line: 4, type: !21, scopeLine: 5, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !14)
+!21 = !DISubroutineType(types: !22)
+!22 = !{null}
+!23 = !DILocation(line: 6, column: 5, scope: !20)
+!24 = !DILocation(line: 7, column: 1, scope: !20)

>From a393f5e42152d49589f4841d5525b2000cdee5c3 Mon Sep 17 00:00:00 2001
From: Vyom Sharma <93402393+vyom1611 at users.noreply.github.com>
Date: Sun, 18 Aug 2024 18:00:23 +0530
Subject: [PATCH 4/4] Update nvptx-debug-symbol-name.ll

Co-authored-by: Joseph Huber <huberjn at outlook.com>
---
 llvm/test/CodeGen/NVPTX/nvptx-debug-symbol-name.ll | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/llvm/test/CodeGen/NVPTX/nvptx-debug-symbol-name.ll b/llvm/test/CodeGen/NVPTX/nvptx-debug-symbol-name.ll
index 60a0065de9605d..c16d54f3224028 100644
--- a/llvm/test/CodeGen/NVPTX/nvptx-debug-symbol-name.ll
+++ b/llvm/test/CodeGen/NVPTX/nvptx-debug-symbol-name.ll
@@ -2,11 +2,6 @@
 
 ; CHECK: .global .align 1 .b8 __func___$__Z10foo_kernelv
 
-; ModuleID = 'test_module'
-source_filename = "test_module.cu"
-target datalayout = "e-i64:64-i128:128-v16:16-v32:32-n16:32:64"
-target triple = "nvptx64-nvidia-cuda"
-
 @__func__._Z10foo_kernelv = private unnamed_addr constant [11 x i8] c"foo_kernel\00", align 1, !dbg !0
 
 define dso_local void @_Z10foo_kernelv() #0 !dbg !20 {



More information about the llvm-commits mailing list