blob: 3a42fe0678eb1ad443c3b6e550485568043e9daf [file] [log] [blame]
Francis Visoiu Mistrih6ada11f2019-05-30 21:45:59 +00001//===- 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
Francis Visoiu Mistrihcbbdc412019-07-23 19:28:03 +000014#include "llvm/Remarks/YAMLRemarkSerializer.h"
Francis Visoiu Mistrih6ada11f2019-05-30 21:45:59 +000015#include "llvm/Support/CommandLine.h"
16
17using namespace llvm;
18using namespace llvm::remarks;
19
Francis Visoiu Mistrih6ada11f2019-05-30 21:45:59 +000020// Use the same keys whether we use a string table or not (respectively, T is an
21// unsigned or a StringRef).
22template <typename T>
23static void mapRemarkHeader(yaml::IO &io, T PassName, T RemarkName,
24 Optional<RemarkLocation> RL, T FunctionName,
25 Optional<uint64_t> Hotness,
26 ArrayRef<Argument> Args) {
27 io.mapRequired("Pass", PassName);
28 io.mapRequired("Name", RemarkName);
29 io.mapOptional("DebugLoc", RL);
30 io.mapRequired("Function", FunctionName);
31 io.mapOptional("Hotness", Hotness);
32 io.mapOptional("Args", Args);
33}
34
35namespace llvm {
36namespace yaml {
37
38template <> struct MappingTraits<remarks::Remark *> {
39 static void mapping(IO &io, remarks::Remark *&Remark) {
40 assert(io.outputting() && "input not yet implemented");
41
42 if (io.mapTag("!Passed", (Remark->RemarkType == Type::Passed)))
43 ;
44 else if (io.mapTag("!Missed", (Remark->RemarkType == Type::Missed)))
45 ;
46 else if (io.mapTag("!Analysis", (Remark->RemarkType == Type::Analysis)))
47 ;
48 else if (io.mapTag("!AnalysisFPCommute",
49 (Remark->RemarkType == Type::AnalysisFPCommute)))
50 ;
51 else if (io.mapTag("!AnalysisAliasing",
52 (Remark->RemarkType == Type::AnalysisAliasing)))
53 ;
54 else if (io.mapTag("!Failure", (Remark->RemarkType == Type::Failure)))
55 ;
56 else
57 llvm_unreachable("Unknown remark type");
58
Francis Visoiu Mistrih77383d82019-09-16 22:45:17 +000059 if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
60 reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
61 assert(Serializer->StrTab.hasValue() &&
62 "YAMLStrTabSerializer with no StrTab.");
63 StringTable &StrTab = *Serializer->StrTab;
64 unsigned PassID = StrTab.add(Remark->PassName).first;
65 unsigned NameID = StrTab.add(Remark->RemarkName).first;
66 unsigned FunctionID = StrTab.add(Remark->FunctionName).first;
Francis Visoiu Mistrih6ada11f2019-05-30 21:45:59 +000067 mapRemarkHeader(io, PassID, NameID, Remark->Loc, FunctionID,
68 Remark->Hotness, Remark->Args);
69 } else {
70 mapRemarkHeader(io, Remark->PassName, Remark->RemarkName, Remark->Loc,
71 Remark->FunctionName, Remark->Hotness, Remark->Args);
72 }
73 }
74};
75
76template <> struct MappingTraits<RemarkLocation> {
77 static void mapping(IO &io, RemarkLocation &RL) {
78 assert(io.outputting() && "input not yet implemented");
79
80 StringRef File = RL.SourceFilePath;
81 unsigned Line = RL.SourceLine;
82 unsigned Col = RL.SourceColumn;
83
Francis Visoiu Mistrih77383d82019-09-16 22:45:17 +000084 if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
85 reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
86 assert(Serializer->StrTab.hasValue() &&
87 "YAMLStrTabSerializer with no StrTab.");
88 StringTable &StrTab = *Serializer->StrTab;
89 unsigned FileID = StrTab.add(File).first;
Francis Visoiu Mistrih6ada11f2019-05-30 21:45:59 +000090 io.mapRequired("File", FileID);
91 } else {
92 io.mapRequired("File", File);
93 }
94
95 io.mapRequired("Line", Line);
96 io.mapRequired("Column", Col);
97 }
98
99 static const bool flow = true;
100};
101
102/// Helper struct for multiline string block literals. Use this type to preserve
103/// newlines in strings.
104struct StringBlockVal {
105 StringRef Value;
Florian Hahn90b7dc92019-10-07 17:05:09 +0000106 StringBlockVal(StringRef R) : Value(R) {}
Francis Visoiu Mistrih6ada11f2019-05-30 21:45:59 +0000107};
108
109template <> struct BlockScalarTraits<StringBlockVal> {
110 static void output(const StringBlockVal &S, void *Ctx, raw_ostream &OS) {
111 return ScalarTraits<StringRef>::output(S.Value, Ctx, OS);
112 }
113
114 static StringRef input(StringRef Scalar, void *Ctx, StringBlockVal &S) {
115 return ScalarTraits<StringRef>::input(Scalar, Ctx, S.Value);
116 }
117};
118
119/// ArrayRef is not really compatible with the YAMLTraits. Everything should be
120/// immutable in an ArrayRef, while the SequenceTraits expect a mutable version
121/// for inputting, but we're only using the outputting capabilities here.
122/// This is a hack, but still nicer than having to manually call the YAMLIO
123/// internal methods.
124/// Keep this in this file so that it doesn't get misused from YAMLTraits.h.
125template <typename T> struct SequenceTraits<ArrayRef<T>> {
126 static size_t size(IO &io, ArrayRef<T> &seq) { return seq.size(); }
127 static Argument &element(IO &io, ArrayRef<T> &seq, size_t index) {
128 assert(io.outputting() && "input not yet implemented");
129 // The assert above should make this "safer" to satisfy the YAMLTraits.
130 return const_cast<T &>(seq[index]);
131 }
132};
133
134/// Implement this as a mapping for now to get proper quotation for the value.
135template <> struct MappingTraits<Argument> {
136 static void mapping(IO &io, Argument &A) {
137 assert(io.outputting() && "input not yet implemented");
138
Francis Visoiu Mistrih77383d82019-09-16 22:45:17 +0000139 if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
140 reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
141 assert(Serializer->StrTab.hasValue() &&
142 "YAMLStrTabSerializer with no StrTab.");
143 StringTable &StrTab = *Serializer->StrTab;
144 auto ValueID = StrTab.add(A.Val).first;
Francis Visoiu Mistrih6ada11f2019-05-30 21:45:59 +0000145 io.mapRequired(A.Key.data(), ValueID);
146 } else if (StringRef(A.Val).count('\n') > 1) {
147 StringBlockVal S(A.Val);
148 io.mapRequired(A.Key.data(), S);
149 } else {
150 io.mapRequired(A.Key.data(), A.Val);
151 }
152 io.mapOptional("DebugLoc", A.Loc);
153 }
154};
155
156} // end namespace yaml
157} // end namespace llvm
158
159LLVM_YAML_IS_SEQUENCE_VECTOR(Argument)
160
Francis Visoiu Mistrih77383d82019-09-16 22:45:17 +0000161YAMLRemarkSerializer::YAMLRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
162 Optional<StringTable> StrTabIn)
163 : YAMLRemarkSerializer(Format::YAML, OS, Mode, std::move(StrTabIn)) {}
164
165YAMLRemarkSerializer::YAMLRemarkSerializer(Format SerializerFormat,
166 raw_ostream &OS, SerializerMode Mode,
167 Optional<StringTable> StrTabIn)
168 : RemarkSerializer(SerializerFormat, OS, Mode),
169 YAMLOutput(OS, reinterpret_cast<void *>(this)) {
170 StrTab = std::move(StrTabIn);
171}
Francis Visoiu Mistrih6ada11f2019-05-30 21:45:59 +0000172
Francis Visoiu Mistrihff4b5152019-07-24 19:47:57 +0000173void YAMLRemarkSerializer::emit(const Remark &Remark) {
Francis Visoiu Mistrih6ada11f2019-05-30 21:45:59 +0000174 // Again, YAMLTraits expect a non-const object for inputting, but we're not
175 // using that here.
176 auto R = const_cast<remarks::Remark *>(&Remark);
177 YAMLOutput << R;
178}
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +0000179
180std::unique_ptr<MetaSerializer>
181YAMLRemarkSerializer::metaSerializer(raw_ostream &OS,
182 Optional<StringRef> ExternalFilename) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000183 return std::make_unique<YAMLMetaSerializer>(OS, ExternalFilename);
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +0000184}
185
Francis Visoiu Mistrih07f967d2019-09-05 18:30:20 +0000186void YAMLStrTabRemarkSerializer::emit(const Remark &Remark) {
187 // In standalone mode, for the serializer with a string table, emit the
188 // metadata first and set DidEmitMeta to avoid emitting it again.
189 if (Mode == SerializerMode::Standalone && !DidEmitMeta) {
190 std::unique_ptr<MetaSerializer> MetaSerializer =
191 metaSerializer(OS, /*ExternalFilename=*/None);
192 MetaSerializer->emit();
193 DidEmitMeta = true;
194 }
195
196 // Then do the usual remark emission.
197 YAMLRemarkSerializer::emit(Remark);
198}
199
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +0000200std::unique_ptr<MetaSerializer> YAMLStrTabRemarkSerializer::metaSerializer(
201 raw_ostream &OS, Optional<StringRef> ExternalFilename) {
202 assert(StrTab);
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000203 return std::make_unique<YAMLStrTabMetaSerializer>(OS, ExternalFilename,
Francis Visoiu Mistrih77383d82019-09-16 22:45:17 +0000204 *StrTab);
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +0000205}
206
207static void emitMagic(raw_ostream &OS) {
208 // Emit the magic number.
209 OS << remarks::Magic;
210 // Explicitly emit a '\0'.
211 OS.write('\0');
212}
213
214static void emitVersion(raw_ostream &OS) {
215 // Emit the version number: little-endian uint64_t.
216 std::array<char, 8> Version;
Francis Visoiu Mistrih84e80972019-07-31 00:13:51 +0000217 support::endian::write64le(Version.data(), remarks::CurrentRemarkVersion);
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +0000218 OS.write(Version.data(), Version.size());
219}
220
Francis Visoiu Mistrih77383d82019-09-16 22:45:17 +0000221static void emitStrTab(raw_ostream &OS, Optional<const StringTable *> StrTab) {
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +0000222 // Emit the string table in the section.
Francis Visoiu Mistrih77383d82019-09-16 22:45:17 +0000223 uint64_t StrTabSize = StrTab ? (*StrTab)->SerializedSize : 0;
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +0000224 // Emit the total size of the string table (the size itself excluded):
225 // little-endian uint64_t.
226 // Note: even if no string table is used, emit 0.
227 std::array<char, 8> StrTabSizeBuf;
228 support::endian::write64le(StrTabSizeBuf.data(), StrTabSize);
229 OS.write(StrTabSizeBuf.data(), StrTabSizeBuf.size());
230 if (StrTab)
Francis Visoiu Mistrih77383d82019-09-16 22:45:17 +0000231 (*StrTab)->serialize(OS);
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +0000232}
233
234static void emitExternalFile(raw_ostream &OS, StringRef Filename) {
235 // Emit the null-terminated absolute path to the remark file.
236 SmallString<128> FilenameBuf = Filename;
237 sys::fs::make_absolute(FilenameBuf);
238 assert(!FilenameBuf.empty() && "The filename can't be empty.");
239 OS.write(FilenameBuf.data(), FilenameBuf.size());
240 OS.write('\0');
241}
242
243void YAMLMetaSerializer::emit() {
244 emitMagic(OS);
245 emitVersion(OS);
246 emitStrTab(OS, None);
247 if (ExternalFilename)
248 emitExternalFile(OS, *ExternalFilename);
249}
250
251void YAMLStrTabMetaSerializer::emit() {
252 emitMagic(OS);
253 emitVersion(OS);
Francis Visoiu Mistrih77383d82019-09-16 22:45:17 +0000254 emitStrTab(OS, &StrTab);
Francis Visoiu Mistrih2d8fdca2019-07-26 01:33:30 +0000255 if (ExternalFilename)
256 emitExternalFile(OS, *ExternalFilename);
257}