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