Index: docs/LangRef.rst =================================================================== --- docs/LangRef.rst +++ docs/LangRef.rst @@ -4566,10 +4566,12 @@ """""""""""" ``DISubprogram`` nodes represent functions from the source language. A -``DISubprogram`` may be attached to a function definition using ``!dbg`` -metadata. The ``variables:`` field points at :ref:`variables ` -that must be retained, even if their IR counterparts are optimized out of -the IR. The ``type:`` field must point at an :ref:`DISubroutineType`. +distinct ``DISubprogram`` may be attached to a function definition using +``!dbg`` metadata. A unique ``DISubprogram`` may be attached to a function +declaration used for call site debug info. The ``variables:`` field points at +:ref:`variables ` that must be retained, even if their IR +counterparts are optimized out of the IR. The ``type:`` field must point at an +:ref:`DISubroutineType`. .. _DISubprogramDeclaration: Index: lib/CodeGen/AsmPrinter/DwarfDebug.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -669,6 +669,11 @@ NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); } + // Create DIEs for function declarations used for call site debug info. + for (auto Scope : DIUnit->getRetainedTypes()) + if (auto *SP = dyn_cast_or_null(Scope)) + NewCU.getOrCreateSubprogramDIE(SP); + CUMap.insert({DIUnit, &NewCU}); CUDieMap.insert({&NewCU.getUnitDie(), &NewCU}); return NewCU; Index: lib/IR/Verifier.cpp =================================================================== --- lib/IR/Verifier.cpp +++ lib/IR/Verifier.cpp @@ -2223,8 +2223,11 @@ MDs.empty() ? nullptr : MDs.front().second); } else if (F.isDeclaration()) { for (const auto &I : MDs) { - AssertDI(I.first != LLVMContext::MD_dbg, - "function declaration may not have a !dbg attachment", &F); + // This is used for call site debug information. + AssertDI(I.first != LLVMContext::MD_dbg || + !cast(I.second)->isDistinct(), + "function declaration may only have a unique !dbg attachment", + &F); Assert(I.first != LLVMContext::MD_prof, "function declaration may not have a !prof attachment", &F); Index: test/Verifier/metadata-function-dbg.ll =================================================================== --- test/Verifier/metadata-function-dbg.ll +++ test/Verifier/metadata-function-dbg.ll @@ -1,8 +1,11 @@ ; RUN: llvm-as %s -disable-output 2>&1 | FileCheck %s -; CHECK: function declaration may not have a !dbg attachment +; CHECK: function declaration may only have a unique !dbg attachment declare !dbg !4 void @f1() +; CHECK-NOT: function declaration may only have a unique !dbg attachment +declare !dbg !6 void @f5() + ; CHECK: function must have a single !dbg attachment define void @f2() !dbg !4 !dbg !4 { unreachable @@ -31,7 +34,9 @@ !0 = !{i32 2, !"Debug Info Version", i32 3} !llvm.dbg.cu = !{!1} -!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2) +!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, retainedTypes: !5) !2 = !DIFile(filename: "t.c", directory: "/path/to/dir") !3 = !{} !4 = distinct !DISubprogram(name: "foo", scope: !1, file: !2, unit: !1) +!5 = !{!6} +!6 = !DISubprogram(name: "f5", scope: !1, file: !2, unit: !1, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)