CodeGenCoverage.cpp 3.6 KB

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