[ICP] Verifier failure: inlinable function call in a function with debug info must have a !dbg location
$ cat /tmp/z.ll
define void @f(ptr %f) !dbg !4 {
entry:
call void %f(), !prof !5
ret void
}
define void @"?Run@CancelableTask@internal@v8@@UEAAXXZ"() !dbg !6 {
entry:
ret void
}
!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3}
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !2, globals: !2, imports: !2, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "f", directory: ".", checksumkind: CSK_MD5, checksum: "d8db21c2cc7d03d3fa1238e0f9cdb45d")
!2 = !{}
!3 = !{i32 2, !"Debug Info Version", i32 3}
!4 = distinct !DISubprogram(name: "RunOnce", linkageName: "a", scope: null, file: !1, line: 811, scopeLine: 812, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
!5 = !{!"VP", i32 0, i64 9760, i64 -7109612007850537772, i64 9632, i64 1050624153776240289, i64 82, i64 1343956138387440415, i64 45}
!6 = distinct !DISubprogram(name: "Run", linkageName: "b", scope: null, file: !1, line: 153, scopeLine: 153, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, declaration: null, retainedNodes: !2)
$ opt -passes='pgo-icall-prom,verify' -disable-output /tmp/z.ll
inlinable function call in a function with debug info must have a !dbg location
call void @"?Run@CancelableTask@internal@v8@@UEAAXXZ"()
LLVM ERROR: Broken module found, compilation aborted!
@teresajohnson
The original indirect call doesn't have any debug metadata, so ICP doesn't have any to clone onto the new direct call. Looks like the problem is in the code coming into ICP.
This makes reducing things a bit more work though. If some IR passes the verifier, we shouldn't transform it into something that will fail the verifier. Would you be ok with a patch to disable ICP if there's a debug info mismatch?
This makes reducing things a bit more work though.
Can you compile with something like -print-after-all to identify which pass is dropping the debug info? Or modify the verifier to fail on the indirect call with missing debug info to catch it earlier?
If some IR passes the verifier, we shouldn't transform it into something that will fail the verifier.
Why isn't the verifier failing on the input call with missing debug? Is it because it considers it uninlineable? That could also ultimately fail if we devirtualize other places in the compiler besides ICP. I.e. WPD, or the devirtualization done during inlining, and possibly during clang for virtual calls to final methods.
Would you be ok with a patch to disable ICP if there's a debug info mismatch?
My concern is that this could be hiding an earlier bug that drops debug info on that call. And also as noted above ICP is not the only pass that could turn an indirect call into a direct one.
My concern is that this could be hiding an earlier bug that drops debug info on that call. And also as noted above ICP is not the only pass that could turn an indirect call into a direct one.
Makes sense.
I think was using llvm-reduce to reduce a crash which likely dropped !dbg and over reduced, but I'd have to double check. Somehow fixing this would make it harder to over reduce.
Looks like this verifier assert was added in 93035c8f47589590b65041603524f48a7c007e1f:
Verifier: Verify that each inlinable callsite of a debug-info-bearing function
in a debug-info-bearing function has a debug location attached to it. Failure to
do so causes an "!dbg attachment points at wrong subprogram for function"
assertion failure when the inliner sets up inline scope info.
That actually looks somewhat related to what we're seeing in https://crbug.com/1355639, so perhaps not an over reduction and there is something going wrong. Maybe it's possible that we're mixing files compiled with and without debug info when LTOing? I wonder if there's a more proper solution to this that involves undoing the verifier assert and fixing the inliner, although I don't understand enough about debug info right now.
This assert was a long quest (saga?) I went on years ago - when we'd inline a function call without debug info and lose a bunch of debug info in the process. By creating the invariant enforced by that assert we've been able to detect and cleanup a lot of cases where inlining would've otherwise been lossy.
We could probably fix it a different way/add a warning instead of an error, perhaps (but warnings tend to go unheeded) - by synthesizing a zero location for the enclosing function when we inline that call - but we'd still get loss if we inlined a caller (losing the context that this call site had been from that caller, because it had no location - at least I think that's how it works currently) /then/ inlined the call (the result would be that the inlined code would appear to being called by the caller, missing the extra inline frame)
I'm open to ideas, though this invariant has generally been rather helpful at identifying bugs in the intervening 6 years (we found a few issues before the verifier check was added too, FWIW) since the verifier check was added in 93035c8f47589590b65041603524f48a7c007e1f
https://reviews.llvm.org/D134825 I believe will fix the issue we were seeing.
To better catch these sorts of issues in the future, given that devirtualization can happen, should we also have a verifier check that indirect calls in a function with debug info should have !dbg metadata?
With
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 272f61d74f7f..230e7f9beba7 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -3470,9 +3470,11 @@ void Verifier::visitCallBase(CallBase &Call) {
// debug-info-bearing function has a debug location attached to it. Failure to
// do so causes assertion failures when the inliner sets up inline scope info
// (Interposable functions are not inlinable).
- if (Call.getFunction()->getSubprogram() && Call.getCalledFunction() &&
- !Call.getCalledFunction()->isInterposable() &&
- Call.getCalledFunction()->getSubprogram())
+ if (Call.getFunction()->getSubprogram() &&
+ ((Call.getCalledFunction() &&
+ !Call.getCalledFunction()->isInterposable() &&
+ Call.getCalledFunction()->getSubprogram()) ||
+ Call.isIndirectCall()))
CheckDI(Call.getDebugLoc(),
"inlinable function call in a function with "
"debug info must have a !dbg location",
the following tests fail
LLVM :: Bitcode/DIImportedEntity_backward.ll
LLVM :: Bitcode/DIImportedEntity_elements.ll
LLVM :: CodeGen/Hexagon/copy-to-combine-dbg.ll
LLVM :: DebugInfo/COFF/thunk.ll
LLVM :: DebugInfo/X86/dwarf-callsite-related-attrs.ll
LLVM :: DebugInfo/X86/dwarfdump-DIImportedEntity_elements.ll
LLVM :: Transforms/SampleProfile/pseudo-probe-profile.ll
LLVM :: tools/llvm-dwp/X86/search_dwos.test
Clang :: CodeGenCXX/debug-info-atexit-stub.cpp
Clang :: CodeGenCXX/debug-info-thunk.cpp
To better catch these sorts of issues in the future, given that devirtualization can happen, should we also have a verifier check that indirect calls in a function with debug info should have !dbg metadata?
Not sure - I'm guessing that would widen the scope and require a bunch of cleanup and maybe some of those indirect calls wouldn't be an issue/are never inlinable for some reasons?
Somewhat of a tradeoff - doing cleanup that turns out to not be necessary V playing whack-a-mole, I guess?
At that point the requirement would be that all calls have locations? (I guess for now we're at "all direct calls", so adding indirect calls becomes "all calls"?)
Tests probably aren't great coverage - because they're reduced, etc. A bootstrap with the verifier enabled would I /guess/ uncover a lot more violations of the invariant (since it's a lot more about the contract for IR generation) - or at least somehow enabling IR verification on Clang's output (not sure if we have a way to do that, or if that's something we already do when emitting textual IR? In which case we've more coverage than I thought we did) then running check-clang with that enabled.