1313#include " llvm/ADT/SmallSet.h"
1414#include " llvm/ADT/SmallVector.h"
1515#include " llvm/ADT/StringRef.h"
16+ #include " llvm/Debuginfod/HTTPClient.h"
1617#include " llvm/IR/LLVMContext.h"
1718#include " llvm/Object/Binary.h"
1819#include " llvm/ProfileData/InstrProfCorrelator.h"
@@ -130,6 +131,12 @@ cl::opt<std::string>
130131 cl::desc(" For merge, use the provided unstripped bianry to "
131132 " correlate the raw profile." ),
132133 cl::sub(MergeSubcommand));
134+ cl::list<std::string> DebugFileDirectory (
135+ " debug-file-directory" ,
136+ cl::desc (" Directories to search for object files by build ID" ));
137+ cl::opt<bool > DebugInfod (" debuginfod" , cl::init(false ), cl::Hidden,
138+ cl::sub(MergeSubcommand),
139+ cl::desc(" Enable debuginfod" ));
133140cl::opt<std::string> FuncNameFilter (
134141 " function" ,
135142 cl::desc (" Only functions matching the filter are shown in the output. For "
@@ -652,7 +659,8 @@ static void overlapInput(const std::string &BaseFilename,
652659// / Load an input into a writer context.
653660static void loadInput (const WeightedFile &Input, SymbolRemapper *Remapper,
654661 const InstrProfCorrelator *Correlator,
655- const StringRef ProfiledBinary, WriterContext *WC) {
662+ const StringRef ProfiledBinary, WriterContext *WC,
663+ const object::BuildIDFetcher *BIDFetcher = nullptr ) {
656664 std::unique_lock<std::mutex> CtxGuard{WC->Lock };
657665
658666 // Copy the filename, because llvm::ThreadPool copied the input "const
@@ -730,8 +738,8 @@ static void loadInput(const WeightedFile &Input, SymbolRemapper *Remapper,
730738 auto [ErrCode, Msg] = InstrProfError::take (std::move (E));
731739 ReaderWarning = {make_error<InstrProfError>(ErrCode, Msg), Filename};
732740 };
733- auto ReaderOrErr =
734- InstrProfReader::create (Input. Filename , *FS, Correlator , Warn);
741+ auto ReaderOrErr = InstrProfReader::create (Input. Filename , *FS, Correlator,
742+ BIDFetcher , Warn);
735743 if (Error E = ReaderOrErr.takeError ()) {
736744 // Skip the empty profiles by returning silently.
737745 auto [ErrCode, Msg] = InstrProfError::take (std::move (E));
@@ -914,9 +922,14 @@ static void mergeInstrProfile(const WeightedFileVector &Inputs,
914922 exitWithError (" unknown format is specified" );
915923
916924 // TODO: Maybe we should support correlation with mixture of different
917- // correlation modes(w/wo debug-info/object correlation).
918- if (!DebugInfoFilename.empty () && !BinaryFilename.empty ())
919- exitWithError (" Expected only one of -debug-info, -binary-file" );
925+ // correlaxtion modes(w/wo debug-info/object correlation).
926+ if (DebugInfoFilename.empty ()) {
927+ if (!BinaryFilename.empty () && DebugInfod)
928+ exitWithError (" Expected only one of -binary-file, -debuginfod" );
929+ } else if (!BinaryFilename.empty () || DebugInfod) {
930+ exitWithError (
931+ " Expected only one of -debug-info, -binary-file, -debuginfod" );
932+ }
920933 std::string CorrelateFilename;
921934 ProfCorrelatorKind CorrelateKind = ProfCorrelatorKind::NONE;
922935 if (!DebugInfoFilename.empty ()) {
@@ -936,6 +949,14 @@ static void mergeInstrProfile(const WeightedFileVector &Inputs,
936949 exitWithError (std::move (Err), CorrelateFilename);
937950 }
938951
952+ std::unique_ptr<object::BuildIDFetcher> BIDFetcher;
953+ if (DebugInfod) {
954+ llvm::HTTPClient::initialize ();
955+ BIDFetcher = std::make_unique<DebuginfodFetcher>(DebugFileDirectory);
956+ } else if (!DebugFileDirectory.empty ()) {
957+ BIDFetcher = std::make_unique<object::BuildIDFetcher>(DebugFileDirectory);
958+ }
959+
939960 std::mutex ErrorLock;
940961 SmallSet<instrprof_error, 4 > WriterErrorCodes;
941962
@@ -954,15 +975,15 @@ static void mergeInstrProfile(const WeightedFileVector &Inputs,
954975 if (NumThreads == 1 ) {
955976 for (const auto &Input : Inputs)
956977 loadInput (Input, Remapper, Correlator.get (), ProfiledBinary,
957- Contexts[0 ].get ());
978+ Contexts[0 ].get (), BIDFetcher. get () );
958979 } else {
959980 DefaultThreadPool Pool (hardware_concurrency (NumThreads));
960981
961982 // Load the inputs in parallel (N/NumThreads serial steps).
962983 unsigned Ctx = 0 ;
963984 for (const auto &Input : Inputs) {
964985 Pool.async (loadInput, Input, Remapper, Correlator.get (), ProfiledBinary,
965- Contexts[Ctx].get ());
986+ Contexts[Ctx].get (), BIDFetcher. get () );
966987 Ctx = (Ctx + 1 ) % NumThreads;
967988 }
968989 Pool.wait ();
0 commit comments