RemarkStreamer.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //===- llvm/IR/RemarkStreamer.cpp - Remark Streamer -*- C++ -------------*-===//
  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. //
  9. // This file contains the implementation of the remark outputting as part of
  10. // LLVMContext.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/IR/RemarkStreamer.h"
  14. #include "llvm/IR/DiagnosticInfo.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/GlobalValue.h"
  17. #include "llvm/Remarks/BitstreamRemarkSerializer.h"
  18. #include "llvm/Remarks/RemarkFormat.h"
  19. #include "llvm/Remarks/RemarkSerializer.h"
  20. using namespace llvm;
  21. RemarkStreamer::RemarkStreamer(
  22. std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer,
  23. Optional<StringRef> FilenameIn)
  24. : PassFilter(), RemarkSerializer(std::move(RemarkSerializer)),
  25. Filename(FilenameIn ? Optional<std::string>(FilenameIn->str()) : None) {}
  26. Error RemarkStreamer::setFilter(StringRef Filter) {
  27. Regex R = Regex(Filter);
  28. std::string RegexError;
  29. if (!R.isValid(RegexError))
  30. return createStringError(std::make_error_code(std::errc::invalid_argument),
  31. RegexError.data());
  32. PassFilter = std::move(R);
  33. return Error::success();
  34. }
  35. /// DiagnosticKind -> remarks::Type
  36. static remarks::Type toRemarkType(enum DiagnosticKind Kind) {
  37. switch (Kind) {
  38. default:
  39. return remarks::Type::Unknown;
  40. case DK_OptimizationRemark:
  41. case DK_MachineOptimizationRemark:
  42. return remarks::Type::Passed;
  43. case DK_OptimizationRemarkMissed:
  44. case DK_MachineOptimizationRemarkMissed:
  45. return remarks::Type::Missed;
  46. case DK_OptimizationRemarkAnalysis:
  47. case DK_MachineOptimizationRemarkAnalysis:
  48. return remarks::Type::Analysis;
  49. case DK_OptimizationRemarkAnalysisFPCommute:
  50. return remarks::Type::AnalysisFPCommute;
  51. case DK_OptimizationRemarkAnalysisAliasing:
  52. return remarks::Type::AnalysisAliasing;
  53. case DK_OptimizationFailure:
  54. return remarks::Type::Failure;
  55. }
  56. }
  57. /// DiagnosticLocation -> remarks::RemarkLocation.
  58. static Optional<remarks::RemarkLocation>
  59. toRemarkLocation(const DiagnosticLocation &DL) {
  60. if (!DL.isValid())
  61. return None;
  62. StringRef File = DL.getRelativePath();
  63. unsigned Line = DL.getLine();
  64. unsigned Col = DL.getColumn();
  65. return remarks::RemarkLocation{File, Line, Col};
  66. }
  67. /// LLVM Diagnostic -> Remark
  68. remarks::Remark
  69. RemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase &Diag) {
  70. remarks::Remark R; // The result.
  71. R.RemarkType = toRemarkType(static_cast<DiagnosticKind>(Diag.getKind()));
  72. R.PassName = Diag.getPassName();
  73. R.RemarkName = Diag.getRemarkName();
  74. R.FunctionName =
  75. GlobalValue::dropLLVMManglingEscape(Diag.getFunction().getName());
  76. R.Loc = toRemarkLocation(Diag.getLocation());
  77. R.Hotness = Diag.getHotness();
  78. for (const DiagnosticInfoOptimizationBase::Argument &Arg : Diag.getArgs()) {
  79. R.Args.emplace_back();
  80. R.Args.back().Key = Arg.Key;
  81. R.Args.back().Val = Arg.Val;
  82. R.Args.back().Loc = toRemarkLocation(Arg.Loc);
  83. }
  84. return R;
  85. }
  86. void RemarkStreamer::emit(const DiagnosticInfoOptimizationBase &Diag) {
  87. if (Optional<Regex> &Filter = PassFilter)
  88. if (!Filter->match(Diag.getPassName()))
  89. return;
  90. // First, convert the diagnostic to a remark.
  91. remarks::Remark R = toRemark(Diag);
  92. // Then, emit the remark through the serializer.
  93. RemarkSerializer->emit(R);
  94. }
  95. char RemarkSetupFileError::ID = 0;
  96. char RemarkSetupPatternError::ID = 0;
  97. char RemarkSetupFormatError::ID = 0;
  98. Expected<std::unique_ptr<ToolOutputFile>>
  99. llvm::setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
  100. StringRef RemarksPasses, StringRef RemarksFormat,
  101. bool RemarksWithHotness,
  102. unsigned RemarksHotnessThreshold) {
  103. if (RemarksWithHotness)
  104. Context.setDiagnosticsHotnessRequested(true);
  105. if (RemarksHotnessThreshold)
  106. Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
  107. if (RemarksFilename.empty())
  108. return nullptr;
  109. std::error_code EC;
  110. auto RemarksFile =
  111. std::make_unique<ToolOutputFile>(RemarksFilename, EC, sys::fs::OF_None);
  112. // We don't use llvm::FileError here because some diagnostics want the file
  113. // name separately.
  114. if (EC)
  115. return make_error<RemarkSetupFileError>(errorCodeToError(EC));
  116. Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);
  117. if (Error E = Format.takeError())
  118. return make_error<RemarkSetupFormatError>(std::move(E));
  119. Expected<std::unique_ptr<remarks::RemarkSerializer>> RemarkSerializer =
  120. remarks::createRemarkSerializer(
  121. *Format, remarks::SerializerMode::Separate, RemarksFile->os());
  122. if (Error E = RemarkSerializer.takeError())
  123. return make_error<RemarkSetupFormatError>(std::move(E));
  124. Context.setRemarkStreamer(std::make_unique<RemarkStreamer>(
  125. std::move(*RemarkSerializer), RemarksFilename));
  126. if (!RemarksPasses.empty())
  127. if (Error E = Context.getRemarkStreamer()->setFilter(RemarksPasses))
  128. return make_error<RemarkSetupPatternError>(std::move(E));
  129. return std::move(RemarksFile);
  130. }
  131. Error llvm::setupOptimizationRemarks(LLVMContext &Context, raw_ostream &OS,
  132. StringRef RemarksPasses,
  133. StringRef RemarksFormat,
  134. bool RemarksWithHotness,
  135. unsigned RemarksHotnessThreshold) {
  136. if (RemarksWithHotness)
  137. Context.setDiagnosticsHotnessRequested(true);
  138. if (RemarksHotnessThreshold)
  139. Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
  140. Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);
  141. if (Error E = Format.takeError())
  142. return make_error<RemarkSetupFormatError>(std::move(E));
  143. Expected<std::unique_ptr<remarks::RemarkSerializer>> RemarkSerializer =
  144. remarks::createRemarkSerializer(*Format,
  145. remarks::SerializerMode::Separate, OS);
  146. if (Error E = RemarkSerializer.takeError())
  147. return make_error<RemarkSetupFormatError>(std::move(E));
  148. Context.setRemarkStreamer(
  149. std::make_unique<RemarkStreamer>(std::move(*RemarkSerializer)));
  150. if (!RemarksPasses.empty())
  151. if (Error E = Context.getRemarkStreamer()->setFilter(RemarksPasses))
  152. return make_error<RemarkSetupPatternError>(std::move(E));
  153. return Error::success();
  154. }