https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/246727
Renames, with the old spellings kept as aliases: ``` -enable-single-byte-coverage -> -single-byte-coverage -enable-value-profiling -> -value-profiling ``` LLM-aided >From 4bcce317f1145c939cf2fdba2f501819ddd9161e Mon Sep 17 00:00:00 2001 From: Fangrui Song <[email protected]> Date: Wed, 18 Nov 2026 21:53:00 -0800 Subject: [PATCH] [clang CodeGen] Declare command line options in TableGen Renames, with the old spellings kept as aliases: ``` -enable-single-byte-coverage -> -single-byte-coverage -enable-value-profiling -> -value-profiling ``` LLM-aided --- clang/lib/CodeGen/BackendUtil.cpp | 61 +++++++------------ clang/lib/CodeGen/CGExpr.cpp | 12 +--- clang/lib/CodeGen/CGExprScalar.cpp | 7 +-- clang/lib/CodeGen/CMakeLists.txt | 7 +++ clang/lib/CodeGen/CodeGenAction.cpp | 3 +- clang/lib/CodeGen/CodeGenCLOptions.cpp | 21 +++++++ clang/lib/CodeGen/CodeGenCLOptions.h | 25 ++++++++ clang/lib/CodeGen/CodeGenCLOptions.td | 51 ++++++++++++++++ clang/lib/CodeGen/CodeGenModule.cpp | 14 ++--- clang/lib/CodeGen/CodeGenModule.h | 3 + clang/lib/CodeGen/CodeGenPGO.cpp | 23 +++---- clang/lib/CodeGen/CoverageMappingGen.cpp | 54 ++++++---------- clang/lib/CodeGen/CoverageMappingGen.h | 15 ++--- .../gn/secondary/clang/lib/CodeGen/BUILD.gn | 10 +++ .../llvm-project-overlay/clang/BUILD.bazel | 11 ++++ 15 files changed, 195 insertions(+), 122 deletions(-) create mode 100644 clang/lib/CodeGen/CodeGenCLOptions.cpp create mode 100644 clang/lib/CodeGen/CodeGenCLOptions.h create mode 100644 clang/lib/CodeGen/CodeGenCLOptions.td diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 0ef49e16ca6072..d86c52432b42eb 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -8,6 +8,7 @@ #include "clang/CodeGen/BackendUtil.h" #include "BackendConsumer.h" +#include "CodeGenCLOptions.h" #include "LinkInModulesPass.h" #include "clang/Basic/CodeGenOptions.h" #include "clang/Basic/Diagnostic.h" @@ -119,31 +120,6 @@ using namespace llvm; llvm::PassPluginLibraryInfo get##Ext##PluginInfo(); #include "llvm/Support/Extension.def" -namespace llvm { -// Experiment to move sanitizers earlier. -static cl::opt<bool> ClSanitizeOnOptimizerEarlyEP( - "sanitizer-early-opt-ep", - cl::desc("Insert sanitizers on OptimizerEarlyEP.")); - -// Experiment to mark cold functions as optsize/minsize/optnone. -// TODO: remove once this is exposed as a proper driver flag. -static cl::opt<PGOOptions::ColdFuncOpt> ClPGOColdFuncAttr( - "pgo-cold-func-opt", cl::init(PGOOptions::ColdFuncOpt::Default), cl::Hidden, - cl::desc( - "Function attribute to apply to cold functions as determined by PGO"), - cl::values(clEnumValN(PGOOptions::ColdFuncOpt::Default, "default", - "Default (no attribute)"), - clEnumValN(PGOOptions::ColdFuncOpt::OptSize, "optsize", - "Mark cold functions with optsize."), - clEnumValN(PGOOptions::ColdFuncOpt::MinSize, "minsize", - "Mark cold functions with minsize."), - clEnumValN(PGOOptions::ColdFuncOpt::OptNone, "optnone", - "Mark cold functions with optnone."))); -} // namespace llvm -namespace clang { -extern llvm::cl::opt<bool> ClSanitizeGuardChecks; -} - // Path and name of file used for profile generation static std::string getProfileGenName(const cl::LibraryOptionsContext &LibraryOptions, @@ -655,7 +631,8 @@ static void addKCFIPass(const Triple &TargetTriple, const LangOptions &LangOpts, }); } -static void addSanitizers(const Triple &TargetTriple, +static void addSanitizers(const CodeGen::CodeGenCLOptions &CLOpts, + const Triple &TargetTriple, const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts, PassBuilder &PB) { auto SanitizersCallback = [&](ModulePassManager &MPM, OptimizationLevel Level, @@ -756,7 +733,7 @@ static void addSanitizers(const Triple &TargetTriple, PB.getVirtualFileSystemPtr())); } }; - if (ClSanitizeOnOptimizerEarlyEP) { + if (CLOpts.sanitizer_early_opt_ep) { PB.registerOptimizerEarlyEPCallback( [SanitizersCallback](ModulePassManager &MPM, OptimizationLevel Level, ThinOrFullLTOPhase Phase) { @@ -848,13 +825,15 @@ void EmitAssemblyHelper::RunOptimizationPipeline( std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC) { const cl::LibraryOptionsContext &LibraryOptions = TheModule->getContext().getLibraryOptions(); + const CodeGen::CodeGenCLOptions &CLOpts = + CodeGen::getCodeGenCLOptions(TheModule->getContext()); std::optional<PGOOptions> PGOOpt; if (CodeGenOpts.hasProfileIRInstr()) // -fprofile-generate. PGOOpt = PGOOptions(getProfileGenName(LibraryOptions, CodeGenOpts), "", "", CodeGenOpts.MemoryProfileUsePath, PGOOptions::IRInstr, - PGOOptions::NoCSAction, ClPGOColdFuncAttr, + PGOOptions::NoCSAction, CLOpts.pgo_cold_func_opt, CodeGenOpts.DebugInfoForProfiling, /*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate); @@ -865,29 +844,30 @@ void EmitAssemblyHelper::RunOptimizationPipeline( PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "", CodeGenOpts.ProfileRemappingFile, CodeGenOpts.MemoryProfileUsePath, PGOOptions::IRUse, - CSAction, ClPGOColdFuncAttr, + CSAction, CLOpts.pgo_cold_func_opt, CodeGenOpts.DebugInfoForProfiling); } else if (!CodeGenOpts.SampleProfileFile.empty()) // -fprofile-sample-use PGOOpt = PGOOptions( CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile, CodeGenOpts.MemoryProfileUsePath, PGOOptions::SampleUse, - PGOOptions::NoCSAction, ClPGOColdFuncAttr, + PGOOptions::NoCSAction, CLOpts.pgo_cold_func_opt, CodeGenOpts.DebugInfoForProfiling, CodeGenOpts.PseudoProbeForProfiling); else if (!CodeGenOpts.MemoryProfileUsePath.empty()) // -fmemory-profile-use (without any of the above options) - PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, - PGOOptions::NoAction, PGOOptions::NoCSAction, - ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling); + PGOOpt = + PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, + PGOOptions::NoAction, PGOOptions::NoCSAction, + CLOpts.pgo_cold_func_opt, CodeGenOpts.DebugInfoForProfiling); else if (CodeGenOpts.PseudoProbeForProfiling) // -fpseudo-probe-for-profiling PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", PGOOptions::NoAction, - PGOOptions::NoCSAction, ClPGOColdFuncAttr, + PGOOptions::NoCSAction, CLOpts.pgo_cold_func_opt, CodeGenOpts.DebugInfoForProfiling, true); else if (CodeGenOpts.DebugInfoForProfiling) // -fdebug-info-for-profiling PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", PGOOptions::NoAction, - PGOOptions::NoCSAction, ClPGOColdFuncAttr, true); + PGOOptions::NoCSAction, CLOpts.pgo_cold_func_opt, true); // Check to see if we want to generate a CS profile. if (CodeGenOpts.hasProfileCSIRInstr()) { @@ -905,7 +885,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline( PGOOpt = PGOOptions( "", getProfileGenName(LibraryOptions, CodeGenOpts), "", /*MemoryProfile=*/"", PGOOptions::NoAction, PGOOptions::CSIRInstr, - ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling); + CLOpts.pgo_cold_func_opt, CodeGenOpts.DebugInfoForProfiling); } if (TM) TM->setPGOOption(PGOOpt); @@ -1061,11 +1041,12 @@ void EmitAssemblyHelper::RunOptimizationPipeline( // Register callbacks to schedule sanitizer passes at the appropriate part // of the pipeline. if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) - PB.registerScalarOptimizerLateEPCallback([this](FunctionPassManager &FPM, - OptimizationLevel Level) { + PB.registerScalarOptimizerLateEPCallback([this, &CLOpts]( + FunctionPassManager &FPM, + OptimizationLevel Level) { BoundsCheckingPass::Options Options; if (CodeGenOpts.SanitizeSkipHotCutoffs[SanitizerKind::SO_LocalBounds] || - ClSanitizeGuardChecks) { + CLOpts.ubsan_guard_checks) { static_assert(SanitizerKind::SO_LocalBounds <= std::numeric_limits< decltype(Options.GuardKind)::value_type>::max(), @@ -1090,7 +1071,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline( if (!IsThinLTOPostLink) { // Most sanitizers only run during PreLink stage. - addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB); + addSanitizers(CLOpts, TargetTriple, CodeGenOpts, LangOpts, PB); addKCFIPass(TargetTriple, LangOpts, PB); addLowerAllowCheckPass(LibraryOptions, CodeGenOpts, LangOpts, PB); diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 4a0a043a00b266..64419f5d62519f 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -20,6 +20,7 @@ #include "CGObjCRuntime.h" #include "CGOpenMPRuntime.h" #include "CGRecordLayout.h" +#include "CodeGenCLOptions.h" #include "CodeGenFunction.h" #include "CodeGenModule.h" #include "CodeGenPGO.h" @@ -67,15 +68,6 @@ using namespace clang; using namespace CodeGen; -namespace clang { -// TODO: consider deprecating ClSanitizeGuardChecks; functionality is subsumed -// by -fsanitize-skip-hot-cutoff -llvm::cl::opt<bool> ClSanitizeGuardChecks( - "ubsan-guard-checks", - llvm::cl::desc("Guard UBSAN checks with `llvm.allow.ubsan.check()`.")); - -} // namespace clang - //===--------------------------------------------------------------------===// // Defines for metadata //===--------------------------------------------------------------------===// @@ -4352,7 +4344,7 @@ void CodeGenFunction::EmitCheck( // LowerAllowCheckPass. for (auto &[Check, Ord] : Checked) { llvm::Value *GuardedCheck = Check; - if (ClSanitizeGuardChecks || + if (CGM.getCLOpts().ubsan_guard_checks || (CGM.getCodeGenOpts().SanitizeSkipHotCutoffs[Ord] > 0)) { llvm::Value *Allow = Builder.CreateCall( CGM.getIntrinsic(llvm::Intrinsic::allow_ubsan_check), diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 768e01deb9ceb3..4ced42154b6a28 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -17,6 +17,7 @@ #include "CGObjCRuntime.h" #include "CGOpenMPRuntime.h" #include "CGRecordLayout.h" +#include "CodeGenCLOptions.h" #include "CodeGenFunction.h" #include "CodeGenModule.h" #include "ConstantEmitter.h" @@ -64,10 +65,6 @@ using llvm::Value; // Scalar Expression Emitter //===----------------------------------------------------------------------===// -namespace llvm { -extern cl::opt<bool> EnableSingleByteCoverage; -} // namespace llvm - namespace { /// Determine whether the given binary operation may overflow. @@ -6015,7 +6012,7 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { // If this is a really simple expression (like x ? 4 : 5), emit this as a // select instead of as control flow. We can only do this if it is cheap and // safe to evaluate the LHS and RHS unconditionally. - if (!llvm::EnableSingleByteCoverage && + if (!CGF.CGM.getCLOpts().single_byte_coverage && CodeGenUtils::isCheapEnoughToEvaluateUnconditionally(lhsExpr, CGF.getContext()) && CodeGenUtils::isCheapEnoughToEvaluateUnconditionally(rhsExpr, diff --git a/clang/lib/CodeGen/CMakeLists.txt b/clang/lib/CodeGen/CMakeLists.txt index 93790567dcf8bb..1ac56d0ad1b83d 100644 --- a/clang/lib/CodeGen/CMakeLists.txt +++ b/clang/lib/CodeGen/CMakeLists.txt @@ -26,6 +26,7 @@ set(LLVM_LINK_COMPONENTS MC ObjCARCOpts Object + Option Passes Plugins ProfileData @@ -57,6 +58,10 @@ if(MSVC AND NOT CMAKE_CXX_COMPILER_ID MATCHES Clang endif() endif() +set(LLVM_TARGET_DEFINITIONS CodeGenCLOptions.td) +llvm_tablegen(CodeGenCLOptions.inc -gen-opt-parser-defs) +add_public_tablegen_target(CodeGenCLOptionsTableGen) + add_clang_library(clangCodeGen ABIInfo.cpp ABIInfoImpl.cpp @@ -102,6 +107,7 @@ add_clang_library(clangCodeGen CGVTables.cpp CodeGenABITypes.cpp CodeGenAction.cpp + CodeGenCLOptions.cpp CodeGenFunction.cpp CodeGenModule.cpp CodeGenPGO.cpp @@ -169,6 +175,7 @@ add_clang_library(clangCodeGen [["pch.h"]] DEPENDS + CodeGenCLOptionsTableGen vt_gen intrinsics_gen ClangDriverOptions diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp index dd7ba0b741c857..91f8b666fb83d7 100644 --- a/clang/lib/CodeGen/CodeGenAction.cpp +++ b/clang/lib/CodeGen/CodeGenAction.cpp @@ -10,6 +10,7 @@ #include "BackendConsumer.h" #include "CGCall.h" #include "CGDebugInfo.h" +#include "CodeGenCLOptions.h" #include "CodeGenModule.h" #include "CoverageMappingGen.h" #include "MacroPPCallbacks.h" @@ -391,7 +392,7 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { // Add the preprocessor callback only when the coverage mapping is generated. if (CI.getCodeGenOpts().CoverageMapping) CoverageInfo = CodeGen::CoverageMappingModuleGen::setUpCoverageCallbacks( - CI.getPreprocessor()); + CodeGen::getCodeGenCLOptions(*VMContext), CI.getPreprocessor()); std::unique_ptr<BackendConsumer> Result(new BackendConsumer( CI, BA, CI.getVirtualFileSystemPtr(), *VMContext, std::move(LinkModules), diff --git a/clang/lib/CodeGen/CodeGenCLOptions.cpp b/clang/lib/CodeGen/CodeGenCLOptions.cpp new file mode 100644 index 00000000000000..a41f7a4364baa4 --- /dev/null +++ b/clang/lib/CodeGen/CodeGenCLOptions.cpp @@ -0,0 +1,21 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "CodeGenCLOptions.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/Option/LibraryOptions.h" + +#define OPTIONS_STRUCT_DEFS +#include "CodeGenCLOptions.inc" + +LLVM_REGISTER_LIBRARY_OPTIONS(clang::CodeGen::CodeGenCLOptions) + +const clang::CodeGen::CodeGenCLOptions & +clang::CodeGen::getCodeGenCLOptions(const llvm::LLVMContext &Ctx) { + return Ctx.getLibraryOptions().get<CodeGenCLOptions>(); +} diff --git a/clang/lib/CodeGen/CodeGenCLOptions.h b/clang/lib/CodeGen/CodeGenCLOptions.h new file mode 100644 index 00000000000000..afb64ed9a92d3d --- /dev/null +++ b/clang/lib/CodeGen/CodeGenCLOptions.h @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENCLOPTIONS_H +#define LLVM_CLANG_LIB_CODEGEN_CODEGENCLOPTIONS_H + +#include "llvm/Support/PGOOptions.h" + +#define OPTIONS_STRUCT_DECL +#include "CodeGenCLOptions.inc" + +namespace llvm { +class LLVMContext; +} // namespace llvm + +namespace clang::CodeGen { +const CodeGenCLOptions &getCodeGenCLOptions(const llvm::LLVMContext &Ctx); +} // namespace clang::CodeGen + +#endif // LLVM_CLANG_LIB_CODEGEN_CODEGENCLOPTIONS_H diff --git a/clang/lib/CodeGen/CodeGenCLOptions.td b/clang/lib/CodeGen/CodeGenCLOptions.td new file mode 100644 index 00000000000000..775a7799c32ba3 --- /dev/null +++ b/clang/lib/CodeGen/CodeGenCLOptions.td @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +include "llvm/Option/LibraryOptions.td" + +def CodeGenCLOptions : OptionsStruct<"clang::CodeGen">; + +// BackendUtil.cpp +// Experiment to mark cold functions as optsize/minsize/optnone. +// TODO: remove once this is exposed as a proper driver flag. +defm : EnumField<"pgo-cold-func-opt", "llvm::PGOOptions::ColdFuncOpt", + "llvm::PGOOptions::ColdFuncOpt::Default", "default,optsize,minsize,optnone", + ["llvm::PGOOptions::ColdFuncOpt::Default", + "llvm::PGOOptions::ColdFuncOpt::OptSize", + "llvm::PGOOptions::ColdFuncOpt::MinSize", + "llvm::PGOOptions::ColdFuncOpt::OptNone"], + "Function attribute to apply to cold functions as determined by PGO">; +// Experiment to move sanitizers earlier. +defm : BoolField<"sanitizer-early-opt-ep", "false", + "Insert sanitizers on OptimizerEarlyEP">; + +// CGExpr.cpp +// TODO: consider deprecating ubsan-guard-checks; functionality is subsumed by +// -fsanitize-skip-hot-cutoff +defm : BoolField<"ubsan-guard-checks", "false", + "Guard UBSAN checks with `llvm.allow.ubsan.check()`">; + +// CodeGenModule.cpp +defm : BoolField<"limited-coverage-experimental", "false", + "Emit limited coverage mapping information (experimental)">; + +// CodeGenPGO.cpp +defm : BoolField<"value-profiling", "false", + "Enable value profiling">; +// TODO: Remove the old spelling after 2027-05. +def : AliasOf<"enable-value-profiling", "value-profiling">; + +// CoverageMappingGen.cpp +defm : BoolField<"emptyline-comment-coverage", "true", + "Emit emptylines and comment lines as skipped regions (only disable it on test)">; +defm : BoolField<"single-byte-coverage", "false", + "Enable single byte coverage">; +defm : BoolField<"system-headers-coverage", "false", + "Enable collecting coverage from system headers">; +// TODO: Remove the old spelling after 2027-05. +def : AliasOf<"enable-single-byte-coverage", "single-byte-coverage">; diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 390ca6dbba4616..d4363862722adc 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -22,6 +22,7 @@ #include "CGOpenCLRuntime.h" #include "CGOpenMPRuntime.h" #include "CGOpenMPRuntimeGPU.h" +#include "CodeGenCLOptions.h" #include "CodeGenFunction.h" #include "CodeGenPGO.h" #include "ConstantEmitter.h" @@ -72,7 +73,6 @@ #include "llvm/Support/ARMBuildAttributes.h" #include "llvm/Support/CRC.h" #include "llvm/Support/CodeGen.h" -#include "llvm/Support/CommandLine.h" #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/TimeProfiler.h" @@ -91,10 +91,6 @@ using namespace clang; using namespace CodeGen; -static llvm::cl::opt<bool> LimitedCoverage( - "limited-coverage-experimental", llvm::cl::Hidden, - llvm::cl::desc("Emit limited coverage mapping information (experimental)")); - static const char AnnotationSection[] = "llvm.metadata"; static constexpr auto ErrnoTBAAMDName = "llvm.errno.tbaa"; @@ -603,7 +599,8 @@ CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO, llvm::Module &M, DiagnosticsEngine &diags, CoverageSourceInfo *CoverageInfo) - : Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO), + : CLOpts(getCodeGenCLOptions(M.getContext())), Context(C), + LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO), PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags), Target(C.getTargetInfo()), ABI(createCXXABI(*this)), VMContext(M.getContext()), VTables(*this), StackHandler(diags), @@ -8441,9 +8438,10 @@ void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) { if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody()) break; SourceManager &SM = getContext().getSourceManager(); - if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc())) + if (CLOpts.limited_coverage_experimental && + SM.getMainFileID() != SM.getFileID(D->getBeginLoc())) break; - if (!llvm::coverage::SystemHeadersCoverage && + if (!CLOpts.system_headers_coverage && SM.isInSystemHeader(D->getBeginLoc())) break; DeferredEmptyCoverageMappingDecls.try_emplace(D, true); diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index c6b13f58817843..d110c454af87ad 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -101,6 +101,7 @@ class CGOpenMPRuntime; class CGCUDARuntime; class CGHLSLRuntime; class CGFunctionInfo; +struct CodeGenCLOptions; class CoverageMappingModuleGen; class QualTypeMapper; class TargetCodeGenInfo; @@ -352,6 +353,7 @@ class CodeGenModule : public CodeGenTypeCache { typedef std::vector<Structor> CtorList; private: + const CodeGenCLOptions &CLOpts; ASTContext &Context; const LangOptions &LangOpts; IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS; // Only used for debug info. @@ -908,6 +910,7 @@ class CodeGenModule : public CodeGenTypeCache { const { return HeaderSearchOpts; } const PreprocessorOptions &getPreprocessorOpts() const { return PreprocessorOpts; } + const CodeGenCLOptions &getCLOpts() const { return CLOpts; } const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } llvm::Module &getModule() const { return TheModule; } DiagnosticsEngine &getDiags() const { return Diags; } diff --git a/clang/lib/CodeGen/CodeGenPGO.cpp b/clang/lib/CodeGen/CodeGenPGO.cpp index 1b16be8106f561..442f35cce191d2 100644 --- a/clang/lib/CodeGen/CodeGenPGO.cpp +++ b/clang/lib/CodeGen/CodeGenPGO.cpp @@ -12,6 +12,7 @@ #include "CodeGenPGO.h" #include "CGDebugInfo.h" +#include "CodeGenCLOptions.h" #include "CodeGenFunction.h" #include "CoverageMappingGen.h" #include "clang/AST/RecursiveASTVisitor.h" @@ -19,20 +20,10 @@ #include "clang/Basic/DiagnosticFrontend.h" #include "llvm/IR/Intrinsics.h" #include "llvm/IR/MDBuilder.h" -#include "llvm/Support/CommandLine.h" #include "llvm/Support/Endian.h" #include "llvm/Support/MD5.h" #include <optional> -namespace llvm { -extern cl::opt<bool> EnableSingleByteCoverage; -} // namespace llvm - -static llvm::cl::opt<bool> - EnableValueProfiling("enable-value-profiling", - llvm::cl::desc("Enable value profiling"), - llvm::cl::Hidden, llvm::cl::init(false)); - using namespace clang; using namespace CodeGen; @@ -952,7 +943,7 @@ void CodeGenPGO::assignRegionCounters(GlobalDecl GD, llvm::Function *Fn) { return; SourceManager &SM = CGM.getContext().getSourceManager(); - if (!llvm::coverage::SystemHeadersCoverage && + if (!CGM.getCLOpts().system_headers_coverage && SM.isInSystemHeader(D->getLocation())) return; @@ -1029,7 +1020,7 @@ bool CodeGenPGO::skipRegionMappingForDecl(const Decl *D) { // Don't map the functions in system headers. const auto &SM = CGM.getContext().getSourceManager(); auto Loc = D->getBody()->getBeginLoc(); - return !llvm::coverage::SystemHeadersCoverage && SM.isInSystemHeader(Loc); + return !CGM.getCLOpts().system_headers_coverage && SM.isInSystemHeader(Loc); } void CodeGenPGO::emitCounterRegionMapping(const Decl *D) { @@ -1143,7 +1134,7 @@ void CodeGenPGO::emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S, NormalizedFuncNameVarPtr, Builder.getInt64(FunctionHash), Builder.getInt32(NumRegionCounters), Builder.getInt32(Counter), StepV}; - if (llvm::EnableSingleByteCoverage) { + if (CGM.getCLOpts().single_byte_coverage) { assert(!StepV && "StepV is not supported in single byte counter mode"); Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::instrprof_cover), ArrayRef(Args, 4)); @@ -1298,12 +1289,12 @@ void CodeGenPGO::emitMCDCCondBitmapUpdate(CGBuilderTy &Builder, const Expr *S, void CodeGenPGO::setValueProfilingFlag(llvm::Module &M) { if (CGM.getCodeGenOpts().hasProfileClangInstr()) M.addModuleFlag(llvm::Module::Warning, "EnableValueProfiling", - uint32_t(EnableValueProfiling)); + uint32_t(CGM.getCLOpts().value_profiling)); } void CodeGenPGO::setProfileVersion(llvm::Module &M) { if (CGM.getCodeGenOpts().hasProfileClangInstr() && - llvm::EnableSingleByteCoverage) { + CGM.getCLOpts().single_byte_coverage) { const StringRef VarName(INSTR_PROF_QUOTE(INSTR_PROF_RAW_VERSION_VAR)); llvm::Type *IntTy64 = llvm::Type::getInt64Ty(M.getContext()); uint64_t ProfileVersion = @@ -1333,7 +1324,7 @@ void CodeGenPGO::setProfileVersion(llvm::Module &M) { void CodeGenPGO::valueProfile(CGBuilderTy &Builder, uint32_t ValueKind, llvm::Instruction *ValueSite, llvm::Value *ValuePtr) { - if (!EnableValueProfiling) + if (!CGM.getCLOpts().value_profiling) return; if (!ValuePtr || !ValueSite || !Builder.GetInsertBlock()) diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp index 970812f7b0f754..e7b5700d9f34a5 100644 --- a/clang/lib/CodeGen/CoverageMappingGen.cpp +++ b/clang/lib/CodeGen/CoverageMappingGen.cpp @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// #include "CoverageMappingGen.h" +#include "CodeGenCLOptions.h" #include "CodeGenFunction.h" #include "CodeGenPGO.h" #include "clang/AST/StmtVisitor.h" @@ -31,36 +32,17 @@ // is textually included. #define COVMAP_V3 -namespace llvm { -cl::opt<bool> - EnableSingleByteCoverage("enable-single-byte-coverage", - llvm::cl::desc("Enable single byte coverage"), - llvm::cl::Hidden, llvm::cl::init(false)); -} // namespace llvm - -static llvm::cl::opt<bool> EmptyLineCommentCoverage( - "emptyline-comment-coverage", - llvm::cl::desc("Emit emptylines and comment lines as skipped regions (only " - "disable it on test)"), - llvm::cl::init(true), llvm::cl::Hidden); - -namespace llvm::coverage { -cl::opt<bool> SystemHeadersCoverage( - "system-headers-coverage", - cl::desc("Enable collecting coverage from system headers"), cl::init(false), - cl::Hidden); -} - using namespace clang; using namespace CodeGen; using namespace llvm::coverage; CoverageSourceInfo * -CoverageMappingModuleGen::setUpCoverageCallbacks(Preprocessor &PP) { - CoverageSourceInfo *CoverageInfo = - new CoverageSourceInfo(PP.getSourceManager()); +CoverageMappingModuleGen::setUpCoverageCallbacks(const CodeGenCLOptions &CLOpts, + Preprocessor &PP) { + CoverageSourceInfo *CoverageInfo = new CoverageSourceInfo( + PP.getSourceManager(), CLOpts.emptyline_comment_coverage); PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(CoverageInfo)); - if (EmptyLineCommentCoverage) { + if (CLOpts.emptyline_comment_coverage) { PP.addCommentHandler(CoverageInfo); PP.setEmptylineHandler(CoverageInfo); PP.setPreprocessToken(true); @@ -249,6 +231,7 @@ struct SpellingRegion { /// coverage mapping region builders. class CoverageMappingBuilder { public: + const CodeGenCLOptions &CLOpts; CoverageMappingModuleGen &CVM; SourceManager &SM; const LangOptions &LangOpts; @@ -274,7 +257,8 @@ class CoverageMappingBuilder { CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts) - : CVM(CVM), SM(SM), LangOpts(LangOpts) {} + : CLOpts(CVM.getCodeGenModule().getCLOpts()), CVM(CVM), SM(SM), + LangOpts(LangOpts) {} /// Return the precise end location for the given token. SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) { @@ -389,7 +373,7 @@ class CoverageMappingBuilder { // tokens to their user-code call site so coverage is attributed to // the user expression. Drop anything still in a system header // (e.g. a plain FileID into a -isystem .def file). - if (!SystemHeadersCoverage && + if (!CLOpts.system_headers_coverage && SM.isInSystemHeader(SM.getSpellingLoc(Loc))) { if (Loc.isMacroID()) { auto BeginLoc = SM.getSpellingLoc(Loc); @@ -521,7 +505,7 @@ class CoverageMappingBuilder { // Ignore regions from system headers unless collecting coverage from // system headers is explicitly enabled. - if (!SystemHeadersCoverage && + if (!CLOpts.system_headers_coverage && SM.isInSystemHeader(SM.getSpellingLoc(LocStart))) { assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() && "Don't suppress the condition in system headers"); @@ -949,7 +933,7 @@ struct CounterCoverageMappingBuilder /// Return a counter for the subtraction of \c RHS from \c LHS Counter subtractCounters(Counter LHS, Counter RHS, bool Simplify = true) { - assert(!llvm::EnableSingleByteCoverage && + assert(!CLOpts.single_byte_coverage && "cannot add counters when single byte coverage mode is enabled"); return Builder.subtract(LHS, RHS, Simplify); } @@ -996,7 +980,7 @@ struct CounterCoverageMappingBuilder BranchCounterPair Counters = {ExecCnt, Builder.subtract(ParentCnt, ExecCnt)}; - if (!llvm::EnableSingleByteCoverage || !Counters.Skipped.isExpression()) { + if (!CLOpts.single_byte_coverage || !Counters.Skipped.isExpression()) { assert( !TheMap.Skipped.hasValue() && "SkipCnt shouldn't be allocated but refer to an existing counter."); @@ -1019,7 +1003,7 @@ struct CounterCoverageMappingBuilder std::pair<Counter, Counter> getSwitchImplicitDefaultCounterPair(const Stmt *Cond, Counter ParentCount, Counter CaseCountSum) { - if (llvm::EnableSingleByteCoverage) { + if (CLOpts.single_byte_coverage) { // Allocate the new Counter since `subtract(Parent - Sum)` is unavailable. unsigned Idx = NextCounterNum++; CounterMap[Cond].Skipped = Idx; @@ -1267,9 +1251,9 @@ struct CounterCoverageMappingBuilder /// Returns Counter that corresponds to SC. Counter createSwitchCaseRegion(const SwitchCase *SC, Counter ParentCount) { Counter TrueCnt = getRegionCounter(SC); - Counter FalseCnt = (llvm::EnableSingleByteCoverage - ? Counter::getZero() // Folded - : subtractCounters(ParentCount, TrueCnt)); + Counter FalseCnt = + (CLOpts.single_byte_coverage ? Counter::getZero() // Folded + : subtractCounters(ParentCount, TrueCnt)); // Push region onto RegionStack but immediately pop it (which adds it to // the function's SourceRegions) because it doesn't apply to any other // source other than the SwitchCase. @@ -1599,7 +1583,7 @@ struct CounterCoverageMappingBuilder // Do not propagate region counts into system headers unless collecting // coverage from system headers is explicitly enabled. - if (!SystemHeadersCoverage && Body && + if (!CLOpts.system_headers_coverage && Body && SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body)))) return; @@ -2267,7 +2251,7 @@ struct CounterCoverageMappingBuilder /// Check if E belongs to system headers. bool isExprInSystemHeader(const BinaryOperator *E) const { - return (!SystemHeadersCoverage && + return (!CLOpts.system_headers_coverage && SM.isInSystemHeader(SM.getSpellingLoc(E->getOperatorLoc())) && SM.isInSystemHeader(SM.getSpellingLoc(E->getBeginLoc())) && SM.isInSystemHeader(SM.getSpellingLoc(E->getEndLoc()))); diff --git a/clang/lib/CodeGen/CoverageMappingGen.h b/clang/lib/CodeGen/CoverageMappingGen.h index 0ed50597e1dc3e..dcb37c26b479a9 100644 --- a/clang/lib/CodeGen/CoverageMappingGen.h +++ b/clang/lib/CodeGen/CoverageMappingGen.h @@ -19,13 +19,8 @@ #include "clang/Lex/Preprocessor.h" #include "llvm/ADT/DenseMap.h" #include "llvm/IR/GlobalValue.h" -#include "llvm/Support/CommandLine.h" #include "llvm/Support/raw_ostream.h" -namespace llvm::coverage { -extern cl::opt<bool> SystemHeadersCoverage; -} - namespace clang { class LangOptions; @@ -72,12 +67,16 @@ class CoverageSourceInfo : public PPCallbacks, SourceManager &SourceMgr; + bool EmptyLineCommentCoverage; + public: // Location of the token parsed before HandleComment is called. This is // updated every time Preprocessor::Lex lexes a new token. SourceLocation PrevTokLoc; - CoverageSourceInfo(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {} + CoverageSourceInfo(SourceManager &SourceMgr, bool EmptyLineCommentCoverage) + : SourceMgr(SourceMgr), + EmptyLineCommentCoverage(EmptyLineCommentCoverage) {} std::vector<SkippedRange> &getSkippedRanges() { return SkippedRanges; } @@ -94,6 +93,7 @@ class CoverageSourceInfo : public PPCallbacks, namespace CodeGen { +struct CodeGenCLOptions; class CodeGenModule; class CounterPair; @@ -126,7 +126,8 @@ class CoverageMappingModuleGen { uint64_t FilenamesRef); public: - static CoverageSourceInfo *setUpCoverageCallbacks(Preprocessor &PP); + static CoverageSourceInfo * + setUpCoverageCallbacks(const CodeGenCLOptions &CLOpts, Preprocessor &PP); CoverageMappingModuleGen(CodeGenModule &CGM, CoverageSourceInfo &SourceInfo); diff --git a/llvm/utils/gn/secondary/clang/lib/CodeGen/BUILD.gn b/llvm/utils/gn/secondary/clang/lib/CodeGen/BUILD.gn index 47a97fc400db92..43e8dbb8537c8a 100644 --- a/llvm/utils/gn/secondary/clang/lib/CodeGen/BUILD.gn +++ b/llvm/utils/gn/secondary/clang/lib/CodeGen/BUILD.gn @@ -1,7 +1,15 @@ +import("//llvm/utils/TableGen/tablegen.gni") + +tablegen("CodeGenCLOptions") { + visibility = [ ":CodeGen" ] + args = [ "-gen-opt-parser-defs" ] +} + static_library("CodeGen") { output_name = "clangCodeGen" configs += [ "//llvm/utils/gn/build:clang_code" ] deps = [ + ":CodeGenCLOptions", "//clang/include/clang/Basic:arm_cde_builtin_cg", "//clang/include/clang/Basic:arm_mve_builtin_cg", "//clang/include/clang/Basic:arm_sme_builtin_cg", @@ -31,6 +39,7 @@ static_library("CodeGen") { "//llvm/lib/Linker", "//llvm/lib/MC", "//llvm/lib/Object", + "//llvm/lib/Option", "//llvm/lib/Passes", "//llvm/lib/Plugins", "//llvm/lib/ProfileData", @@ -92,6 +101,7 @@ static_library("CodeGen") { "CGVTables.cpp", "CodeGenABITypes.cpp", "CodeGenAction.cpp", + "CodeGenCLOptions.cpp", "CodeGenFunction.cpp", "CodeGenModule.cpp", "CodeGenPGO.cpp", diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel index e08a9146fd1396..89131c24b6e3ba 100644 --- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel @@ -2205,6 +2205,15 @@ cc_library( ], ) +gentbl_cc_library( + name = "codegen_cl_options_gen", + strip_include_prefix = "lib/CodeGen", + tbl_outs = {"lib/CodeGen/CodeGenCLOptions.inc": ["-gen-opt-parser-defs"]}, + tblgen = "//llvm:llvm-tblgen", + td_file = "lib/CodeGen/CodeGenCLOptions.td", + deps = ["//llvm:OptParserTdFiles"], +) + cc_library( name = "codegen", srcs = glob( @@ -2232,6 +2241,7 @@ cc_library( ":basic_riscv_andes_vector_builtin_cg_gen", ":basic_riscv_sifive_vector_builtin_cg_gen", ":basic_riscv_vector_builtin_cg_gen", + ":codegen_cl_options_gen", ":codegen_utils", ":driver", ":frontend", @@ -2267,6 +2277,7 @@ cc_library( "//llvm:MC", "//llvm:ObjCARC", "//llvm:Object", + "//llvm:Option", "//llvm:Passes", "//llvm:Plugins", "//llvm:ProfileData", _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
