xref: /freebsd/contrib/llvm-project/llvm/lib/Remarks/YAMLRemarkSerializer.cpp (revision 95b4436e989df29f6368f13832cb13d7cbc52eac)
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 
19 using namespace llvm;
20 using 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).
24 static void
25 mapRemarkHeader(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 
36 namespace llvm {
37 namespace yaml {
38 
39 template <> 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 
60     mapRemarkHeader(io, Remark->PassName, Remark->RemarkName, Remark->Loc,
61                     Remark->FunctionName, Remark->Hotness, Remark->Args);
62   }
63 };
64 
65 template <> 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.
84 struct StringBlockVal {
85   StringRef Value;
86   StringBlockVal(StringRef R) : Value(R) {}
87 };
88 
89 template <> 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.
105 template <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.
115 template <> 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 
132 LLVM_YAML_IS_SEQUENCE_VECTOR(Argument)
133 
134 YAMLRemarkSerializer::YAMLRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
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 
141 void YAMLRemarkSerializer::emit(const Remark &Remark) {
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 
148 std::unique_ptr<MetaSerializer> YAMLRemarkSerializer::metaSerializer(
149     raw_ostream &OS, std::optional<StringRef> ExternalFilename) {
150   return std::make_unique<YAMLMetaSerializer>(OS, ExternalFilename);
151 }
152 
153 static void emitMagic(raw_ostream &OS) {
154   // Emit the magic number.
155   OS << remarks::Magic;
156   // Explicitly emit a '\0'.
157   OS.write('\0');
158 }
159 
160 static void emitVersion(raw_ostream &OS) {
161   // Emit the version number: little-endian uint64_t.
162   std::array<char, 8> Version;
163   support::endian::write64le(Version.data(), remarks::CurrentRemarkVersion);
164   OS.write(Version.data(), Version.size());
165 }
166 
167 static 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 
176 void YAMLMetaSerializer::emit() {
177   emitMagic(OS);
178   emitVersion(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());
189   if (ExternalFilename)
190     emitExternalFile(OS, *ExternalFilename);
191 }
192