xref: /freebsd-src/contrib/llvm-project/llvm/lib/Support/CodeGenCoverage.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- lib/Support/CodeGenCoverage.cpp -------------------------------------==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric /// \file
90b57cec5SDimitry Andric /// This file implements the CodeGenCoverage class.
100b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "llvm/Support/CodeGenCoverage.h"
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
150b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
160b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
170b57cec5SDimitry Andric #include "llvm/Support/Mutex.h"
185ffd83dbSDimitry Andric #include "llvm/Support/Process.h"
190b57cec5SDimitry Andric #include "llvm/Support/ScopedPrinter.h"
200b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric 
2481ad6265SDimitry Andric CodeGenCoverage::CodeGenCoverage() = default;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric void CodeGenCoverage::setCovered(uint64_t RuleID) {
270b57cec5SDimitry Andric   if (RuleCoverage.size() <= RuleID)
2804eeddc0SDimitry Andric     RuleCoverage.resize(RuleID + 1, false);
290b57cec5SDimitry Andric   RuleCoverage[RuleID] = true;
300b57cec5SDimitry Andric }
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric bool CodeGenCoverage::isCovered(uint64_t RuleID) const {
330b57cec5SDimitry Andric   if (RuleCoverage.size() <= RuleID)
340b57cec5SDimitry Andric     return false;
350b57cec5SDimitry Andric   return RuleCoverage[RuleID];
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric iterator_range<CodeGenCoverage::const_covered_iterator>
390b57cec5SDimitry Andric CodeGenCoverage::covered() const {
400b57cec5SDimitry Andric   return RuleCoverage.set_bits();
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) {
440b57cec5SDimitry Andric   const char *CurPtr = Buffer.getBufferStart();
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   while (CurPtr != Buffer.getBufferEnd()) {
470b57cec5SDimitry Andric     // Read the backend name from the input.
480b57cec5SDimitry Andric     const char *LexedBackendName = CurPtr;
490b57cec5SDimitry Andric     while (*CurPtr++ != 0)
500b57cec5SDimitry Andric       ;
510b57cec5SDimitry Andric     if (CurPtr == Buffer.getBufferEnd())
520b57cec5SDimitry Andric       return false; // Data is invalid, expected rule id's to follow.
530b57cec5SDimitry Andric 
54*0fca6ea1SDimitry Andric     bool IsForThisBackend = BackendName == LexedBackendName;
550b57cec5SDimitry Andric     while (CurPtr != Buffer.getBufferEnd()) {
560b57cec5SDimitry Andric       if (std::distance(CurPtr, Buffer.getBufferEnd()) < 8)
570b57cec5SDimitry Andric         return false; // Data is invalid. Not enough bytes for another rule id.
580b57cec5SDimitry Andric 
595f757f3fSDimitry Andric       uint64_t RuleID =
605f757f3fSDimitry Andric           support::endian::read64(CurPtr, llvm::endianness::native);
610b57cec5SDimitry Andric       CurPtr += 8;
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric       // ~0ull terminates the rule id list.
640b57cec5SDimitry Andric       if (RuleID == ~0ull)
650b57cec5SDimitry Andric         break;
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric       // Anything else, is recorded or ignored depending on whether it's
680b57cec5SDimitry Andric       // intended for the backend we're interested in.
690b57cec5SDimitry Andric       if (IsForThisBackend)
700b57cec5SDimitry Andric         setCovered(RuleID);
710b57cec5SDimitry Andric     }
720b57cec5SDimitry Andric   }
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   return true;
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric bool CodeGenCoverage::emit(StringRef CoveragePrefix,
780b57cec5SDimitry Andric                            StringRef BackendName) const {
790b57cec5SDimitry Andric   if (!CoveragePrefix.empty() && !RuleCoverage.empty()) {
80*0fca6ea1SDimitry Andric     static sys::SmartMutex<true> OutputMutex;
810b57cec5SDimitry Andric     sys::SmartScopedLock<true> Lock(OutputMutex);
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric     // We can handle locking within a process easily enough but we don't want to
840b57cec5SDimitry Andric     // manage it between multiple processes. Use the process ID to ensure no
850b57cec5SDimitry Andric     // more than one process is ever writing to the same file at the same time.
865ffd83dbSDimitry Andric     std::string Pid = llvm::to_string(sys::Process::getProcessId());
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric     std::string CoverageFilename = (CoveragePrefix + Pid).str();
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric     std::error_code EC;
918bcb0991SDimitry Andric     sys::fs::OpenFlags OpenFlags = sys::fs::OF_Append;
920b57cec5SDimitry Andric     std::unique_ptr<ToolOutputFile> CoverageFile =
938bcb0991SDimitry Andric         std::make_unique<ToolOutputFile>(CoverageFilename, EC, OpenFlags);
940b57cec5SDimitry Andric     if (EC)
950b57cec5SDimitry Andric       return false;
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric     uint64_t Zero = 0;
980b57cec5SDimitry Andric     uint64_t InvZero = ~0ull;
990b57cec5SDimitry Andric     CoverageFile->os() << BackendName;
1000b57cec5SDimitry Andric     CoverageFile->os().write((const char *)&Zero, sizeof(unsigned char));
1010b57cec5SDimitry Andric     for (uint64_t I : RuleCoverage.set_bits())
1020b57cec5SDimitry Andric       CoverageFile->os().write((const char *)&I, sizeof(uint64_t));
1030b57cec5SDimitry Andric     CoverageFile->os().write((const char *)&InvZero, sizeof(uint64_t));
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric     CoverageFile->keep();
1060b57cec5SDimitry Andric   }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   return true;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric void CodeGenCoverage::reset() { RuleCoverage.resize(0); }
112