diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -135,6 +135,8 @@ ///< enabled. CODEGENOPT(NoWarn , 1, 0) ///< Set when -Wa,--no-warn is enabled. CODEGENOPT(EnableSegmentedStacks , 1, 0) ///< Set when -fsplit-stack is enabled. +CODEGENOPT(NoInlineLineTables, 1, 0) ///< Whether debug info should contain + ///< inline line tables. CODEGENOPT(NoImplicitFloat , 1, 0) ///< Set when -mno-implicit-float is enabled. CODEGENOPT(NoInfsFPMath , 1, 0) ///< Assume FP arguments, results not +-Inf. CODEGENOPT(NoSignedZeros , 1, 0) ///< Allow ignoring the signedness of FP zero diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -1958,6 +1958,9 @@ HelpText<"Emit type record hashes in a .debug$H section">, Flags<[CC1Option, CoreOption]>; def gno_codeview_ghash : Flag<["-"], "gno-codeview-ghash">, Flags<[CoreOption]>; +def ginline_line_tables : Flag<["-"], "ginline-line-tables">, Flags<[CoreOption]>; +def gno_inline_line_tables : Flag<["-"], "gno-inline-line-tables">, + Flags<[CC1Option, CoreOption]>, HelpText<"Don't emit inline line tables">; // Equivalent to our default dwarf version. Forces usual dwarf emission when // CodeView is enabled. diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -764,6 +764,10 @@ Fn->addFnAttr("no-jump-tables", llvm::toStringRef(CGM.getCodeGenOpts().NoUseJumpTables)); + // Add no-inline-line-tables value. + if (CGM.getCodeGenOpts().NoInlineLineTables) + Fn->addFnAttr("no-inline-line-tables"); + // Add profile-sample-accurate value. if (CGM.getCodeGenOpts().ProfileSampleAccurate) Fn->addFnAttr("profile-sample-accurate"); diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -3384,6 +3384,12 @@ options::OPT_gno_codeview_ghash, false)) { CmdArgs.push_back("-gcodeview-ghash"); } + + // Omit inline line tables if requested. + if (!Args.hasFlag(options::OPT_ginline_line_tables, + options::OPT_gno_inline_line_tables, false)) { + CmdArgs.push_back("-gno-inline-line-tables"); + } } // Adjust the debug info kind for the given toolchain. diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -806,6 +806,7 @@ Opts.RecordCommandLine = Args.getLastArgValue(OPT_record_command_line); Opts.MergeAllConstants = Args.hasArg(OPT_fmerge_all_constants); Opts.NoCommon = Args.hasArg(OPT_fno_common); + Opts.NoInlineLineTables = Args.hasArg(OPT_gno_inline_line_tables); Opts.NoImplicitFloat = Args.hasArg(OPT_no_implicit_float); Opts.OptimizeSize = getOptimizationLevelSize(Args); Opts.SimplifyLibCalls = !(Args.hasArg(OPT_fno_builtin) || diff --git a/clang/test/CodeGen/debug-info-no-inline-line-tables.c b/clang/test/CodeGen/debug-info-no-inline-line-tables.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/debug-info-no-inline-line-tables.c @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 -triple x86_64-windows-msvc -gcodeview -debug-info-kind=limited \ +// RUN: -gno-inline-line-tables -emit-llvm -o - %s | FileCheck %s + +int x; +__attribute((always_inline)) void f() { + x += 1; +} +int main() { + f(); + x += 2; + return x; +} + +// Check that clang emits the location of the call site and not the inlined +// function in the debug info. +// CHECK: define dso_local i32 @main() +// CHECK: %{{.+}} = load i32, i32* @x, align 4, !dbg [[DbgLoc:![0-9]+]] + +// Check that the no-inline-line-tables attribute is added. +// CHECK: attributes #0 = {{.*}}"no-inline-line-tables"{{.*}} +// CHECK: attributes #1 = {{.*}}"no-inline-line-tables"{{.*}} + +// CHECK: [[DbgLoc]] = !DILocation(line: 9, +// CHECK-NOT: inlinedAt: diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst --- a/llvm/docs/LangRef.rst +++ b/llvm/docs/LangRef.rst @@ -1441,6 +1441,13 @@ ``naked`` This attribute disables prologue / epilogue emission for the function. This can have very system-specific consequences. +``"no-inline-line-tables"`` + When this attribute is set to true, the inliner discards source locations + when inlining code and instead uses the source location of the call site. + Breakpoints set on code that was inlined into the current function will + not fire during the execution of the inlined call sites. If the debugger + stops inside an inlined call site, it will appear to be stopped at the + outermost inlined call site. ``no-jump-tables`` When this attribute is set to true, the jump tables and lookup tables that can be generated from a switch case lowering are disabled. diff --git a/llvm/include/llvm/IR/Attributes.td b/llvm/include/llvm/IR/Attributes.td --- a/llvm/include/llvm/IR/Attributes.td +++ b/llvm/include/llvm/IR/Attributes.td @@ -220,6 +220,7 @@ def NoNansFPMath : StrBoolAttr<"no-nans-fp-math">; def UnsafeFPMath : StrBoolAttr<"unsafe-fp-math">; def NoJumpTables : StrBoolAttr<"no-jump-tables">; +def NoInlineLineTables : StrBoolAttr<"no-inline-line-tables">; def ProfileSampleAccurate : StrBoolAttr<"profile-sample-accurate">; class CompatRule { diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp --- a/llvm/lib/Transforms/Utils/InlineFunction.cpp +++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -1405,6 +1405,10 @@ // other. DenseMap IANodes; + // Check if we are not generating inline line tables and want to use + // the call site location instead. + bool NoInlineLineTables = Fn->hasFnAttribute("no-inline-line-tables"); + for (; FI != Fn->end(); ++FI) { for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) { @@ -1416,6 +1420,18 @@ BI->setMetadata(LLVMContext::MD_loop, NewLoopID); } + // If we are not generating inline line tables, set the debug location + // of the inlined code to be the call location. + if (NoInlineLineTables) { + // Remove debug info intrinsics. + if (auto *DbgInst = dyn_cast(BI)) { + BI = --(DbgInst->eraseFromParent()); + continue; + } + BI->setDebugLoc(TheCallDL); + continue; + } + if (DebugLoc DL = BI->getDebugLoc()) { DebugLoc IDL = inlineDebugLoc(DL, InlinedAtNode, BI->getContext(), IANodes); diff --git a/llvm/test/Transforms/Inline/no-inline-line-tables.ll b/llvm/test/Transforms/Inline/no-inline-line-tables.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/Inline/no-inline-line-tables.ll @@ -0,0 +1,64 @@ +; RUN: opt < %s -inline -S | FileCheck %s + +; This tests that functions with the attribute `no-inline-line-tables` have the +; correct debug information when they are inlined. + +; ModuleID = 't.c' +source_filename = "t.c" +target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-windows-msvc" + +; Function Attrs: alwaysinline nounwind +define dso_local i32 @f(i32 %i) #0 !dbg !7 { +entry: + %i.addr = alloca i32, align 4 + store i32 %i, i32* %i.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %i.addr, metadata !12, metadata !DIExpression()), !dbg !13 + %0 = load i32, i32* %i.addr, align 4, !dbg !14 + ret i32 %0, !dbg !14 +} + +; Function Attrs: nounwind readnone speculatable willreturn +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +; Check that debug info for inlined code uses the call location and that debug +; intrinsics are removed. +; Function Attrs: noinline nounwind optnone +define dso_local i32 @main() #2 !dbg !15 { +entry: +; CHECK-LABEL: @main() +; CHECK-NOT: @f +; CHECK-NOT: @llvm.dbg.declare +; CHECK: %{{[0-9]+}} = load i32, i32* %i.addr.i, align 4, !dbg ![[VAR:[0-9]+]] +; CHECK: ![[VAR]] = !DILocation(line: 5, scope: !{{[0-9]+}}) + %call = call i32 @f(i32 23), !dbg !18 + ret i32 0, !dbg !19 +} + +attributes #0 = { alwaysinline nounwind "no-inline-line-tables" } +attributes #2 = { noinline nounwind optnone "no-inline-line-tables"} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 10.0.0 (https://github.com/llvm/llvm-project.git cb37bd6bbb4ca4b23838b08412d976bdab07e4fe)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None) +!1 = !DIFile(filename: "", directory: "/usr/local/google/home/akhuang/testing/inline-line-tables", checksumkind: CSK_MD5, checksum: "69f4cc67a00fe0c3f251a593209753fd") +!2 = !{} +!3 = !{i32 2, !"CodeView", i32 1} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 2} +!6 = !{!"clang version 10.0.0 (https://github.com/llvm/llvm-project.git cb37bd6bbb4ca4b23838b08412d976bdab07e4fe)"} +!7 = distinct !DISubprogram(name: "f", scope: !8, file: !8, line: 1, type: !9, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!8 = !DIFile(filename: "t.c", directory: "/usr/local/google/home/akhuang/testing/inline-line-tables", checksumkind: CSK_MD5, checksum: "69f4cc67a00fe0c3f251a593209753fd") +!9 = !DISubroutineType(types: !10) +!10 = !{!11, !11} +!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!12 = !DILocalVariable(name: "i", arg: 1, scope: !7, file: !8, line: 1, type: !11) +!13 = !DILocation(line: 1, scope: !7) +!14 = !DILocation(line: 2, scope: !7) +!15 = distinct !DISubprogram(name: "main", scope: !8, file: !8, line: 4, type: !16, scopeLine: 4, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!16 = !DISubroutineType(types: !17) +!17 = !{!11} +!18 = !DILocation(line: 5, scope: !15) +!19 = !DILocation(line: 6, scope: !15)