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/RemarkSerializer.h"
15 #include "llvm/Support/CommandLine.h"
16 
17 using namespace llvm;
18 using namespace llvm::remarks;
19 
20 cl::opt<bool> RemarksYAMLStringTable("remarks-yaml-string-table",
21                                      cl::init(false));
22 
23 // Use the same keys whether we use a string table or not (respectively, T is an
24 // unsigned or a StringRef).
25 template <typename T>
26 static void mapRemarkHeader(yaml::IO &io, T PassName, T RemarkName,
27                             Optional<RemarkLocation> RL, T FunctionName,
28                             Optional<uint64_t> Hotness,
29                             ArrayRef<Argument> Args) {
30   io.mapRequired("Pass", PassName);
31   io.mapRequired("Name", RemarkName);
32   io.mapOptional("DebugLoc", RL);
33   io.mapRequired("Function", FunctionName);
34   io.mapOptional("Hotness", Hotness);
35   io.mapOptional("Args", Args);
36 }
37 
38 namespace llvm {
39 namespace yaml {
40 
41 template <> struct MappingTraits<remarks::Remark *> {
42   static void mapping(IO &io, remarks::Remark *&Remark) {
43     assert(io.outputting() && "input not yet implemented");
44 
45     if (io.mapTag("!Passed", (Remark->RemarkType == Type::Passed)))
46       ;
47     else if (io.mapTag("!Missed", (Remark->RemarkType == Type::Missed)))
48       ;
49     else if (io.mapTag("!Analysis", (Remark->RemarkType == Type::Analysis)))
50       ;
51     else if (io.mapTag("!AnalysisFPCommute",
52                        (Remark->RemarkType == Type::AnalysisFPCommute)))
53       ;
54     else if (io.mapTag("!AnalysisAliasing",
55                        (Remark->RemarkType == Type::AnalysisAliasing)))
56       ;
57     else if (io.mapTag("!Failure", (Remark->RemarkType == Type::Failure)))
58       ;
59     else
60       llvm_unreachable("Unknown remark type");
61 
62     if (Optional<StringTable> &StrTab =
63             reinterpret_cast<YAMLSerializer *>(io.getContext())->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;
67       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 
76 template <> 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 
84     if (Optional<StringTable> &StrTab =
85             reinterpret_cast<YAMLSerializer *>(io.getContext())->StrTab) {
86       unsigned FileID = StrTab->add(File).first;
87       io.mapRequired("File", FileID);
88     } else {
89       io.mapRequired("File", File);
90     }
91 
92     io.mapRequired("Line", Line);
93     io.mapRequired("Column", Col);
94   }
95 
96   static const bool flow = true;
97 };
98 
99 /// Helper struct for multiline string block literals. Use this type to preserve
100 /// newlines in strings.
101 struct StringBlockVal {
102   StringRef Value;
103   StringBlockVal(const std::string &Value) : Value(Value) {}
104 };
105 
106 template <> struct BlockScalarTraits<StringBlockVal> {
107   static void output(const StringBlockVal &S, void *Ctx, raw_ostream &OS) {
108     return ScalarTraits<StringRef>::output(S.Value, Ctx, OS);
109   }
110 
111   static StringRef input(StringRef Scalar, void *Ctx, StringBlockVal &S) {
112     return ScalarTraits<StringRef>::input(Scalar, Ctx, S.Value);
113   }
114 };
115 
116 /// ArrayRef is not really compatible with the YAMLTraits. Everything should be
117 /// immutable in an ArrayRef, while the SequenceTraits expect a mutable version
118 /// for inputting, but we're only using the outputting capabilities here.
119 /// This is a hack, but still nicer than having to manually call the YAMLIO
120 /// internal methods.
121 /// Keep this in this file so that it doesn't get misused from YAMLTraits.h.
122 template <typename T> struct SequenceTraits<ArrayRef<T>> {
123   static size_t size(IO &io, ArrayRef<T> &seq) { return seq.size(); }
124   static Argument &element(IO &io, ArrayRef<T> &seq, size_t index) {
125     assert(io.outputting() && "input not yet implemented");
126     // The assert above should make this "safer" to satisfy the YAMLTraits.
127     return const_cast<T &>(seq[index]);
128   }
129 };
130 
131 /// Implement this as a mapping for now to get proper quotation for the value.
132 template <> struct MappingTraits<Argument> {
133   static void mapping(IO &io, Argument &A) {
134     assert(io.outputting() && "input not yet implemented");
135 
136     if (Optional<StringTable> &StrTab =
137             reinterpret_cast<YAMLSerializer *>(io.getContext())->StrTab) {
138       auto ValueID = StrTab->add(A.Val).first;
139       io.mapRequired(A.Key.data(), ValueID);
140     } else if (StringRef(A.Val).count('\n') > 1) {
141       StringBlockVal S(A.Val);
142       io.mapRequired(A.Key.data(), S);
143     } else {
144       io.mapRequired(A.Key.data(), A.Val);
145     }
146     io.mapOptional("DebugLoc", A.Loc);
147   }
148 };
149 
150 } // end namespace yaml
151 } // end namespace llvm
152 
153 LLVM_YAML_IS_SEQUENCE_VECTOR(Argument)
154 
155 YAMLSerializer::YAMLSerializer(raw_ostream &OS, UseStringTable UseStringTable)
156     : Serializer(OS), YAMLOutput(OS, reinterpret_cast<void *>(this)) {
157   if (UseStringTable == remarks::UseStringTable::Yes || RemarksYAMLStringTable)
158     StrTab.emplace();
159 }
160 
161 void YAMLSerializer::emit(const Remark &Remark) {
162   // Again, YAMLTraits expect a non-const object for inputting, but we're not
163   // using that here.
164   auto R = const_cast<remarks::Remark *>(&Remark);
165   YAMLOutput << R;
166 }
167