Skip to content

Commit 9d65f77

Browse files
authored
[KeyInstr] Enable -gkey-instructions by default if optimisations are enabled (#149509)
That's enabling Clang's -gkey-instructions, cc1's -gkey-instructions remains off by default. Key Instructions improves the optimized-code debug-stepping experience in debuggers that use DWARF's `is_stmt` line table register to determine stepping behaviour. The feature can be disabled with -gno-key-instructions (note that the positive and negative flag both imply -g). RFC: https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668
1 parent b88e5ca commit 9d65f77

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4394,10 +4394,15 @@ static void renderDwarfFormat(const Driver &D, const llvm::Triple &T,
43944394

43954395
static void
43964396
renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T,
4397-
const ArgList &Args, bool IRInput, ArgStringList &CmdArgs,
4398-
const InputInfo &Output,
4397+
const ArgList &Args, types::ID InputType,
4398+
ArgStringList &CmdArgs, const InputInfo &Output,
43994399
llvm::codegenoptions::DebugInfoKind &DebugInfoKind,
44004400
DwarfFissionKind &DwarfFission) {
4401+
bool IRInput = isLLVMIR(InputType);
4402+
bool PlainCOrCXX = isDerivedFromC(InputType) && !isCuda(InputType) &&
4403+
!isHIP(InputType) && !isObjC(InputType) &&
4404+
!isOpenCL(InputType);
4405+
44014406
if (Args.hasFlag(options::OPT_fdebug_info_for_profiling,
44024407
options::OPT_fno_debug_info_for_profiling, false) &&
44034408
checkDebugInfoOption(
@@ -4591,8 +4596,15 @@ renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T,
45914596
CmdArgs.push_back("-gembed-source");
45924597
}
45934598

4599+
// Enable Key Instructions by default if we're emitting DWARF, the language is
4600+
// plain C or C++, and optimisations are enabled.
4601+
Arg *OptLevel = Args.getLastArg(options::OPT_O_Group);
4602+
bool KeyInstructionsOnByDefault =
4603+
EmitDwarf && PlainCOrCXX && OptLevel &&
4604+
!OptLevel->getOption().matches(options::OPT_O0);
45944605
if (Args.hasFlag(options::OPT_gkey_instructions,
4595-
options::OPT_gno_key_instructions, false))
4606+
options::OPT_gno_key_instructions,
4607+
KeyInstructionsOnByDefault))
45964608
CmdArgs.push_back("-gkey-instructions");
45974609

45984610
if (EmitCodeView) {
@@ -6060,8 +6072,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
60606072
llvm::codegenoptions::DebugInfoKind DebugInfoKind =
60616073
llvm::codegenoptions::NoDebugInfo;
60626074
DwarfFissionKind DwarfFission = DwarfFissionKind::None;
6063-
renderDebugOptions(TC, D, RawTriple, Args, types::isLLVMIR(InputType),
6064-
CmdArgs, Output, DebugInfoKind, DwarfFission);
6075+
renderDebugOptions(TC, D, RawTriple, Args, InputType, CmdArgs, Output,
6076+
DebugInfoKind, DwarfFission);
60656077

60666078
// Add the split debug info name to the command lines here so we
60676079
// can propagate it to the backend.

clang/test/DebugInfo/KeyInstructions/flag.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %clang -### -target x86_64 -c -gdwarf -gkey-instructions %s 2>&1 | FileCheck %s --check-prefixes=KEY-INSTRUCTIONS
22
// RUN: %clang -### -target x86_64 -c -gdwarf -gno-key-instructions %s 2>&1 | FileCheck %s --check-prefixes=NO-KEY-INSTRUCTIONS
3-
//// Default: Off.
4-
// RUN: %clang -### -target x86_64 -c -gdwarf %s 2>&1 | FileCheck %s --check-prefixes=NO-KEY-INSTRUCTIONS
53

64
//// Help.
75
// RUN %clang --help | FileCheck %s --check-prefix=HELP
@@ -23,3 +21,43 @@ void f() {}
2321
// RUN: %clang_cc1 %s -triple x86_64-linux-gnu -gkey-instructions -debug-info-kind=line-tables-only -emit-llvm -o - | FileCheck %s --check-prefix=SMOKETEST-ON
2422
// SMOKETEST-ON: keyInstructions: true
2523
// SMOKETEST-ON: atomGroup: 1
24+
25+
//// Enable Key Instructions by default if optimisations are enabled and we're
26+
//// emitting DWARF.
27+
////
28+
//// | opt level | -gkey-instructions | feature |
29+
//// | 0 | no | off |
30+
//// | 0 | yes | on |
31+
//// | >=1 | no | on |
32+
//// | >=1 | yes | on |
33+
//// | >=1 | no & no -g flags | off |
34+
//// | >=1 | no & emit codeview | off |
35+
//
36+
// RUN: %clang %s -target x86_64 -gdwarf -gmlt -### 2>&1 | FileCheck %s --check-prefix=NO-KEY-INSTRUCTIONS
37+
// RUN: %clang %s -target x86_64 -gdwarf -gmlt -gkey-instructions -### 2>&1 | FileCheck %s --check-prefix=KEY-INSTRUCTIONS
38+
// RUN: %clang %s -O0 -target x86_64 -gdwarf -gmlt -### 2>&1 | FileCheck %s --check-prefix=NO-KEY-INSTRUCTIONS
39+
// RUN: %clang %s -O0 -target x86_64 -gdwarf -gmlt -gkey-instructions -### 2>&1 | FileCheck %s --check-prefix=KEY-INSTRUCTIONS
40+
// RUN: %clang %s -O1 -target x86_64 -gdwarf -gmlt -### 2>&1 | FileCheck %s --check-prefix=KEY-INSTRUCTIONS
41+
// RUN: %clang %s -O1 -target x86_64 -gdwarf -gmlt -gkey-instructions -### 2>&1 | FileCheck %s --check-prefix=KEY-INSTRUCTIONS
42+
// RUN: %clang %s -O1 -target x86_64 -### 2>&1 | FileCheck %s --check-prefixes=NO-KEY-INSTRUCTIONS
43+
// RUN: %clang %s -O1 -target x86_64 -gcodeview -gmlt -### 2>&1 | FileCheck %s --check-prefixes=NO-KEY-INSTRUCTIONS
44+
//
45+
// RUN: %clang %s -target x86_64 -gdwarf -gmlt -S -emit-llvm -o - | FileCheck %s --check-prefix=SMOKETEST-OFF
46+
// RUN: %clang %s -target x86_64 -gdwarf -gmlt -gkey-instructions -S -emit-llvm -o - | FileCheck %s --check-prefix=SMOKETEST-ON
47+
// RUN: %clang %s -O0 -target x86_64 -gdwarf -gmlt -S -emit-llvm -o - | FileCheck %s --check-prefix=SMOKETEST-OFF
48+
// RUN: %clang %s -O0 -target x86_64 -gdwarf -gmlt -gkey-instructions -S -emit-llvm -o - | FileCheck %s --check-prefix=SMOKETEST-ON
49+
// RUN: %clang %s -O1 -target x86_64 -gdwarf -gmlt -S -emit-llvm -o - | FileCheck %s --check-prefix=SMOKETEST-ON
50+
// RUN: %clang %s -O1 -target x86_64 -gdwarf -gmlt -gkey-instructions -S -emit-llvm -o - | FileCheck %s --check-prefix=SMOKETEST-ON
51+
// RUN: %clang %s -O1 -target x86_64 -S -emit-llvm -o - | FileCheck %s --check-prefixes=SMOKETEST-OFF,SMOKETEST-NO-DEBUG
52+
// RUN: %clang %s -O1 -target x86_64 -gcodeview -gmlt -S -emit-llvm -o - | FileCheck %s --check-prefixes=SMOKETEST-OFF
53+
// SMOKETEST-NO-DEBUG: llvm.module.flags
54+
// SMOKETEST-NO-DEBUG-NOT: DICompileUnit
55+
56+
//// Check only "plain" C/C++ turns on Key Instructions by default.
57+
// RUN: %clang -x c %s -O1 -target x86_64 -gdwarf -gmlt -### 2>&1 | FileCheck %s --check-prefix=KEY-INSTRUCTIONS
58+
// RUN: %clang -x c++ %s -O1 -target x86_64 -gdwarf -gmlt -### 2>&1 | FileCheck %s --check-prefix=KEY-INSTRUCTIONS
59+
// RUN: %clang -x cuda -nocudalib -nocudainc %s -O1 -target x86_64 -gdwarf -gmlt -### 2>&1 | FileCheck %s --check-prefix=NO-KEY-INSTRUCTIONS
60+
// RUN: %clang -x hip -nogpulib -nogpuinc %s -O1 -target x86_64 -gdwarf -gmlt -### 2>&1 | FileCheck %s --check-prefix=NO-KEY-INSTRUCTIONS
61+
// RUN: %clang -x cl %s -O1 -target x86_64 -gdwarf -gmlt -### 2>&1 | FileCheck %s --check-prefix=NO-KEY-INSTRUCTIONS
62+
// RUN: %clang -x objective-c %s -O1 -target x86_64 -gdwarf -gmlt -### 2>&1 | FileCheck %s --check-prefix=NO-KEY-INSTRUCTIONS
63+
// RUN: %clang -x objective-c++ %s -O1 -target x86_64 -gdwarf -gmlt -### 2>&1 | FileCheck %s --check-prefix=NO-KEY-INSTRUCTIONS

0 commit comments

Comments
 (0)