Skip to content

Commit a6817b7

Browse files
chapunitstellar
authored andcommitted
CoverageMappingWriter: Emit Decision before Expansion (#78966)
To relax scanning record, tweak order by `Decision < Expansion`, or `Expansion` could not be distinguished whether it belonged to `Decision` or not. Relevant to #77871 (cherry picked from commit 438fe1d)
1 parent 43db795 commit a6817b7

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,15 @@ void CoverageMappingWriter::write(raw_ostream &OS) {
167167
return LHS.FileID < RHS.FileID;
168168
if (LHS.startLoc() != RHS.startLoc())
169169
return LHS.startLoc() < RHS.startLoc();
170-
return LHS.Kind < RHS.Kind;
170+
171+
// Put `Decision` before `Expansion`.
172+
auto getKindKey = [](CounterMappingRegion::RegionKind Kind) {
173+
return (Kind == CounterMappingRegion::MCDCDecisionRegion
174+
? 2 * CounterMappingRegion::ExpansionRegion - 1
175+
: 2 * Kind);
176+
};
177+
178+
return getKindKey(LHS.Kind) < getKindKey(RHS.Kind);
171179
});
172180

173181
// Write out the fileid -> filename mapping.

llvm/unittests/ProfileData/CoverageMappingTest.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,42 @@ TEST_P(CoverageMappingTest, non_code_region_bitmask) {
890890
ASSERT_EQ(1U, Names.size());
891891
}
892892

893+
// Test the order of MCDCDecision before Expansion
894+
TEST_P(CoverageMappingTest, decision_before_expansion) {
895+
startFunction("foo", 0x1234);
896+
addCMR(Counter::getCounter(0), "foo", 3, 23, 5, 2);
897+
898+
// This(4:11) was put after Expansion(4:11) before the fix
899+
addMCDCDecisionCMR(0, 2, "foo", 4, 11, 4, 20);
900+
901+
addExpansionCMR("foo", "A", 4, 11, 4, 12);
902+
addExpansionCMR("foo", "B", 4, 19, 4, 20);
903+
addCMR(Counter::getCounter(0), "A", 1, 14, 1, 17);
904+
addCMR(Counter::getCounter(0), "A", 1, 14, 1, 17);
905+
addMCDCBranchCMR(Counter::getCounter(0), Counter::getCounter(1), 1, 2, 0, "A",
906+
1, 14, 1, 17);
907+
addCMR(Counter::getCounter(1), "B", 1, 14, 1, 17);
908+
addMCDCBranchCMR(Counter::getCounter(1), Counter::getCounter(2), 2, 0, 0, "B",
909+
1, 14, 1, 17);
910+
911+
// InputFunctionCoverageData::Regions is rewritten after the write.
912+
auto InputRegions = InputFunctions.back().Regions;
913+
914+
writeAndReadCoverageRegions();
915+
916+
const auto &OutputRegions = OutputFunctions.back().Regions;
917+
918+
size_t N = ArrayRef(InputRegions).size();
919+
ASSERT_EQ(N, OutputRegions.size());
920+
for (size_t I = 0; I < N; ++I) {
921+
ASSERT_EQ(InputRegions[I].Kind, OutputRegions[I].Kind);
922+
ASSERT_EQ(InputRegions[I].FileID, OutputRegions[I].FileID);
923+
ASSERT_EQ(InputRegions[I].ExpandedFileID, OutputRegions[I].ExpandedFileID);
924+
ASSERT_EQ(InputRegions[I].startLoc(), OutputRegions[I].startLoc());
925+
ASSERT_EQ(InputRegions[I].endLoc(), OutputRegions[I].endLoc());
926+
}
927+
}
928+
893929
TEST_P(CoverageMappingTest, strip_filename_prefix) {
894930
ProfileWriter.addRecord({"file1:func", 0x1234, {0}}, Err);
895931

0 commit comments

Comments
 (0)