Index: lib/ProfileData/InstrProfWriter.cpp =================================================================== --- lib/ProfileData/InstrProfWriter.cpp +++ lib/ProfileData/InstrProfWriter.cpp @@ -376,14 +376,41 @@ if (ProfileKind == PF_IRLevel) OS << "# IR level Instrumentation Flag\n:ir\n"; InstrProfSymtab Symtab; + + using FuncPair = detail::DenseMapPair; + using RecordType = std::pair; + + SmallVector OrderedFuncData; for (const auto &I : FunctionData) - if (shouldEncodeData(I.getValue())) - if (Error E = Symtab.addFuncName(I.getKey())) + for (const auto &Func : I.getValue()) + OrderedFuncData.push_back(std::make_pair(I.getKey(), Func)); + + llvm::sort(OrderedFuncData, [](const RecordType &A, const RecordType &B) { + auto nameA = A.first; + auto nameB = B.first; + int comp = nameA.compare(nameB); + if (comp) + return comp < 0; + + auto hashA = A.second.first; + auto hashB = B.second.first; + return hashA < hashB; + }); + + for (const auto &pair : OrderedFuncData) { + const auto &key = pair.first; + if (shouldEncodeData(FunctionData[key])) + if (Error E = Symtab.addFuncName(key)) return E; + } + + for (const auto &pair : OrderedFuncData) { + const auto &key = pair.first; + if (shouldEncodeData(FunctionData[key])) { + const auto &Func = pair.second; + writeRecordInText(pair.first, Func.first, Func.second, Symtab, OS); + } + } - for (const auto &I : FunctionData) - if (shouldEncodeData(I.getValue())) - for (const auto &Func : I.getValue()) - writeRecordInText(I.getKey(), Func.first, Func.second, Symtab, OS); return Error::success(); }