summaryrefslogtreecommitdiff
path: root/tools/llvm-profdata
diff options
context:
space:
mode:
authorVedant Kumar <vsk@apple.com>2016-07-19 00:57:09 +0000
committerVedant Kumar <vsk@apple.com>2016-07-19 00:57:09 +0000
commit40a2c5247c293749c005681aba150f7ae9f0cc51 (patch)
tree339b57225725f869a0766230d51b58db6dddebd6 /tools/llvm-profdata
parent92a8d601a38984688cba36766ffba0d668196e30 (diff)
Revert "[llvm-profdata] Speed up merging by using a thread pool"
This reverts commit r275921. It broke the ppc64be bot: http://lab.llvm.org:8011/builders/clang-ppc64be-linux-multistage/builds/3537 I'm not sure why it broke, but based on the output, it looks like an off-by-one (one profile left un-merged). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275937 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/llvm-profdata')
-rw-r--r--tools/llvm-profdata/llvm-profdata.cpp138
1 files changed, 23 insertions, 115 deletions
diff --git a/tools/llvm-profdata/llvm-profdata.cpp b/tools/llvm-profdata/llvm-profdata.cpp
index e57b19f5a06..8e4b4c3d4ed 100644
--- a/tools/llvm-profdata/llvm-profdata.cpp
+++ b/tools/llvm-profdata/llvm-profdata.cpp
@@ -29,7 +29,6 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"
-#include "llvm/Support/ThreadPool.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
@@ -118,68 +117,9 @@ struct WeightedFile {
};
typedef SmallVector<WeightedFile, 5> WeightedFileVector;
-/// Keep track of merged data and reported errors.
-struct WriterContext {
- std::mutex Lock;
- InstrProfWriter Writer;
- Error Err;
- StringRef ErrWhence;
- std::mutex &ErrLock;
- SmallSet<instrprof_error, 4> &WriterErrorCodes;
-
- WriterContext(bool IsSparse, std::mutex &ErrLock,
- SmallSet<instrprof_error, 4> &WriterErrorCodes)
- : Lock(), Writer(IsSparse), Err(Error::success()), ErrWhence(""),
- ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {}
-};
-
-/// Load an input into a writer context.
-static void loadInput(const WeightedFile &Input, WriterContext *WC) {
- std::unique_lock<std::mutex> CtxGuard{WC->Lock};
-
- // If there's a pending hard error, don't do more work.
- if (WC->Err)
- return;
-
- WC->ErrWhence = Input.Filename;
-
- auto ReaderOrErr = InstrProfReader::create(Input.Filename);
- if ((WC->Err = ReaderOrErr.takeError()))
- return;
-
- auto Reader = std::move(ReaderOrErr.get());
- bool IsIRProfile = Reader->isIRLevelProfile();
- if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) {
- WC->Err = make_error<StringError>(
- "Merge IR generated profile with Clang generated profile.",
- std::error_code());
- return;
- }
-
- for (auto &I : *Reader) {
- if (Error E = WC->Writer.addRecord(std::move(I), Input.Weight)) {
- // Only show hint the first time an error occurs.
- instrprof_error IPE = InstrProfError::take(std::move(E));
- std::unique_lock<std::mutex> ErrGuard{WC->ErrLock};
- bool firstTime = WC->WriterErrorCodes.insert(IPE).second;
- handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
- I.Name, firstTime);
- }
- }
- if (Reader->hasError())
- WC->Err = Reader->getError();
-}
-
-/// Merge the \p Src writer context into \p Dst.
-static void mergeWriterContexts(WriterContext *Dst, WriterContext *Src) {
- if (Error E = Dst->Writer.mergeRecordsFromWriter(std::move(Src->Writer)))
- Dst->Err = std::move(E);
-}
-
static void mergeInstrProfile(const WeightedFileVector &Inputs,
StringRef OutputFilename,
- ProfileFormat OutputFormat, bool OutputSparse,
- unsigned NumThreads) {
+ ProfileFormat OutputFormat, bool OutputSparse) {
if (OutputFilename.compare("-") == 0)
exitWithError("Cannot write indexed profdata format to stdout.");
@@ -191,57 +131,30 @@ static void mergeInstrProfile(const WeightedFileVector &Inputs,
if (EC)
exitWithErrorCode(EC, OutputFilename);
- std::mutex ErrorLock;
+ InstrProfWriter Writer(OutputSparse);
SmallSet<instrprof_error, 4> WriterErrorCodes;
-
- // If NumThreads is not specified, auto-detect a good default.
- if (NumThreads == 0)
- NumThreads = std::max(1U, std::min(std::thread::hardware_concurrency(),
- unsigned(Inputs.size() / 2)));
-
- // Initialize the writer contexts.
- SmallVector<std::unique_ptr<WriterContext>, 4> Contexts;
- for (unsigned I = 0; I < NumThreads; ++I)
- Contexts.emplace_back(llvm::make_unique<WriterContext>(
- OutputSparse, ErrorLock, WriterErrorCodes));
-
- if (NumThreads == 1) {
- for (const auto &Input : Inputs)
- loadInput(Input, Contexts[0].get());
- } else {
- ThreadPool Pool(NumThreads);
-
- // Load the inputs in parallel (N/NumThreads serial steps).
- unsigned Ctx = 0;
- for (const auto &Input : Inputs) {
- Pool.async(loadInput, Input, Contexts[Ctx].get());
- Ctx = (Ctx + 1) % NumThreads;
+ for (const auto &Input : Inputs) {
+ auto ReaderOrErr = InstrProfReader::create(Input.Filename);
+ if (Error E = ReaderOrErr.takeError())
+ exitWithError(std::move(E), Input.Filename);
+
+ auto Reader = std::move(ReaderOrErr.get());
+ bool IsIRProfile = Reader->isIRLevelProfile();
+ if (Writer.setIsIRLevelProfile(IsIRProfile))
+ exitWithError("Merge IR generated profile with Clang generated profile.");
+
+ for (auto &I : *Reader) {
+ if (Error E = Writer.addRecord(std::move(I), Input.Weight)) {
+ // Only show hint the first time an error occurs.
+ instrprof_error IPE = InstrProfError::take(std::move(E));
+ bool firstTime = WriterErrorCodes.insert(IPE).second;
+ handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
+ I.Name, firstTime);
+ }
}
- Pool.wait();
-
- // Merge the writer contexts together (lg(NumThreads) serial steps).
- unsigned Mid = Contexts.size() / 2;
- unsigned End = Contexts.size();
- assert(Mid > 0 && "Expected more than one context");
- do {
- for (unsigned I = 0; I < Mid; ++I)
- Pool.async(mergeWriterContexts, Contexts[I].get(),
- Contexts[I + Mid].get());
- if (End & 1)
- Pool.async(mergeWriterContexts, Contexts[0].get(),
- Contexts[End - 1].get());
- Pool.wait();
- End = Mid;
- Mid /= 2;
- } while (Mid > 0);
+ if (Reader->hasError())
+ exitWithError(Reader->getError(), Input.Filename);
}
-
- // Handle deferred hard errors encountered during merging.
- for (std::unique_ptr<WriterContext> &WC : Contexts)
- if (WC->Err)
- exitWithError(std::move(WC->Err), WC->ErrWhence);
-
- InstrProfWriter &Writer = Contexts[0]->Writer;
if (OutputFormat == PF_Text)
Writer.writeText(Output);
else
@@ -375,11 +288,6 @@ static int merge_main(int argc, const char *argv[]) {
clEnumValEnd));
cl::opt<bool> OutputSparse("sparse", cl::init(false),
cl::desc("Generate a sparse profile (only meaningful for -instr)"));
- cl::opt<unsigned> NumThreads(
- "num-threads", cl::init(0),
- cl::desc("Number of merge threads to use (default: autodetect)"));
- cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
- cl::aliasopt(NumThreads));
cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
@@ -406,7 +314,7 @@ static int merge_main(int argc, const char *argv[]) {
if (ProfileKind == instr)
mergeInstrProfile(WeightedInputs, OutputFilename, OutputFormat,
- OutputSparse, NumThreads);
+ OutputSparse);
else
mergeSampleProfile(WeightedInputs, OutputFilename, OutputFormat);