Skip to content

Commit

Permalink
[coverage] Allow llvm not to change mappings order
Browse files Browse the repository at this point in the history
  • Loading branch information
Lambdaris committed May 28, 2024
1 parent 6e1a042 commit 502b362
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ class CoverageMappingWriter {
ArrayRef<unsigned> VirtualFileMapping;
ArrayRef<CounterExpression> Expressions;
MutableArrayRef<CounterMappingRegion> MappingRegions;
bool KeepMappingOrder;

public:
CoverageMappingWriter(ArrayRef<unsigned> VirtualFileMapping,
ArrayRef<CounterExpression> Expressions,
MutableArrayRef<CounterMappingRegion> MappingRegions)
MutableArrayRef<CounterMappingRegion> 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);
Expand Down
35 changes: 19 additions & 16 deletions llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 502b362

Please sign in to comment.