Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NVPTX] Fixing debug symbols for ptx target emitting #101891

Closed
wants to merge 11 commits into from

Conversation

vyom1611
Copy link

@vyom1611 vyom1611 commented Aug 4, 2024

Based on issue #101819

Copy link

github-actions bot commented Aug 4, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Aug 4, 2024

@llvm/pr-subscribers-backend-nvptx

Author: Vyom Sharma (vyom1611)

Changes

Based on issue #101819


Full diff: https://github.com/llvm/llvm-project/pull/101891.diff

1 Files Affected:

  • (modified) llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp (+47)
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();
 }

@enferex
Copy link
Contributor

enferex commented Aug 5, 2024

Please add a lit test that exercises this change.

@vyom1611 vyom1611 force-pushed the nvptx-symbol-transform branch from bdd58eb to 52d2722 Compare August 7, 2024 09:40
@vyom1611
Copy link
Author

@enferex clang-format has been done on the changed file

@enferex
Copy link
Contributor

enferex commented Aug 10, 2024

Thanks @vyom1611, can you please provide a lit test.

@vyom1611
Copy link
Author

Hey, sorry I thought you said lint test. BTW, when you say add a lit test, do I need to create another test in the test directory for this change? Or just run the lit test and show if all the tests pass. Sorry I have never contributed to llvm and I do not know what the usual workflow is.

@enferex
Copy link
Contributor

enferex commented Aug 10, 2024

Hey, sorry I thought you said lint test. BTW, when you say add a lit test, do I need to create another test in the test directory for this change? Or just run the lit test and show if all the tests pass. Sorry I have never contributed to llvm and I do not know what the usual workflow is.

No problem. Yes, I'd like to see a lit test. This will help myself and others better understand the behavior of your change, as well as prevent regressions if this change is approved. The LLVM docs (link) contain a guide for submitting patches.

@jhuber6 jhuber6 requested review from jlebar and Artem-B August 14, 2024 19:03
@jhuber6
Copy link
Contributor

jhuber6 commented Aug 14, 2024

Hey, sorry I thought you said lint test. BTW, when you say add a lit test, do I need to create another test in the test directory for this change? Or just run the lit test and show if all the tests pass. Sorry I have never contributed to llvm and I do not know what the usual workflow is.

You want to create some minimal LLVM-IR that reproduces the issue, checking the output of the failing test case is a good start https://godbolt.org/z/aYh8WGrj3. Look at llvm/test/CodeGen/NVPTX for similar tests. These will run llc on the IR to turn it into PTX. The test should verify that the debug symbol's name matches the global string.

@Artem-B
Copy link
Member

Artem-B commented Aug 14, 2024

Here's a small reproducer: https://godbolt.org/z/3jWrKWeEM

Copy link
Member

@Artem-B Artem-B left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM in principle, Should be good to go, once we have the test.

@vyom1611
Copy link
Author

Unit test passes
Screenshot 2024-08-18 at 12 03 37 PM

vyom1611 and others added 2 commits August 18, 2024 18:00
@@ -0,0 +1,33 @@
; RUN: llc -mtriple=nvptx64-nvidia-cuda -mcpu=sm_86 < %s | FileCheck %s

; CHECK: .global .align 1 .b8 __func___$__Z10foo_kernelv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a check for the debug line.

@jhuber6
Copy link
Contributor

jhuber6 commented Aug 18, 2024

Messed around with this locally,

; RUN: llc -mtriple=nvptx64-nvidia-cuda -mcpu=sm_86 < %s | FileCheck %s
; RUN: %if ptxas %{ llc < %s -mtriple=nvptx64-nvidia-cuda -mcpu=sm_86 -verify-machineinstrs | %ptxas-verify %}

; CHECK: .global .align 1 .b8 __func___$kernel
; CHECK: .b64 __func__$kernel

@__func__.kernel = private constant [11 x i8] c"foo_kernel\00", align 1, !dbg !0

define void @kernel() !dbg !18 {
  call void @escape(ptr noundef @__func__.kernel), !dbg !21
  ret void, !dbg !22
}

declare void @escape(ptr noundef)

!llvm.module.flags = !{!8, !9, !10, !11, !12, !13}
!llvm.dbg.cu = !{!14}
!nvvm.annotations = !{!17}

!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(scope: null, file: !2, line: 6, type: !3, isLocal: true, isDefinition: true)
!2 = !DIFile(filename: "example.cu", directory: "/app")
!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)
!8 = !{i32 2, !"SDK Version", [2 x i32] [i32 11, i32 3]}
!9 = !{i32 7, !"Dwarf Version", i32 2}
!10 = !{i32 2, !"Debug Info Version", i32 3}
!11 = !{i32 1, !"wchar_size", i32 4}
!12 = !{i32 4, !"nvvm-reflect-ftz", i32 0}
!13 = !{i32 7, !"frame-pointer", i32 2}
!14 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !15, producer: "clang version 20.0.0git (https://github.com/llvm/llvm-project.git 69f76c782b554a004078af6909c19a11e3846415)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !16, splitDebugInlining: false, nameTableKind: None)
!15 = !DIFile(filename: "/app/example.cu", directory: "/app")
!16 = !{!0}
!17 = !{ptr @kernel, !"kernel", i32 1}
!18 = distinct !DISubprogram(name: "foo_kernel", linkageName: "kernel", scope: !2, file: !2, line: 4, type: !19, scopeLine: 5, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !14)
!19 = !DISubroutineType(types: !20)
!20 = !{null}
!21 = !DILocation(line: 6, column: 5, scope: !18)
!22 = !DILocation(line: 7, column: 1, scope: !18)

This test didn't seem to have the expected result, as the debug string still had the . in it.

@vyom1611
Copy link
Author

Hey, is the last committed test producing the correct symbols? Or is it a problem with the pass logic itself? Please let me know and I can update accordingly, and get this PR ready to be merged

@jhuber6
Copy link
Contributor

jhuber6 commented Aug 25, 2024

Hey, is the last committed test producing the correct symbols? Or is it a problem with the pass logic itself? Please let me know and I can update accordingly, and get this PR ready to be merged

The test file I provided should pass, I don't think it does right now.

@vyom1611
Copy link
Author

Since you already committed, the updated test file should be in the PR codebase right? Is it good to merge?

@jhuber6
Copy link
Contributor

jhuber6 commented Aug 31, 2024

Since you already committed, the updated test file should be in the PR codebase right? Is it good to merge?

The bit I updated was to check for the pass changing the debug symbol's name. This failed the CI and didn't work when I applied the patch manually, so you probably need to rework the pass and make sure it passes the test I posted above.

@jhuber6
Copy link
Contributor

jhuber6 commented Oct 27, 2024

What's the status of this?

@Artem-B
Copy link
Member

Artem-B commented Oct 28, 2024

Is it still needed after #113216 which laded in 020fa86?

@jhuber6
Copy link
Contributor

jhuber6 commented Oct 28, 2024

Is it still needed after #113216 which laded in 020fa86?

Godbolt doesn't error, so I'd say yes.

@jhuber6 jhuber6 closed this Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants