xref: /freebsd/contrib/llvm-project/llvm/lib/IR/LLVMRemarkStreamer.cpp (revision 19261079b74319502c6ffa1249920079f0f69a72)
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 
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"
19 
20 using namespace llvm;
21 
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 
45 /// DiagnosticLocation -> remarks::RemarkLocation.
46 static Optional<remarks::RemarkLocation>
47 toRemarkLocation(const DiagnosticLocation &DL) {
48   if (!DL.isValid())
49     return None;
50   StringRef File = DL.getRelativePath();
51   unsigned Line = DL.getLine();
52   unsigned Col = DL.getColumn();
53   return remarks::RemarkLocation{File, Line, Col};
54 }
55 
56 /// LLVM Diagnostic -> Remark
57 remarks::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();
63   R.FunctionName =
64       GlobalValue::dropLLVMManglingEscape(Diag.getFunction().getName());
65   R.Loc = toRemarkLocation(Diag.getLocation());
66   R.Hotness = Diag.getHotness();
67 
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);
73   }
74 
75   return R;
76 }
77 
78 void LLVMRemarkStreamer::emit(const DiagnosticInfoOptimizationBase &Diag) {
79   if (!RS.matchesFilter(Diag.getPassName()))
80       return;
81 
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);
86 }
87 
88 char LLVMRemarkSetupFileError::ID = 0;
89 char LLVMRemarkSetupPatternError::ID = 0;
90 char LLVMRemarkSetupFormatError::ID = 0;
91 
92 Expected<std::unique_ptr<ToolOutputFile>> llvm::setupLLVMOptimizationRemarks(
93     LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,
94     StringRef RemarksFormat, bool RemarksWithHotness,
95     Optional<uint64_t> RemarksHotnessThreshold) {
96   if (RemarksWithHotness)
97     Context.setDiagnosticsHotnessRequested(true);
98 
99   Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
100 
101   if (RemarksFilename.empty())
102     return nullptr;
103 
104   Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);
105   if (Error E = Format.takeError())
106     return make_error<LLVMRemarkSetupFormatError>(std::move(E));
107 
108   std::error_code EC;
109   auto Flags = *Format == remarks::Format::YAML ? sys::fs::OF_Text
110                                                 : sys::fs::OF_None;
111   auto RemarksFile =
112       std::make_unique<ToolOutputFile>(RemarksFilename, EC, Flags);
113   // We don't use llvm::FileError here because some diagnostics want the file
114   // name separately.
115   if (EC)
116     return make_error<LLVMRemarkSetupFileError>(errorCodeToError(EC));
117 
118   Expected<std::unique_ptr<remarks::RemarkSerializer>> RemarkSerializer =
119       remarks::createRemarkSerializer(
120           *Format, remarks::SerializerMode::Separate, RemarksFile->os());
121   if (Error E = RemarkSerializer.takeError())
122     return make_error<LLVMRemarkSetupFormatError>(std::move(E));
123 
124   // Create the main remark streamer.
125   Context.setMainRemarkStreamer(std::make_unique<remarks::RemarkStreamer>(
126       std::move(*RemarkSerializer), RemarksFilename));
127 
128   // Create LLVM's optimization remarks streamer.
129   Context.setLLVMRemarkStreamer(
130       std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
131 
132   if (!RemarksPasses.empty())
133     if (Error E = Context.getMainRemarkStreamer()->setFilter(RemarksPasses))
134       return make_error<LLVMRemarkSetupPatternError>(std::move(E));
135 
136   return std::move(RemarksFile);
137 }
138 
139 Error llvm::setupLLVMOptimizationRemarks(
140     LLVMContext &Context, raw_ostream &OS, StringRef RemarksPasses,
141     StringRef RemarksFormat, bool RemarksWithHotness,
142     Optional<uint64_t> RemarksHotnessThreshold) {
143   if (RemarksWithHotness)
144     Context.setDiagnosticsHotnessRequested(true);
145 
146   Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
147 
148   Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);
149   if (Error E = Format.takeError())
150     return make_error<LLVMRemarkSetupFormatError>(std::move(E));
151 
152   Expected<std::unique_ptr<remarks::RemarkSerializer>> RemarkSerializer =
153       remarks::createRemarkSerializer(*Format,
154                                       remarks::SerializerMode::Separate, OS);
155   if (Error E = RemarkSerializer.takeError())
156     return make_error<LLVMRemarkSetupFormatError>(std::move(E));
157 
158   // Create the main remark streamer.
159   Context.setMainRemarkStreamer(
160       std::make_unique<remarks::RemarkStreamer>(std::move(*RemarkSerializer)));
161 
162   // Create LLVM's optimization remarks streamer.
163   Context.setLLVMRemarkStreamer(
164       std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
165 
166   if (!RemarksPasses.empty())
167     if (Error E = Context.getMainRemarkStreamer()->setFilter(RemarksPasses))
168       return make_error<LLVMRemarkSetupPatternError>(std::move(E));
169 
170   return Error::success();
171 }
172