1*0b57cec5SDimitry Andric //===- lib/Support/CodeGenCoverage.cpp -------------------------------------==//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric /// \file
9*0b57cec5SDimitry Andric /// This file implements the CodeGenCoverage class.
10*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
11*0b57cec5SDimitry Andric 
12*0b57cec5SDimitry Andric #include "llvm/Support/CodeGenCoverage.h"
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
15*0b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
16*0b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
17*0b57cec5SDimitry Andric #include "llvm/Support/Mutex.h"
18*0b57cec5SDimitry Andric #include "llvm/Support/Process.h"
19*0b57cec5SDimitry Andric #include "llvm/Support/ScopedPrinter.h"
20*0b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
21*0b57cec5SDimitry Andric 
22*0b57cec5SDimitry Andric using namespace llvm;
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric static sys::SmartMutex<true> OutputMutex;
25*0b57cec5SDimitry Andric 
CodeGenCoverage()26*0b57cec5SDimitry Andric CodeGenCoverage::CodeGenCoverage() {}
27*0b57cec5SDimitry Andric 
setCovered(uint64_t RuleID)28*0b57cec5SDimitry Andric void CodeGenCoverage::setCovered(uint64_t RuleID) {
29*0b57cec5SDimitry Andric   if (RuleCoverage.size() <= RuleID)
30*0b57cec5SDimitry Andric     RuleCoverage.resize(RuleID + 1, 0);
31*0b57cec5SDimitry Andric   RuleCoverage[RuleID] = true;
32*0b57cec5SDimitry Andric }
33*0b57cec5SDimitry Andric 
isCovered(uint64_t RuleID) const34*0b57cec5SDimitry Andric bool CodeGenCoverage::isCovered(uint64_t RuleID) const {
35*0b57cec5SDimitry Andric   if (RuleCoverage.size() <= RuleID)
36*0b57cec5SDimitry Andric     return false;
37*0b57cec5SDimitry Andric   return RuleCoverage[RuleID];
38*0b57cec5SDimitry Andric }
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric iterator_range<CodeGenCoverage::const_covered_iterator>
covered() const41*0b57cec5SDimitry Andric CodeGenCoverage::covered() const {
42*0b57cec5SDimitry Andric   return RuleCoverage.set_bits();
43*0b57cec5SDimitry Andric }
44*0b57cec5SDimitry Andric 
parse(MemoryBuffer & Buffer,StringRef BackendName)45*0b57cec5SDimitry Andric bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) {
46*0b57cec5SDimitry Andric   const char *CurPtr = Buffer.getBufferStart();
47*0b57cec5SDimitry Andric 
48*0b57cec5SDimitry Andric   while (CurPtr != Buffer.getBufferEnd()) {
49*0b57cec5SDimitry Andric     // Read the backend name from the input.
50*0b57cec5SDimitry Andric     const char *LexedBackendName = CurPtr;
51*0b57cec5SDimitry Andric     while (*CurPtr++ != 0)
52*0b57cec5SDimitry Andric       ;
53*0b57cec5SDimitry Andric     if (CurPtr == Buffer.getBufferEnd())
54*0b57cec5SDimitry Andric       return false; // Data is invalid, expected rule id's to follow.
55*0b57cec5SDimitry Andric 
56*0b57cec5SDimitry Andric     bool IsForThisBackend = BackendName.equals(LexedBackendName);
57*0b57cec5SDimitry Andric     while (CurPtr != Buffer.getBufferEnd()) {
58*0b57cec5SDimitry Andric       if (std::distance(CurPtr, Buffer.getBufferEnd()) < 8)
59*0b57cec5SDimitry Andric         return false; // Data is invalid. Not enough bytes for another rule id.
60*0b57cec5SDimitry Andric 
61*0b57cec5SDimitry Andric       uint64_t RuleID = support::endian::read64(CurPtr, support::native);
62*0b57cec5SDimitry Andric       CurPtr += 8;
63*0b57cec5SDimitry Andric 
64*0b57cec5SDimitry Andric       // ~0ull terminates the rule id list.
65*0b57cec5SDimitry Andric       if (RuleID == ~0ull)
66*0b57cec5SDimitry Andric         break;
67*0b57cec5SDimitry Andric 
68*0b57cec5SDimitry Andric       // Anything else, is recorded or ignored depending on whether it's
69*0b57cec5SDimitry Andric       // intended for the backend we're interested in.
70*0b57cec5SDimitry Andric       if (IsForThisBackend)
71*0b57cec5SDimitry Andric         setCovered(RuleID);
72*0b57cec5SDimitry Andric     }
73*0b57cec5SDimitry Andric   }
74*0b57cec5SDimitry Andric 
75*0b57cec5SDimitry Andric   return true;
76*0b57cec5SDimitry Andric }
77*0b57cec5SDimitry Andric 
emit(StringRef CoveragePrefix,StringRef BackendName) const78*0b57cec5SDimitry Andric bool CodeGenCoverage::emit(StringRef CoveragePrefix,
79*0b57cec5SDimitry Andric                            StringRef BackendName) const {
80*0b57cec5SDimitry Andric   if (!CoveragePrefix.empty() && !RuleCoverage.empty()) {
81*0b57cec5SDimitry Andric     sys::SmartScopedLock<true> Lock(OutputMutex);
82*0b57cec5SDimitry Andric 
83*0b57cec5SDimitry Andric     // We can handle locking within a process easily enough but we don't want to
84*0b57cec5SDimitry Andric     // manage it between multiple processes. Use the process ID to ensure no
85*0b57cec5SDimitry Andric     // more than one process is ever writing to the same file at the same time.
86*0b57cec5SDimitry Andric     std::string Pid = llvm::to_string(sys::Process::getProcessId());
87*0b57cec5SDimitry Andric 
88*0b57cec5SDimitry Andric     std::string CoverageFilename = (CoveragePrefix + Pid).str();
89*0b57cec5SDimitry Andric 
90*0b57cec5SDimitry Andric     std::error_code EC;
91*0b57cec5SDimitry Andric     sys::fs::OpenFlags OpenFlags = sys::fs::OF_Append;
92*0b57cec5SDimitry Andric     std::unique_ptr<ToolOutputFile> CoverageFile =
93*0b57cec5SDimitry Andric         std::make_unique<ToolOutputFile>(CoverageFilename, EC, OpenFlags);
94*0b57cec5SDimitry Andric     if (EC)
95*0b57cec5SDimitry Andric       return false;
96*0b57cec5SDimitry Andric 
97*0b57cec5SDimitry Andric     uint64_t Zero = 0;
98*0b57cec5SDimitry Andric     uint64_t InvZero = ~0ull;
99*0b57cec5SDimitry Andric     CoverageFile->os() << BackendName;
100*0b57cec5SDimitry Andric     CoverageFile->os().write((const char *)&Zero, sizeof(unsigned char));
101*0b57cec5SDimitry Andric     for (uint64_t I : RuleCoverage.set_bits())
102*0b57cec5SDimitry Andric       CoverageFile->os().write((const char *)&I, sizeof(uint64_t));
103*0b57cec5SDimitry Andric     CoverageFile->os().write((const char *)&InvZero, sizeof(uint64_t));
104*0b57cec5SDimitry Andric 
105*0b57cec5SDimitry Andric     CoverageFile->keep();
106*0b57cec5SDimitry Andric   }
107*0b57cec5SDimitry Andric 
108*0b57cec5SDimitry Andric   return true;
109*0b57cec5SDimitry Andric }
110*0b57cec5SDimitry Andric 
reset()111*0b57cec5SDimitry Andric void CodeGenCoverage::reset() { RuleCoverage.resize(0); }
112*0b57cec5SDimitry Andric