171d5a254SDimitry Andric //===- CoverageMappingWriter.cpp - Code coverage mapping writer -----------===//
201095a5dSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
601095a5dSDimitry Andric //
701095a5dSDimitry Andric //===----------------------------------------------------------------------===//
801095a5dSDimitry Andric //
901095a5dSDimitry Andric // This file contains support for writing coverage mapping data for
1001095a5dSDimitry Andric // instrumentation based coverage.
1101095a5dSDimitry Andric //
1201095a5dSDimitry Andric //===----------------------------------------------------------------------===//
1301095a5dSDimitry Andric
147ab83427SDimitry Andric #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
1571d5a254SDimitry Andric #include "llvm/ADT/ArrayRef.h"
1671d5a254SDimitry Andric #include "llvm/ADT/SmallVector.h"
177fa27ce4SDimitry Andric #include "llvm/ADT/StringExtras.h"
187fa27ce4SDimitry Andric #include "llvm/ProfileData/InstrProf.h"
19cfca06d7SDimitry Andric #include "llvm/Support/Compression.h"
2001095a5dSDimitry Andric #include "llvm/Support/LEB128.h"
2171d5a254SDimitry Andric #include "llvm/Support/raw_ostream.h"
2271d5a254SDimitry Andric #include <algorithm>
2371d5a254SDimitry Andric #include <cassert>
2471d5a254SDimitry Andric #include <limits>
2571d5a254SDimitry Andric #include <vector>
2601095a5dSDimitry Andric
2701095a5dSDimitry Andric using namespace llvm;
2801095a5dSDimitry Andric using namespace coverage;
2901095a5dSDimitry Andric
CoverageFilenamesSectionWriter(ArrayRef<std::string> Filenames)301d5ae102SDimitry Andric CoverageFilenamesSectionWriter::CoverageFilenamesSectionWriter(
31344a3780SDimitry Andric ArrayRef<std::string> Filenames)
321d5ae102SDimitry Andric : Filenames(Filenames) {
331d5ae102SDimitry Andric #ifndef NDEBUG
341d5ae102SDimitry Andric StringSet<> NameSet;
351d5ae102SDimitry Andric for (StringRef Name : Filenames)
361d5ae102SDimitry Andric assert(NameSet.insert(Name).second && "Duplicate filename");
371d5ae102SDimitry Andric #endif
381d5ae102SDimitry Andric }
391d5ae102SDimitry Andric
write(raw_ostream & OS,bool Compress)40cfca06d7SDimitry Andric void CoverageFilenamesSectionWriter::write(raw_ostream &OS, bool Compress) {
41cfca06d7SDimitry Andric std::string FilenamesStr;
42cfca06d7SDimitry Andric {
43cfca06d7SDimitry Andric raw_string_ostream FilenamesOS{FilenamesStr};
4401095a5dSDimitry Andric for (const auto &Filename : Filenames) {
45cfca06d7SDimitry Andric encodeULEB128(Filename.size(), FilenamesOS);
46cfca06d7SDimitry Andric FilenamesOS << Filename;
4701095a5dSDimitry Andric }
4801095a5dSDimitry Andric }
4901095a5dSDimitry Andric
501f917f69SDimitry Andric SmallVector<uint8_t, 128> CompressedStr;
511f917f69SDimitry Andric bool doCompression = Compress && compression::zlib::isAvailable() &&
521f917f69SDimitry Andric DoInstrProfNameCompression;
53145449b1SDimitry Andric if (doCompression)
541f917f69SDimitry Andric compression::zlib::compress(arrayRefFromStringRef(FilenamesStr),
551f917f69SDimitry Andric CompressedStr,
561f917f69SDimitry Andric compression::zlib::BestSizeCompression);
57cfca06d7SDimitry Andric
58cfca06d7SDimitry Andric // ::= <num-filenames>
59cfca06d7SDimitry Andric // <uncompressed-len>
60cfca06d7SDimitry Andric // <compressed-len-or-zero>
61cfca06d7SDimitry Andric // (<compressed-filenames> | <uncompressed-filenames>)
62cfca06d7SDimitry Andric encodeULEB128(Filenames.size(), OS);
63cfca06d7SDimitry Andric encodeULEB128(FilenamesStr.size(), OS);
64cfca06d7SDimitry Andric encodeULEB128(doCompression ? CompressedStr.size() : 0U, OS);
651f917f69SDimitry Andric OS << (doCompression ? toStringRef(CompressedStr) : StringRef(FilenamesStr));
66cfca06d7SDimitry Andric }
67cfca06d7SDimitry Andric
6801095a5dSDimitry Andric namespace {
6971d5a254SDimitry Andric
70eb11fae6SDimitry Andric /// Gather only the expressions that are used by the mapping
7101095a5dSDimitry Andric /// regions in this function.
7201095a5dSDimitry Andric class CounterExpressionsMinimizer {
7301095a5dSDimitry Andric ArrayRef<CounterExpression> Expressions;
7471d5a254SDimitry Andric SmallVector<CounterExpression, 16> UsedExpressions;
7501095a5dSDimitry Andric std::vector<unsigned> AdjustedExpressionIDs;
7601095a5dSDimitry Andric
7701095a5dSDimitry Andric public:
CounterExpressionsMinimizer(ArrayRef<CounterExpression> Expressions,ArrayRef<CounterMappingRegion> MappingRegions)7871d5a254SDimitry Andric CounterExpressionsMinimizer(ArrayRef<CounterExpression> Expressions,
7971d5a254SDimitry Andric ArrayRef<CounterMappingRegion> MappingRegions)
8071d5a254SDimitry Andric : Expressions(Expressions) {
8171d5a254SDimitry Andric AdjustedExpressionIDs.resize(Expressions.size(), 0);
82b60736ecSDimitry Andric for (const auto &I : MappingRegions) {
8371d5a254SDimitry Andric mark(I.Count);
84b60736ecSDimitry Andric mark(I.FalseCount);
85b60736ecSDimitry Andric }
86b60736ecSDimitry Andric for (const auto &I : MappingRegions) {
8771d5a254SDimitry Andric gatherUsed(I.Count);
88b60736ecSDimitry Andric gatherUsed(I.FalseCount);
89b60736ecSDimitry Andric }
9071d5a254SDimitry Andric }
9171d5a254SDimitry Andric
mark(Counter C)9201095a5dSDimitry Andric void mark(Counter C) {
9301095a5dSDimitry Andric if (!C.isExpression())
9401095a5dSDimitry Andric return;
9501095a5dSDimitry Andric unsigned ID = C.getExpressionID();
9601095a5dSDimitry Andric AdjustedExpressionIDs[ID] = 1;
9701095a5dSDimitry Andric mark(Expressions[ID].LHS);
9801095a5dSDimitry Andric mark(Expressions[ID].RHS);
9901095a5dSDimitry Andric }
10001095a5dSDimitry Andric
gatherUsed(Counter C)10101095a5dSDimitry Andric void gatherUsed(Counter C) {
10201095a5dSDimitry Andric if (!C.isExpression() || !AdjustedExpressionIDs[C.getExpressionID()])
10301095a5dSDimitry Andric return;
10401095a5dSDimitry Andric AdjustedExpressionIDs[C.getExpressionID()] = UsedExpressions.size();
10501095a5dSDimitry Andric const auto &E = Expressions[C.getExpressionID()];
10601095a5dSDimitry Andric UsedExpressions.push_back(E);
10701095a5dSDimitry Andric gatherUsed(E.LHS);
10801095a5dSDimitry Andric gatherUsed(E.RHS);
10901095a5dSDimitry Andric }
11001095a5dSDimitry Andric
getExpressions() const11101095a5dSDimitry Andric ArrayRef<CounterExpression> getExpressions() const { return UsedExpressions; }
11201095a5dSDimitry Andric
113eb11fae6SDimitry Andric /// Adjust the given counter to correctly transition from the old
11401095a5dSDimitry Andric /// expression ids to the new expression ids.
adjust(Counter C) const11501095a5dSDimitry Andric Counter adjust(Counter C) const {
11601095a5dSDimitry Andric if (C.isExpression())
11701095a5dSDimitry Andric C = Counter::getExpression(AdjustedExpressionIDs[C.getExpressionID()]);
11801095a5dSDimitry Andric return C;
11901095a5dSDimitry Andric }
12001095a5dSDimitry Andric };
12171d5a254SDimitry Andric
12271d5a254SDimitry Andric } // end anonymous namespace
12301095a5dSDimitry Andric
124eb11fae6SDimitry Andric /// Encode the counter.
12501095a5dSDimitry Andric ///
12601095a5dSDimitry Andric /// The encoding uses the following format:
12701095a5dSDimitry Andric /// Low 2 bits - Tag:
12801095a5dSDimitry Andric /// Counter::Zero(0) - A Counter with kind Counter::Zero
12901095a5dSDimitry Andric /// Counter::CounterValueReference(1) - A counter with kind
13001095a5dSDimitry Andric /// Counter::CounterValueReference
13101095a5dSDimitry Andric /// Counter::Expression(2) + CounterExpression::Subtract(0) -
13201095a5dSDimitry Andric /// A counter with kind Counter::Expression and an expression
13301095a5dSDimitry Andric /// with kind CounterExpression::Subtract
13401095a5dSDimitry Andric /// Counter::Expression(2) + CounterExpression::Add(1) -
13501095a5dSDimitry Andric /// A counter with kind Counter::Expression and an expression
13601095a5dSDimitry Andric /// with kind CounterExpression::Add
13701095a5dSDimitry Andric /// Remaining bits - Counter/Expression ID.
encodeCounter(ArrayRef<CounterExpression> Expressions,Counter C)13801095a5dSDimitry Andric static unsigned encodeCounter(ArrayRef<CounterExpression> Expressions,
13901095a5dSDimitry Andric Counter C) {
14001095a5dSDimitry Andric unsigned Tag = unsigned(C.getKind());
14101095a5dSDimitry Andric if (C.isExpression())
14201095a5dSDimitry Andric Tag += Expressions[C.getExpressionID()].Kind;
14301095a5dSDimitry Andric unsigned ID = C.getCounterID();
14401095a5dSDimitry Andric assert(ID <=
14501095a5dSDimitry Andric (std::numeric_limits<unsigned>::max() >> Counter::EncodingTagBits));
14601095a5dSDimitry Andric return Tag | (ID << Counter::EncodingTagBits);
14701095a5dSDimitry Andric }
14801095a5dSDimitry Andric
writeCounter(ArrayRef<CounterExpression> Expressions,Counter C,raw_ostream & OS)14901095a5dSDimitry Andric static void writeCounter(ArrayRef<CounterExpression> Expressions, Counter C,
15001095a5dSDimitry Andric raw_ostream &OS) {
15101095a5dSDimitry Andric encodeULEB128(encodeCounter(Expressions, C), OS);
15201095a5dSDimitry Andric }
15301095a5dSDimitry Andric
write(raw_ostream & OS)15401095a5dSDimitry Andric void CoverageMappingWriter::write(raw_ostream &OS) {
155044eb2f6SDimitry Andric // Check that we don't have any bogus regions.
156044eb2f6SDimitry Andric assert(all_of(MappingRegions,
157044eb2f6SDimitry Andric [](const CounterMappingRegion &CMR) {
158044eb2f6SDimitry Andric return CMR.startLoc() <= CMR.endLoc();
159044eb2f6SDimitry Andric }) &&
160044eb2f6SDimitry Andric "Source region does not begin before it ends");
161044eb2f6SDimitry Andric
16201095a5dSDimitry Andric // Sort the regions in an ascending order by the file id and the starting
163b915e9e0SDimitry Andric // location. Sort by region kinds to ensure stable order for tests.
164e6d15924SDimitry Andric llvm::stable_sort(MappingRegions, [](const CounterMappingRegion &LHS,
165e6d15924SDimitry Andric const CounterMappingRegion &RHS) {
166b915e9e0SDimitry Andric if (LHS.FileID != RHS.FileID)
167b915e9e0SDimitry Andric return LHS.FileID < RHS.FileID;
168b915e9e0SDimitry Andric if (LHS.startLoc() != RHS.startLoc())
169b915e9e0SDimitry Andric return LHS.startLoc() < RHS.startLoc();
170ac9a064cSDimitry Andric
171ac9a064cSDimitry Andric // Put `Decision` before `Expansion`.
172ac9a064cSDimitry Andric auto getKindKey = [](CounterMappingRegion::RegionKind Kind) {
173ac9a064cSDimitry Andric return (Kind == CounterMappingRegion::MCDCDecisionRegion
174ac9a064cSDimitry Andric ? 2 * CounterMappingRegion::ExpansionRegion - 1
175ac9a064cSDimitry Andric : 2 * Kind);
176ac9a064cSDimitry Andric };
177ac9a064cSDimitry Andric
178ac9a064cSDimitry Andric return getKindKey(LHS.Kind) < getKindKey(RHS.Kind);
179b915e9e0SDimitry Andric });
18001095a5dSDimitry Andric
18101095a5dSDimitry Andric // Write out the fileid -> filename mapping.
18201095a5dSDimitry Andric encodeULEB128(VirtualFileMapping.size(), OS);
18301095a5dSDimitry Andric for (const auto &FileID : VirtualFileMapping)
18401095a5dSDimitry Andric encodeULEB128(FileID, OS);
18501095a5dSDimitry Andric
18601095a5dSDimitry Andric // Write out the expressions.
18701095a5dSDimitry Andric CounterExpressionsMinimizer Minimizer(Expressions, MappingRegions);
18801095a5dSDimitry Andric auto MinExpressions = Minimizer.getExpressions();
18901095a5dSDimitry Andric encodeULEB128(MinExpressions.size(), OS);
19001095a5dSDimitry Andric for (const auto &E : MinExpressions) {
19101095a5dSDimitry Andric writeCounter(MinExpressions, Minimizer.adjust(E.LHS), OS);
19201095a5dSDimitry Andric writeCounter(MinExpressions, Minimizer.adjust(E.RHS), OS);
19301095a5dSDimitry Andric }
19401095a5dSDimitry Andric
19501095a5dSDimitry Andric // Write out the mapping regions.
19601095a5dSDimitry Andric // Split the regions into subarrays where each region in a
19701095a5dSDimitry Andric // subarray has a fileID which is the index of that subarray.
19801095a5dSDimitry Andric unsigned PrevLineStart = 0;
19901095a5dSDimitry Andric unsigned CurrentFileID = ~0U;
20001095a5dSDimitry Andric for (auto I = MappingRegions.begin(), E = MappingRegions.end(); I != E; ++I) {
20101095a5dSDimitry Andric if (I->FileID != CurrentFileID) {
20201095a5dSDimitry Andric // Ensure that all file ids have at least one mapping region.
20301095a5dSDimitry Andric assert(I->FileID == (CurrentFileID + 1));
20401095a5dSDimitry Andric // Find the number of regions with this file id.
20501095a5dSDimitry Andric unsigned RegionCount = 1;
20601095a5dSDimitry Andric for (auto J = I + 1; J != E && I->FileID == J->FileID; ++J)
20701095a5dSDimitry Andric ++RegionCount;
20801095a5dSDimitry Andric // Start a new region sub-array.
20901095a5dSDimitry Andric encodeULEB128(RegionCount, OS);
21001095a5dSDimitry Andric
21101095a5dSDimitry Andric CurrentFileID = I->FileID;
21201095a5dSDimitry Andric PrevLineStart = 0;
21301095a5dSDimitry Andric }
21401095a5dSDimitry Andric Counter Count = Minimizer.adjust(I->Count);
215b60736ecSDimitry Andric Counter FalseCount = Minimizer.adjust(I->FalseCount);
216ac9a064cSDimitry Andric bool ParamsShouldBeNull = true;
21701095a5dSDimitry Andric switch (I->Kind) {
21801095a5dSDimitry Andric case CounterMappingRegion::CodeRegion:
219044eb2f6SDimitry Andric case CounterMappingRegion::GapRegion:
22001095a5dSDimitry Andric writeCounter(MinExpressions, Count, OS);
22101095a5dSDimitry Andric break;
22201095a5dSDimitry Andric case CounterMappingRegion::ExpansionRegion: {
22301095a5dSDimitry Andric assert(Count.isZero());
22401095a5dSDimitry Andric assert(I->ExpandedFileID <=
22501095a5dSDimitry Andric (std::numeric_limits<unsigned>::max() >>
22601095a5dSDimitry Andric Counter::EncodingCounterTagAndExpansionRegionTagBits));
22701095a5dSDimitry Andric // Mark an expansion region with a set bit that follows the counter tag,
22801095a5dSDimitry Andric // and pack the expanded file id into the remaining bits.
22901095a5dSDimitry Andric unsigned EncodedTagExpandedFileID =
23001095a5dSDimitry Andric (1 << Counter::EncodingTagBits) |
23101095a5dSDimitry Andric (I->ExpandedFileID
23201095a5dSDimitry Andric << Counter::EncodingCounterTagAndExpansionRegionTagBits);
23301095a5dSDimitry Andric encodeULEB128(EncodedTagExpandedFileID, OS);
23401095a5dSDimitry Andric break;
23501095a5dSDimitry Andric }
23601095a5dSDimitry Andric case CounterMappingRegion::SkippedRegion:
23701095a5dSDimitry Andric assert(Count.isZero());
23801095a5dSDimitry Andric encodeULEB128(unsigned(I->Kind)
23901095a5dSDimitry Andric << Counter::EncodingCounterTagAndExpansionRegionTagBits,
24001095a5dSDimitry Andric OS);
24101095a5dSDimitry Andric break;
242b60736ecSDimitry Andric case CounterMappingRegion::BranchRegion:
243b60736ecSDimitry Andric encodeULEB128(unsigned(I->Kind)
244b60736ecSDimitry Andric << Counter::EncodingCounterTagAndExpansionRegionTagBits,
245b60736ecSDimitry Andric OS);
246b60736ecSDimitry Andric writeCounter(MinExpressions, Count, OS);
247b60736ecSDimitry Andric writeCounter(MinExpressions, FalseCount, OS);
248b60736ecSDimitry Andric break;
249312c0ed1SDimitry Andric case CounterMappingRegion::MCDCBranchRegion:
250312c0ed1SDimitry Andric encodeULEB128(unsigned(I->Kind)
251312c0ed1SDimitry Andric << Counter::EncodingCounterTagAndExpansionRegionTagBits,
252312c0ed1SDimitry Andric OS);
253312c0ed1SDimitry Andric writeCounter(MinExpressions, Count, OS);
254312c0ed1SDimitry Andric writeCounter(MinExpressions, FalseCount, OS);
255ac9a064cSDimitry Andric {
256ac9a064cSDimitry Andric // They are written as internal values plus 1.
257ac9a064cSDimitry Andric const auto &BranchParams = I->getBranchParams();
258ac9a064cSDimitry Andric ParamsShouldBeNull = false;
259ac9a064cSDimitry Andric unsigned ID1 = BranchParams.ID + 1;
260ac9a064cSDimitry Andric unsigned TID1 = BranchParams.Conds[true] + 1;
261ac9a064cSDimitry Andric unsigned FID1 = BranchParams.Conds[false] + 1;
262ac9a064cSDimitry Andric encodeULEB128(ID1, OS);
263ac9a064cSDimitry Andric encodeULEB128(TID1, OS);
264ac9a064cSDimitry Andric encodeULEB128(FID1, OS);
265ac9a064cSDimitry Andric }
266312c0ed1SDimitry Andric break;
267312c0ed1SDimitry Andric case CounterMappingRegion::MCDCDecisionRegion:
268312c0ed1SDimitry Andric encodeULEB128(unsigned(I->Kind)
269312c0ed1SDimitry Andric << Counter::EncodingCounterTagAndExpansionRegionTagBits,
270312c0ed1SDimitry Andric OS);
271ac9a064cSDimitry Andric {
272ac9a064cSDimitry Andric const auto &DecisionParams = I->getDecisionParams();
273ac9a064cSDimitry Andric ParamsShouldBeNull = false;
274ac9a064cSDimitry Andric encodeULEB128(static_cast<unsigned>(DecisionParams.BitmapIdx), OS);
275ac9a064cSDimitry Andric encodeULEB128(static_cast<unsigned>(DecisionParams.NumConditions), OS);
276ac9a064cSDimitry Andric }
277312c0ed1SDimitry Andric break;
27801095a5dSDimitry Andric }
27901095a5dSDimitry Andric assert(I->LineStart >= PrevLineStart);
28001095a5dSDimitry Andric encodeULEB128(I->LineStart - PrevLineStart, OS);
28101095a5dSDimitry Andric encodeULEB128(I->ColumnStart, OS);
28201095a5dSDimitry Andric assert(I->LineEnd >= I->LineStart);
28301095a5dSDimitry Andric encodeULEB128(I->LineEnd - I->LineStart, OS);
28401095a5dSDimitry Andric encodeULEB128(I->ColumnEnd, OS);
28501095a5dSDimitry Andric PrevLineStart = I->LineStart;
286ac9a064cSDimitry Andric assert((!ParamsShouldBeNull || std::get_if<0>(&I->MCDCParams)) &&
287ac9a064cSDimitry Andric "MCDCParams should be empty");
288ac9a064cSDimitry Andric (void)ParamsShouldBeNull;
28901095a5dSDimitry Andric }
29001095a5dSDimitry Andric // Ensure that all file ids have at least one mapping region.
29101095a5dSDimitry Andric assert(CurrentFileID == (VirtualFileMapping.size() - 1));
29201095a5dSDimitry Andric }
293b1c73532SDimitry Andric
write(raw_ostream & OS,TestingFormatVersion Version)294b1c73532SDimitry Andric void TestingFormatWriter::write(raw_ostream &OS, TestingFormatVersion Version) {
295b1c73532SDimitry Andric auto ByteSwap = [](uint64_t N) {
296b1c73532SDimitry Andric return support::endian::byte_swap<uint64_t, llvm::endianness::little>(N);
297b1c73532SDimitry Andric };
298b1c73532SDimitry Andric
299b1c73532SDimitry Andric // Output a 64bit magic number.
300b1c73532SDimitry Andric auto Magic = ByteSwap(TestingFormatMagic);
301b1c73532SDimitry Andric OS.write(reinterpret_cast<char *>(&Magic), sizeof(Magic));
302b1c73532SDimitry Andric
303b1c73532SDimitry Andric // Output a 64bit version field.
304b1c73532SDimitry Andric auto VersionLittle = ByteSwap(uint64_t(Version));
305b1c73532SDimitry Andric OS.write(reinterpret_cast<char *>(&VersionLittle), sizeof(VersionLittle));
306b1c73532SDimitry Andric
307b1c73532SDimitry Andric // Output the ProfileNames data.
308b1c73532SDimitry Andric encodeULEB128(ProfileNamesData.size(), OS);
309b1c73532SDimitry Andric encodeULEB128(ProfileNamesAddr, OS);
310b1c73532SDimitry Andric OS << ProfileNamesData;
311b1c73532SDimitry Andric
312b1c73532SDimitry Andric // Version2 adds an extra field to indicate the size of the
313b1c73532SDimitry Andric // CoverageMappingData.
314b1c73532SDimitry Andric if (Version == TestingFormatVersion::Version2)
315b1c73532SDimitry Andric encodeULEB128(CoverageMappingData.size(), OS);
316b1c73532SDimitry Andric
317b1c73532SDimitry Andric // Coverage mapping data is expected to have an alignment of 8.
318b1c73532SDimitry Andric for (unsigned Pad = offsetToAlignment(OS.tell(), Align(8)); Pad; --Pad)
319b1c73532SDimitry Andric OS.write(uint8_t(0));
320b1c73532SDimitry Andric OS << CoverageMappingData;
321b1c73532SDimitry Andric
322b1c73532SDimitry Andric // Coverage records data is expected to have an alignment of 8.
323b1c73532SDimitry Andric for (unsigned Pad = offsetToAlignment(OS.tell(), Align(8)); Pad; --Pad)
324b1c73532SDimitry Andric OS.write(uint8_t(0));
325b1c73532SDimitry Andric OS << CoverageRecordsData;
326b1c73532SDimitry Andric }
327