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
240b57cec5SDimitry Andric static sys::SmartMutex<true> OutputMutex;
250b57cec5SDimitry Andric
2681ad6265SDimitry Andric CodeGenCoverage::CodeGenCoverage() = default;
270b57cec5SDimitry Andric
setCovered(uint64_t RuleID)280b57cec5SDimitry Andric void CodeGenCoverage::setCovered(uint64_t RuleID) {
290b57cec5SDimitry Andric if (RuleCoverage.size() <= RuleID)
3004eeddc0SDimitry Andric RuleCoverage.resize(RuleID + 1, false);
310b57cec5SDimitry Andric RuleCoverage[RuleID] = true;
320b57cec5SDimitry Andric }
330b57cec5SDimitry Andric
isCovered(uint64_t RuleID) const340b57cec5SDimitry Andric bool CodeGenCoverage::isCovered(uint64_t RuleID) const {
350b57cec5SDimitry Andric if (RuleCoverage.size() <= RuleID)
360b57cec5SDimitry Andric return false;
370b57cec5SDimitry Andric return RuleCoverage[RuleID];
380b57cec5SDimitry Andric }
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric iterator_range<CodeGenCoverage::const_covered_iterator>
covered() const410b57cec5SDimitry Andric CodeGenCoverage::covered() const {
420b57cec5SDimitry Andric return RuleCoverage.set_bits();
430b57cec5SDimitry Andric }
440b57cec5SDimitry Andric
parse(MemoryBuffer & Buffer,StringRef BackendName)450b57cec5SDimitry Andric bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) {
460b57cec5SDimitry Andric const char *CurPtr = Buffer.getBufferStart();
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric while (CurPtr != Buffer.getBufferEnd()) {
490b57cec5SDimitry Andric // Read the backend name from the input.
500b57cec5SDimitry Andric const char *LexedBackendName = CurPtr;
510b57cec5SDimitry Andric while (*CurPtr++ != 0)
520b57cec5SDimitry Andric ;
530b57cec5SDimitry Andric if (CurPtr == Buffer.getBufferEnd())
540b57cec5SDimitry Andric return false; // Data is invalid, expected rule id's to follow.
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric bool IsForThisBackend = BackendName.equals(LexedBackendName);
570b57cec5SDimitry Andric while (CurPtr != Buffer.getBufferEnd()) {
580b57cec5SDimitry Andric if (std::distance(CurPtr, Buffer.getBufferEnd()) < 8)
590b57cec5SDimitry Andric return false; // Data is invalid. Not enough bytes for another rule id.
600b57cec5SDimitry Andric
61*c9157d92SDimitry Andric uint64_t RuleID =
62*c9157d92SDimitry Andric support::endian::read64(CurPtr, llvm::endianness::native);
630b57cec5SDimitry Andric CurPtr += 8;
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric // ~0ull terminates the rule id list.
660b57cec5SDimitry Andric if (RuleID == ~0ull)
670b57cec5SDimitry Andric break;
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric // Anything else, is recorded or ignored depending on whether it's
700b57cec5SDimitry Andric // intended for the backend we're interested in.
710b57cec5SDimitry Andric if (IsForThisBackend)
720b57cec5SDimitry Andric setCovered(RuleID);
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric return true;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric
emit(StringRef CoveragePrefix,StringRef BackendName) const790b57cec5SDimitry Andric bool CodeGenCoverage::emit(StringRef CoveragePrefix,
800b57cec5SDimitry Andric StringRef BackendName) const {
810b57cec5SDimitry Andric if (!CoveragePrefix.empty() && !RuleCoverage.empty()) {
820b57cec5SDimitry Andric sys::SmartScopedLock<true> Lock(OutputMutex);
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric // We can handle locking within a process easily enough but we don't want to
850b57cec5SDimitry Andric // manage it between multiple processes. Use the process ID to ensure no
860b57cec5SDimitry Andric // more than one process is ever writing to the same file at the same time.
875ffd83dbSDimitry Andric std::string Pid = llvm::to_string(sys::Process::getProcessId());
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric std::string CoverageFilename = (CoveragePrefix + Pid).str();
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric std::error_code EC;
928bcb0991SDimitry Andric sys::fs::OpenFlags OpenFlags = sys::fs::OF_Append;
930b57cec5SDimitry Andric std::unique_ptr<ToolOutputFile> CoverageFile =
948bcb0991SDimitry Andric std::make_unique<ToolOutputFile>(CoverageFilename, EC, OpenFlags);
950b57cec5SDimitry Andric if (EC)
960b57cec5SDimitry Andric return false;
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric uint64_t Zero = 0;
990b57cec5SDimitry Andric uint64_t InvZero = ~0ull;
1000b57cec5SDimitry Andric CoverageFile->os() << BackendName;
1010b57cec5SDimitry Andric CoverageFile->os().write((const char *)&Zero, sizeof(unsigned char));
1020b57cec5SDimitry Andric for (uint64_t I : RuleCoverage.set_bits())
1030b57cec5SDimitry Andric CoverageFile->os().write((const char *)&I, sizeof(uint64_t));
1040b57cec5SDimitry Andric CoverageFile->os().write((const char *)&InvZero, sizeof(uint64_t));
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric CoverageFile->keep();
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric return true;
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric
reset()1120b57cec5SDimitry Andric void CodeGenCoverage::reset() { RuleCoverage.resize(0); }
113