diff --git a/llvm/include/llvm/ProfileData/Coverage/CoverageMappingWriter.h b/llvm/include/llvm/ProfileData/Coverage/CoverageMappingWriter.h index 02848deaba9db1..00363a25e8806b 100644 --- a/llvm/include/llvm/ProfileData/Coverage/CoverageMappingWriter.h +++ b/llvm/include/llvm/ProfileData/Coverage/CoverageMappingWriter.h @@ -42,13 +42,15 @@ class CoverageMappingWriter { ArrayRef VirtualFileMapping; ArrayRef Expressions; MutableArrayRef MappingRegions; + bool KeepMappingOrder; public: CoverageMappingWriter(ArrayRef VirtualFileMapping, ArrayRef Expressions, - MutableArrayRef MappingRegions) + MutableArrayRef MappingRegions, + bool KeepMappingOrder = false) : VirtualFileMapping(VirtualFileMapping), Expressions(Expressions), - MappingRegions(MappingRegions) {} + MappingRegions(MappingRegions), KeepMappingOrder(KeepMappingOrder) {} /// Write encoded coverage mapping data to the given output stream. void write(raw_ostream &OS); diff --git a/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp b/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp index adfd22804356e1..974138de359002 100644 --- a/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp +++ b/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp @@ -161,22 +161,25 @@ void CoverageMappingWriter::write(raw_ostream &OS) { // Sort the regions in an ascending order by the file id and the starting // location. Sort by region kinds to ensure stable order for tests. - llvm::stable_sort(MappingRegions, [](const CounterMappingRegion &LHS, - const CounterMappingRegion &RHS) { - if (LHS.FileID != RHS.FileID) - return LHS.FileID < RHS.FileID; - if (LHS.startLoc() != RHS.startLoc()) - return LHS.startLoc() < RHS.startLoc(); - - // Put `Decision` before `Expansion`. - auto getKindKey = [](CounterMappingRegion::RegionKind Kind) { - return (Kind == CounterMappingRegion::MCDCDecisionRegion - ? 2 * CounterMappingRegion::ExpansionRegion - 1 - : 2 * Kind); - }; - - return getKindKey(LHS.Kind) < getKindKey(RHS.Kind); - }); + if (!KeepMappingOrder) { + llvm::stable_sort(MappingRegions, [](const CounterMappingRegion &LHS, + const CounterMappingRegion &RHS) { + if (LHS.FileID != RHS.FileID) + return LHS.FileID < RHS.FileID; + + if (LHS.startLoc() != RHS.startLoc()) + return LHS.startLoc() < RHS.startLoc(); + + // Put `Decision` before `Expansion`. + auto getKindKey = [](CounterMappingRegion::RegionKind Kind) { + return (Kind == CounterMappingRegion::MCDCDecisionRegion + ? 2 * CounterMappingRegion::ExpansionRegion - 1 + : 2 * Kind); + }; + + return getKindKey(LHS.Kind) < getKindKey(RHS.Kind); + }); + } // Write out the fileid -> filename mapping. encodeULEB128(VirtualFileMapping.size(), OS);