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
22static void
24 std::optional<RemarkLocation> RL, StringRef FunctionName,
25 std::optional<uint64_t> Hotness, ArrayRef<Argument> Args) {
26 io.mapRequired("Pass", PassName);
27 io.mapRequired("Name", RemarkName);
28 io.mapOptional("DebugLoc", RL);
29 io.mapRequired("Function", FunctionName);
30 io.mapOptional("Hotness", Hotness);
31 io.mapOptional("Args", Args);
32}
33
34namespace llvm {
35namespace yaml {
36
37template <> struct MappingTraits<remarks::Remark *> {
38 static void mapping(IO &io, remarks::Remark *&Remark) {
39 assert(io.outputting() && "input not yet implemented");
40
41 if (io.mapTag("!Passed", (Remark->RemarkType == Type::Passed)))
42 ;
43 else if (io.mapTag("!Missed", (Remark->RemarkType == Type::Missed)))
44 ;
45 else if (io.mapTag("!Analysis", (Remark->RemarkType == Type::Analysis)))
46 ;
47 else if (io.mapTag("!AnalysisFPCommute",
48 (Remark->RemarkType == Type::AnalysisFPCommute)))
49 ;
50 else if (io.mapTag("!AnalysisAliasing",
51 (Remark->RemarkType == Type::AnalysisAliasing)))
52 ;
53 else if (io.mapTag("!Failure", (Remark->RemarkType == Type::Failure)))
54 ;
55 else
56 llvm_unreachable("Unknown remark type");
57
58 mapRemarkHeader(io, Remark->PassName, Remark->RemarkName, Remark->Loc,
59 Remark->FunctionName, Remark->Hotness, Remark->Args);
60 }
61};
62
63template <> struct MappingTraits<RemarkLocation> {
64 static void mapping(IO &io, RemarkLocation &RL) {
65 assert(io.outputting() && "input not yet implemented");
66
67 StringRef File = RL.SourceFilePath;
68 unsigned Line = RL.SourceLine;
69 unsigned Col = RL.SourceColumn;
70
71 io.mapRequired("File", File);
72
73 io.mapRequired("Line", Line);
74 io.mapRequired("Column", Col);
75 }
76
77 static const bool flow = true;
78};
79
80/// Helper struct for multiline string block literals. Use this type to preserve
81/// newlines in strings.
86
87template <> struct BlockScalarTraits<StringBlockVal> {
88 static void output(const StringBlockVal &S, void *Ctx, raw_ostream &OS) {
89 return ScalarTraits<StringRef>::output(S.Value, Ctx, OS);
90 }
91
94 }
95};
96
97/// ArrayRef is not really compatible with the YAMLTraits. Everything should be
98/// immutable in an ArrayRef, while the SequenceTraits expect a mutable version
99/// for inputting, but we're only using the outputting capabilities here.
100/// This is a hack, but still nicer than having to manually call the YAMLIO
101/// internal methods.
102/// Keep this in this file so that it doesn't get misused from YAMLTraits.h.
103template <typename T> struct SequenceTraits<ArrayRef<T>> {
104 static size_t size(IO &io, ArrayRef<T> &seq) { return seq.size(); }
105 static Argument &element(IO &io, ArrayRef<T> &seq, size_t index) {
106 assert(io.outputting() && "input not yet implemented");
107 // The assert above should make this "safer" to satisfy the YAMLTraits.
108 return const_cast<T &>(seq[index]);
109 }
110};
111
112/// Implement this as a mapping for now to get proper quotation for the value.
113template <> struct MappingTraits<Argument> {
114 static void mapping(IO &io, Argument &A) {
115 assert(io.outputting() && "input not yet implemented");
116
117 // A.Key.data() is not necessarily null-terminated, so we must make a copy,
118 // otherwise we potentially read out of bounds.
119 // FIXME: Add support for StringRef Keys in YAML IO.
120 std::string Key(A.Key);
121 if (StringRef(A.Val).count('\n') > 1) {
122 StringBlockVal S(A.Val);
123 io.mapRequired(Key.c_str(), S);
124 } else {
125 io.mapRequired(Key.c_str(), A.Val);
126 }
127 io.mapOptional("DebugLoc", A.Loc);
128 }
129};
130
131} // end namespace yaml
132} // end namespace llvm
133
135
139
145
147 // Again, YAMLTraits expect a non-const object for inputting, but we're not
148 // using that here.
149 auto *R = const_cast<remarks::Remark *>(&Remark);
150 YAMLOutput << R;
151}
152
153std::unique_ptr<MetaSerializer>
155 StringRef ExternalFilename) {
156 return std::make_unique<YAMLMetaSerializer>(OS, ExternalFilename);
157}
158
159static void emitMagic(raw_ostream &OS) {
160 // Emit the magic number.
161 OS << remarks::Magic;
162 // Explicitly emit a '\0'.
163 OS.write('\0');
164}
165
166static void emitVersion(raw_ostream &OS) {
167 // Emit the version number: little-endian uint64_t.
168 std::array<char, 8> Version;
170 OS.write(Version.data(), Version.size());
171}
172
173static void emitExternalFile(raw_ostream &OS, StringRef Filename) {
174 // Emit the null-terminated absolute path to the remark file.
175 SmallString<128> FilenameBuf = Filename;
176 sys::fs::make_absolute(FilenameBuf);
177 assert(!FilenameBuf.empty() && "The filename can't be empty.");
178 OS.write(FilenameBuf.data(), FilenameBuf.size());
179 OS.write('\0');
180}
181
183 emitMagic(OS);
185
186 // Emit StringTable with size 0. This is left over after removing StringTable
187 // support from the YAML format. For now, don't unnecessarily change how the
188 // the metadata is serialized. When changing the format, we should think about
189 // just reusing the bitstream remark meta for this.
190 uint64_t StrTabSize = 0;
191 std::array<char, 8> StrTabSizeBuf;
192 support::endian::write64le(StrTabSizeBuf.data(), StrTabSize);
193
194 OS.write(StrTabSizeBuf.data(), StrTabSizeBuf.size());
196}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define T
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)
Utility for declaring that a std::vector of a particular type should be considered a YAML sequence.
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
pointer data()
Return a pointer to the vector's buffer, even if empty().
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:453
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)
void mapOptional(const char *Key, T &Val)
Definition YAMLTraits.h:800
virtual bool outputting() const =0
virtual bool mapTag(StringRef Tag, bool Default=false)=0
void mapRequired(const char *Key, T &Val)
Definition YAMLTraits.h:790
#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.
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:474
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.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:305
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
RemarkSerializer(Format SerializerFormat, raw_ostream &OS)
std::optional< StringTable > StrTab
The string table containing all the unique strings used in the output.
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
The string table used for serializing remarks.
void emit(const Remark &Remark) override
Emit a remark to the stream.
yaml::Output YAMLOutput
The YAML streamer.
std::unique_ptr< MetaSerializer > metaSerializer(raw_ostream &OS, StringRef ExternalFilename) override
Return the corresponding metadata serializer.
static void output(const StringBlockVal &S, void *Ctx, raw_ostream &OS)
static StringRef input(StringRef Scalar, void *Ctx, StringBlockVal &S)
This class should be specialized by type that requires custom conversion to/from a YAML literal block...
Definition YAMLTraits.h:179
static void mapping(IO &io, Argument &A)
static void mapping(IO &io, RemarkLocation &RL)
static void mapping(IO &io, remarks::Remark *&Remark)
This class should be specialized by any type that needs to be converted to/from a YAML mapping.
Definition YAMLTraits.h:62
This class should be specialized by type that requires custom conversion to/from a yaml scalar.
Definition YAMLTraits.h:149
static Argument & element(IO &io, ArrayRef< T > &seq, size_t index)
static size_t size(IO &io, ArrayRef< T > &seq)
This class should be specialized by any type that needs to be converted to/from a YAML sequence.
Definition YAMLTraits.h:245
Helper struct for multiline string block literals.