Index: lib/ProfileData/InstrProfWriter.cpp =================================================================== --- lib/ProfileData/InstrProfWriter.cpp +++ lib/ProfileData/InstrProfWriter.cpp @@ -408,14 +408,30 @@ else if (ProfileKind == PF_IRLevelWithCS) OS << "# CSIR level Instrumentation Flag\n:csir\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) { + return std::tie(A.first, A.second.first) < + std::tie(B.first, B.second.first); + }); + + 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(); }