Index: lib/ProfileData/InstrProfReader.cpp =================================================================== --- lib/ProfileData/InstrProfReader.cpp +++ lib/ProfileData/InstrProfReader.cpp @@ -733,7 +733,7 @@ } Error IndexedInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) { - static unsigned RecordIndex = 0; + thread_local static unsigned RecordIndex = 0; ArrayRef Data; Index: test/tools/llvm-profdata/Inputs/multiple-profdata-merge.proftext =================================================================== --- test/tools/llvm-profdata/Inputs/multiple-profdata-merge.proftext +++ test/tools/llvm-profdata/Inputs/multiple-profdata-merge.proftext @@ -0,0 +1,106 @@ +# IR level Instrumentation Flag +:ir +foo +# Func Hash: +36982789018 +# Num Counters: +4 +# Counter Values: +700000 +700000 +0 +0 + +foo +# Func Hash: +59188585735 +# Num Counters: +6 +# Counter Values: +400000 +400000 +0 +0 +0 +0 + +foo +# Func Hash: +27904764724 +# Num Counters: +3 +# Counter Values: +200000 +200000 +0 + +foo +# Func Hash: +60466382370 +# Num Counters: +6 +# Counter Values: +0 +100000 +0 +0 +0 +0 + +bar +# Func Hash: +12884901887 +# Num Counters: +1 +# Counter Values: +0 + +foo2 +# Func Hash: +12884901887 +# Num Counters: +1 +# Counter Values: +0 + +foo3 +# Func Hash: +12884901887 +# Num Counters: +1 +# Counter Values: +0 + +foo4 +# Func Hash: +12884901887 +# Num Counters: +1 +# Counter Values: +0 + +foo5 +# Func Hash: +12884901887 +# Num Counters: +1 +# Counter Values: +0 + +foo1 +# Func Hash: +12884901887 +# Num Counters: +1 +# Counter Values: +100000 + +main +# Func Hash: +29212902728 +# Num Counters: +2 +# Counter Values: +1400000 +14 + Index: test/tools/llvm-profdata/multiple-profdata-merge.test =================================================================== --- test/tools/llvm-profdata/multiple-profdata-merge.test +++ test/tools/llvm-profdata/multiple-profdata-merge.test @@ -0,0 +1,11 @@ +Test multi-thread merge of multiple profdata files. + +RUN: llvm-profdata merge %p/Inputs/multiple-profdata-merge.proftext -o %t +RUN: llvm-profdata merge -j 4 %t %t %t %t -o %t_2 +RUN: llvm-profdata show %t_2 | FileCheck %s + +; CHECK:Total functions: 11 +; CHECK:Maximum function count: 5600000 +; CHECK:Maximum internal block count: 2800000 + + Index: tools/llvm-profdata/llvm-profdata.cpp =================================================================== --- tools/llvm-profdata/llvm-profdata.cpp +++ tools/llvm-profdata/llvm-profdata.cpp @@ -227,10 +227,18 @@ 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; + unsigned ChunkSize = Inputs.size() / NumThreads; + unsigned ChunkRemainder = Inputs.size() % NumThreads; + for (unsigned Ctx = 0; Ctx < NumThreads; Ctx++) { + Pool.async( + [&](unsigned Ctx) { + for (unsigned I = 0; I < ChunkSize; I++) + loadInput(Inputs[Ctx * ChunkSize + I], Contexts[Ctx].get()); + if (Ctx < ChunkRemainder) + loadInput(Inputs[NumThreads * ChunkSize + Ctx], + Contexts[Ctx].get()); + }, + Ctx); } Pool.wait();