1 //===- lib/Support/CodeGenCoverage.cpp -------------------------------------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file implements the CodeGenCoverage class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/CodeGenCoverage.h"
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"
29 static sys::SmartMutex<true> OutputMutex;
31 CodeGenCoverage::CodeGenCoverage() {}
33 void CodeGenCoverage::setCovered(uint64_t RuleID) {
34 if (RuleCoverage.size() <= RuleID)
35 RuleCoverage.resize(RuleID + 1, 0);
36 RuleCoverage[RuleID] = true;
39 bool CodeGenCoverage::isCovered(uint64_t RuleID) {
40 if (RuleCoverage.size() <= RuleID)
42 return RuleCoverage[RuleID];
45 bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) {
46 const char *CurPtr = Buffer.getBufferStart();
48 while (CurPtr != Buffer.getBufferEnd()) {
49 // Read the backend name from the input.
50 const char *LexedBackendName = CurPtr;
51 while (*CurPtr++ != 0)
53 if (CurPtr == Buffer.getBufferEnd())
54 return false; // Data is invalid, expected rule id's to follow.
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.
61 uint64_t RuleID = support::endian::read64(CurPtr, support::native);
64 // ~0ull terminates the rule id list.
68 // Anything else, is recorded or ignored depending on whether it's
69 // intended for the backend we're interested in.
78 bool CodeGenCoverage::emit(StringRef CoveragePrefix,
79 StringRef BackendName) const {
80 if (!CoveragePrefix.empty() && !RuleCoverage.empty()) {
81 sys::SmartScopedLock<true> Lock(OutputMutex);
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.
88 llvm::to_string(::getpid());
90 llvm::to_string(::GetCurrentProcessId());
95 std::string CoverageFilename = (CoveragePrefix + Pid).str();
98 sys::fs::OpenFlags OpenFlags = sys::fs::F_Append;
99 std::unique_ptr<ToolOutputFile> CoverageFile =
100 llvm::make_unique<ToolOutputFile>(CoverageFilename, EC, OpenFlags);
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));
112 CoverageFile->keep();
118 void CodeGenCoverage::reset() { RuleCoverage.resize(0); }