CodeGenCoverage.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "llvm/Support/CodeGenCoverage.h"
  12. #include "llvm/Config/llvm-config.h"
  13. #include "llvm/Support/Endian.h"
  14. #include "llvm/Support/FileSystem.h"
  15. #include "llvm/Support/MemoryBuffer.h"
  16. #include "llvm/Support/Mutex.h"
  17. #include "llvm/Support/ScopedPrinter.h"
  18. #include "llvm/Support/ToolOutputFile.h"
  19. #if LLVM_ON_UNIX
  20. #include <unistd.h>
  21. #elif defined(_WIN32)
  22. #include <windows.h>
  23. #endif
  24. using namespace llvm;
  25. static sys::SmartMutex<true> OutputMutex;
  26. CodeGenCoverage::CodeGenCoverage() {}
  27. void CodeGenCoverage::setCovered(uint64_t RuleID) {
  28. if (RuleCoverage.size() <= RuleID)
  29. RuleCoverage.resize(RuleID + 1, 0);
  30. RuleCoverage[RuleID] = true;
  31. }
  32. bool CodeGenCoverage::isCovered(uint64_t RuleID) const {
  33. if (RuleCoverage.size() <= RuleID)
  34. return false;
  35. return RuleCoverage[RuleID];
  36. }
  37. iterator_range<CodeGenCoverage::const_covered_iterator>
  38. CodeGenCoverage::covered() const {
  39. return RuleCoverage.set_bits();
  40. }
  41. bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) {
  42. const char *CurPtr = Buffer.getBufferStart();
  43. while (CurPtr != Buffer.getBufferEnd()) {
  44. // Read the backend name from the input.
  45. const char *LexedBackendName = CurPtr;
  46. while (*CurPtr++ != 0)
  47. ;
  48. if (CurPtr == Buffer.getBufferEnd())
  49. return false; // Data is invalid, expected rule id's to follow.
  50. bool IsForThisBackend = BackendName.equals(LexedBackendName);
  51. while (CurPtr != Buffer.getBufferEnd()) {
  52. if (std::distance(CurPtr, Buffer.getBufferEnd()) < 8)
  53. return false; // Data is invalid. Not enough bytes for another rule id.
  54. uint64_t RuleID = support::endian::read64(CurPtr, support::native);
  55. CurPtr += 8;
  56. // ~0ull terminates the rule id list.
  57. if (RuleID == ~0ull)
  58. break;
  59. // Anything else, is recorded or ignored depending on whether it's
  60. // intended for the backend we're interested in.
  61. if (IsForThisBackend)
  62. setCovered(RuleID);
  63. }
  64. }
  65. return true;
  66. }
  67. bool CodeGenCoverage::emit(StringRef CoveragePrefix,
  68. StringRef BackendName) const {
  69. if (!CoveragePrefix.empty() && !RuleCoverage.empty()) {
  70. sys::SmartScopedLock<true> Lock(OutputMutex);
  71. // We can handle locking within a process easily enough but we don't want to
  72. // manage it between multiple processes. Use the process ID to ensure no
  73. // more than one process is ever writing to the same file at the same time.
  74. std::string Pid =
  75. #if LLVM_ON_UNIX
  76. llvm::to_string(::getpid());
  77. #elif defined(_WIN32)
  78. llvm::to_string(::GetCurrentProcessId());
  79. #else
  80. "";
  81. #endif
  82. std::string CoverageFilename = (CoveragePrefix + Pid).str();
  83. std::error_code EC;
  84. sys::fs::OpenFlags OpenFlags = sys::fs::OF_Append;
  85. std::unique_ptr<ToolOutputFile> CoverageFile =
  86. llvm::make_unique<ToolOutputFile>(CoverageFilename, EC, OpenFlags);
  87. if (EC)
  88. return false;
  89. uint64_t Zero = 0;
  90. uint64_t InvZero = ~0ull;
  91. CoverageFile->os() << BackendName;
  92. CoverageFile->os().write((const char *)&Zero, sizeof(unsigned char));
  93. for (uint64_t I : RuleCoverage.set_bits())
  94. CoverageFile->os().write((const char *)&I, sizeof(uint64_t));
  95. CoverageFile->os().write((const char *)&InvZero, sizeof(uint64_t));
  96. CoverageFile->keep();
  97. }
  98. return true;
  99. }
  100. void CodeGenCoverage::reset() { RuleCoverage.resize(0); }