1 //===- lib/Support/CodeGenCoverage.cpp -------------------------------------==//
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
7 //===----------------------------------------------------------------------===//
9 /// This file implements the CodeGenCoverage class.
10 //===----------------------------------------------------------------------===//
12 #include "llvm/Support/CodeGenCoverage.h"
14 #include "llvm/Config/llvm-config.h"
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"
30 static sys::SmartMutex
<true> OutputMutex
;
32 CodeGenCoverage::CodeGenCoverage() {}
34 void CodeGenCoverage::setCovered(uint64_t RuleID
) {
35 if (RuleCoverage
.size() <= RuleID
)
36 RuleCoverage
.resize(RuleID
+ 1, 0);
37 RuleCoverage
[RuleID
] = true;
40 bool CodeGenCoverage::isCovered(uint64_t RuleID
) const {
41 if (RuleCoverage
.size() <= RuleID
)
43 return RuleCoverage
[RuleID
];
46 iterator_range
<CodeGenCoverage::const_covered_iterator
>
47 CodeGenCoverage::covered() const {
48 return RuleCoverage
.set_bits();
51 bool CodeGenCoverage::parse(MemoryBuffer
&Buffer
, StringRef BackendName
) {
52 const char *CurPtr
= Buffer
.getBufferStart();
54 while (CurPtr
!= Buffer
.getBufferEnd()) {
55 // Read the backend name from the input.
56 const char *LexedBackendName
= CurPtr
;
57 while (*CurPtr
++ != 0)
59 if (CurPtr
== Buffer
.getBufferEnd())
60 return false; // Data is invalid, expected rule id's to follow.
62 bool IsForThisBackend
= BackendName
.equals(LexedBackendName
);
63 while (CurPtr
!= Buffer
.getBufferEnd()) {
64 if (std::distance(CurPtr
, Buffer
.getBufferEnd()) < 8)
65 return false; // Data is invalid. Not enough bytes for another rule id.
67 uint64_t RuleID
= support::endian::read64(CurPtr
, support::native
);
70 // ~0ull terminates the rule id list.
74 // Anything else, is recorded or ignored depending on whether it's
75 // intended for the backend we're interested in.
84 bool CodeGenCoverage::emit(StringRef CoveragePrefix
,
85 StringRef BackendName
) const {
86 if (!CoveragePrefix
.empty() && !RuleCoverage
.empty()) {
87 sys::SmartScopedLock
<true> Lock(OutputMutex
);
89 // We can handle locking within a process easily enough but we don't want to
90 // manage it between multiple processes. Use the process ID to ensure no
91 // more than one process is ever writing to the same file at the same time.
94 llvm::to_string(::getpid());
96 llvm::to_string(::GetCurrentProcessId());
101 std::string CoverageFilename
= (CoveragePrefix
+ Pid
).str();
104 sys::fs::OpenFlags OpenFlags
= sys::fs::OF_Append
;
105 std::unique_ptr
<ToolOutputFile
> CoverageFile
=
106 std::make_unique
<ToolOutputFile
>(CoverageFilename
, EC
, OpenFlags
);
111 uint64_t InvZero
= ~0ull;
112 CoverageFile
->os() << BackendName
;
113 CoverageFile
->os().write((const char *)&Zero
, sizeof(unsigned char));
114 for (uint64_t I
: RuleCoverage
.set_bits())
115 CoverageFile
->os().write((const char *)&I
, sizeof(uint64_t));
116 CoverageFile
->os().write((const char *)&InvZero
, sizeof(uint64_t));
118 CoverageFile
->keep();
124 void CodeGenCoverage::reset() { RuleCoverage
.resize(0); }