1 //===-- RemarkSerializer.h - Remark serialization interface -----*- 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 provides an interface for serializing remarks to different formats.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_REMARKS_REMARKSERIALIZER_H
14 #define LLVM_REMARKS_REMARKSERIALIZER_H
15 
16 #include "llvm/Remarks/RemarkFormat.h"
17 #include "llvm/Remarks/RemarkStringTable.h"
18 #include <optional>
19 
20 namespace llvm {
21 
22 class raw_ostream;
23 
24 namespace remarks {
25 
26 struct Remark;
27 
28 enum class SerializerMode {
29   Separate,  // A mode where the metadata is serialized separately from the
30              // remarks. Typically, this is used when the remarks need to be
31              // streamed to a side file and the metadata is embedded into the
32              // final result of the compilation.
33   Standalone // A mode where everything can be retrieved in the same
34              // file/buffer. Typically, this is used for storing remarks for
35              // later use.
36 };
37 
38 struct MetaSerializer;
39 
40 /// This is the base class for a remark serializer.
41 /// It includes support for using a string table while emitting.
42 struct RemarkSerializer {
43   /// The format of the serializer.
44   Format SerializerFormat;
45   /// The open raw_ostream that the remark diagnostics are emitted to.
46   raw_ostream &OS;
47   /// The serialization mode.
48   SerializerMode Mode;
49   /// The string table containing all the unique strings used in the output.
50   /// The table can be serialized to be consumed after the compilation.
51   std::optional<StringTable> StrTab;
52 
RemarkSerializerRemarkSerializer53   RemarkSerializer(Format SerializerFormat, raw_ostream &OS,
54                    SerializerMode Mode)
55       : SerializerFormat(SerializerFormat), OS(OS), Mode(Mode) {}
56 
57   /// This is just an interface.
58   virtual ~RemarkSerializer() = default;
59   /// Emit a remark to the stream.
60   virtual void emit(const Remark &Remark) = 0;
61   /// Return the corresponding metadata serializer.
62   virtual std::unique_ptr<MetaSerializer>
63   metaSerializer(raw_ostream &OS,
64                  std::optional<StringRef> ExternalFilename = std::nullopt) = 0;
65 };
66 
67 /// This is the base class for a remark metadata serializer.
68 struct MetaSerializer {
69   /// The open raw_ostream that the metadata is emitted to.
70   raw_ostream &OS;
71 
MetaSerializerMetaSerializer72   MetaSerializer(raw_ostream &OS) : OS(OS) {}
73 
74   /// This is just an interface.
75   virtual ~MetaSerializer() = default;
76   virtual void emit() = 0;
77 };
78 
79 /// Create a remark serializer.
80 Expected<std::unique_ptr<RemarkSerializer>>
81 createRemarkSerializer(Format RemarksFormat, SerializerMode Mode,
82                        raw_ostream &OS);
83 
84 /// Create a remark serializer that uses a pre-filled string table.
85 Expected<std::unique_ptr<RemarkSerializer>>
86 createRemarkSerializer(Format RemarksFormat, SerializerMode Mode,
87                        raw_ostream &OS, remarks::StringTable StrTab);
88 
89 } // end namespace remarks
90 } // end namespace llvm
91 
92 #endif // LLVM_REMARKS_REMARKSERIALIZER_H
93