xref: /src/contrib/llvm-project/llvm/lib/Support/CodeGenCoverage.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1044eb2f6SDimitry Andric //===- lib/Support/CodeGenCoverage.cpp -------------------------------------==//
2044eb2f6SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6044eb2f6SDimitry Andric //
7044eb2f6SDimitry Andric //===----------------------------------------------------------------------===//
8044eb2f6SDimitry Andric /// \file
9044eb2f6SDimitry Andric /// This file implements the CodeGenCoverage class.
10044eb2f6SDimitry Andric //===----------------------------------------------------------------------===//
11044eb2f6SDimitry Andric 
12044eb2f6SDimitry Andric #include "llvm/Support/CodeGenCoverage.h"
13044eb2f6SDimitry Andric 
14044eb2f6SDimitry Andric #include "llvm/Support/Endian.h"
15044eb2f6SDimitry Andric #include "llvm/Support/FileSystem.h"
16044eb2f6SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
17044eb2f6SDimitry Andric #include "llvm/Support/Mutex.h"
18cfca06d7SDimitry Andric #include "llvm/Support/Process.h"
19044eb2f6SDimitry Andric #include "llvm/Support/ScopedPrinter.h"
20044eb2f6SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
21044eb2f6SDimitry Andric 
22044eb2f6SDimitry Andric using namespace llvm;
23044eb2f6SDimitry Andric 
24145449b1SDimitry Andric CodeGenCoverage::CodeGenCoverage() = default;
25044eb2f6SDimitry Andric 
setCovered(uint64_t RuleID)26044eb2f6SDimitry Andric void CodeGenCoverage::setCovered(uint64_t RuleID) {
27044eb2f6SDimitry Andric   if (RuleCoverage.size() <= RuleID)
286f8fc217SDimitry Andric     RuleCoverage.resize(RuleID + 1, false);
29044eb2f6SDimitry Andric   RuleCoverage[RuleID] = true;
30044eb2f6SDimitry Andric }
31044eb2f6SDimitry Andric 
isCovered(uint64_t RuleID) const32eb11fae6SDimitry Andric bool CodeGenCoverage::isCovered(uint64_t RuleID) const {
33044eb2f6SDimitry Andric   if (RuleCoverage.size() <= RuleID)
34044eb2f6SDimitry Andric     return false;
35044eb2f6SDimitry Andric   return RuleCoverage[RuleID];
36044eb2f6SDimitry Andric }
37044eb2f6SDimitry Andric 
38eb11fae6SDimitry Andric iterator_range<CodeGenCoverage::const_covered_iterator>
covered() const39eb11fae6SDimitry Andric CodeGenCoverage::covered() const {
40eb11fae6SDimitry Andric   return RuleCoverage.set_bits();
41eb11fae6SDimitry Andric }
42eb11fae6SDimitry Andric 
parse(MemoryBuffer & Buffer,StringRef BackendName)43044eb2f6SDimitry Andric bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) {
44044eb2f6SDimitry Andric   const char *CurPtr = Buffer.getBufferStart();
45044eb2f6SDimitry Andric 
46044eb2f6SDimitry Andric   while (CurPtr != Buffer.getBufferEnd()) {
47044eb2f6SDimitry Andric     // Read the backend name from the input.
48044eb2f6SDimitry Andric     const char *LexedBackendName = CurPtr;
49044eb2f6SDimitry Andric     while (*CurPtr++ != 0)
50044eb2f6SDimitry Andric       ;
51044eb2f6SDimitry Andric     if (CurPtr == Buffer.getBufferEnd())
52044eb2f6SDimitry Andric       return false; // Data is invalid, expected rule id's to follow.
53044eb2f6SDimitry Andric 
54ac9a064cSDimitry Andric     bool IsForThisBackend = BackendName == LexedBackendName;
55044eb2f6SDimitry Andric     while (CurPtr != Buffer.getBufferEnd()) {
56044eb2f6SDimitry Andric       if (std::distance(CurPtr, Buffer.getBufferEnd()) < 8)
57044eb2f6SDimitry Andric         return false; // Data is invalid. Not enough bytes for another rule id.
58044eb2f6SDimitry Andric 
59b1c73532SDimitry Andric       uint64_t RuleID =
60b1c73532SDimitry Andric           support::endian::read64(CurPtr, llvm::endianness::native);
61044eb2f6SDimitry Andric       CurPtr += 8;
62044eb2f6SDimitry Andric 
63044eb2f6SDimitry Andric       // ~0ull terminates the rule id list.
64044eb2f6SDimitry Andric       if (RuleID == ~0ull)
65044eb2f6SDimitry Andric         break;
66044eb2f6SDimitry Andric 
67044eb2f6SDimitry Andric       // Anything else, is recorded or ignored depending on whether it's
68044eb2f6SDimitry Andric       // intended for the backend we're interested in.
69044eb2f6SDimitry Andric       if (IsForThisBackend)
70044eb2f6SDimitry Andric         setCovered(RuleID);
71044eb2f6SDimitry Andric     }
72044eb2f6SDimitry Andric   }
73044eb2f6SDimitry Andric 
74044eb2f6SDimitry Andric   return true;
75044eb2f6SDimitry Andric }
76044eb2f6SDimitry Andric 
emit(StringRef CoveragePrefix,StringRef BackendName) const77044eb2f6SDimitry Andric bool CodeGenCoverage::emit(StringRef CoveragePrefix,
78044eb2f6SDimitry Andric                            StringRef BackendName) const {
79044eb2f6SDimitry Andric   if (!CoveragePrefix.empty() && !RuleCoverage.empty()) {
80ac9a064cSDimitry Andric     static sys::SmartMutex<true> OutputMutex;
81044eb2f6SDimitry Andric     sys::SmartScopedLock<true> Lock(OutputMutex);
82044eb2f6SDimitry Andric 
83044eb2f6SDimitry Andric     // We can handle locking within a process easily enough but we don't want to
84044eb2f6SDimitry Andric     // manage it between multiple processes. Use the process ID to ensure no
85044eb2f6SDimitry Andric     // more than one process is ever writing to the same file at the same time.
86cfca06d7SDimitry Andric     std::string Pid = llvm::to_string(sys::Process::getProcessId());
87044eb2f6SDimitry Andric 
88044eb2f6SDimitry Andric     std::string CoverageFilename = (CoveragePrefix + Pid).str();
89044eb2f6SDimitry Andric 
90044eb2f6SDimitry Andric     std::error_code EC;
911d5ae102SDimitry Andric     sys::fs::OpenFlags OpenFlags = sys::fs::OF_Append;
92044eb2f6SDimitry Andric     std::unique_ptr<ToolOutputFile> CoverageFile =
931d5ae102SDimitry Andric         std::make_unique<ToolOutputFile>(CoverageFilename, EC, OpenFlags);
94044eb2f6SDimitry Andric     if (EC)
95044eb2f6SDimitry Andric       return false;
96044eb2f6SDimitry Andric 
97044eb2f6SDimitry Andric     uint64_t Zero = 0;
98044eb2f6SDimitry Andric     uint64_t InvZero = ~0ull;
99044eb2f6SDimitry Andric     CoverageFile->os() << BackendName;
100044eb2f6SDimitry Andric     CoverageFile->os().write((const char *)&Zero, sizeof(unsigned char));
101044eb2f6SDimitry Andric     for (uint64_t I : RuleCoverage.set_bits())
102044eb2f6SDimitry Andric       CoverageFile->os().write((const char *)&I, sizeof(uint64_t));
103044eb2f6SDimitry Andric     CoverageFile->os().write((const char *)&InvZero, sizeof(uint64_t));
104044eb2f6SDimitry Andric 
105044eb2f6SDimitry Andric     CoverageFile->keep();
106044eb2f6SDimitry Andric   }
107044eb2f6SDimitry Andric 
108044eb2f6SDimitry Andric   return true;
109044eb2f6SDimitry Andric }
110044eb2f6SDimitry Andric 
reset()111044eb2f6SDimitry Andric void CodeGenCoverage::reset() { RuleCoverage.resize(0); }
112