-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be 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 If you have received no comments on your PR for a week, you can request a review 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. |
@llvm/pr-subscribers-backend-nvptx Author: Vyom Sharma (vyom1611) ChangesBased on issue #101819 Full diff: https://github.com/llvm/llvm-project/pull/101891.diff 1 Files Affected:
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();
}
|
Please add a lit test that exercises this change. |
bdd58eb
to
52d2722
Compare
@enferex clang-format has been done on the changed file |
Thanks @vyom1611, can you please provide a lit test. |
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. |
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 |
Here's a small reproducer: https://godbolt.org/z/3jWrKWeEM |
There was a problem hiding this 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.
Co-authored-by: Joseph Huber <[email protected]>
Co-authored-by: Joseph Huber <[email protected]>
@@ -0,0 +1,33 @@ | |||
; RUN: llc -mtriple=nvptx64-nvidia-cuda -mcpu=sm_86 < %s | FileCheck %s | |||
|
|||
; CHECK: .global .align 1 .b8 __func___$__Z10foo_kernelv |
There was a problem hiding this comment.
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.
Co-authored-by: Joseph Huber <[email protected]>
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 |
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. |
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. |
What's the status of this? |
Based on issue #101819