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
14#include "llvm/Remarks/YAMLRemarkSerializer.h"
15#include "llvm/Remarks/Remark.h"
16#include "llvm/Support/FileSystem.h"
17#include <optional>
18
19using namespace llvm;
20using namespace llvm::remarks;
21
22static void
23mapRemarkHeader(yaml::IO &io, StringRef PassName, StringRef RemarkName,
24 std::optional<RemarkLocation> RL, StringRef FunctionName,
25 std::optional<uint64_t> Hotness, ArrayRef<Argument> Args) {
26 io.mapRequired(Key: "Pass", Val&: PassName);
27 io.mapRequired(Key: "Name", Val&: RemarkName);
28 io.mapOptional(Key: "DebugLoc", Val&: RL);
29 io.mapRequired(Key: "Function", Val&: FunctionName);
30 io.mapOptional(Key: "Hotness", Val&: Hotness);
31 io.mapOptional(Key: "Args", Val&: 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(Tag: "!Passed", Default: (Remark->RemarkType == Type::Passed)))
42 ;
43 else if (io.mapTag(Tag: "!Missed", Default: (Remark->RemarkType == Type::Missed)))
44 ;
45 else if (io.mapTag(Tag: "!Analysis", Default: (Remark->RemarkType == Type::Analysis)))
46 ;
47 else if (io.mapTag(Tag: "!AnalysisFPCommute",
48 Default: (Remark->RemarkType == Type::AnalysisFPCommute)))
49 ;
50 else if (io.mapTag(Tag: "!AnalysisAliasing",
51 Default: (Remark->RemarkType == Type::AnalysisAliasing)))
52 ;
53 else if (io.mapTag(Tag: "!Failure", Default: (Remark->RemarkType == Type::Failure)))
54 ;
55 else
56 llvm_unreachable("Unknown remark type");
57
58 mapRemarkHeader(io, PassName: Remark->PassName, RemarkName: Remark->RemarkName, RL: Remark->Loc,
59 FunctionName: Remark->FunctionName, Hotness: Remark->Hotness, Args: 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(Key: "File", Val&: File);
72
73 io.mapRequired(Key: "Line", Val&: Line);
74 io.mapRequired(Key: "Column", Val&: 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.
82struct StringBlockVal {
83 StringRef Value;
84 StringBlockVal(StringRef R) : Value(R) {}
85};
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
92 static StringRef input(StringRef Scalar, void *Ctx, StringBlockVal &S) {
93 return ScalarTraits<StringRef>::input(Scalar, Ctx, S.Value);
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 // NB: A.Key.data() is not necessarily null-terminated, as the StringRef may
118 // be a span into the middle of a string.
119 if (StringRef(A.Val).count(C: '\n') > 1) {
120 StringBlockVal S(A.Val);
121 io.mapRequired(Key: A.Key, Val&: S);
122 } else {
123 io.mapRequired(Key: A.Key, Val&: A.Val);
124 }
125 io.mapOptional(Key: "DebugLoc", Val&: A.Loc);
126 }
127};
128
129} // end namespace yaml
130} // end namespace llvm
131
132LLVM_YAML_IS_SEQUENCE_VECTOR(Argument)
133
134YAMLRemarkSerializer::YAMLRemarkSerializer(raw_ostream &OS)
135 : RemarkSerializer(Format::YAML, OS),
136 YAMLOutput(OS, reinterpret_cast<void *>(this)) {}
137
138YAMLRemarkSerializer::YAMLRemarkSerializer(raw_ostream &OS,
139 StringTable StrTabIn)
140 : YAMLRemarkSerializer(OS) {
141 StrTab = std::move(StrTabIn);
142}
143
144void YAMLRemarkSerializer::emit(const Remark &Remark) {
145 // Again, YAMLTraits expect a non-const object for inputting, but we're not
146 // using that here.
147 auto *R = const_cast<remarks::Remark *>(&Remark);
148 YAMLOutput << R;
149}
150
151std::unique_ptr<MetaSerializer>
152YAMLRemarkSerializer::metaSerializer(raw_ostream &OS,
153 StringRef ExternalFilename) {
154 return std::make_unique<YAMLMetaSerializer>(args&: OS, args&: ExternalFilename);
155}
156
157static void emitMagic(raw_ostream &OS) {
158 // Emit the magic number.
159 OS << remarks::Magic;
160 // Explicitly emit a '\0'.
161 OS.write(C: '\0');
162}
163
164static void emitVersion(raw_ostream &OS) {
165 // Emit the version number: little-endian uint64_t.
166 std::array<char, 8> Version;
167 support::endian::write64le(P: Version.data(), V: remarks::CurrentRemarkVersion);
168 OS.write(Ptr: Version.data(), Size: Version.size());
169}
170
171static void emitExternalFile(raw_ostream &OS, StringRef Filename) {
172 // Emit the null-terminated absolute path to the remark file.
173 SmallString<128> FilenameBuf = Filename;
174 sys::fs::make_absolute(path&: FilenameBuf);
175 assert(!FilenameBuf.empty() && "The filename can't be empty.");
176 OS.write(Ptr: FilenameBuf.data(), Size: FilenameBuf.size());
177 OS.write(C: '\0');
178}
179
180void YAMLMetaSerializer::emit() {
181 emitMagic(OS);
182 emitVersion(OS);
183
184 // Emit StringTable with size 0. This is left over after removing StringTable
185 // support from the YAML format. For now, don't unnecessarily change how the
186 // the metadata is serialized. When changing the format, we should think about
187 // just reusing the bitstream remark meta for this.
188 uint64_t StrTabSize = 0;
189 std::array<char, 8> StrTabSizeBuf;
190 support::endian::write64le(P: StrTabSizeBuf.data(), V: StrTabSize);
191
192 OS.write(Ptr: StrTabSizeBuf.data(), Size: StrTabSizeBuf.size());
193 emitExternalFile(OS, Filename: ExternalFilename);
194}
195