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