1 //===- lib/Support/CodeGenCoverage.cpp -------------------------------------==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// \file 10 /// This file implements the CodeGenCoverage class. 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Support/CodeGenCoverage.h" 14 15 #include "llvm/Config/config.h" 16 #include "llvm/Support/Endian.h" 17 #include "llvm/Support/FileSystem.h" 18 #include "llvm/Support/MemoryBuffer.h" 19 #include "llvm/Support/Mutex.h" 20 #include "llvm/Support/ScopedPrinter.h" 21 #include "llvm/Support/ToolOutputFile.h" 22 23 #if LLVM_ON_UNIX 24 #include <unistd.h> 25 #elif LLVM_ON_WIN32 26 #include <windows.h> 27 #endif 28 29 using namespace llvm; 30 31 static sys::SmartMutex<true> OutputMutex; 32 33 CodeGenCoverage::CodeGenCoverage() {} 34 35 void CodeGenCoverage::setCovered(uint64_t RuleID) { 36 if (RuleCoverage.size() <= RuleID) 37 RuleCoverage.resize(RuleID + 1, 0); 38 RuleCoverage[RuleID] = true; 39 } 40 41 bool CodeGenCoverage::isCovered(uint64_t RuleID) { 42 if (RuleCoverage.size() <= RuleID) 43 return false; 44 return RuleCoverage[RuleID]; 45 } 46 47 bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) { 48 const char *CurPtr = Buffer.getBufferStart(); 49 50 while (CurPtr != Buffer.getBufferEnd()) { 51 // Read the backend name from the input. 52 const char *LexedBackendName = CurPtr; 53 while (*CurPtr++ != 0) 54 ; 55 if (CurPtr == Buffer.getBufferEnd()) 56 return false; // Data is invalid, expected rule id's to follow. 57 58 bool IsForThisBackend = BackendName.equals(LexedBackendName); 59 while (CurPtr != Buffer.getBufferEnd()) { 60 if (std::distance(CurPtr, Buffer.getBufferEnd()) < 8) 61 return false; // Data is invalid. Not enough bytes for another rule id. 62 63 uint64_t RuleID = support::endian::read64(CurPtr, support::native); 64 CurPtr += 8; 65 66 // ~0ull terminates the rule id list. 67 if (RuleID == ~0ull) 68 break; 69 70 // Anything else, is recorded or ignored depending on whether it's 71 // intended for the backend we're interested in. 72 if (IsForThisBackend) 73 setCovered(RuleID); 74 } 75 } 76 77 return true; 78 } 79 80 bool CodeGenCoverage::emit(StringRef CoveragePrefix, 81 StringRef BackendName) const { 82 if (!CoveragePrefix.empty() && !RuleCoverage.empty()) { 83 sys::SmartScopedLock<true> Lock(OutputMutex); 84 85 // We can handle locking within a process easily enough but we don't want to 86 // manage it between multiple processes. Use the process ID to ensure no 87 // more than one process is ever writing to the same file at the same time. 88 std::string Pid = 89 #if LLVM_ON_UNIX 90 llvm::to_string(::getpid()); 91 #elif LLVM_ON_WIN32 92 llvm::to_string(::GetCurrentProcessId()); 93 #else 94 ""; 95 #endif 96 97 std::string CoverageFilename = (CoveragePrefix + Pid).str(); 98 99 std::error_code EC; 100 sys::fs::OpenFlags OpenFlags = sys::fs::F_Append; 101 std::unique_ptr<ToolOutputFile> CoverageFile = 102 llvm::make_unique<ToolOutputFile>(CoverageFilename, EC, OpenFlags); 103 if (EC) 104 return false; 105 106 uint64_t Zero = 0; 107 uint64_t InvZero = ~0ull; 108 CoverageFile->os() << BackendName; 109 CoverageFile->os().write((const char *)&Zero, sizeof(unsigned char)); 110 for (uint64_t I : RuleCoverage.set_bits()) 111 CoverageFile->os().write((const char *)&I, sizeof(uint64_t)); 112 CoverageFile->os().write((const char *)&InvZero, sizeof(uint64_t)); 113 114 CoverageFile->keep(); 115 } 116 117 return true; 118 } 119 120 void CodeGenCoverage::reset() { RuleCoverage.resize(0); } 121