| 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/Remarks/RemarkStreamer.h" |
| 19 | #include "llvm/Support/FileSystem.h" |
| 20 | #include "llvm/Support/ToolOutputFile.h" |
| 21 | #include <optional> |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | /// DiagnosticKind -> remarks::Type |
| 26 | static remarks::Type (enum DiagnosticKind Kind) { |
| 27 | switch (Kind) { |
| 28 | default: |
| 29 | return remarks::Type::Unknown; |
| 30 | case DK_OptimizationRemark: |
| 31 | case DK_MachineOptimizationRemark: |
| 32 | return remarks::Type::Passed; |
| 33 | case DK_OptimizationRemarkMissed: |
| 34 | case DK_MachineOptimizationRemarkMissed: |
| 35 | return remarks::Type::Missed; |
| 36 | case DK_OptimizationRemarkAnalysis: |
| 37 | case DK_MachineOptimizationRemarkAnalysis: |
| 38 | return remarks::Type::Analysis; |
| 39 | case DK_OptimizationRemarkAnalysisFPCommute: |
| 40 | return remarks::Type::AnalysisFPCommute; |
| 41 | case DK_OptimizationRemarkAnalysisAliasing: |
| 42 | return remarks::Type::AnalysisAliasing; |
| 43 | case DK_OptimizationFailure: |
| 44 | return remarks::Type::Failure; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// DiagnosticLocation -> remarks::RemarkLocation. |
| 49 | static std::optional<remarks::RemarkLocation> |
| 50 | (const DiagnosticLocation &DL) { |
| 51 | if (!DL.isValid()) |
| 52 | return std::nullopt; |
| 53 | StringRef File = DL.getRelativePath(); |
| 54 | unsigned Line = DL.getLine(); |
| 55 | unsigned Col = DL.getColumn(); |
| 56 | return remarks::RemarkLocation{.SourceFilePath: File, .SourceLine: Line, .SourceColumn: Col}; |
| 57 | } |
| 58 | |
| 59 | /// LLVM Diagnostic -> Remark |
| 60 | remarks::Remark |
| 61 | LLVMRemarkStreamer::(const DiagnosticInfoOptimizationBase &Diag) const { |
| 62 | remarks::Remark R; // The result. |
| 63 | R.RemarkType = toRemarkType(Kind: static_cast<DiagnosticKind>(Diag.getKind())); |
| 64 | R.PassName = Diag.getPassName(); |
| 65 | R.RemarkName = Diag.getRemarkName(); |
| 66 | R.FunctionName = |
| 67 | GlobalValue::dropLLVMManglingEscape(Name: Diag.getFunction().getName()); |
| 68 | R.Loc = toRemarkLocation(DL: Diag.getLocation()); |
| 69 | R.Hotness = Diag.getHotness(); |
| 70 | |
| 71 | for (const DiagnosticInfoOptimizationBase::Argument &Arg : Diag.getArgs()) { |
| 72 | R.Args.emplace_back(); |
| 73 | R.Args.back().Key = Arg.Key; |
| 74 | R.Args.back().Val = Arg.Val; |
| 75 | R.Args.back().Loc = toRemarkLocation(DL: Arg.Loc); |
| 76 | } |
| 77 | |
| 78 | return R; |
| 79 | } |
| 80 | |
| 81 | void LLVMRemarkStreamer::(const DiagnosticInfoOptimizationBase &Diag) { |
| 82 | if (!RS.matchesFilter(Str: Diag.getPassName())) |
| 83 | return; |
| 84 | |
| 85 | // First, convert the diagnostic to a remark. |
| 86 | remarks::Remark R = toRemark(Diag); |
| 87 | // Then, emit the remark through the serializer. |
| 88 | RS.getSerializer().emit(Remark: R); |
| 89 | } |
| 90 | |
| 91 | char LLVMRemarkSetupFileError:: = 0; |
| 92 | char LLVMRemarkSetupPatternError:: = 0; |
| 93 | char LLVMRemarkSetupFormatError:: = 0; |
| 94 | |
| 95 | Expected<LLVMRemarkFileHandle> llvm::( |
| 96 | LLVMContext &Context, StringRef , StringRef , |
| 97 | StringRef , bool , |
| 98 | std::optional<uint64_t> ) { |
| 99 | if (RemarksWithHotness || RemarksHotnessThreshold.value_or(u: 1)) |
| 100 | Context.setDiagnosticsHotnessRequested(true); |
| 101 | |
| 102 | Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold); |
| 103 | |
| 104 | if (RemarksFilename.empty()) |
| 105 | return LLVMRemarkFileHandle(); |
| 106 | |
| 107 | Expected<remarks::Format> Format = remarks::parseFormat(FormatStr: RemarksFormat); |
| 108 | if (Error E = Format.takeError()) |
| 109 | return make_error<LLVMRemarkSetupFormatError>(Args: std::move(E)); |
| 110 | |
| 111 | std::error_code EC; |
| 112 | auto Flags = *Format == remarks::Format::YAML ? sys::fs::OF_TextWithCRLF |
| 113 | : sys::fs::OF_None; |
| 114 | auto = |
| 115 | std::make_unique<ToolOutputFile>(args&: RemarksFilename, args&: EC, args&: Flags); |
| 116 | // We don't use llvm::FileError here because some diagnostics want the file |
| 117 | // name separately. |
| 118 | if (EC) |
| 119 | return make_error<LLVMRemarkSetupFileError>(Args: errorCodeToError(EC)); |
| 120 | |
| 121 | Expected<std::unique_ptr<remarks::RemarkSerializer>> = |
| 122 | remarks::createRemarkSerializer(RemarksFormat: *Format, OS&: RemarksFile->os()); |
| 123 | if (Error E = RemarkSerializer.takeError()) |
| 124 | return make_error<LLVMRemarkSetupFormatError>(Args: std::move(E)); |
| 125 | |
| 126 | auto RS = std::make_unique<remarks::RemarkStreamer>( |
| 127 | args: std::move(*RemarkSerializer), args&: RemarksFilename); |
| 128 | |
| 129 | if (!RemarksPasses.empty()) |
| 130 | if (Error E = RS->setFilter(RemarksPasses)) { |
| 131 | RS->releaseSerializer(); |
| 132 | return make_error<LLVMRemarkSetupPatternError>(Args: std::move(E)); |
| 133 | } |
| 134 | |
| 135 | // Install the main remark streamer. Only install this after setting the |
| 136 | // filter, because this might fail. |
| 137 | Context.setMainRemarkStreamer(std::move(RS)); |
| 138 | |
| 139 | // Create LLVM's optimization remarks streamer. |
| 140 | Context.setLLVMRemarkStreamer( |
| 141 | std::make_unique<LLVMRemarkStreamer>(args&: *Context.getMainRemarkStreamer())); |
| 142 | |
| 143 | return LLVMRemarkFileHandle{std::move(RemarksFile), Context}; |
| 144 | } |
| 145 | |
| 146 | void LLVMRemarkFileHandle::Finalizer::finalize() { |
| 147 | if (!Context) |
| 148 | return; |
| 149 | finalizeLLVMOptimizationRemarks(Context&: *Context); |
| 150 | Context = nullptr; |
| 151 | } |
| 152 | |
| 153 | Error llvm::( |
| 154 | LLVMContext &Context, raw_ostream &OS, StringRef , |
| 155 | StringRef , bool , |
| 156 | std::optional<uint64_t> ) { |
| 157 | if (RemarksWithHotness || RemarksHotnessThreshold.value_or(u: 1)) |
| 158 | Context.setDiagnosticsHotnessRequested(true); |
| 159 | |
| 160 | Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold); |
| 161 | |
| 162 | Expected<remarks::Format> Format = remarks::parseFormat(FormatStr: RemarksFormat); |
| 163 | if (Error E = Format.takeError()) |
| 164 | return make_error<LLVMRemarkSetupFormatError>(Args: std::move(E)); |
| 165 | |
| 166 | Expected<std::unique_ptr<remarks::RemarkSerializer>> = |
| 167 | remarks::createRemarkSerializer(RemarksFormat: *Format, OS); |
| 168 | if (Error E = RemarkSerializer.takeError()) |
| 169 | return make_error<LLVMRemarkSetupFormatError>(Args: std::move(E)); |
| 170 | |
| 171 | auto RS = |
| 172 | std::make_unique<remarks::RemarkStreamer>(args: std::move(*RemarkSerializer)); |
| 173 | |
| 174 | if (!RemarksPasses.empty()) |
| 175 | if (Error E = RS->setFilter(RemarksPasses)) { |
| 176 | RS->releaseSerializer(); |
| 177 | return make_error<LLVMRemarkSetupPatternError>(Args: std::move(E)); |
| 178 | } |
| 179 | |
| 180 | // Install the main remark streamer. Only install this after setting the |
| 181 | // filter, because this might fail. |
| 182 | Context.setMainRemarkStreamer(std::move(RS)); |
| 183 | |
| 184 | // Create LLVM's optimization remarks streamer. |
| 185 | Context.setLLVMRemarkStreamer( |
| 186 | std::make_unique<LLVMRemarkStreamer>(args&: *Context.getMainRemarkStreamer())); |
| 187 | |
| 188 | return Error::success(); |
| 189 | } |
| 190 | |
| 191 | void llvm::(LLVMContext &Context) { |
| 192 | Context.setLLVMRemarkStreamer(nullptr); |
| 193 | if (auto *RS = Context.getMainRemarkStreamer()) { |
| 194 | RS->releaseSerializer(); |
| 195 | Context.setMainRemarkStreamer(nullptr); |
| 196 | } |
| 197 | } |
| 198 | |