Skip to content
39 changes: 39 additions & 0 deletions llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);
};
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -86,6 +93,38 @@ 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();
}
33 changes: 33 additions & 0 deletions llvm/test/CodeGen/NVPTX/nvptx-debug-symbol-name.ll
Original file line number Diff line number Diff line change
@@ -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.


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

define void @_Z10foo_kernelv() !dbg !20 {
entry:
call void @_Z6escapePKc(ptr noundef @__func__._Z10foo_kernelv) #2, !dbg !23
ret void, !dbg !24
}

declare void @_Z6escapePKc(ptr)

!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)