Index: lib/ProfileData/InstrProfWriter.cpp =================================================================== --- lib/ProfileData/InstrProfWriter.cpp +++ lib/ProfileData/InstrProfWriter.cpp @@ -376,14 +376,37 @@ if (ProfileKind == PF_IRLevel) OS << "# IR level Instrumentation Flag\n:ir\n"; InstrProfSymtab Symtab; - for (const auto &I : FunctionData) - if (shouldEncodeData(I.getValue())) + + 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())) return E; - - for (const auto &I : FunctionData) - if (shouldEncodeData(I.getValue())) for (const auto &Func : I.getValue()) - writeRecordInText(I.getKey(), Func.first, Func.second, Symtab, OS); + 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 &record : OrderedFuncData) { + const auto &name = record.first; + const auto &Func = record.second; + writeRecordInText(name, Func.first, Func.second, Symtab, OS); + } + return Error::success(); }