Skip to content

Commit 524c640

Browse files
eddyz87yonghong-song
authored andcommitted
[clang][DebugInfo] Emit DISubprogram for extern functions with reserved names
Callsite `DISubprogram` entries are not generated for: - builtin functions; - external functions with reserved names (e.g. names starting from "__"). This limitation was added by the commit [1] as a workaround for the situation described in [2] that triggered the IR verifier error. The goal of the present commit is to lift this limitation by adjusting the IR verifier logic. The logic behind [1] is to avoid the following situation: - a `DISubprogram` is added for some builtin function; - there is some location where this builtin is also emitted by a transformation (w/o debug location); - the `Verifier::visitCallBase` sees a call to a function with `DISubprogram` but w/o debug location and emits an error. Here is an updated example of such situation taken from [2]: ``` extern "C" int memcmp(void *, void *, long); struct a { int b; int c; int d; }; struct e { int f[1000]; }; bool foo(e g, e &h) { // DISubprogram for memcmp is created here when [1] is commented out return memcmp(&g, &h, sizeof(e)); } bool bar(a &g, a &h) { // memcmp might be generated here by MergeICmps return g.b == h.b && g.c == h.c && g.d == h.d; } ``` This triggers the verifier error when: - compiled for AArch64: `clang++ -c -g -Oz -target aarch64-unknown-linux-android21 test.cpp`; - [1] check is commented out. Instead of forbidding generation of `DISubprogram` entries as in [1] one can instead adjust the verifier to additionally check if callee has a body. Functions w/o bodies cannot be inlined and thus verifier warning is not necessary. E.g. `llvm::InlineFunction` requires functions for which `GlobalValue::isDeclaration() == false`. [1] 568db78 [2] https://bugs.chromium.org/p/chromium/issues/detail?id=1022296 Differential Revision: https://reviews.llvm.org/D136041
1 parent ee9bbfa commit 524c640

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

Diff for: clang/lib/CodeGen/CGDebugInfo.cpp

+3-9
Original file line numberDiff line numberDiff line change
@@ -4228,17 +4228,11 @@ void CGDebugInfo::EmitFuncDeclForCallSite(llvm::CallBase *CallOrInvoke,
42284228
if (Func->getSubprogram())
42294229
return;
42304230

4231-
// Do not emit a declaration subprogram for a builtin, a function with nodebug
4232-
// attribute, or if call site info isn't required. Also, elide declarations
4233-
// for functions with reserved names, as call site-related features aren't
4234-
// interesting in this case (& also, the compiler may emit calls to these
4235-
// functions without debug locations, which makes the verifier complain).
4236-
if (CalleeDecl->getBuiltinID() != 0 || CalleeDecl->hasAttr<NoDebugAttr>() ||
4231+
// Do not emit a declaration subprogram for a function with nodebug
4232+
// attribute, or if call site info isn't required.
4233+
if (CalleeDecl->hasAttr<NoDebugAttr>() ||
42374234
getCallSiteRelatedAttrs() == llvm::DINode::FlagZero)
42384235
return;
4239-
if (CalleeDecl->isReserved(CGM.getLangOpts()) !=
4240-
ReservedIdentifierStatus::NotReserved)
4241-
return;
42424236

42434237
// If there is no DISubprogram attached to the function being called,
42444238
// create the one describing the function in order to have complete

Diff for: clang/test/CodeGen/debug-info-extern-call.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
// DECLS-FOR-EXTERN: [[FN1_TYPES]] = !{[[X_TYPE:![0-9]+]],
3030
// DECLS-FOR-EXTERN: [[X_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "x",
3131
// DECLS-FOR-EXTERN-SAME: baseType: [[INT_TYPE]])
32-
// DECLS-FOR-EXTERN-NOT: !DISubprogram(name: "memcmp"
33-
// DECLS-FOR-EXTERN-NOT: !DISubprogram(name: "__some_reserved_name"
32+
// DECLS-FOR-EXTERN: !DISubprogram(name: "memcmp"
33+
// DECLS-FOR-EXTERN: !DISubprogram(name: "__some_reserved_name"
3434

3535
// NO-DECLS-FOR-EXTERN-NOT: !DISubprogram(name: "fn1"
3636
// NO-DECLS-FOR-EXTERN-NOT: !DISubprogram(name: "memcmp"

Diff for: llvm/lib/IR/Verifier.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -3468,9 +3468,11 @@ void Verifier::visitCallBase(CallBase &Call) {
34683468
// Verify that each inlinable callsite of a debug-info-bearing function in a
34693469
// debug-info-bearing function has a debug location attached to it. Failure to
34703470
// do so causes assertion failures when the inliner sets up inline scope info
3471-
// (Interposable functions are not inlinable).
3471+
// (Interposable functions are not inlinable, neither are functions without
3472+
// definitions.)
34723473
if (Call.getFunction()->getSubprogram() && Call.getCalledFunction() &&
34733474
!Call.getCalledFunction()->isInterposable() &&
3475+
!Call.getCalledFunction()->isDeclaration() &&
34743476
Call.getCalledFunction()->getSubprogram())
34753477
CheckDI(Call.getDebugLoc(),
34763478
"inlinable function call in a function with "

Diff for: llvm/test/Verifier/callsite-dbgloc.ll

+10-1
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,24 @@ entry:
3333
ret void, !dbg !22
3434
}
3535

36-
declare void @i(...) #1
36+
declare !dbg !23 void @i(...) #1
3737

3838
; Function Attrs: nounwind ssp uwtable
3939
define void @g() #0 !dbg !11 {
4040
entry:
4141
; Manually removed !dbg.
4242
; CHECK: inlinable function call in a function with debug info must have a !dbg location
43+
; CHECK: @h()
4344
call void @h()
4445
; CHECK-NOT: inlinable function call in a function with debug info must have a !dbg location
46+
; CHECK-NOT: @j()
4547
call void @j()
4648
; CHECK: inlinable function call in a function with debug info must have a !dbg location
49+
; CHECK: @k()
4750
call void @k()
51+
; CHECK-NOT: inlinable function call in a function with debug info must have a !dbg location
52+
; CHECK-NOT: @i()
53+
call void (...) @i()
4854
ret void, !dbg !13
4955
}
5056

@@ -86,3 +92,6 @@ attributes #0 = { nounwind ssp uwtable }
8692
!20 = distinct !DISubprogram(name: "k", scope: !1, file: !1, line: 6, type: !8, isLocal: false, isDefinition: true, scopeLine: 6, isOptimized: false, unit: !0, retainedNodes: !2)
8793
!21 = !DILocation(line: 4, column: 12, scope: !20)
8894
!22 = !DILocation(line: 4, column: 17, scope: !20)
95+
!23 = !DISubprogram(name: "i", scope: !1, file: !1, line: 1, type: !24, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2)
96+
!24 = !DISubroutineType(types: !25)
97+
!25 = !{null}

0 commit comments

Comments
 (0)