blob: 9b6d82ee30c0c6a9608539c55e0b3f90021bb326 [file] [log] [blame]
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001//===- llvm/IR/RemarkStreamer.h - 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 declares the main interface for outputting remarks.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_IR_REMARKSTREAMER_H
14#define LLVM_IR_REMARKSTREAMER_H
15
16#include "llvm/IR/DiagnosticInfo.h"
17#include "llvm/Remarks/RemarkSerializer.h"
18#include "llvm/Support/Error.h"
19#include "llvm/Support/Regex.h"
20#include "llvm/Support/ToolOutputFile.h"
21#include "llvm/Support/raw_ostream.h"
22#include <string>
23#include <vector>
24
25namespace llvm {
26/// Streamer for remarks.
27class RemarkStreamer {
28 /// The filename that the remark diagnostics are emitted to.
29 const std::string Filename;
30 /// The regex used to filter remarks based on the passes that emit them.
31 Optional<Regex> PassFilter;
32 /// The object used to serialize the remarks to a specific format.
33 std::unique_ptr<remarks::Serializer> Serializer;
34
35 /// Temporary buffer for converting diagnostics into remark objects. This is
36 /// used for the remark arguments that are converted from a vector of
37 /// diagnostic arguments to a vector of remark arguments.
38 SmallVector<remarks::Argument, 8> TmpArgs;
39 /// Convert diagnostics into remark objects. The result uses \p TmpArgs as a
40 /// temporary buffer for the remark arguments, and relies on all the strings
41 /// to be kept in memory until the next call to `toRemark`.
42 /// The lifetime of the members of the result is bound to the lifetime of both
43 /// the remark streamer and the LLVM diagnostics.
44 remarks::Remark toRemark(const DiagnosticInfoOptimizationBase &Diag);
45
46public:
47 RemarkStreamer(StringRef Filename,
48 std::unique_ptr<remarks::Serializer> Serializer);
49 /// Return the filename that the remark diagnostics are emitted to.
50 StringRef getFilename() const { return Filename; }
51 /// Return stream that the remark diagnostics are emitted to.
52 raw_ostream &getStream() { return Serializer->OS; }
53 /// Return the serializer used for this stream.
54 remarks::Serializer &getSerializer() { return *Serializer; }
55 /// Set a pass filter based on a regex \p Filter.
56 /// Returns an error if the regex is invalid.
57 Error setFilter(StringRef Filter);
58 /// Emit a diagnostic through the streamer.
59 void emit(const DiagnosticInfoOptimizationBase &Diag);
60};
61
62template <typename ThisError>
63struct RemarkSetupErrorInfo : public ErrorInfo<ThisError> {
64 std::string Msg;
65 std::error_code EC;
66
67 RemarkSetupErrorInfo(Error E) {
68 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
69 Msg = EIB.message();
70 EC = EIB.convertToErrorCode();
71 });
72 }
73
74 void log(raw_ostream &OS) const override { OS << Msg; }
75 std::error_code convertToErrorCode() const override { return EC; }
76};
77
78struct RemarkSetupFileError : RemarkSetupErrorInfo<RemarkSetupFileError> {
79 static char ID;
80 using RemarkSetupErrorInfo<RemarkSetupFileError>::RemarkSetupErrorInfo;
81};
82
83struct RemarkSetupPatternError : RemarkSetupErrorInfo<RemarkSetupPatternError> {
84 static char ID;
85 using RemarkSetupErrorInfo<RemarkSetupPatternError>::RemarkSetupErrorInfo;
86};
87
88struct RemarkSetupFormatError : RemarkSetupErrorInfo<RemarkSetupFormatError> {
89 static char ID;
90 using RemarkSetupErrorInfo<RemarkSetupFormatError>::RemarkSetupErrorInfo;
91};
92
93enum class RemarksSerializerFormat { Unknown, YAML };
94
95Expected<RemarksSerializerFormat> parseSerializerFormat(StringRef Format);
96
97/// Setup optimization remarks.
98Expected<std::unique_ptr<ToolOutputFile>>
99setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
100 StringRef RemarksPasses, StringRef RemarksFormat,
101 bool RemarksWithHotness,
102 unsigned RemarksHotnessThreshold = 0);
103
104} // end namespace llvm
105
106#endif // LLVM_IR_REMARKSTREAMER_H