1//===- llvm/IR/LLVMRemarkStreamer.h - Streamer for LLVM remarks--*- 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 implements the conversion between IR Diagnostics and
10// serializable remarks::Remark objects.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_LLVMREMARKSTREAMER_H
15#define LLVM_IR_LLVMREMARKSTREAMER_H
16
17#include "llvm/Remarks/Remark.h"
18#include "llvm/Support/Error.h"
19#include <memory>
20#include <optional>
21#include <string>
22
23namespace llvm {
24
25class DiagnosticInfoOptimizationBase;
26class LLVMContext;
27class ToolOutputFile;
28namespace remarks {
29class RemarkStreamer;
30}
31
32/// Streamer for LLVM remarks which has logic for dealing with DiagnosticInfo
33/// objects.
34class 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;
40
41public:
42  LLVMRemarkStreamer(remarks::RemarkStreamer &RS) : RS(RS) {}
43  /// Emit a diagnostic through the streamer.
44  void emit(const DiagnosticInfoOptimizationBase &Diag);
45};
46
47template <typename ThisError>
48struct LLVMRemarkSetupErrorInfo : public ErrorInfo<ThisError> {
49  std::string Msg;
50  std::error_code EC;
51
52  LLVMRemarkSetupErrorInfo(Error E) {
53    handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
54      Msg = EIB.message();
55      EC = EIB.convertToErrorCode();
56    });
57  }
58
59  void log(raw_ostream &OS) const override { OS << Msg; }
60  std::error_code convertToErrorCode() const override { return EC; }
61};
62
63struct LLVMRemarkSetupFileError
64    : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFileError> {
65  static char ID;
66  using LLVMRemarkSetupErrorInfo<
67      LLVMRemarkSetupFileError>::LLVMRemarkSetupErrorInfo;
68};
69
70struct LLVMRemarkSetupPatternError
71    : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupPatternError> {
72  static char ID;
73  using LLVMRemarkSetupErrorInfo<
74      LLVMRemarkSetupPatternError>::LLVMRemarkSetupErrorInfo;
75};
76
77struct LLVMRemarkSetupFormatError
78    : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFormatError> {
79  static char ID;
80  using LLVMRemarkSetupErrorInfo<
81      LLVMRemarkSetupFormatError>::LLVMRemarkSetupErrorInfo;
82};
83
84/// Setup optimization remarks that output to a file.
85Expected<std::unique_ptr<ToolOutputFile>>
86setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
87                             StringRef RemarksPasses, StringRef RemarksFormat,
88                             bool RemarksWithHotness,
89                             std::optional<uint64_t> RemarksHotnessThreshold = 0);
90
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.
94Error setupLLVMOptimizationRemarks(
95    LLVMContext &Context, raw_ostream &OS, StringRef RemarksPasses,
96    StringRef RemarksFormat, bool RemarksWithHotness,
97    std::optional<uint64_t> RemarksHotnessThreshold = 0);
98
99} // end namespace llvm
100
101#endif // LLVM_IR_LLVMREMARKSTREAMER_H
102