aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/include/llvm/IR/LLVMRemarkStreamer.h
blob: 60389fcbf2ba4b54d4c30ebafe5469c6c0f42c56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===- llvm/IR/LLVMRemarkStreamer.h - Streamer for LLVM remarks--*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the conversion between IR Diagnostics and
// serializable remarks::Remark objects.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_IR_LLVMREMARKSTREAMER_H
#define LLVM_IR_LLVMREMARKSTREAMER_H

#include "llvm/Remarks/Remark.h"
#include "llvm/Support/Error.h"
#include <memory>
#include <optional>
#include <string>

namespace llvm {

class DiagnosticInfoOptimizationBase;
class LLVMContext;
class ToolOutputFile;
namespace remarks {
class RemarkStreamer;
}

/// Streamer for LLVM remarks which has logic for dealing with DiagnosticInfo
/// objects.
class LLVMRemarkStreamer {
  remarks::RemarkStreamer &RS;
  /// Convert diagnostics into remark objects.
  /// The lifetime of the members of the result is bound to the lifetime of
  /// the LLVM diagnostics.
  remarks::Remark toRemark(const DiagnosticInfoOptimizationBase &Diag) const;

public:
  LLVMRemarkStreamer(remarks::RemarkStreamer &RS) : RS(RS) {}
  /// Emit a diagnostic through the streamer.
  void emit(const DiagnosticInfoOptimizationBase &Diag);
};

template <typename ThisError>
struct LLVMRemarkSetupErrorInfo : public ErrorInfo<ThisError> {
  std::string Msg;
  std::error_code EC;

  LLVMRemarkSetupErrorInfo(Error E) {
    handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
      Msg = EIB.message();
      EC = EIB.convertToErrorCode();
    });
  }

  void log(raw_ostream &OS) const override { OS << Msg; }
  std::error_code convertToErrorCode() const override { return EC; }
};

struct LLVMRemarkSetupFileError
    : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFileError> {
  static char ID;
  using LLVMRemarkSetupErrorInfo<
      LLVMRemarkSetupFileError>::LLVMRemarkSetupErrorInfo;
};

struct LLVMRemarkSetupPatternError
    : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupPatternError> {
  static char ID;
  using LLVMRemarkSetupErrorInfo<
      LLVMRemarkSetupPatternError>::LLVMRemarkSetupErrorInfo;
};

struct LLVMRemarkSetupFormatError
    : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFormatError> {
  static char ID;
  using LLVMRemarkSetupErrorInfo<
      LLVMRemarkSetupFormatError>::LLVMRemarkSetupErrorInfo;
};

/// Setup optimization remarks that output to a file.
Expected<std::unique_ptr<ToolOutputFile>>
setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
                             StringRef RemarksPasses, StringRef RemarksFormat,
                             bool RemarksWithHotness,
                             std::optional<uint64_t> RemarksHotnessThreshold = 0);

/// Setup optimization remarks that output directly to a raw_ostream.
/// \p OS is managed by the caller and should be open for writing as long as \p
/// Context is streaming remarks to it.
Error setupLLVMOptimizationRemarks(
    LLVMContext &Context, raw_ostream &OS, StringRef RemarksPasses,
    StringRef RemarksFormat, bool RemarksWithHotness,
    std::optional<uint64_t> RemarksHotnessThreshold = 0);

} // end namespace llvm

#endif // LLVM_IR_LLVMREMARKSTREAMER_H

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif