Skip to content

Commit

Permalink
[gcov] Simplify cc1 options and remove CodeGenOptions EmitCovNotes/Em…
Browse files Browse the repository at this point in the history
…itCovArcs

After a07b135, we always pass
-coverage-notes-file/-coverage-data-file for driver options
-ftest-coverage/-fprofile-arcs/--coverage. As a bonus, we can make the following
simplification to cc1 options:

* `-ftest-coverage -coverage-notes-file a.gcno` => `-coverage-notes-file a.gcno`
* `-fprofile-arcs -coverage-data-file a.gcda` => `-coverage-data-file a.gcda`

and remove EmitCovNotes/EmitCovArcs.
  • Loading branch information
MaskRay committed May 17, 2023
1 parent 155191e commit 71a35f7
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 44 deletions.
2 changes: 0 additions & 2 deletions clang/include/clang/Basic/CodeGenOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ CODEGENOPT(EmitDeclMetadata , 1, 0) ///< Emit special metadata indicating what
///< Only useful when running CodeGen as a
///< subroutine.
CODEGENOPT(EmitVersionIdentMetadata , 1, 1) ///< Emit compiler version metadata.
CODEGENOPT(EmitGcovArcs , 1, 0) ///< Emit coverage data files, aka. GCDA.
CODEGENOPT(EmitGcovNotes , 1, 0) ///< Emit coverage "notes" files, aka GCNO.
CODEGENOPT(EmitOpenCLArgMetadata , 1, 0) ///< Emit OpenCL kernel arg metadata.
CODEGENOPT(EmulatedTLS , 1, 0) ///< Set by default or -f[no-]emulated-tls.
/// Embed Bitcode mode (off/all/bitcode/marker).
Expand Down
24 changes: 10 additions & 14 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1388,22 +1388,20 @@ def fno_profile_instr_use : Flag<["-"], "fno-profile-instr-use">,
HelpText<"Disable using instrumentation data for profile-guided optimization">;
def fno_profile_use : Flag<["-"], "fno-profile-use">,
Alias<fno_profile_instr_use>;
defm profile_arcs : BoolFOption<"profile-arcs",
CodeGenOpts<"EmitGcovArcs">, DefaultFalse,
PosFlag<SetTrue, [CC1Option, LinkOption]>, NegFlag<SetFalse>>;
defm test_coverage : BoolFOption<"test-coverage",
CodeGenOpts<"EmitGcovNotes">, DefaultFalse,
PosFlag<SetTrue, [CC1Option]>, NegFlag<SetFalse>>;
def ftest_coverage : Flag<["-"], "ftest-coverage">, Group<f_Group>,
HelpText<"Produce gcov notes files (*.gcno)">;
def fno_test_coverage : Flag<["-"], "fno-test-coverage">, Group<f_Group>;
def fprofile_arcs : Flag<["-"], "fprofile-arcs">, Group<f_Group>,
HelpText<"Instrument code to produce gcov data files (*.gcda)">;
def fno_profile_arcs : Flag<["-"], "fno-profile-arcs">, Group<f_Group>;
def fprofile_filter_files_EQ : Joined<["-"], "fprofile-filter-files=">,
Group<f_Group>, Flags<[CC1Option, CoreOption]>,
HelpText<"Instrument only functions from files where names match any regex separated by a semi-colon">,
MarshallingInfoString<CodeGenOpts<"ProfileFilterFiles">>,
ShouldParseIf<!strconcat(fprofile_arcs.KeyPath, "||", ftest_coverage.KeyPath)>;
MarshallingInfoString<CodeGenOpts<"ProfileFilterFiles">>;
def fprofile_exclude_files_EQ : Joined<["-"], "fprofile-exclude-files=">,
Group<f_Group>, Flags<[CC1Option, CoreOption]>,
HelpText<"Instrument only functions from files where names don't match all the regexes separated by a semi-colon">,
MarshallingInfoString<CodeGenOpts<"ProfileExcludeFiles">>,
ShouldParseIf<!strconcat(fprofile_arcs.KeyPath, "||", ftest_coverage.KeyPath)>;
MarshallingInfoString<CodeGenOpts<"ProfileExcludeFiles">>;
def fprofile_update_EQ : Joined<["-"], "fprofile-update=">,
Group<f_Group>, Flags<[CC1Option, CoreOption]>, Values<"atomic,prefer-atomic,single">,
MetaVarName<"<method>">, HelpText<"Set update method of profile counters">,
Expand Down Expand Up @@ -5602,14 +5600,12 @@ def fmerge_functions : Flag<["-"], "fmerge-functions">,
MarshallingInfoFlag<CodeGenOpts<"MergeFunctions">>;
def coverage_data_file : Separate<["-"], "coverage-data-file">,
HelpText<"Emit coverage data to this filename.">,
MarshallingInfoString<CodeGenOpts<"CoverageDataFile">>,
ShouldParseIf<!strconcat(fprofile_arcs.KeyPath, "||", ftest_coverage.KeyPath)>;
MarshallingInfoString<CodeGenOpts<"CoverageDataFile">>;
def coverage_data_file_EQ : Joined<["-"], "coverage-data-file=">,
Alias<coverage_data_file>;
def coverage_notes_file : Separate<["-"], "coverage-notes-file">,
HelpText<"Emit coverage notes to this filename.">,
MarshallingInfoString<CodeGenOpts<"CoverageNotesFile">>,
ShouldParseIf<!strconcat(fprofile_arcs.KeyPath, "||", ftest_coverage.KeyPath)>;
MarshallingInfoString<CodeGenOpts<"CoverageNotesFile">>;
def coverage_notes_file_EQ : Joined<["-"], "coverage-notes-file=">,
Alias<coverage_notes_file>;
def coverage_version_EQ : Joined<["-"], "coverage-version=">,
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,14 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,

