LLVMRemarkStreamer.cpp 6.3 KB

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