From 71a35f7e3d6c78f8035f2eb7d58beba3b7208f9d Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Wed, 17 May 2023 16:09:12 -0700 Subject: [PATCH] [gcov] Simplify cc1 options and remove CodeGenOptions EmitCovNotes/EmitCovArcs After a07b135ce0c0111bd83450b5dc29ef0381cdbc39, 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. --- clang/include/clang/Basic/CodeGenOptions.def | 2 -- clang/include/clang/Driver/Options.td | 24 ++++++++++-------------- clang/lib/CodeGen/BackendUtil.cpp | 7 ++++--- clang/lib/CodeGen/CGDebugInfo.cpp | 4 ++-- clang/lib/CodeGen/CodeGenModule.cpp | 10 ++++------ clang/lib/Driver/ToolChains/Clang.cpp | 4 ---- clang/lib/Frontend/CompilerInvocation.cpp | 2 +- clang/test/CodeGen/attr-function-return.c | 2 +- clang/test/CodeGen/code-coverage-tsan.c | 2 +- clang/test/CodeGen/code-coverage.c | 12 ++++++------ clang/test/CodeGen/no_profile.c | 2 +- clang/test/Driver/coverage.c | 2 -- clang/test/Driver/cuda-no-pgo-or-coverage.cu | 2 +- 13 files changed, 31 insertions(+), 44 deletions(-) diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def index c026d8e..5752c6e 100644 --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -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). diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 9d765cc..f302669 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -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; -defm profile_arcs : BoolFOption<"profile-arcs", - CodeGenOpts<"EmitGcovArcs">, DefaultFalse, - PosFlag, NegFlag>; -defm test_coverage : BoolFOption<"test-coverage", - CodeGenOpts<"EmitGcovNotes">, DefaultFalse, - PosFlag, NegFlag>; +def ftest_coverage : Flag<["-"], "ftest-coverage">, Group, + HelpText<"Produce gcov notes files (*.gcno)">; +def fno_test_coverage : Flag<["-"], "fno-test-coverage">, Group; +def fprofile_arcs : Flag<["-"], "fprofile-arcs">, Group, + HelpText<"Instrument code to produce gcov data files (*.gcda)">; +def fno_profile_arcs : Flag<["-"], "fno-profile-arcs">, Group; def fprofile_filter_files_EQ : Joined<["-"], "fprofile-filter-files=">, Group, Flags<[CC1Option, CoreOption]>, HelpText<"Instrument only functions from files where names match any regex separated by a semi-colon">, - MarshallingInfoString>, - ShouldParseIf; + MarshallingInfoString>; def fprofile_exclude_files_EQ : Joined<["-"], "fprofile-exclude-files=">, Group, Flags<[CC1Option, CoreOption]>, HelpText<"Instrument only functions from files where names don't match all the regexes separated by a semi-colon">, - MarshallingInfoString>, - ShouldParseIf; + MarshallingInfoString>; def fprofile_update_EQ : Joined<["-"], "fprofile-update=">, Group, Flags<[CC1Option, CoreOption]>, Values<"atomic,prefer-atomic,single">, MetaVarName<"">, HelpText<"Set update method of profile counters">, @@ -5602,14 +5600,12 @@ def fmerge_functions : Flag<["-"], "fmerge-functions">, MarshallingInfoFlag>; def coverage_data_file : Separate<["-"], "coverage-data-file">, HelpText<"Emit coverage data to this filename.">, - MarshallingInfoString>, - ShouldParseIf; + MarshallingInfoString>; def coverage_data_file_EQ : Joined<["-"], "coverage-data-file=">, Alias; def coverage_notes_file : Separate<["-"], "coverage-notes-file">, HelpText<"Emit coverage notes to this filename.">, - MarshallingInfoString>, - ShouldParseIf; + MarshallingInfoString>; def coverage_notes_file_EQ : Joined<["-"], "coverage-notes-file=">, Alias; def coverage_version_EQ : Joined<["-"], "coverage-version=">, diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 4e80c58..d62d00a 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -486,13 +486,14 @@ static bool initTargetOptions(DiagnosticsEngine &Diags, static std::optional 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; diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 2cf7b02..2857ea2 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -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)) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index d3cde11..5cd29d3 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -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; @@ -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()) @@ -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; diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index cbf12a1..f9b0f8d 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -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)) { diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index c111607..a1d836f 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -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) { diff --git a/clang/test/CodeGen/attr-function-return.c b/clang/test/CodeGen/attr-function-return.c index 2187e01..cbb1008 100644 --- a/clang/test/CodeGen/attr-function-return.c +++ b/clang/test/CodeGen/attr-function-return.c @@ -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 \ diff --git a/clang/test/CodeGen/code-coverage-tsan.c b/clang/test/CodeGen/code-coverage-tsan.c index c7928fd..cb37a38 100644 --- a/clang/test/CodeGen/code-coverage-tsan.c +++ b/clang/test/CodeGen/code-coverage-tsan.c @@ -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() diff --git a/clang/test/CodeGen/code-coverage.c b/clang/test/CodeGen/code-coverage.c index 3748f23..10fe4c3 100644 --- a/clang/test/CodeGen/code-coverage.c +++ b/clang/test/CodeGen/code-coverage.c @@ -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 diff --git a/clang/test/CodeGen/no_profile.c b/clang/test/CodeGen/no_profile.c index dde3da1..e426ae5 100644 --- a/clang/test/CodeGen/no_profile.c +++ b/clang/test/CodeGen/no_profile.c @@ -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); diff --git a/clang/test/Driver/coverage.c b/clang/test/Driver/coverage.c index bb0bf45..b3118f0 100644 --- a/clang/test/Driver/coverage.c +++ b/clang/test/Driver/coverage.c @@ -1,7 +1,6 @@ // 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" @@ -9,7 +8,6 @@ // 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 diff --git a/clang/test/Driver/cuda-no-pgo-or-coverage.cu b/clang/test/Driver/cuda-no-pgo-or-coverage.cu index 7045a2b..15bc010 100644 --- a/clang/test/Driver/cuda-no-pgo-or-coverage.cu +++ b/clang/test/Driver/cuda-no-pgo-or-coverage.cu @@ -30,4 +30,4 @@ // CHECK-NOT: "-fprofile{{[^"]*}}" // CHECK: "-triple" "x86_64-unknown-linux-gnu" // PROF: "-fprofile{{.*}}" -// GCOV: "-ftest-coverage" +// GCOV: "-coverage-notes-file" -- 2.7.4