static std::optional<GCOVOptions>
getGCOVOptions(const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts) {
if (!CodeGenOpts.EmitGcovArcs && !CodeGenOpts.EmitGcovNotes)
if (CodeGenOpts.CoverageNotesFile.empty() &&
CodeGenOpts.CoverageDataFile.empty())
return std::nullopt;
// Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
// LLVM's -default-gcov-version flag is set to something invalid.
GCOVOptions Options;
Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
Options.EmitData = CodeGenOpts.EmitGcovArcs;
Options.EmitNotes = !CodeGenOpts.CoverageNotesFile.empty();
Options.EmitData = !CodeGenOpts.CoverageDataFile.empty();
llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version));
Options.NoRedZone = CodeGenOpts.DisableRedZone;
Options.Filter = CodeGenOpts.ProfileFilterFiles;
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3837,8 +3837,8 @@ void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit,
// subprogram name, no need to have it at all unless coverage is enabled or
// debug is set to more than just line tables or extra debug info is needed.
if (LinkageName == Name ||
(!CGM.getCodeGenOpts().EmitGcovArcs &&
!CGM.getCodeGenOpts().EmitGcovNotes &&
(CGM.getCodeGenOpts().CoverageNotesFile.empty() &&
CGM.getCodeGenOpts().CoverageDataFile.empty() &&
!CGM.getCodeGenOpts().DebugInfoForProfiling &&
!CGM.getCodeGenOpts().PseudoProbeForProfiling &&
DebugKind <= llvm::codegenoptions::DebugLineTablesOnly))
Expand Down
10 changes: 4 additions & 6 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ CodeGenModule::CodeGenModule(ASTContext &C,
// If debug info or coverage generation is enabled, create the CGDebugInfo
// object.
if (CodeGenOpts.getDebugInfo() != llvm::codegenoptions::NoDebugInfo ||
CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)
CodeGenOpts.CoverageNotesFile.size() ||
CodeGenOpts.CoverageDataFile.size())
DebugInfo.reset(new CGDebugInfo(*this));

Block.GlobalUniqueCount = 0;
Expand Down Expand Up @@ -927,7 +928,8 @@ void CodeGenModule::Release() {
if (getCodeGenOpts().EmitDeclMetadata)
EmitDeclMetadata();

if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes)
if (getCodeGenOpts().CoverageNotesFile.size() ||
getCodeGenOpts().CoverageDataFile.size())
EmitCoverageFile();

if (CGDebugInfo *DI = getModuleDebugInfo())
Expand Down Expand Up @@ -6892,10 +6894,6 @@ void CodeGenModule::EmitCommandLineMetadata() {
}

void CodeGenModule::EmitCoverageFile() {
if (getCodeGenOpts().CoverageDataFile.empty() &&
getCodeGenOpts().CoverageNotesFile.empty())
return;

llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu");
if (!CUNode)
return;
Expand Down
4 changes: 0 additions & 4 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,10 +820,6 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
options::OPT_fno_test_coverage, false) ||
Args.hasArg(options::OPT_coverage);
bool EmitCovData = TC.needsGCovInstrumentation(Args);
if (EmitCovNotes)
CmdArgs.push_back("-ftest-coverage");
if (EmitCovData)
CmdArgs.push_back("-fprofile-arcs");

if (Args.hasFlag(options::OPT_fcoverage_mapping,
options::OPT_fno_coverage_mapping, false)) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
Opts.MemoryProfileOutput = MemProfileBasename;

memcpy(Opts.CoverageVersion, "408*", 4);
if (Opts.EmitGcovArcs || Opts.EmitGcovNotes) {
if (Opts.CoverageNotesFile.size() || Opts.CoverageDataFile.size()) {
if (Args.hasArg(OPT_coverage_version_EQ)) {
StringRef CoverageVersion = Args.getLastArgValue(OPT_coverage_version_EQ);
if (CoverageVersion.size() != 4) {
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGen/attr-function-return.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// RUN: -Werror=ignored-attributes -mfunction-return=thunk-extern \
// RUN: | FileCheck %s --check-prefixes=CHECK,CHECK-EXTERN
// RUN: %clang_cc1 -std=gnu2x -triple x86_64-linux-gnu %s -emit-llvm -o - \
// RUN: -mfunction-return=thunk-extern -fprofile-arcs \
// RUN: -mfunction-return=thunk-extern -coverage-data-file /dev/null \
// RUN: | FileCheck %s --check-prefix=CHECK-GCOV
// RUN: %clang_cc1 -std=gnu2x -triple x86_64-linux-gnu %s -emit-llvm -o - \
// RUN: -mfunction-return=thunk-extern -fsanitize=address \
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGen/code-coverage-tsan.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// -fprofile-update=atomic (implied by -fsanitize=thread) requires the
/// (potentially concurrent) counter updates to be atomic.
// RUN: %clang_cc1 %s -triple x86_64 -emit-llvm -fprofile-update=atomic -ftest-coverage -fprofile-arcs \
// RUN: %clang_cc1 %s -triple x86_64 -emit-llvm -fprofile-update=atomic \
// RUN: -coverage-notes-file /dev/null -coverage-data-file /dev/null -o - | FileCheck %s

// CHECK-LABEL: void @foo()
Expand Down
12 changes: 6 additions & 6 deletions clang/test/CodeGen/code-coverage.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
/// 3.4 redesigns the format and changed .da to .gcda
/// 4.7 enables cfg_checksum.
/// 4.8 (default, compatible with gcov 7) emits the exit block the second.
// RUN: %clang_cc1 -emit-llvm -disable-red-zone -fprofile-arcs -coverage-version='304*' %s -o - | \
// RUN: %clang_cc1 -emit-llvm -disable-red-zone -coverage-data-file /dev/null -coverage-version='304*' %s -o - | \
// RUN: FileCheck --check-prefixes=CHECK,304 %s
// RUN: %clang_cc1 -emit-llvm -disable-red-zone -fprofile-arcs -coverage-version='407*' %s -o - | \
// RUN: %clang_cc1 -emit-llvm -disable-red-zone -coverage-data-file /dev/null -coverage-version='407*' %s -o - | \
// RUN: FileCheck --check-prefixes=CHECK,407 %s
// RUN: %clang_cc1 -emit-llvm -disable-red-zone -fprofile-arcs %s -o - | \
// RUN: %clang_cc1 -emit-llvm -disable-red-zone -coverage-data-file /dev/null %s -o - | \
// RUN: FileCheck --check-prefixes=CHECK,408 %s

// RUN: %clang_cc1 -emit-llvm -disable-red-zone -fprofile-arcs -coverage-notes-file=aaa.gcno -coverage-data-file=bbb.gcda -debug-info-kind=limited -dwarf-version=4 %s -o - | FileCheck %s --check-prefix GCOV_FILE_INFO
// RUN: %clang_cc1 -emit-llvm -disable-red-zone -coverage-notes-file=aaa.gcno -coverage-data-file=bbb.gcda -debug-info-kind=limited -dwarf-version=4 %s -o - | FileCheck %s --check-prefix GCOV_FILE_INFO

// RUN: %clang_cc1 -emit-llvm-bc -o /dev/null -fdebug-pass-manager -fprofile-arcs %s 2>&1 | FileCheck --check-prefix=NEWPM %s
// RUN: %clang_cc1 -emit-llvm-bc -o /dev/null -fdebug-pass-manager -fprofile-arcs -O3 %s 2>&1 | FileCheck --check-prefix=NEWPM-O3 %s
// RUN: %clang_cc1 -emit-llvm-bc -o /dev/null -fdebug-pass-manager -coverage-data-file=/dev/null %s 2>&1 | FileCheck --check-prefix=NEWPM %s
// RUN: %clang_cc1 -emit-llvm-bc -o /dev/null -fdebug-pass-manager -coverage-data-file=/dev/null -O3 %s 2>&1 | FileCheck --check-prefix=NEWPM-O3 %s

// NEWPM-NOT: Running pass
// NEWPM: Running pass: GCOVProfilerPass
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGen/no_profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// RUN: -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -fprofile-instrument=clang -disable-llvm-passes \
// RUN: -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -fprofile-arcs -disable-llvm-passes \
// RUN: %clang_cc1 -coverage-data-file /dev/null -disable-llvm-passes \
// RUN: -emit-llvm -o - %s | FileCheck %s
int g(int);

Expand Down
2 changes: 0 additions & 2 deletions clang/test/Driver/coverage.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// RUN: %clang -### -S -ftest-coverage %s 2>&1 | FileCheck --check-prefix=TEST-COVERAGE %s
// RUN: %clang -### -S -ftest-coverage -fno-test-coverage %s 2>&1 | FileCheck --check-prefix=NO-TEST-COVERAGE %s

// TEST-COVERAGE: "-ftest-coverage"
// TEST-COVERAGE: "-coverage-notes-file" "coverage.gcno"
// NO-TEST-COVERAGE-NOT: "-coverage-notes-file"

// RUN: %clang -### -S -fprofile-arcs %s 2>&1 | FileCheck --check-prefix=PROFILE-ARCS %s
// RUN: %clang -### -S -fprofile-arcs -fno-profile-arcs %s 2>&1 | FileCheck --check-prefix=NO-PROFILE-ARCS %s

// NO-PROFILE-ARCS-NOT: "-coverage-notes-file"
// PROFILE-ARCS: "-fprofile-arcs"
// PROFILE-ARCS: "-coverage-data-file" "coverage.gcda"

// RUN: %clang -### -S -ftest-coverage %s -o /foo/bar.o 2>&1 | FileCheck --check-prefix=GCNO-LOCATION %s
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Driver/cuda-no-pgo-or-coverage.cu
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
// CHECK-NOT: "-fprofile{{[^"]*}}"
// CHECK: "-triple" "x86_64-unknown-linux-gnu"
// PROF: "-fprofile{{.*}}"
// GCOV: "-ftest-coverage"
// GCOV: "-coverage-notes-file"

0 comments on commit 71a35f7

Please sign in to comment.