1 //===- llvm/IR/LLVMRemarkStreamer.cpp - Remark Streamer -*- C++ ---------*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains the implementation of the conversion between IR
10 // Diagnostics and serializable remarks::Remark objects.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/LLVMRemarkStreamer.h"
15 #include "llvm/IR/DiagnosticInfo.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/GlobalValue.h"
18 #include "llvm/Support/FileSystem.h"
22 /// DiagnosticKind -> remarks::Type
23 static remarks::Type toRemarkType(enum DiagnosticKind Kind) {
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;
45 /// DiagnosticLocation -> remarks::RemarkLocation.
46 static Optional<remarks::RemarkLocation>
47 toRemarkLocation(const DiagnosticLocation &DL) {
50 StringRef File = DL.getRelativePath();
51 unsigned Line = DL.getLine();
52 unsigned Col = DL.getColumn();
53 return remarks::RemarkLocation{File, Line, Col};
56 /// LLVM Diagnostic -> Remark
58 LLVMRemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase &Diag) const {
59 remarks::Remark R; // The result.
60 R.RemarkType = toRemarkType(static_cast<DiagnosticKind>(Diag.getKind()));
61 R.PassName = Diag.getPassName();
62 R.RemarkName = Diag.getRemarkName();
64 GlobalValue::dropLLVMManglingEscape(Diag.getFunction().getName());
65 R.Loc = toRemarkLocation(Diag.getLocation());
66 R.Hotness = Diag.getHotness();
68 for (const DiagnosticInfoOptimizationBase::Argument &Arg : Diag.getArgs()) {
69 R.Args.emplace_back();
70 R.Args.back().Key = Arg.Key;
71 R.Args.back().Val = Arg.Val;
72 R.Args.back().Loc = toRemarkLocation(Arg.Loc);
78 void LLVMRemarkStreamer::emit(const DiagnosticInfoOptimizationBase &Diag) {
79 if (!RS.matchesFilter(Diag.getPassName()))
82 // First, convert the diagnostic to a remark.
83 remarks::Remark R = toRemark(Diag);
84 // Then, emit the remark through the serializer.
85 RS.getSerializer().emit(R);
88 char LLVMRemarkSetupFileError::ID = 0;
89 char LLVMRemarkSetupPatternError::ID = 0;
90 char LLVMRemarkSetupFormatError::ID = 0;
92 Expected<std::unique_ptr<ToolOutputFile>> llvm::setupLLVMOptimizationRemarks(
93 LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,
94 StringRef RemarksFormat, bool RemarksWithHotness,
95 unsigned RemarksHotnessThreshold) {
96 if (RemarksWithHotness)
97 Context.setDiagnosticsHotnessRequested(true);
99 if (RemarksHotnessThreshold)
100 Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
102 if (RemarksFilename.empty())
105 Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);
106 if (Error E = Format.takeError())
107 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
110 auto Flags = *Format == remarks::Format::YAML ? sys::fs::OF_Text
113 std::make_unique<ToolOutputFile>(RemarksFilename, EC, Flags);
114 // We don't use llvm::FileError here because some diagnostics want the file
117 return make_error<LLVMRemarkSetupFileError>(errorCodeToError(EC));
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<LLVMRemarkSetupFormatError>(std::move(E));
125 // Create the main remark streamer.
126 Context.setMainRemarkStreamer(std::make_unique<remarks::RemarkStreamer>(
127 std::move(*RemarkSerializer), RemarksFilename));
129 // Create LLVM's optimization remarks streamer.
130 Context.setLLVMRemarkStreamer(
131 std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
133 if (!RemarksPasses.empty())
134 if (Error E = Context.getMainRemarkStreamer()->setFilter(RemarksPasses))
135 return make_error<LLVMRemarkSetupPatternError>(std::move(E));
137 return std::move(RemarksFile);
140 Error llvm::setupLLVMOptimizationRemarks(LLVMContext &Context, raw_ostream &OS,
141 StringRef RemarksPasses,
142 StringRef RemarksFormat,
143 bool RemarksWithHotness,
144 unsigned RemarksHotnessThreshold) {
145 if (RemarksWithHotness)
146 Context.setDiagnosticsHotnessRequested(true);
148 if (RemarksHotnessThreshold)
149 Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
151 Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);
152 if (Error E = Format.takeError())
153 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
155 Expected<std::unique_ptr<remarks::RemarkSerializer>> RemarkSerializer =
156 remarks::createRemarkSerializer(*Format,
157 remarks::SerializerMode::Separate, OS);
158 if (Error E = RemarkSerializer.takeError())
159 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
161 // Create the main remark streamer.
162 Context.setMainRemarkStreamer(
163 std::make_unique<remarks::RemarkStreamer>(std::move(*RemarkSerializer)));
165 // Create LLVM's optimization remarks streamer.
166 Context.setLLVMRemarkStreamer(
167 std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
169 if (!RemarksPasses.empty())
170 if (Error E = Context.getMainRemarkStreamer()->setFilter(RemarksPasses))
171 return make_error<LLVMRemarkSetupPatternError>(std::move(E));
173 return Error::success();