diff --git a/llvm/tools/llvm-profgen/CSPreInliner.h b/llvm/tools/llvm-profgen/CSPreInliner.h index 8a3f16a4f13cb..022c3f8d0daed 100644 --- a/llvm/tools/llvm-profgen/CSPreInliner.h +++ b/llvm/tools/llvm-profgen/CSPreInliner.h @@ -16,9 +16,6 @@ #include "llvm/Transforms/IPO/ProfiledCallGraph.h" #include "llvm/Transforms/IPO/SampleContextTracker.h" -using namespace llvm; -using namespace sampleprof; - namespace llvm { namespace sampleprof { diff --git a/llvm/tools/llvm-profgen/ErrorHandling.h b/llvm/tools/llvm-profgen/ErrorHandling.h index b797add8a892f..17084bd785e64 100644 --- a/llvm/tools/llvm-profgen/ErrorHandling.h +++ b/llvm/tools/llvm-profgen/ErrorHandling.h @@ -16,7 +16,7 @@ #include "llvm/Support/WithColor.h" #include -using namespace llvm; +namespace llvm { [[noreturn]] inline void exitWithError(const Twine &Message, StringRef Whence = StringRef(), @@ -53,4 +53,6 @@ inline void emitWarningSummary(uint64_t Num, uint64_t Total, StringRef Msg) { << "%(" << Num << "/" << Total << ") " << Msg << "\n"; } +} // end namespace llvm + #endif diff --git a/llvm/tools/llvm-profgen/MissingFrameInferrer.cpp b/llvm/tools/llvm-profgen/MissingFrameInferrer.cpp index edfe8979c7121..7ebca23ba7956 100644 --- a/llvm/tools/llvm-profgen/MissingFrameInferrer.cpp +++ b/llvm/tools/llvm-profgen/MissingFrameInferrer.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "MissingFrameInferrer.h" +#include "Options.h" #include "PerfReader.h" #include "ProfiledBinary.h" #include "llvm/ADT/SCCIterator.h" @@ -37,7 +38,8 @@ STATISTIC(TailCallMaxTailCallPath, "Length of the longest tail call path"); static cl::opt MaximumSearchDepth("max-search-depth", cl::init(UINT32_MAX - 1), cl::desc("The maximum levels the DFS-based missing " - "frame search should go with")); + "frame search should go with"), + cl::cat(ProfGenCategory)); void MissingFrameInferrer::initialize( const ContextSampleCounterMap *SampleCounters) { diff --git a/llvm/tools/llvm-profgen/Options.h b/llvm/tools/llvm-profgen/Options.h new file mode 100644 index 0000000000000..f94cf9118c06a --- /dev/null +++ b/llvm/tools/llvm-profgen/Options.h @@ -0,0 +1,28 @@ +//===-- Options.h -----------------------------------------------*- C++ -*-===// +// +// 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_TOOLS_LLVM_PROFGEN_OPTIONS_H +#define LLVM_TOOLS_LLVM_PROFGEN_OPTIONS_H + +#include "llvm/Support/CommandLine.h" + +namespace llvm { + +extern cl::OptionCategory ProfGenCategory; + +extern cl::opt OutputFilename; +extern cl::opt ShowDisassemblyOnly; +extern cl::opt ShowSourceLocations; +extern cl::opt SkipSymbolization; +extern cl::opt ShowDetailedWarning; +extern cl::opt InferMissingFrames; +extern cl::opt EnableCSPreInliner; +extern cl::opt UseContextCostForPreInliner; + +} // end namespace llvm + +#endif diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp index ad113eda27914..7e045b0c06229 100644 --- a/llvm/tools/llvm-profgen/PerfReader.cpp +++ b/llvm/tools/llvm-profgen/PerfReader.cpp @@ -6,6 +6,8 @@ // //===----------------------------------------------------------------------===// #include "PerfReader.h" + +#include "Options.h" #include "ProfileGenerator.h" #include "llvm/ADT/SmallString.h" #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h" @@ -15,44 +17,47 @@ #define DEBUG_TYPE "perf-reader" +namespace llvm { + cl::opt SkipSymbolization("skip-symbolization", cl::desc("Dump the unsymbolized profile to the " "output file. It will show unwinder " - "output for CS profile generation.")); + "output for CS profile generation."), + cl::cat(ProfGenCategory)); static cl::opt ShowMmapEvents("show-mmap-events", - cl::desc("Print binary load events.")); + cl::desc("Print binary load events."), + cl::cat(ProfGenCategory)); static cl::opt UseOffset("use-offset", cl::init(true), cl::desc("Work with `--skip-symbolization` or " "`--unsymbolized-profile` to write/read the " - "offset instead of virtual address.")); + "offset instead of virtual address."), + cl::cat(ProfGenCategory)); static cl::opt UseLoadableSegmentAsBase( "use-first-loadable-segment-as-base", cl::desc("Use first loadable segment address as base address " "for offsets in unsymbolized profile. By default " - "first executable segment address is used")); + "first executable segment address is used"), + cl::cat(ProfGenCategory)); static cl::opt IgnoreStackSamples("ignore-stack-samples", cl::desc("Ignore call stack samples for hybrid samples " - "and produce context-insensitive profile.")); + "and produce context-insensitive profile."), + cl::cat(ProfGenCategory)); cl::opt ShowDetailedWarning("show-detailed-warning", - cl::desc("Show detailed warning message.")); + cl::desc("Show detailed warning message."), + cl::cat(ProfGenCategory)); static cl::opt CSProfMaxUnsymbolizedCtxDepth( "csprof-max-unsymbolized-context-depth", cl::init(-1), cl::desc("Keep the last K contexts while merging unsymbolized profile. -1 " - "means no depth limit.")); + "means no depth limit."), + cl::cat(ProfGenCategory)); -extern cl::opt PerfTraceFilename; -extern cl::opt ShowDisassemblyOnly; -extern cl::opt ShowSourceLocations; -extern cl::opt OutputFilename; - -namespace llvm { namespace sampleprof { void VirtualUnwinder::unwindCall(UnwindState &State) { diff --git a/llvm/tools/llvm-profgen/PerfReader.h b/llvm/tools/llvm-profgen/PerfReader.h index 4b3ac8f569755..19451915812e1 100644 --- a/llvm/tools/llvm-profgen/PerfReader.h +++ b/llvm/tools/llvm-profgen/PerfReader.h @@ -17,9 +17,6 @@ #include #include -using namespace llvm; -using namespace sampleprof; - namespace llvm { class CleanupInstaller; diff --git a/llvm/tools/llvm-profgen/ProfileGenerator.cpp b/llvm/tools/llvm-profgen/ProfileGenerator.cpp index db686c3b597eb..33575b9c67625 100644 --- a/llvm/tools/llvm-profgen/ProfileGenerator.cpp +++ b/llvm/tools/llvm-profgen/ProfileGenerator.cpp @@ -8,6 +8,7 @@ #include "ProfileGenerator.h" #include "ErrorHandling.h" #include "MissingFrameInferrer.h" +#include "Options.h" #include "PerfReader.h" #include "ProfiledBinary.h" #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h" @@ -17,20 +18,24 @@ #include #include +namespace llvm { + cl::opt OutputFilename("output", cl::value_desc("output"), cl::Required, - cl::desc("Output profile file")); + cl::desc("Output profile file"), + cl::cat(ProfGenCategory)); static cl::alias OutputA("o", cl::desc("Alias for --output"), cl::aliasopt(OutputFilename)); static cl::opt OutputFormat( "format", cl::desc("Format of output profile"), cl::init(SPF_Ext_Binary), - cl::values( - clEnumValN(SPF_Binary, "binary", "Binary encoding (default)"), - clEnumValN(SPF_Ext_Binary, "extbinary", "Extensible binary encoding"), - clEnumValN(SPF_Text, "text", "Text encoding"), - clEnumValN(SPF_GCC, "gcc", - "GCC encoding (only meaningful for -sample)"))); + cl::values(clEnumValN(SPF_Binary, "binary", "Binary encoding (default)"), + clEnumValN(SPF_Ext_Binary, "extbinary", + "Extensible binary encoding"), + clEnumValN(SPF_Text, "text", "Text encoding"), + clEnumValN(SPF_GCC, "gcc", + "GCC encoding (only meaningful for -sample)")), + cl::cat(ProfGenCategory)); static cl::opt UseMD5( "use-md5", cl::Hidden, @@ -56,58 +61,57 @@ static cl::opt RecursionCompression( static cl::opt TrimColdProfile("trim-cold-profile", cl::desc("If the total count of the profile is smaller " - "than threshold, it will be trimmed.")); + "than threshold, it will be trimmed."), + cl::cat(ProfGenCategory)); static cl::opt CSProfMergeColdContext( "csprof-merge-cold-context", cl::init(true), cl::desc("If the total count of context profile is smaller than " "the threshold, it will be merged into context-less base " - "profile.")); + "profile."), + cl::cat(ProfGenCategory)); static cl::opt CSProfMaxColdContextDepth( "csprof-max-cold-context-depth", cl::init(1), cl::desc("Keep the last K contexts while merging cold profile. 1 means the " - "context-less base profile")); + "context-less base profile"), + cl::cat(ProfGenCategory)); static cl::opt CSProfMaxContextDepth( "csprof-max-context-depth", cl::desc("Keep the last K contexts while merging profile. -1 means no " "depth limit."), - cl::location(llvm::sampleprof::CSProfileGenerator::MaxContextDepth)); + cl::location(llvm::sampleprof::CSProfileGenerator::MaxContextDepth), + cl::cat(ProfGenCategory)); static cl::opt ProfileDensityThreshold( - "profile-density-threshold", llvm::cl::init(50), - llvm::cl::desc("If the profile density is below the given threshold, it " - "will be suggested to increase the sampling rate."), - llvm::cl::Optional); -static cl::opt ShowDensity("show-density", llvm::cl::init(false), - llvm::cl::desc("show profile density details"), - llvm::cl::Optional); + "profile-density-threshold", cl::init(50), + cl::desc("If the profile density is below the given threshold, it " + "will be suggested to increase the sampling rate."), + cl::Optional, cl::cat(ProfGenCategory)); +static cl::opt ShowDensity("show-density", cl::init(false), + cl::desc("show profile density details"), + cl::Optional, cl::cat(ProfGenCategory)); static cl::opt ProfileDensityCutOffHot( - "profile-density-cutoff-hot", llvm::cl::init(990000), - llvm::cl::desc("Total samples cutoff for functions used to calculate " - "profile density.")); + "profile-density-cutoff-hot", cl::init(990000), + cl::desc("Total samples cutoff for functions used to calculate " + "profile density."), + cl::cat(ProfGenCategory)); static cl::opt UpdateTotalSamples( - "update-total-samples", llvm::cl::init(false), - llvm::cl::desc( - "Update total samples by accumulating all its body samples."), - llvm::cl::Optional); + "update-total-samples", cl::init(false), + cl::desc("Update total samples by accumulating all its body samples."), + cl::Optional, cl::cat(ProfGenCategory)); static cl::opt GenCSNestedProfile( "gen-cs-nested-profile", cl::Hidden, cl::init(true), cl::desc("Generate nested function profiles for CSSPGO")); cl::opt InferMissingFrames( - "infer-missing-frames", llvm::cl::init(true), - llvm::cl::desc( + "infer-missing-frames", cl::init(true), + cl::desc( "Infer missing call frames due to compiler tail call elimination."), - llvm::cl::Optional); - -using namespace llvm; -using namespace sampleprof; - -namespace llvm { + cl::Optional, cl::cat(ProfGenCategory)); namespace sampleprof { diff --git a/llvm/tools/llvm-profgen/ProfileGenerator.h b/llvm/tools/llvm-profgen/ProfileGenerator.h index 5e36128530cd9..d3e04563a81c2 100644 --- a/llvm/tools/llvm-profgen/ProfileGenerator.h +++ b/llvm/tools/llvm-profgen/ProfileGenerator.h @@ -17,9 +17,6 @@ #include #include -using namespace llvm; -using namespace sampleprof; - namespace llvm { namespace sampleprof { diff --git a/llvm/tools/llvm-profgen/ProfiledBinary.cpp b/llvm/tools/llvm-profgen/ProfiledBinary.cpp index 6847ba1b21b1f..7dd5d3f773733 100644 --- a/llvm/tools/llvm-profgen/ProfiledBinary.cpp +++ b/llvm/tools/llvm-profgen/ProfiledBinary.cpp @@ -7,8 +7,10 @@ //===----------------------------------------------------------------------===// #include "ProfiledBinary.h" + #include "ErrorHandling.h" #include "MissingFrameInferrer.h" +#include "Options.h" #include "ProfileGenerator.h" #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h" #include "llvm/Demangle/Demangle.h" @@ -24,46 +26,51 @@ #define DEBUG_TYPE "load-binary" -using namespace llvm; -using namespace sampleprof; +namespace llvm { + +using namespace object; cl::opt ShowDisassemblyOnly("show-disassembly-only", - cl::desc("Print disassembled code.")); + cl::desc("Print disassembled code."), + cl::cat(ProfGenCategory)); cl::opt ShowSourceLocations("show-source-locations", - cl::desc("Print source locations.")); + cl::desc("Print source locations."), + cl::cat(ProfGenCategory)); static cl::opt ShowCanonicalFnName("show-canonical-fname", - cl::desc("Print canonical function name.")); + cl::desc("Print canonical function name."), + cl::cat(ProfGenCategory)); static cl::opt ShowPseudoProbe( "show-pseudo-probe", - cl::desc("Print pseudo probe section and disassembled info.")); + cl::desc("Print pseudo probe section and disassembled info."), + cl::cat(ProfGenCategory)); static cl::opt UseDwarfCorrelation( "use-dwarf-correlation", cl::desc("Use dwarf for profile correlation even when binary contains " - "pseudo probe.")); + "pseudo probe."), + cl::cat(ProfGenCategory)); static cl::opt DWPPath("dwp", cl::init(""), cl::desc("Path of .dwp file. When not specified, it will be " - ".dwp in the same directory as the main binary.")); + ".dwp in the same directory as the main binary."), + cl::cat(ProfGenCategory)); static cl::list DisassembleFunctions( "disassemble-functions", cl::CommaSeparated, cl::desc("List of functions to print disassembly for. Accept demangled " - "names only. Only work with show-disassembly-only")); + "names only. Only work with show-disassembly-only"), + cl::cat(ProfGenCategory)); static cl::opt KernelBinary("kernel", - cl::desc("Generate the profile for Linux kernel binary.")); + cl::desc("Generate the profile for Linux kernel binary."), + cl::cat(ProfGenCategory)); -extern cl::opt ShowDetailedWarning; -extern cl::opt InferMissingFrames; - -namespace llvm { namespace sampleprof { static const Target *getTarget(const ObjectFile *Obj) { diff --git a/llvm/tools/llvm-profgen/ProfiledBinary.h b/llvm/tools/llvm-profgen/ProfiledBinary.h index 0588cb48b2af6..9c0bff591337a 100644 --- a/llvm/tools/llvm-profgen/ProfiledBinary.h +++ b/llvm/tools/llvm-profgen/ProfiledBinary.h @@ -41,15 +41,6 @@ #include #include -namespace llvm { -extern cl::opt EnableCSPreInliner; -extern cl::opt UseContextCostForPreInliner; -} // namespace llvm - -using namespace llvm; -using namespace sampleprof; -using namespace llvm::object; - namespace llvm { namespace sampleprof { @@ -303,34 +294,34 @@ class ProfiledBinary { bool IsCOFF = false; - void setPreferredTextSegmentAddresses(const ObjectFile *O); + void setPreferredTextSegmentAddresses(const object::ObjectFile *O); template - void setPreferredTextSegmentAddresses(const ELFFile &Obj, + void setPreferredTextSegmentAddresses(const object::ELFFile &Obj, StringRef FileName); - void setPreferredTextSegmentAddresses(const COFFObjectFile *Obj, + void setPreferredTextSegmentAddresses(const object::COFFObjectFile *Obj, StringRef FileName); - void checkPseudoProbe(const ELFObjectFileBase *Obj); + void checkPseudoProbe(const object::ELFObjectFileBase *Obj); - void decodePseudoProbe(const ELFObjectFileBase *Obj); + void decodePseudoProbe(const object::ELFObjectFileBase *Obj); - void - checkUseFSDiscriminator(const ObjectFile *Obj, - std::map &AllSymbols); + void checkUseFSDiscriminator( + const object::ObjectFile *Obj, + std::map &AllSymbols); // Set up disassembler and related components. - void setUpDisassembler(const ObjectFile *Obj); + void setUpDisassembler(const object::ObjectFile *Obj); symbolize::LLVMSymbolizer::Options getSymbolizerOpts() const; // Load debug info of subprograms from DWARF section. - void loadSymbolsFromDWARF(ObjectFile &Obj); + void loadSymbolsFromDWARF(object::ObjectFile &Obj); // Load debug info from DWARF unit. void loadSymbolsFromDWARFUnit(DWARFUnit &CompilationUnit); // Create elf symbol to its start address mapping. - void populateElfSymbolAddressList(const ELFObjectFileBase *O); + void populateElfSymbolAddressList(const object::ELFObjectFileBase *O); // A function may be spilt into multiple non-continuous address ranges. We use // this to set whether start a function range is the real entry of the @@ -341,11 +332,12 @@ class ProfiledBinary { void warnNoFuncEntry(); /// Dissassemble the text section and build various address maps. - void disassemble(const ObjectFile *O); + void disassemble(const object::ObjectFile *O); /// Helper function to dissassemble the symbol and extract info for unwinding bool dissassembleSymbol(std::size_t SI, ArrayRef Bytes, - SectionSymbolsTy &Symbols, const SectionRef &Section); + SectionSymbolsTy &Symbols, + const object::SectionRef &Section); /// Symbolize a given instruction pointer and return a full call context. SampleContextFrameVector symbolize(const InstructionPointer &IP, bool UseCanonicalFnName = false, diff --git a/llvm/tools/llvm-profgen/llvm-profgen.cpp b/llvm/tools/llvm-profgen/llvm-profgen.cpp index 3b974e25103ad..7e070a1ea6489 100644 --- a/llvm/tools/llvm-profgen/llvm-profgen.cpp +++ b/llvm/tools/llvm-profgen/llvm-profgen.cpp @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// #include "ErrorHandling.h" +#include "Options.h" #include "PerfReader.h" #include "ProfileGenerator.h" #include "ProfiledBinary.h" @@ -21,7 +22,12 @@ #include "llvm/Support/TargetSelect.h" #include "llvm/Support/VirtualFileSystem.h" -static cl::OptionCategory ProfGenCategory("ProfGen Options"); +using namespace llvm; +using namespace sampleprof; + +namespace llvm { + +cl::OptionCategory ProfGenCategory("ProfGen Options"); static cl::opt PerfScriptFilename( "perfscript", cl::value_desc("perfscript"), @@ -67,13 +73,6 @@ static cl::opt DebugBinPath( "from it instead of the executable binary."), cl::cat(ProfGenCategory)); -extern cl::opt ShowDisassemblyOnly; -extern cl::opt ShowSourceLocations; -extern cl::opt SkipSymbolization; - -using namespace llvm; -using namespace sampleprof; - // Validate the command line input. static void validateCommandLine() { // Allow the missing perfscript if we only use to show binary disassembly. @@ -138,6 +137,8 @@ static PerfInputFile getPerfInputFile() { return File; } +} // end namespace llvm + int main(int argc, const char *argv[]) { InitLLVM X(argc, argv);