From d8ee75b8f549f6f065a56e680f87e669717d6c7f Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Mon, 6 Jun 2016 22:39:22 +0000 Subject: Retry "[llvm-profdata] Add option to ingest filepaths from a file" Changes since the initial commit: - Normalize file paths read from the file to prevent Windows path separators from escaping parts of the path. - Since we need to store the normalized file paths in WeightedFile, don't do tricky things to keep the source MemoryBuffer alive. Differential Revision: http://reviews.llvm.org/D20980 llvm-svn: 271949 --- llvm/tools/llvm-profdata/llvm-profdata.cpp | 71 ++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 13 deletions(-) (limited to 'llvm/tools/llvm-profdata') diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp index 20a167226a3..212a399e866 100644 --- a/llvm/tools/llvm-profdata/llvm-profdata.cpp +++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp @@ -108,12 +108,12 @@ static void handleMergeWriterError(Error E, StringRef WhenceFile = "", } struct WeightedFile { - StringRef Filename; + std::string Filename; uint64_t Weight; WeightedFile() {} - WeightedFile(StringRef F, uint64_t W) : Filename{F}, Weight{W} {} + WeightedFile(std::string F, uint64_t W) : Filename{F}, Weight{W} {} }; typedef SmallVector WeightedFileVector; @@ -209,18 +209,47 @@ static void mergeSampleProfile(const WeightedFileVector &Inputs, } static WeightedFile parseWeightedFile(const StringRef &WeightedFilename) { - StringRef WeightStr, FileName; - std::tie(WeightStr, FileName) = WeightedFilename.split(','); + StringRef WeightStr, FilenameStr; + std::tie(WeightStr, FilenameStr) = WeightedFilename.split(','); uint64_t Weight; if (WeightStr.getAsInteger(10, Weight) || Weight < 1) exitWithError("Input weight must be a positive integer."); - if (!sys::fs::exists(FileName)) + SmallString<256> CanonicalFilename; + sys::path::native(FilenameStr, CanonicalFilename); + + if (!sys::fs::exists(CanonicalFilename)) exitWithErrorCode(make_error_code(errc::no_such_file_or_directory), - FileName); + CanonicalFilename); - return WeightedFile(FileName, Weight); + return WeightedFile(StringRef(CanonicalFilename).str(), Weight); +} + +static void parseInputFilenamesFile(const StringRef &InputFilenamesFile, + WeightedFileVector &WFV) { + if (InputFilenamesFile == "") + return; + + auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilenamesFile); + if (!BufOrError) + exitWithErrorCode(BufOrError.getError(), InputFilenamesFile); + + auto Buffer = std::move(*BufOrError); + StringRef Data = Buffer->getBuffer(); + SmallVector Entries; + Data.split(Entries, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false); + for (const StringRef &FileWeightEntry : Entries) { + StringRef SanitizedEntry = FileWeightEntry.trim(" \t\v\f\r"); + // Skip comments. + if (SanitizedEntry.startswith("#")) + continue; + // If there's no comma, it's an unweighted profile. + else if (SanitizedEntry.rfind(',') == StringRef::npos) + WFV.emplace_back(SanitizedEntry, 1); + else + WFV.emplace_back(parseWeightedFile(SanitizedEntry)); + } } static int merge_main(int argc, const char *argv[]) { @@ -228,6 +257,15 @@ static int merge_main(int argc, const char *argv[]) { cl::desc("")); cl::list WeightedInputFilenames("weighted-input", cl::desc(",")); + cl::opt InputFilenamesFile( + "input-files", cl::init(""), + cl::desc("Path to file containing newline-separated " + "[,] entries")); + cl::alias InputFilenamesFileA("f", cl::desc("Alias for --input-files"), + cl::aliasopt(InputFilenamesFile)); + cl::opt DumpInputFileList( + "dump-input-file-list", cl::init(false), cl::Hidden, + cl::desc("Dump the list of input files and their weights, then exit")); cl::opt OutputFilename("output", cl::value_desc("output"), cl::init("-"), cl::Required, cl::desc("Output file")); @@ -249,15 +287,22 @@ static int merge_main(int argc, const char *argv[]) { cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n"); - if (InputFilenames.empty() && WeightedInputFilenames.empty()) - exitWithError("No input files specified. See " + - sys::path::filename(argv[0]) + " -help"); - WeightedFileVector WeightedInputs; for (StringRef Filename : InputFilenames) - WeightedInputs.push_back(WeightedFile(Filename, 1)); + WeightedInputs.emplace_back(Filename, 1); for (StringRef WeightedFilename : WeightedInputFilenames) - WeightedInputs.push_back(parseWeightedFile(WeightedFilename)); + WeightedInputs.emplace_back(parseWeightedFile(WeightedFilename)); + parseInputFilenamesFile(InputFilenamesFile, WeightedInputs); + + if (WeightedInputs.empty()) + exitWithError("No input files specified. See " + + sys::path::filename(argv[0]) + " -help"); + + if (DumpInputFileList) { + for (auto &WF : WeightedInputs) + outs() << WF.Weight << "," << WF.Filename << "\n"; + return 0; + } if (ProfileKind == instr) mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat, -- cgit v1.2.3