1 //===- llvm/IR/LLVMRemarkStreamer.h - Streamer for LLVM remarks--*- 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 implements the conversion between IR Diagnostics and
10 // serializable remarks::Remark objects.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_IR_LLVMREMARKSTREAMER_H
15 #define LLVM_IR_LLVMREMARKSTREAMER_H
17 #include "llvm/Remarks/Remark.h"
18 #include "llvm/Support/Error.h"
25 class DiagnosticInfoOptimizationBase;
32 /// Streamer for LLVM remarks which has logic for dealing with DiagnosticInfo
34 class LLVMRemarkStreamer {
35 remarks::RemarkStreamer &RS;
36 /// Convert diagnostics into remark objects.
37 /// The lifetime of the members of the result is bound to the lifetime of
38 /// the LLVM diagnostics.
39 remarks::Remark toRemark(const DiagnosticInfoOptimizationBase &Diag) const;
42 LLVMRemarkStreamer(remarks::RemarkStreamer &RS) : RS(RS) {}
43 /// Emit a diagnostic through the streamer.
44 void emit(const DiagnosticInfoOptimizationBase &Diag);
47 template <typename ThisError>
48 struct LLVMRemarkSetupErrorInfo : public ErrorInfo<ThisError> {
52 LLVMRemarkSetupErrorInfo(Error E) {
53 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
55 EC = EIB.convertToErrorCode();
59 void log(raw_ostream &OS) const override { OS << Msg; }
60 std::error_code convertToErrorCode() const override { return EC; }
63 struct LLVMRemarkSetupFileError
64 : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFileError> {
66 using LLVMRemarkSetupErrorInfo<
67 LLVMRemarkSetupFileError>::LLVMRemarkSetupErrorInfo;
70 struct LLVMRemarkSetupPatternError
71 : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupPatternError> {
73 using LLVMRemarkSetupErrorInfo<
74 LLVMRemarkSetupPatternError>::LLVMRemarkSetupErrorInfo;
77 struct LLVMRemarkSetupFormatError
78 : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFormatError> {
80 using LLVMRemarkSetupErrorInfo<
81 LLVMRemarkSetupFormatError>::LLVMRemarkSetupErrorInfo;
84 /// Setup optimization remarks that output to a file.
85 Expected<std::unique_ptr<ToolOutputFile>>
86 setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
87 StringRef RemarksPasses, StringRef RemarksFormat,
88 bool RemarksWithHotness,
89 std::optional<uint64_t> RemarksHotnessThreshold = 0);
91 /// Setup optimization remarks that output directly to a raw_ostream.
92 /// \p OS is managed by the caller and should be open for writing as long as \p
93 /// Context is streaming remarks to it.
94 Error setupLLVMOptimizationRemarks(
95 LLVMContext &Context, raw_ostream &OS, StringRef RemarksPasses,
96 StringRef RemarksFormat, bool RemarksWithHotness,
97 std::optional<uint64_t> RemarksHotnessThreshold = 0);
99 } // end namespace llvm
101 #endif // LLVM_IR_LLVMREMARKSTREAMER_H