1 //===- CoverageMappingWriter.cpp - Code coverage mapping writer -----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains support for writing coverage mapping data for 10 // instrumentation based coverage. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ProfileData/InstrProf.h" 15 #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h" 16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/Support/Compression.h" 19 #include "llvm/Support/LEB128.h" 20 #include "llvm/Support/raw_ostream.h" 21 #include <algorithm> 22 #include <cassert> 23 #include <limits> 24 #include <vector> 25 26 using namespace llvm; 27 using namespace coverage; 28 29 CoverageFilenamesSectionWriter::CoverageFilenamesSectionWriter( 30 ArrayRef<StringRef> Filenames) 31 : Filenames(Filenames) { 32 #ifndef NDEBUG 33 StringSet<> NameSet; 34 for (StringRef Name : Filenames) 35 assert(NameSet.insert(Name).second && "Duplicate filename"); 36 #endif 37 } 38 39 void CoverageFilenamesSectionWriter::write(raw_ostream &OS, bool Compress) { 40 std::string FilenamesStr; 41 { 42 raw_string_ostream FilenamesOS{FilenamesStr}; 43 for (const auto &Filename : Filenames) { 44 encodeULEB128(Filename.size(), FilenamesOS); 45 FilenamesOS << Filename; 46 } 47 } 48 49 SmallString<128> CompressedStr; 50 bool doCompression = 51 Compress && zlib::isAvailable() && DoInstrProfNameCompression; 52 if (doCompression) 53 cantFail( 54 zlib::compress(FilenamesStr, CompressedStr, zlib::BestSizeCompression)); 55 56 // ::= <num-filenames> 57 // <uncompressed-len> 58 // <compressed-len-or-zero> 59 // (<compressed-filenames> | <uncompressed-filenames>) 60 encodeULEB128(Filenames.size(), OS); 61 encodeULEB128(FilenamesStr.size(), OS); 62 encodeULEB128(doCompression ? CompressedStr.size() : 0U, OS); 63 OS << (doCompression ? StringRef(CompressedStr) : StringRef(FilenamesStr)); 64 } 65 66 namespace { 67 68 /// Gather only the expressions that are used by the mapping 69 /// regions in this function. 70 class CounterExpressionsMinimizer { 71 ArrayRef<CounterExpression> Expressions; 72 SmallVector<CounterExpression, 16> UsedExpressions; 73 std::vector<unsigned> AdjustedExpressionIDs; 74 75 public: 76 CounterExpressionsMinimizer(ArrayRef<CounterExpression> Expressions, 77 ArrayRef<CounterMappingRegion> MappingRegions) 78 : Expressions(Expressions) { 79 AdjustedExpressionIDs.resize(Expressions.size(), 0); 80 for (const auto &I : MappingRegions) 81 mark(I.Count); 82 for (const auto &I : MappingRegions) 83 gatherUsed(I.Count); 84 } 85 86 void mark(Counter C) { 87 if (!C.isExpression()) 88 return; 89 unsigned ID = C.getExpressionID(); 90 AdjustedExpressionIDs[ID] = 1; 91 mark(Expressions[ID].LHS); 92 mark(Expressions[ID].RHS); 93 } 94 95 void gatherUsed(Counter C) { 96 if (!C.isExpression() || !AdjustedExpressionIDs[C.getExpressionID()]) 97 return; 98 AdjustedExpressionIDs[C.getExpressionID()] = UsedExpressions.size(); 99 const auto &E = Expressions[C.getExpressionID()]; 100 UsedExpressions.push_back(E); 101 gatherUsed(E.LHS); 102 gatherUsed(E.RHS); 103 } 104 105 ArrayRef<CounterExpression> getExpressions() const { return UsedExpressions; } 106 107 /// Adjust the given counter to correctly transition from the old 108 /// expression ids to the new expression ids. 109 Counter adjust(Counter C) const { 110 if (C.isExpression()) 111 C = Counter::getExpression(AdjustedExpressionIDs[C.getExpressionID()]); 112 return C; 113 } 114 }; 115 116 } // end anonymous namespace 117 118 /// Encode the counter. 119 /// 120 /// The encoding uses the following format: 121 /// Low 2 bits - Tag: 122 /// Counter::Zero(0) - A Counter with kind Counter::Zero 123 /// Counter::CounterValueReference(1) - A counter with kind 124 /// Counter::CounterValueReference 125 /// Counter::Expression(2) + CounterExpression::Subtract(0) - 126 /// A counter with kind Counter::Expression and an expression 127 /// with kind CounterExpression::Subtract 128 /// Counter::Expression(2) + CounterExpression::Add(1) - 129 /// A counter with kind Counter::Expression and an expression 130 /// with kind CounterExpression::Add 131 /// Remaining bits - Counter/Expression ID. 132 static unsigned encodeCounter(ArrayRef<CounterExpression> Expressions, 133 Counter C) { 134 unsigned Tag = unsigned(C.getKind()); 135 if (C.isExpression()) 136 Tag += Expressions[C.getExpressionID()].Kind; 137 unsigned ID = C.getCounterID(); 138 assert(ID <= 139 (std::numeric_limits<unsigned>::max() >> Counter::EncodingTagBits)); 140 return Tag | (ID << Counter::EncodingTagBits); 141 } 142 143 static void writeCounter(ArrayRef<CounterExpression> Expressions, Counter C, 144 raw_ostream &OS) { 145 encodeULEB128(encodeCounter(Expressions, C), OS); 146 } 147 148 void CoverageMappingWriter::write(raw_ostream &OS) { 149 // Check that we don't have any bogus regions. 150 assert(all_of(MappingRegions, 151 [](const CounterMappingRegion &CMR) { 152 return CMR.startLoc() <= CMR.endLoc(); 153 }) && 154 "Source region does not begin before it ends"); 155 156 // Sort the regions in an ascending order by the file id and the starting 157 // location. Sort by region kinds to ensure stable order for tests. 158 llvm::stable_sort(MappingRegions, [](const CounterMappingRegion &LHS, 159 const CounterMappingRegion &RHS) { 160 if (LHS.FileID != RHS.FileID) 161 return LHS.FileID < RHS.FileID; 162 if (LHS.startLoc() != RHS.startLoc()) 163 return LHS.startLoc() < RHS.startLoc(); 164 return LHS.Kind < RHS.Kind; 165 }); 166 167 // Write out the fileid -> filename mapping. 168 encodeULEB128(VirtualFileMapping.size(), OS); 169 for (const auto &FileID : VirtualFileMapping) 170 encodeULEB128(FileID, OS); 171 172 // Write out the expressions. 173 CounterExpressionsMinimizer Minimizer(Expressions, MappingRegions); 174 auto MinExpressions = Minimizer.getExpressions(); 175 encodeULEB128(MinExpressions.size(), OS); 176 for (const auto &E : MinExpressions) { 177 writeCounter(MinExpressions, Minimizer.adjust(E.LHS), OS); 178 writeCounter(MinExpressions, Minimizer.adjust(E.RHS), OS); 179 } 180 181 // Write out the mapping regions. 182 // Split the regions into subarrays where each region in a 183 // subarray has a fileID which is the index of that subarray. 184 unsigned PrevLineStart = 0; 185 unsigned CurrentFileID = ~0U; 186 for (auto I = MappingRegions.begin(), E = MappingRegions.end(); I != E; ++I) { 187 if (I->FileID != CurrentFileID) { 188 // Ensure that all file ids have at least one mapping region. 189 assert(I->FileID == (CurrentFileID + 1)); 190 // Find the number of regions with this file id. 191 unsigned RegionCount = 1; 192 for (auto J = I + 1; J != E && I->FileID == J->FileID; ++J) 193 ++RegionCount; 194 // Start a new region sub-array. 195 encodeULEB128(RegionCount, OS); 196 197 CurrentFileID = I->FileID; 198 PrevLineStart = 0; 199 } 200 Counter Count = Minimizer.adjust(I->Count); 201 switch (I->Kind) { 202 case CounterMappingRegion::CodeRegion: 203 case CounterMappingRegion::GapRegion: 204 writeCounter(MinExpressions, Count, OS); 205 break; 206 case CounterMappingRegion::ExpansionRegion: { 207 assert(Count.isZero()); 208 assert(I->ExpandedFileID <= 209 (std::numeric_limits<unsigned>::max() >> 210 Counter::EncodingCounterTagAndExpansionRegionTagBits)); 211 // Mark an expansion region with a set bit that follows the counter tag, 212 // and pack the expanded file id into the remaining bits. 213 unsigned EncodedTagExpandedFileID = 214 (1 << Counter::EncodingTagBits) | 215 (I->ExpandedFileID 216 << Counter::EncodingCounterTagAndExpansionRegionTagBits); 217 encodeULEB128(EncodedTagExpandedFileID, OS); 218 break; 219 } 220 case CounterMappingRegion::SkippedRegion: 221 assert(Count.isZero()); 222 encodeULEB128(unsigned(I->Kind) 223 << Counter::EncodingCounterTagAndExpansionRegionTagBits, 224 OS); 225 break; 226 } 227 assert(I->LineStart >= PrevLineStart); 228 encodeULEB128(I->LineStart - PrevLineStart, OS); 229 encodeULEB128(I->ColumnStart, OS); 230 assert(I->LineEnd >= I->LineStart); 231 encodeULEB128(I->LineEnd - I->LineStart, OS); 232 encodeULEB128(I->ColumnEnd, OS); 233 PrevLineStart = I->LineStart; 234 } 235 // Ensure that all file ids have at least one mapping region. 236 assert(CurrentFileID == (VirtualFileMapping.size() - 1)); 237 } 238