YAMLRemarkSerializer.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #include "llvm/Remarks/YAMLRemarkSerializer.h"
  14. #include "llvm/Remarks/Remark.h"
  15. #include "llvm/Support/FileSystem.h"
  16. #include <optional>
  17. using namespace llvm;
  18. using namespace llvm::remarks;
  19. // Use the same keys whether we use a string table or not (respectively, T is an
  20. // unsigned or a StringRef).
  21. template <typename T>
  22. static void mapRemarkHeader(yaml::IO &io, T PassName, T RemarkName,
  23. std::optional<RemarkLocation> RL, T FunctionName,
  24. std::optional<uint64_t> Hotness,
  25. 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. namespace llvm {
  34. namespace yaml {
  35. template <> struct MappingTraits<remarks::Remark *> {
  36. static void mapping(IO &io, remarks::Remark *&Remark) {
  37. assert(io.outputting() && "input not yet implemented");
  38. if (io.mapTag("!Passed", (Remark->RemarkType == Type::Passed)))
  39. ;
  40. else if (io.mapTag("!Missed", (Remark->RemarkType == Type::Missed)))
  41. ;
  42. else if (io.mapTag("!Analysis", (Remark->RemarkType == Type::Analysis)))
  43. ;
  44. else if (io.mapTag("!AnalysisFPCommute",
  45. (Remark->RemarkType == Type::AnalysisFPCommute)))
  46. ;
  47. else if (io.mapTag("!AnalysisAliasing",
  48. (Remark->RemarkType == Type::AnalysisAliasing)))
  49. ;
  50. else if (io.mapTag("!Failure", (Remark->RemarkType == Type::Failure)))
  51. ;
  52. else
  53. llvm_unreachable("Unknown remark type");
  54. if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
  55. reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
  56. assert(Serializer->StrTab && "YAMLStrTabSerializer with no StrTab.");
  57. StringTable &StrTab = *Serializer->StrTab;
  58. unsigned PassID = StrTab.add(Remark->PassName).first;
  59. unsigned NameID = StrTab.add(Remark->RemarkName).first;
  60. unsigned FunctionID = StrTab.add(Remark->FunctionName).first;
  61. mapRemarkHeader(io, PassID, NameID, Remark->Loc, FunctionID,
  62. Remark->Hotness, Remark->Args);
  63. } else {
  64. mapRemarkHeader(io, Remark->PassName, Remark->RemarkName, Remark->Loc,
  65. Remark->FunctionName, Remark->Hotness, Remark->Args);
  66. }
  67. }
  68. };
  69. template <> struct MappingTraits<RemarkLocation> {
  70. static void mapping(IO &io, RemarkLocation &RL) {
  71. assert(io.outputting() && "input not yet implemented");
  72. StringRef File = RL.SourceFilePath;
  73. unsigned Line = RL.SourceLine;
  74. unsigned Col = RL.SourceColumn;
  75. if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
  76. reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
  77. assert(Serializer->StrTab && "YAMLStrTabSerializer with no StrTab.");
  78. StringTable &StrTab = *Serializer->StrTab;
  79. unsigned FileID = StrTab.add(File).first;
  80. io.mapRequired("File", FileID);
  81. } else {
  82. io.mapRequired("File", File);
  83. }
  84. io.mapRequired("Line", Line);
  85. io.mapRequired("Column", Col);
  86. }
  87. static const bool flow = true;
  88. };
  89. /// Helper struct for multiline string block literals. Use this type to preserve
  90. /// newlines in strings.
  91. struct StringBlockVal {
  92. StringRef Value;
  93. StringBlockVal(StringRef R) : Value(R) {}
  94. };
  95. template <> struct BlockScalarTraits<StringBlockVal> {
  96. static void output(const StringBlockVal &S, void *Ctx, raw_ostream &OS) {
  97. return ScalarTraits<StringRef>::output(S.Value, Ctx, OS);
  98. }
  99. static StringRef input(StringRef Scalar, void *Ctx, StringBlockVal &S) {
  100. return ScalarTraits<StringRef>::input(Scalar, Ctx, S.Value);
  101. }
  102. };
  103. /// ArrayRef is not really compatible with the YAMLTraits. Everything should be
  104. /// immutable in an ArrayRef, while the SequenceTraits expect a mutable version
  105. /// for inputting, but we're only using the outputting capabilities here.
  106. /// This is a hack, but still nicer than having to manually call the YAMLIO
  107. /// internal methods.
  108. /// Keep this in this file so that it doesn't get misused from YAMLTraits.h.
  109. template <typename T> struct SequenceTraits<ArrayRef<T>> {
  110. static size_t size(IO &io, ArrayRef<T> &seq) { return seq.size(); }
  111. static Argument &element(IO &io, ArrayRef<T> &seq, size_t index) {
  112. assert(io.outputting() && "input not yet implemented");
  113. // The assert above should make this "safer" to satisfy the YAMLTraits.
  114. return const_cast<T &>(seq[index]);
  115. }
  116. };
  117. /// Implement this as a mapping for now to get proper quotation for the value.
  118. template <> struct MappingTraits<Argument> {
  119. static void mapping(IO &io, Argument &A) {
  120. assert(io.outputting() && "input not yet implemented");
  121. if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
  122. reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
  123. assert(Serializer->StrTab && "YAMLStrTabSerializer with no StrTab.");
  124. StringTable &StrTab = *Serializer->StrTab;
  125. auto ValueID = StrTab.add(A.Val).first;
  126. io.mapRequired(A.Key.data(), ValueID);
  127. } else if (StringRef(A.Val).count('\n') > 1) {
  128. StringBlockVal S(A.Val);
  129. io.mapRequired(A.Key.data(), S);
  130. } else {
  131. io.mapRequired(A.Key.data(), A.Val);
  132. }
  133. io.mapOptional("DebugLoc", A.Loc);
  134. }
  135. };
  136. } // end namespace yaml
  137. } // end namespace llvm
  138. LLVM_YAML_IS_SEQUENCE_VECTOR(Argument)
  139. YAMLRemarkSerializer::YAMLRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
  140. std::optional<StringTable> StrTabIn)
  141. : YAMLRemarkSerializer(Format::YAML, OS, Mode, std::move(StrTabIn)) {}
  142. YAMLRemarkSerializer::YAMLRemarkSerializer(Format SerializerFormat,
  143. raw_ostream &OS, SerializerMode Mode,
  144. std::optional<StringTable> StrTabIn)
  145. : RemarkSerializer(SerializerFormat, OS, Mode),
  146. YAMLOutput(OS, reinterpret_cast<void *>(this)) {
  147. StrTab = std::move(StrTabIn);
  148. }
  149. void YAMLRemarkSerializer::emit(const Remark &Remark) {
  150. // Again, YAMLTraits expect a non-const object for inputting, but we're not
  151. // using that here.
  152. auto R = const_cast<remarks::Remark *>(&Remark);
  153. YAMLOutput << R;
  154. }
  155. std::unique_ptr<MetaSerializer> YAMLRemarkSerializer::metaSerializer(
  156. raw_ostream &OS, std::optional<StringRef> ExternalFilename) {
  157. return std::make_unique<YAMLMetaSerializer>(OS, ExternalFilename);
  158. }
  159. void YAMLStrTabRemarkSerializer::emit(const Remark &Remark) {
  160. // In standalone mode, for the serializer with a string table, emit the
  161. // metadata first and set DidEmitMeta to avoid emitting it again.
  162. if (Mode == SerializerMode::Standalone && !DidEmitMeta) {
  163. std::unique_ptr<MetaSerializer> MetaSerializer =
  164. metaSerializer(OS, /*ExternalFilename=*/std::nullopt);
  165. MetaSerializer->emit();
  166. DidEmitMeta = true;
  167. }
  168. // Then do the usual remark emission.
  169. YAMLRemarkSerializer::emit(Remark);
  170. }
  171. std::unique_ptr<MetaSerializer> YAMLStrTabRemarkSerializer::metaSerializer(
  172. raw_ostream &OS, std::optional<StringRef> ExternalFilename) {
  173. assert(StrTab);
  174. return std::make_unique<YAMLStrTabMetaSerializer>(OS, ExternalFilename,
  175. *StrTab);
  176. }
  177. static void emitMagic(raw_ostream &OS) {
  178. // Emit the magic number.
  179. OS << remarks::Magic;
  180. // Explicitly emit a '\0'.
  181. OS.write('\0');
  182. }
  183. static void emitVersion(raw_ostream &OS) {
  184. // Emit the version number: little-endian uint64_t.
  185. std::array<char, 8> Version;
  186. support::endian::write64le(Version.data(), remarks::CurrentRemarkVersion);
  187. OS.write(Version.data(), Version.size());
  188. }
  189. static void emitStrTab(raw_ostream &OS,
  190. std::optional<const StringTable *> StrTab) {
  191. // Emit the string table in the section.
  192. uint64_t StrTabSize = StrTab ? (*StrTab)->SerializedSize : 0;
  193. // Emit the total size of the string table (the size itself excluded):
  194. // little-endian uint64_t.
  195. // Note: even if no string table is used, emit 0.
  196. std::array<char, 8> StrTabSizeBuf;
  197. support::endian::write64le(StrTabSizeBuf.data(), StrTabSize);
  198. OS.write(StrTabSizeBuf.data(), StrTabSizeBuf.size());
  199. if (StrTab)
  200. (*StrTab)->serialize(OS);
  201. }
  202. static void emitExternalFile(raw_ostream &OS, StringRef Filename) {
  203. // Emit the null-terminated absolute path to the remark file.
  204. SmallString<128> FilenameBuf = Filename;
  205. sys::fs::make_absolute(FilenameBuf);
  206. assert(!FilenameBuf.empty() && "The filename can't be empty.");
  207. OS.write(FilenameBuf.data(), FilenameBuf.size());
  208. OS.write('\0');
  209. }
  210. void YAMLMetaSerializer::emit() {
  211. emitMagic(OS);
  212. emitVersion(OS);
  213. emitStrTab(OS, std::nullopt);
  214. if (ExternalFilename)
  215. emitExternalFile(OS, *ExternalFilename);
  216. }
  217. void YAMLStrTabMetaSerializer::emit() {
  218. emitMagic(OS);
  219. emitVersion(OS);
  220. emitStrTab(OS, &StrTab);
  221. if (ExternalFilename)
  222. emitExternalFile(OS, *ExternalFilename);
  223. }