Skip to content

Commit d08b57c

Browse files
dzhidzhoevaadeshps-mcw
authored andcommitted
[clang][DebugInfo] Clear retained nodes list of vararg trunk's DISubprogram (llvm#167758)
This fixes the issue reported in llvm#166855 (comment) that had been revealed after llvm#166855 was merged. `CodeGenFunction::GenerateVarArgsThunk` creates thunks for vararg functions by cloning and modifying them. It is different from `CodeGenFunction::generateThunk`, which is used for Itanium ABI. According to https://reviews.llvm.org/D39396, `CodeGenFunction::GenerateVarArgsThunk` may be called before metadata nodes are resolved. So, it tries to avoid remapping DISubprogram and all metadata nodes it references inside `CloneFunction()` by manually cloning DISubprogram. If optimization level is not OptNone, DILocalVariables for a function are saved in DISubprogram's retainedNodes field. When `CodeGenFunction::GenerateVarArgsThunk` clones such DISubprogram without remapping, it produces a subprogram with incorrectly-scoped retained nodes. It triggers Verifier checks added in llvm#166855. To solve that, retained nodes list of a cloned DISubprogram is cleared.
1 parent b3b5120 commit d08b57c

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

clang/lib/CodeGen/CGVTables.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ static void resolveTopLevelMetadata(llvm::Function *Fn,
125125
if (!DIS)
126126
return;
127127
auto *NewDIS = llvm::MDNode::replaceWithDistinct(DIS->clone());
128+
// As DISubprogram remapping is avoided, clear retained nodes list of
129+
// cloned DISubprogram from retained nodes local to original DISubprogram.
130+
// FIXME: Thunk function signature is produced wrong in DWARF, as retained
131+
// nodes are not remapped.
132+
NewDIS->replaceRetainedNodes(llvm::MDTuple::get(Fn->getContext(), {}));
128133
VMap.MD()[DIS].reset(NewDIS);
129134

130135
// Find all llvm.dbg.declare intrinsics and resolve the DILocalVariable nodes

clang/test/CodeGenCXX/tmp-md-nodes1.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
// RUN: %clang_cc1 -O0 -triple %itanium_abi_triple -debug-info-kind=limited -emit-llvm %s -o - | \
33
// RUN: FileCheck %s
44

5+
// Trigger GenerateVarArgsThunk.
6+
// RUN: %clang_cc1 -O0 -triple riscv64-linux-gnu -debug-info-kind=limited -emit-llvm %s -o - | \
7+
// RUN: FileCheck %s
8+
9+
// Check that retainedNodes are properly maintained at function cloning.
10+
// RUN: %clang_cc1 -O1 -triple riscv64-linux-gnu -debug-info-kind=limited -emit-llvm %s -o - | \
11+
// RUN: FileCheck %s --check-prefixes=CHECK,CHECK-DI
12+
513
// This test simply checks that the varargs thunk is created. The failing test
614
// case asserts.
715

@@ -16,3 +24,11 @@ struct CharlieImpl : Charlie, Alpha {
1624
} delta;
1725

1826
// CHECK: define {{.*}} void @_ZThn{{[48]}}_N11CharlieImpl5bravoEz(
27+
28+
// CHECK-DI: distinct !DISubprogram({{.*}}, linkageName: "_ZN11CharlieImpl5bravoEz", {{.*}}, retainedNodes: [[RN1:![0-9]+]]
29+
// A non-empty retainedNodes list of original DISubprogram.
30+
// CHECK-DI: [[RN1]] = !{!{{.*}}}
31+
32+
// CHECK-DI: distinct !DISubprogram({{.*}}, linkageName: "_ZN11CharlieImpl5bravoEz", {{.*}}, retainedNodes: [[EMPTY:![0-9]+]]
33+
// An empty retainedNodes list of cloned DISubprogram.
34+
// CHECK-DI: [[EMPTY]] = !{}

clang/test/CodeGenCXX/tmp-md-nodes2.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
// RUN: %clang_cc1 -O0 -triple %itanium_abi_triple -debug-info-kind=limited -emit-llvm %s -o - | \
33
// RUN: FileCheck %s
44

5+
// Trigger GenerateVarArgsThunk.
6+
// RUN: %clang_cc1 -O0 -triple riscv64-linux-gnu -debug-info-kind=limited -emit-llvm %s -o - | \
7+
// RUN: FileCheck %s
8+
9+
// Check that retainedNodes are properly maintained at function cloning.
10+
// RUN: %clang_cc1 -O1 -triple riscv64-linux-gnu -debug-info-kind=limited -emit-llvm %s -o - | \
11+
// RUN: FileCheck %s --check-prefixes=CHECK,CHECK-DI
12+
513
// This test simply checks that the varargs thunk is created. The failing test
614
// case asserts.
715

@@ -31,3 +39,11 @@ BOOL CBdVfsImpl::ReqCacheHint( CMsgAgent* p_ma, CACHE_HINT hint, ... ) {
3139
}
3240

3341
// CHECK: define {{.*}} @_ZThn{{[48]}}_N10CBdVfsImpl12ReqCacheHintEP9CMsgAgentN3CFs10CACHE_HINTEz(
42+
43+
// An empty retainedNodes list of cloned DISubprogram.
44+
// CHECK-DI: [[EMPTY:![0-9]+]] = !{}
45+
// CHECK-DI: distinct !DISubprogram({{.*}}, linkageName: "_ZN10CBdVfsImpl12ReqCacheHintEP9CMsgAgentN3CFs10CACHE_HINTEz", {{.*}}, retainedNodes: [[RN1:![0-9]+]]
46+
// A non-empty retainedNodes list of original DISubprogram.
47+
// CHECK-DI: [[RN1]] = !{!{{.*}}}
48+
49+
// CHECK-DI: distinct !DISubprogram({{.*}}, linkageName: "_ZN10CBdVfsImpl12ReqCacheHintEP9CMsgAgentN3CFs10CACHE_HINTEz", {{.*}}, retainedNodes: [[EMPTY]]

0 commit comments

Comments
 (0)