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<std::string> 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 = Compress && compression::zlib::isAvailable() && 51 DoInstrProfNameCompression; 52 if (doCompression) 53 compression::zlib::compress(FilenamesStr, CompressedStr, 54 compression::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 ? CompressedStr.str() : 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 mark(I.FalseCount); 83 } 84 for (const auto &I : MappingRegions) { 85 gatherUsed(I.Count); 86 gatherUsed(I.FalseCount); 87 } 88 } 89 90 void mark(Counter C) { 91 if (!C.isExpression()) 92 return; 93 unsigned ID = C.getExpressionID(); 94 AdjustedExpressionIDs[ID] = 1; 95 mark(Expressions[ID].LHS); 96 mark(Expressions[ID].RHS); 97 } 98 99 void gatherUsed(Counter C) { 100 if (!C.isExpression() || !AdjustedExpressionIDs[C.getExpressionID()]) 101 return; 102 AdjustedExpressionIDs[C.getExpressionID()] = UsedExpressions.size(); 103 const auto &E = Expressions[C.getExpressionID()]; 104 UsedExpressions.push_back(E); 105 gatherUsed(E.LHS); 106 gatherUsed(E.RHS); 107 } 108 109 ArrayRef<CounterExpression> getExpressions() const { return UsedExpressions; } 110 111 /// Adjust the given counter to correctly transition from the old 112 /// expression ids to the new expression ids. 113 Counter adjust(Counter C) const { 114 if (C.isExpression()) 115 C = Counter::getExpression(AdjustedExpressionIDs[C.getExpressionID()]); 116 return C; 117 } 118 }; 119 120 } // end anonymous namespace 121 122 /// Encode the counter. 123 /// 124 /// The encoding uses the following format: 125 /// Low 2 bits - Tag: 126 /// Counter::Zero(0) - A Counter with kind Counter::Zero 127 /// Counter::CounterValueReference(1) - A counter with kind 128 /// Counter::CounterValueReference 129 /// Counter::Expression(2) + CounterExpression::Subtract(0) - 130 /// A counter with kind Counter::Expression and an expression 131 /// with kind CounterExpression::Subtract 132 /// Counter::Expression(2) + CounterExpression::Add(1) - 133 /// A counter with kind Counter::Expression and an expression 134 /// with kind CounterExpression::Add 135 /// Remaining bits - Counter/Expression ID. 136 static unsigned encodeCounter(ArrayRef<CounterExpression> Expressions, 137 Counter C) { 138 unsigned Tag = unsigned(C.getKind()); 139 if (C.isExpression()) 140 Tag += Expressions[C.getExpressionID()].Kind; 141 unsigned ID = C.getCounterID(); 142 assert(ID <= 143 (std::numeric_limits<unsigned>::max() >> Counter::EncodingTagBits)); 144 return Tag | (ID << Counter::EncodingTagBits); 145 } 146 147 static void writeCounter(ArrayRef<CounterExpression> Expressions, Counter C, 148 raw_ostream &OS) { 149 encodeULEB128(encodeCounter(Expressions, C), OS); 150 } 151 152 void CoverageMappingWriter::write(raw_ostream &OS) { 153 // Check that we don't have any bogus regions. 154 assert(all_of(MappingRegions, 155 [](const CounterMappingRegion &CMR) { 156 return CMR.startLoc() <= CMR.endLoc(); 157 }) && 158 "Source region does not begin before it ends"); 159 160 // Sort the regions in an ascending order by the file id and the starting 161 // location. Sort by region kinds to ensure stable order for tests. 162 llvm::stable_sort(MappingRegions, [](const CounterMappingRegion &LHS, 163 const CounterMappingRegion &RHS) { 164 if (LHS.FileID != RHS.FileID) 165 return LHS.FileID < RHS.FileID; 166 if (LHS.startLoc() != RHS.startLoc()) 167 return LHS.startLoc() < RHS.startLoc(); 168 return LHS.Kind < RHS.Kind; 169 }); 170 171 // Write out the fileid -> filename mapping. 172 encodeULEB128(VirtualFileMapping.size(), OS); 173 for (const auto &FileID : VirtualFileMapping) 174 encodeULEB128(FileID, OS); 175 176 // Write out the expressions. 177 CounterExpressionsMinimizer Minimizer(Expressions, MappingRegions); 178 auto MinExpressions = Minimizer.getExpressions(); 179 encodeULEB128(MinExpressions.size(), OS); 180 for (const auto &E : MinExpressions) { 181 writeCounter(MinExpressions, Minimizer.adjust(E.LHS), OS); 182 writeCounter(MinExpressions, Minimizer.adjust(E.RHS), OS); 183 } 184 185 // Write out the mapping regions. 186 // Split the regions into subarrays where each region in a 187 // subarray has a fileID which is the index of that subarray. 188 unsigned PrevLineStart = 0; 189 unsigned CurrentFileID = ~0U; 190 for (auto I = MappingRegions.begin(), E = MappingRegions.end(); I != E; ++I) { 191 if (I->FileID != CurrentFileID) { 192 // Ensure that all file ids have at least one mapping region. 193 assert(I->FileID == (CurrentFileID + 1)); 194 // Find the number of regions with this file id. 195 unsigned RegionCount = 1; 196 for (auto J = I + 1; J != E && I->FileID == J->FileID; ++J) 197 ++RegionCount; 198 // Start a new region sub-array. 199 encodeULEB128(RegionCount, OS); 200 201 CurrentFileID = I->FileID; 202 PrevLineStart = 0; 203 } 204 Counter Count = Minimizer.adjust(I->Count); 205 Counter FalseCount = Minimizer.adjust(I->FalseCount); 206 switch (I->Kind) { 207 case CounterMappingRegion::CodeRegion: 208 case CounterMappingRegion::GapRegion: 209 writeCounter(MinExpressions, Count, OS); 210 break; 211 case CounterMappingRegion::ExpansionRegion: { 212 assert(Count.isZero()); 213 assert(I->ExpandedFileID <= 214 (std::numeric_limits<unsigned>::max() >> 215 Counter::EncodingCounterTagAndExpansionRegionTagBits)); 216 // Mark an expansion region with a set bit that follows the counter tag, 217 // and pack the expanded file id into the remaining bits. 218 unsigned EncodedTagExpandedFileID = 219 (1 << Counter::EncodingTagBits) | 220 (I->ExpandedFileID 221 << Counter::EncodingCounterTagAndExpansionRegionTagBits); 222 encodeULEB128(EncodedTagExpandedFileID, OS); 223 break; 224 } 225 case CounterMappingRegion::SkippedRegion: 226 assert(Count.isZero()); 227 encodeULEB128(unsigned(I->Kind) 228 << Counter::EncodingCounterTagAndExpansionRegionTagBits, 229 OS); 230 break; 231 case CounterMappingRegion::BranchRegion: 232 encodeULEB128(unsigned(I->Kind) 233 << Counter::EncodingCounterTagAndExpansionRegionTagBits, 234 OS); 235 writeCounter(MinExpressions, Count, OS); 236 writeCounter(MinExpressions, FalseCount, OS); 237 break; 238 } 239 assert(I->LineStart >= PrevLineStart); 240 encodeULEB128(I->LineStart - PrevLineStart, OS); 241 encodeULEB128(I->ColumnStart, OS); 242 assert(I->LineEnd >= I->LineStart); 243 encodeULEB128(I->LineEnd - I->LineStart, OS); 244 encodeULEB128(I->ColumnEnd, OS); 245 PrevLineStart = I->LineStart; 246 } 247 // Ensure that all file ids have at least one mapping region. 248 assert(CurrentFileID == (VirtualFileMapping.size() - 1)); 249 } 250