Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 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 | |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame^] | 15 | #include "llvm/Config/llvm-config.h" |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Endian.h" |
| 17 | #include "llvm/Support/FileSystem.h" |
Zachary Turner | bd159d3 | 2017-11-17 01:00:35 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 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> |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 25 | #elif _WIN32 |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 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 | |
Roman Tereshin | 38489ed | 2018-04-26 20:22:17 +0000 | [diff] [blame] | 41 | bool CodeGenCoverage::isCovered(uint64_t RuleID) const { |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 42 | if (RuleCoverage.size() <= RuleID) |
| 43 | return false; |
| 44 | return RuleCoverage[RuleID]; |
| 45 | } |
| 46 | |
Roman Tereshin | 38489ed | 2018-04-26 20:22:17 +0000 | [diff] [blame] | 47 | iterator_range<CodeGenCoverage::const_covered_iterator> |
| 48 | CodeGenCoverage::covered() const { |
| 49 | return RuleCoverage.set_bits(); |
| 50 | } |
| 51 | |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 52 | bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) { |
| 53 | const char *CurPtr = Buffer.getBufferStart(); |
| 54 | |
| 55 | while (CurPtr != Buffer.getBufferEnd()) { |
| 56 | // Read the backend name from the input. |
| 57 | const char *LexedBackendName = CurPtr; |
| 58 | while (*CurPtr++ != 0) |
| 59 | ; |
| 60 | if (CurPtr == Buffer.getBufferEnd()) |
| 61 | return false; // Data is invalid, expected rule id's to follow. |
| 62 | |
| 63 | bool IsForThisBackend = BackendName.equals(LexedBackendName); |
| 64 | while (CurPtr != Buffer.getBufferEnd()) { |
| 65 | if (std::distance(CurPtr, Buffer.getBufferEnd()) < 8) |
| 66 | return false; // Data is invalid. Not enough bytes for another rule id. |
| 67 | |
| 68 | uint64_t RuleID = support::endian::read64(CurPtr, support::native); |
| 69 | CurPtr += 8; |
| 70 | |
| 71 | // ~0ull terminates the rule id list. |
| 72 | if (RuleID == ~0ull) |
| 73 | break; |
| 74 | |
| 75 | // Anything else, is recorded or ignored depending on whether it's |
| 76 | // intended for the backend we're interested in. |
| 77 | if (IsForThisBackend) |
| 78 | setCovered(RuleID); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | bool CodeGenCoverage::emit(StringRef CoveragePrefix, |
| 86 | StringRef BackendName) const { |
| 87 | if (!CoveragePrefix.empty() && !RuleCoverage.empty()) { |
| 88 | sys::SmartScopedLock<true> Lock(OutputMutex); |
| 89 | |
| 90 | // We can handle locking within a process easily enough but we don't want to |
| 91 | // manage it between multiple processes. Use the process ID to ensure no |
| 92 | // more than one process is ever writing to the same file at the same time. |
| 93 | std::string Pid = |
| 94 | #if LLVM_ON_UNIX |
| 95 | llvm::to_string(::getpid()); |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 96 | #elif _WIN32 |
Daniel Sanders | f76f315 | 2017-11-16 00:46:35 +0000 | [diff] [blame] | 97 | llvm::to_string(::GetCurrentProcessId()); |
| 98 | #else |
| 99 | ""; |
| 100 | #endif |
| 101 | |
| 102 | std::string CoverageFilename = (CoveragePrefix + Pid).str(); |
| 103 | |
| 104 | std::error_code EC; |
| 105 | sys::fs::OpenFlags OpenFlags = sys::fs::F_Append; |
| 106 | std::unique_ptr<ToolOutputFile> CoverageFile = |
| 107 | llvm::make_unique<ToolOutputFile>(CoverageFilename, EC, OpenFlags); |
| 108 | if (EC) |
| 109 | return false; |
| 110 | |
| 111 | uint64_t Zero = 0; |
| 112 | uint64_t InvZero = ~0ull; |
| 113 | CoverageFile->os() << BackendName; |
| 114 | CoverageFile->os().write((const char *)&Zero, sizeof(unsigned char)); |
| 115 | for (uint64_t I : RuleCoverage.set_bits()) |
| 116 | CoverageFile->os().write((const char *)&I, sizeof(uint64_t)); |
| 117 | CoverageFile->os().write((const char *)&InvZero, sizeof(uint64_t)); |
| 118 | |
| 119 | CoverageFile->keep(); |
| 120 | } |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | void CodeGenCoverage::reset() { RuleCoverage.resize(0); } |