LLVM 22.0.0git
YAMLRemarkSerializer.cpp
Go to the documentation of this file.
1//===- YAMLRemarkSerializer.cpp -------------------------------------------===//
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 the implementation of the YAML remark serializer using
10// LLVM's YAMLTraits.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/Remarks/Remark.h"
17#include <optional>
18
19using namespace llvm;
20using namespace llvm::remarks;
21
22// Use the same keys whether we use a string table or not (respectively, T is an
23// unsigned or a StringRef).
24static void
25mapRemarkHeader(yaml::IO &io, StringRef PassName, StringRef RemarkName,
26 std::optional<RemarkLocation> RL, StringRef FunctionName,
27 std::optional<uint64_t> Hotness, ArrayRef<Argument> Args) {
28 io.mapRequired("Pass", PassName);
29 io.mapRequired("Name", RemarkName);
30 io.mapOptional("DebugLoc", RL);
31 io.mapRequired("Function", FunctionName);
32 io.mapOptional("Hotness", Hotness);
33 io.mapOptional("Args", Args);
34}
35
36namespace llvm {
37namespace yaml {
38
39template <> struct MappingTraits<remarks::Remark *> {
40 static void mapping(IO &io, remarks::Remark *&Remark) {
41 assert(io.outputting() && "input not yet implemented");
42
43 if (io.mapTag("!Passed", (Remark->RemarkType == Type::Passed)))
44 ;
45 else if (io.mapTag("!Missed", (Remark->RemarkType == Type::Missed)))
46 ;
47 else if (io.mapTag("!Analysis", (Remark->RemarkType == Type::Analysis)))
48 ;
49 else if (io.mapTag("!AnalysisFPCommute",
50 (Remark->RemarkType == Type::AnalysisFPCommute)))
51 ;
52 else if (io.mapTag("!AnalysisAliasing",
53 (Remark->RemarkType == Type::AnalysisAliasing)))
54 ;
55 else if (io.mapTag("!Failure", (Remark->RemarkType == Type::Failure)))
56 ;
57 else
58 llvm_unreachable("Unknown remark type");
59
62 }
63};
64
65template <> struct MappingTraits<RemarkLocation> {
66 static void mapping(IO &io, RemarkLocation &RL) {
67 assert(io.outputting() && "input not yet implemented");
68
69 StringRef File = RL.SourceFilePath;
70 unsigned Line = RL.SourceLine;
71 unsigned Col = RL.SourceColumn;
72
73 io.mapRequired("File", File);
74
75 io.mapRequired("Line", Line);
76 io.mapRequired("Column", Col);
77 }
78
79 static const bool flow = true;
80};
81
82/// Helper struct for multiline string block literals. Use this type to preserve
83/// newlines in strings.
87};
88
89template <> struct BlockScalarTraits<StringBlockVal> {
90 static void output(const StringBlockVal &S, void *Ctx, raw_ostream &OS) {
91 return ScalarTraits<StringRef>::output(S.Value, Ctx, OS);
92 }
93
94 static StringRef input(StringRef Scalar, void *Ctx, StringBlockVal &S) {
95 return ScalarTraits<StringRef>::input(Scalar, Ctx, S.Value);
96 }
97};
98
99/// ArrayRef is not really compatible with the YAMLTraits. Everything should be
100/// immutable in an ArrayRef, while the SequenceTraits expect a mutable version
101/// for inputting, but we're only using the outputting capabilities here.
102/// This is a hack, but still nicer than having to manually call the YAMLIO
103/// internal methods.
104/// Keep this in this file so that it doesn't get misused from YAMLTraits.h.
105template <typename T> struct SequenceTraits<ArrayRef<T>> {
106 static size_t size(IO &io, ArrayRef<T> &seq) { return seq.size(); }
107 static Argument &element(IO &io, ArrayRef<T> &seq, size_t index) {
108 assert(io.outputting() && "input not yet implemented");
109 // The assert above should make this "safer" to satisfy the YAMLTraits.
110 return const_cast<T &>(seq[index]);
111 }
112};
113
114/// Implement this as a mapping for now to get proper quotation for the value.
115template <> struct MappingTraits<Argument> {
116 static void mapping(IO &io, Argument &A) {
117 assert(io.outputting() && "input not yet implemented");
118
119 if (StringRef(A.Val).count('\n') > 1) {
120 StringBlockVal S(A.Val);
121 io.mapRequired(A.Key.data(), S);
122 } else {
123 io.mapRequired(A.Key.data(), A.Val);
124 }
125 io.mapOptional("DebugLoc", A.Loc);
126 }
127};
128
129} // end namespace yaml
130} // end namespace llvm
131
133
135 std::optional<StringTable> StrTabIn)
136 : RemarkSerializer(Format::YAML, OS, Mode),
137 YAMLOutput(OS, reinterpret_cast<void *>(this)) {
138 StrTab = std::move(StrTabIn);
139}
140
142 // Again, YAMLTraits expect a non-const object for inputting, but we're not
143 // using that here.
144 auto *R = const_cast<remarks::Remark *>(&Remark);
145 YAMLOutput << R;
146}
147
148std::unique_ptr<MetaSerializer> YAMLRemarkSerializer::metaSerializer(
149 raw_ostream &OS, std::optional<StringRef> ExternalFilename) {
150 return std::make_unique<YAMLMetaSerializer>(OS, ExternalFilename);
151}
152
153static void emitMagic(raw_ostream &OS) {
154 // Emit the magic number.
156 // Explicitly emit a '\0'.
157 OS.write('\0');
158}
159
161 // Emit the version number: little-endian uint64_t.
162 std::array<char, 8> Version;
164 OS.write(Version.data(), Version.size());
165}
166
167static void emitExternalFile(raw_ostream &OS, StringRef Filename) {
168 // Emit the null-terminated absolute path to the remark file.
169 SmallString<128> FilenameBuf = Filename;
170 sys::fs::make_absolute(FilenameBuf);
171 assert(!FilenameBuf.empty() && "The filename can't be empty.");
172 OS.write(FilenameBuf.data(), FilenameBuf.size());
173 OS.write('\0');
174}
175
177 emitMagic(OS);
179
180 // Emit StringTable with size 0. This is left over after removing StringTable
181 // support from the YAML format. For now, don't unnecessarily change how the
182 // the metadata is serialized. When changing the format, we should think about
183 // just reusing the bitstream remark meta for this.
184 uint64_t StrTabSize = 0;
185 std::array<char, 8> StrTabSizeBuf;
186 support::endian::write64le(StrTabSizeBuf.data(), StrTabSize);
187
188 OS.write(StrTabSizeBuf.data(), StrTabSizeBuf.size());
191}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
raw_pwrite_stream & OS
static const char PassName[]
static void emitExternalFile(raw_ostream &OS, StringRef Filename)
static void emitMagic(raw_ostream &OS)
static void mapRemarkHeader(yaml::IO &io, StringRef PassName, StringRef RemarkName, std::optional< RemarkLocation > RL, StringRef FunctionName, std::optional< uint64_t > Hotness, ArrayRef< Argument > Args)
static void emitVersion(raw_ostream &OS)
#define LLVM_YAML_IS_SEQUENCE_VECTOR(type)
This class represents an incoming formal argument to a Function.
Definition: Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:287
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:461
LLVM Value Representation.
Definition: Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
raw_ostream & write(unsigned char C)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr StringLiteral Magic("REMARKS")
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:26
constexpr uint64_t CurrentRemarkVersion
The current version of the remark entry.
Definition: Remark.h:29
void write64le(void *P, uint64_t V)
Definition: Endian.h:475
LLVM_ABI void make_absolute(const Twine &current_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
Definition: Path.cpp:906
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition: Sequence.h:305
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
raw_ostream & OS
The open raw_ostream that the metadata is emitted to.
The debug location used to track a remark back to the source file.
Definition: Remark.h:32
StringRef SourceFilePath
Absolute path of the source file corresponding to this remark.
Definition: Remark.h:34
This is the base class for a remark serializer.
raw_ostream & OS
The open raw_ostream that the remark diagnostics are emitted to.
A remark type used for both emission and parsing.
Definition: Remark.h:98
Type RemarkType
The type of the remark.
Definition: Remark.h:100
StringRef PassName
Name of the pass that triggers the emission of this remark.
Definition: Remark.h:103
std::optional< RemarkLocation > Loc
The location in the source file of the remark.
Definition: Remark.h:114
std::optional< uint64_t > Hotness
If profile information is available, this is the number of times the corresponding code was executed ...
Definition: Remark.h:118
StringRef RemarkName
Textual identifier for the remark (single-word, camel-case).
Definition: Remark.h:108
StringRef FunctionName
Mangled name of the function that triggers the emssion of this remark.
Definition: Remark.h:111
SmallVector< Argument, 5 > Args
Arguments collected via the streaming interface.
Definition: Remark.h:121
The string table used for serializing remarks.
std::optional< StringRef > ExternalFilename
Serialize the remarks to YAML.
std::unique_ptr< MetaSerializer > metaSerializer(raw_ostream &OS, std::optional< StringRef > ExternalFilename=std::nullopt) override
Return the corresponding metadata serializer.
void emit(const Remark &Remark) override
Emit a remark to the stream.
yaml::Output YAMLOutput
The YAML streamer.
static void output(const StringBlockVal &S, void *Ctx, raw_ostream &OS)
static StringRef input(StringRef Scalar, void *Ctx, StringBlockVal &S)
static void mapping(IO &io, Argument &A)
static void mapping(IO &io, RemarkLocation &RL)
static void mapping(IO &io, remarks::Remark *&Remark)
static Argument & element(IO &io, ArrayRef< T > &seq, size_t index)
static size_t size(IO &io, ArrayRef< T > &seq)
Helper struct for multiline string block literals.