LLVM  14.0.0git
CodeGenCoverage.cpp
Go to the documentation of this file.
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 
13 
14 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/Mutex.h"
18 #include "llvm/Support/Process.h"
21 
22 using namespace llvm;
23 
25 
27 
29  if (RuleCoverage.size() <= RuleID)
30  RuleCoverage.resize(RuleID + 1, 0);
31  RuleCoverage[RuleID] = true;
32 }
33 
35  if (RuleCoverage.size() <= RuleID)
36  return false;
37  return RuleCoverage[RuleID];
38 }
39 
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(LexedBackendName);
57  while (CurPtr != Buffer.getBufferEnd()) {
58  if (std::distance(CurPtr, Buffer.getBufferEnd()) < 8)
59  return false; // Data is invalid. Not enough bytes for another rule id.
60 
62  CurPtr += 8;
63 
64  // ~0ull terminates the rule id list.
65  if (RuleID == ~0ull)
66  break;
67 
68  // Anything else, is recorded or ignored depending on whether it's
69  // intended for the backend we're interested in.
70  if (IsForThisBackend)
71  setCovered(RuleID);
72  }
73  }
74 
75  return true;
76 }
77 
79  StringRef BackendName) const {
80  if (!CoveragePrefix.empty() && !RuleCoverage.empty()) {
82 
83  // We can handle locking within a process easily enough but we don't want to
84  // manage it between multiple processes. Use the process ID to ensure no
85  // more than one process is ever writing to the same file at the same time.
86  std::string Pid = llvm::to_string(sys::Process::getProcessId());
87 
88  std::string CoverageFilename = (CoveragePrefix + Pid).str();
89 
90  std::error_code EC;
92  std::unique_ptr<ToolOutputFile> CoverageFile =
93  std::make_unique<ToolOutputFile>(CoverageFilename, EC, OpenFlags);
94  if (EC)
95  return false;
96 
97  uint64_t Zero = 0;
98  uint64_t InvZero = ~0ull;
99  CoverageFile->os() << BackendName;
100  CoverageFile->os().write((const char *)&Zero, sizeof(unsigned char));
101  for (uint64_t I : RuleCoverage.set_bits())
102  CoverageFile->os().write((const char *)&I, sizeof(uint64_t));
103  CoverageFile->os().write((const char *)&InvZero, sizeof(uint64_t));
104 
105  CoverageFile->keep();
106  }
107 
108  return true;
109 }
110 
MemoryBuffer.h
llvm::CodeGenCoverage::CodeGenCoverage
CodeGenCoverage()
Definition: CodeGenCoverage.cpp:26
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
FileSystem.h
llvm::CodeGenCoverage::isCovered
bool isCovered(uint64_t RuleID) const
Definition: CodeGenCoverage.cpp:34
llvm::CodeGenCoverage::parse
bool parse(MemoryBuffer &Buffer, StringRef BackendName)
Definition: CodeGenCoverage.cpp:45
llvm::BitVector::set_bits
iterator_range< const_set_bits_iterator > set_bits() const
Definition: BitVector.h:132
llvm::BitVector::resize
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:333
llvm::CodeGenCoverage::setCovered
void setCovered(uint64_t RuleID)
Definition: CodeGenCoverage.cpp:28
llvm::CodeGenCoverage::covered
iterator_range< const_covered_iterator > covered() const
Definition: CodeGenCoverage.cpp:41
llvm::CodeGenCoverage::emit
bool emit(StringRef FilePrefix, StringRef BackendName) const
Definition: CodeGenCoverage.cpp:78
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
llvm::Lock
static sys::Mutex Lock
Definition: NVPTXUtilities.cpp:39
Process.h
OutputMutex
static sys::SmartMutex< true > OutputMutex
Definition: CodeGenCoverage.cpp:24
llvm::MemoryBuffer::getBufferEnd
const char * getBufferEnd() const
Definition: MemoryBuffer.h:66
llvm::BitVector::size
size_type size() const
size - Returns the number of bits in this bitvector.
Definition: BitVector.h:151
llvm::sys::SmartMutex
SmartMutex - A mutex with a compile time constant parameter that indicates whether this mutex should ...
Definition: Mutex.h:28
llvm::StringRef::equals
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:187
llvm::BitVector::empty
bool empty() const
empty - Tests whether there are no bits in this bitvector.
Definition: BitVector.h:148
uint64_t
CoveragePrefix
static const std::string CoveragePrefix
Definition: InstructionSelect.cpp:48
I
#define I(x, y, z)
Definition: MD5.cpp:59
CodeGenCoverage.h
llvm::sys::SmartScopedLock
std::lock_guard< SmartMutex< mt_only > > SmartScopedLock
Definition: Mutex.h:71
ToolOutputFile.h
llvm::CodeGenCoverage::reset
void reset()
Definition: CodeGenCoverage.cpp:111
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
Mutex.h
llvm::CodeGenCoverage::RuleCoverage
BitVector RuleCoverage
Definition: CodeGenCoverage.h:22
llvm::MemoryBuffer::getBufferStart
const char * getBufferStart() const
Definition: MemoryBuffer.h:65
llvm::sys::fs::OpenFlags
OpenFlags
Definition: FileSystem.h:756
llvm::support::endian::read64
uint64_t read64(const void *P, endianness E)
Definition: Endian.h:366
llvm::sys::Process::getProcessId
static Pid getProcessId()
Get the process's identifier.
llvm::sys::fs::OF_Append
@ OF_Append
The file should be opened in append mode.
Definition: FileSystem.h:773
llvm::to_string
std::string to_string(const T &Value)
Definition: ScopedPrinter.h:63
llvm::support::native
@ native
Definition: Endian.h:27
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
Endian.h
ScopedPrinter.h