1 | //===- lib/Support/CodeGenCoverage.cpp -------------------------------------==// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | /// \file |
9 | /// This file implements the CodeGenCoverage class. |
10 | //===----------------------------------------------------------------------===// |
11 | |
12 | #include "llvm/Support/CodeGenCoverage.h" |
13 | |
14 | #include "llvm/Support/Endian.h" |
15 | #include "llvm/Support/FileSystem.h" |
16 | #include "llvm/Support/MemoryBuffer.h" |
17 | #include "llvm/Support/Mutex.h" |
18 | #include "llvm/Support/Process.h" |
19 | #include "llvm/Support/ScopedPrinter.h" |
20 | #include "llvm/Support/ToolOutputFile.h" |
21 | |
22 | using namespace llvm; |
23 | |
24 | static sys::SmartMutex<true> OutputMutex; |
25 | |
26 | CodeGenCoverage::CodeGenCoverage() = default; |
27 | |
28 | void CodeGenCoverage::setCovered(uint64_t RuleID) { |
29 | if (RuleCoverage.size() <= RuleID) |
30 | RuleCoverage.resize(N: RuleID + 1, t: false); |
31 | RuleCoverage[RuleID] = true; |
32 | } |
33 | |
34 | bool CodeGenCoverage::isCovered(uint64_t RuleID) const { |
35 | if (RuleCoverage.size() <= RuleID) |
36 | return false; |
37 | return RuleCoverage[RuleID]; |
38 | } |
39 | |
40 | iterator_range<CodeGenCoverage::const_covered_iterator> |
41 | CodeGenCoverage::covered() const { |
42 | return RuleCoverage.set_bits(); |
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(RHS: LexedBackendName); |
57 | while (CurPtr != Buffer.getBufferEnd()) { |
58 | if (std::distance(first: CurPtr, last: Buffer.getBufferEnd()) < 8) |
59 | return false; // Data is invalid. Not enough bytes for another rule id. |
60 | |
61 | uint64_t RuleID = |
62 | support::endian::read64(P: CurPtr, E: llvm::endianness::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 = llvm::to_string(Value: sys::Process::getProcessId()); |
88 | |
89 | std::string CoverageFilename = (CoveragePrefix + Pid).str(); |
90 | |
91 | std::error_code EC; |
92 | sys::fs::OpenFlags OpenFlags = sys::fs::OF_Append; |
93 | std::unique_ptr<ToolOutputFile> CoverageFile = |
94 | std::make_unique<ToolOutputFile>(args&: CoverageFilename, args&: EC, args&: OpenFlags); |
95 | if (EC) |
96 | return false; |
97 | |
98 | uint64_t Zero = 0; |
99 | uint64_t InvZero = ~0ull; |
100 | CoverageFile->os() << BackendName; |
101 | CoverageFile->os().write(Ptr: (const char *)&Zero, Size: sizeof(unsigned char)); |
102 | for (uint64_t I : RuleCoverage.set_bits()) |
103 | CoverageFile->os().write(Ptr: (const char *)&I, Size: sizeof(uint64_t)); |
104 | CoverageFile->os().write(Ptr: (const char *)&InvZero, Size: sizeof(uint64_t)); |
105 | |
106 | CoverageFile->keep(); |
107 | } |
108 | |
109 | return true; |
110 | } |
111 | |
112 | void CodeGenCoverage::reset() { RuleCoverage.resize(N: 0); } |
113 | |