Skip to content

Commit 65a921d

Browse files
teresajohnsongithub-actions[bot]
authored andcommitted
Automerge: [MemProf] Update the declaration DISubprogram linkageName for clones (#149864)
Follow up to PR145385 to also update the linkageName on any separate DISubprogram for the clone function declaration.
2 parents 1098681 + 0e42c66 commit 65a921d

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3998,6 +3998,24 @@ void IndexCallsiteContextGraph::updateCall(CallInfo &CallerCall,
39983998
CI->Clones[CallerCall.cloneNo()] = CalleeFunc.cloneNo();
39993999
}
40004000

4001+
// Update the debug information attached to NewFunc to use the clone Name. Note
4002+
// this needs to be done for both any existing DISubprogram for the definition,
4003+
// as well as any separate declaration DISubprogram.
4004+
static void updateSubprogramLinkageName(Function *NewFunc, StringRef Name) {
4005+
assert(Name == NewFunc->getName());
4006+
auto *SP = NewFunc->getSubprogram();
4007+
if (!SP)
4008+
return;
4009+
auto *MDName = MDString::get(NewFunc->getParent()->getContext(), Name);
4010+
SP->replaceLinkageName(MDName);
4011+
DISubprogram *Decl = SP->getDeclaration();
4012+
if (!Decl)
4013+
return;
4014+
TempDISubprogram NewDecl = Decl->clone();
4015+
NewDecl->replaceLinkageName(MDName);
4016+
SP->replaceDeclaration(MDNode::replaceWithUniqued(std::move(NewDecl)));
4017+
}
4018+
40014019
CallsiteContextGraph<ModuleCallsiteContextGraph, Function,
40024020
Instruction *>::FuncInfo
40034021
ModuleCallsiteContextGraph::cloneFunctionForCallsite(
@@ -4009,9 +4027,7 @@ ModuleCallsiteContextGraph::cloneFunctionForCallsite(
40094027
std::string Name = getMemProfFuncName(Func.func()->getName(), CloneNo);
40104028
assert(!Func.func()->getParent()->getFunction(Name));
40114029
NewFunc->setName(Name);
4012-
if (auto *SP = NewFunc->getSubprogram())
4013-
SP->replaceLinkageName(
4014-
MDString::get(NewFunc->getParent()->getContext(), Name));
4030+
updateSubprogramLinkageName(NewFunc, Name);
40154031
for (auto &Inst : CallsWithMetadataInFunc) {
40164032
// This map always has the initial version in it.
40174033
assert(Inst.cloneNo() == 0);
@@ -4950,9 +4966,7 @@ static SmallVector<std::unique_ptr<ValueToValueMapTy>, 4> createFunctionClones(
49504966
PrevF->eraseFromParent();
49514967
} else
49524968
NewF->setName(Name);
4953-
if (auto *SP = NewF->getSubprogram())
4954-
SP->replaceLinkageName(
4955-
MDString::get(NewF->getParent()->getContext(), Name));
4969+
updateSubprogramLinkageName(NewF, Name);
49564970
ORE.emit(OptimizationRemark(DEBUG_TYPE, "MemprofClone", &F)
49574971
<< "created clone " << ore::NV("NewFunction", NewF));
49584972

llvm/test/ThinLTO/X86/memprof-basic.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,14 @@ attributes #0 = { noinline optnone }
143143
!12 = !{i64 789, i64 300}
144144
!13 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !14, producer: "clang version 21.0.0git ([email protected]:llvm/llvm-project.git e391301e0e4d9183fe06e69602e87b0bc889aeda)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
145145
!14 = !DIFile(filename: "basic.cc", directory: "", checksumkind: CSK_MD5, checksum: "8636c46e81402013b9d54e8307d2f149")
146-
!15 = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv", scope: !14, file: !14, line: 1, type: !16, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !13)
146+
!15 = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv", scope: !14, file: !14, line: 1, type: !16, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !13, declaration: !22)
147147
!16 = !DISubroutineType(types: !17)
148148
!17 = !{!18}
149149
!18 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !19, size: 64)
150150
!19 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
151151
!20 = !{i32 7, !"Dwarf Version", i32 5}
152152
!21 = !{i32 2, !"Debug Info Version", i32 3}
153+
!22 = !DISubprogram(name: "bar", linkageName: "_Z3barv", scope: !14, file: !14, line: 1, type: !16, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)
153154

154155
; DUMP: CCG before cloning:
155156
; DUMP: Callsite Context Graph:
@@ -321,7 +322,8 @@ attributes #0 = { noinline optnone }
321322
; IR: attributes #[[NOTCOLD]] = { "memprof"="notcold" }
322323
; IR: attributes #[[COLD]] = { "memprof"="cold" }
323324
;; Make sure the clone's linkageName was updated.
324-
; IR: ![[SP]] = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv.memprof.1"
325+
; IR: ![[SP]] = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv.memprof.1", {{.*}} declaration: ![[SP2:[0-9]+]])
326+
; IR: ![[SP2]] = !DISubprogram(name: "bar", linkageName: "_Z3barv.memprof.1"
325327

326328

327329
; STATS: 1 memprof-context-disambiguation - Number of cold static allocations (possibly cloned)

llvm/test/Transforms/MemProfContextDisambiguation/basic.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,14 @@ attributes #6 = { builtin }
121121
!12 = !{i64 789, i64 300}
122122
!13 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !14, producer: "clang version 21.0.0git ([email protected]:llvm/llvm-project.git e391301e0e4d9183fe06e69602e87b0bc889aeda)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
123123
!14 = !DIFile(filename: "basic.cc", directory: "", checksumkind: CSK_MD5, checksum: "8636c46e81402013b9d54e8307d2f149")
124-
!15 = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv", scope: !14, file: !14, line: 1, type: !16, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !13)
124+
!15 = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv", scope: !14, file: !14, line: 1, type: !16, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !13, declaration: !22)
125125
!16 = !DISubroutineType(types: !17)
126126
!17 = !{!18}
127127
!18 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !19, size: 64)
128128
!19 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
129129
!20 = !{i32 7, !"Dwarf Version", i32 5}
130130
!21 = !{i32 2, !"Debug Info Version", i32 3}
131+
!22 = !DISubprogram(name: "bar", linkageName: "_Z3barv", scope: !14, file: !14, line: 1, type: !16, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)
131132

132133
; DUMP: CCG before cloning:
133134
; DUMP: Callsite Context Graph:
@@ -290,7 +291,8 @@ attributes #6 = { builtin }
290291
; IR: attributes #[[NOTCOLD]] = { builtin "memprof"="notcold" }
291292
; IR: attributes #[[COLD]] = { builtin "memprof"="cold" }
292293
;; Make sure the clone's linkageName was updated.
293-
; IR: ![[SP]] = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv.memprof.1"
294+
; IR: ![[SP]] = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv.memprof.1", {{.*}} declaration: ![[SP2:[0-9]+]])
295+
; IR: ![[SP2]] = !DISubprogram(name: "bar", linkageName: "_Z3barv.memprof.1"
294296

295297

296298
; STATS: 1 memprof-context-disambiguation - Number of cold static allocations (possibly cloned)

0 commit comments

Comments
 (0)