YAMLRemarkSerializer.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/Support/FileSystem.h"
  15. using namespace llvm;
  16. using namespace llvm::remarks;
  17. // Use the same keys whether we use a string table or not (respectively, T is an
  18. // unsigned or a StringRef).
  19. template <typename T>
  20. static void mapRemarkHeader(yaml::IO &io, T PassName, T RemarkName,
  21. Optional<RemarkLocation> RL, T FunctionName,
  22. Optional<uint64_t> Hotness,
  23. ArrayRef<Argument> Args) {
  24. io.mapRequired("Pass", PassName);
  25. io.mapRequired("Name", RemarkName);
  26. io.mapOptional("DebugLoc", RL);
  27. io.mapRequired("Function", FunctionName);
  28. io.mapOptional("Hotness", Hotness);
  29. io.mapOptional("Args", Args);
  30. }
  31. namespace llvm {
  32. namespace yaml {
  33. template <> struct MappingTraits<remarks::Remark *> {
  34. static void mapping(IO &io, remarks::Remark *&Remark) {
  35. assert(io.outputting() && "input not yet implemented");
  36. if (io.mapTag("!Passed", (Remark->RemarkType == Type::Passed)))
  37. ;
  38. else if (io.mapTag("!Missed", (Remark->RemarkType == Type::Missed)))
  39. ;
  40. else if (io.mapTag("!Analysis", (Remark->RemarkType == Type::Analysis)))
  41. ;
  42. else if (io.mapTag("!AnalysisFPCommute",
  43. (Remark->RemarkType == Type::AnalysisFPCommute)))
  44. ;
  45. else if (io.mapTag("!AnalysisAliasing",
  46. (Remark->RemarkType == Type::AnalysisAliasing)))
  47. ;
  48. else if (io.mapTag("!Failure", (Remark->RemarkType == Type::Failure)))
  49. ;
  50. else
  51. llvm_unreachable("Unknown remark type");
  52. if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
  53. reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
  54. assert(Serializer->StrTab.hasValue() &&
  55. "YAMLStrTabSerializer with no StrTab.");
  56. StringTable &StrTab = *Serializer->StrTab;
  57. unsigned PassID = StrTab.add(Remark->PassName).first;
  58. unsigned NameID = StrTab.add(Remark->RemarkName).first;
  59. unsigned FunctionID = StrTab.add(Remark->FunctionName).first;
  60. mapRemarkHeader(io, PassID, NameID, Remark->Loc, FunctionID,
  61. Remark->Hotness, Remark->Args);
  62. } else {
  63. mapRemarkHeader(io, Remark->PassName, Remark->RemarkName, Remark->Loc,
  64. Remark->FunctionName, Remark->Hotness, Remark->Args);
  65. }
  66. }
  67. };
  68. template <> struct MappingTraits<RemarkLocation> {
  69. static void mapping(IO &io, RemarkLocation &RL) {
  70. assert(io.outputting() && "input not yet implemented");
  71. StringRef File = RL.SourceFilePath;
  72. unsigned Line = RL.SourceLine;
  73. unsigned Col = RL.SourceColumn;
  74. if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
  75. reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
  76. assert(Serializer->StrTab.hasValue() &&
  77. "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.hasValue() &&
  124. "YAMLStrTabSerializer with no StrTab.");
  125. StringTable &StrTab = *Serializer->StrTab;
  126. auto ValueID = StrTab.add(A.Val).first;
  127. io.mapRequired(A.Key.data(), ValueID);
  128. } else if (StringRef(A.Val).count('\n') > 1) {
  129. StringBlockVal S(A.Val);
  130. io.mapRequired(A.Key.data(), S);
  131. } else {
  132. io.mapRequired(A.Key.data(), A.Val);
  133. }
  134. io.mapOptional("DebugLoc", A.Loc);
  135. }
  136. };
  137. } // end namespace yaml
  138. } // end namespace llvm
  139. LLVM_YAML_IS_SEQUENCE_VECTOR(Argument)
  140. YAMLRemarkSerializer::YAMLRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
  141. Optional<StringTable> StrTabIn)
  142. : YAMLRemarkSerializer(Format::YAML, OS, Mode, std::move(StrTabIn)) {}
  143. YAMLRemarkSerializer::YAMLRemarkSerializer(Format SerializerFormat,
  144. raw_ostream &OS, SerializerMode Mode,
  145. Optional<StringTable> StrTabIn)
  146. : RemarkSerializer(SerializerFormat, OS, Mode),
  147. YAMLOutput(OS, reinterpret_cast<void *>(this)) {
  148. StrTab = std::move(StrTabIn);
  149. }
  150. void YAMLRemarkSerializer::emit(const Remark &Remark) {
  151. // Again, YAMLTraits expect a non-const object for inputting, but we're not
  152. // using that here.
  153. auto R = const_cast<remarks::Remark *>(&Remark);
  154. YAMLOutput << R;
  155. }
  156. std::unique_ptr<MetaSerializer>
  157. YAMLRemarkSerializer::metaSerializer(raw_ostream &OS,
  158. Optional<StringRef> ExternalFilename) {
  159. return std::make_unique<YAMLMetaSerializer>(OS, ExternalFilename);
  160. }
  161. void YAMLStrTabRemarkSerializer::emit(const Remark &Remark) {
  162. // In standalone mode, for the serializer with a string table, emit the
  163. // metadata first and set DidEmitMeta to avoid emitting it again.
  164. if (Mode == SerializerMode::Standalone && !DidEmitMeta) {
  165. std::unique_ptr<MetaSerializer> MetaSerializer =
  166. metaSerializer(OS, /*ExternalFilename=*/None);
  167. MetaSerializer->emit();
  168. DidEmitMeta = true;
  169. }
  170. // Then do the usual remark emission.
  171. YAMLRemarkSerializer::emit(Remark);
  172. }
  173. std::unique_ptr<MetaSerializer> YAMLStrTabRemarkSerializer::metaSerializer(
  174. raw_ostream &OS, Optional<StringRef> ExternalFilename) {
  175. assert(StrTab);
  176. return std::make_unique<YAMLStrTabMetaSerializer>(OS, ExternalFilename,
  177. *StrTab);
  178. }
  179. static void emitMagic(raw_ostream &OS) {
  180. // Emit the magic number.
  181. OS << remarks::Magic;
  182. // Explicitly emit a '\0'.
  183. OS.write('\0');
  184. }
  185. static void emitVersion(raw_ostream &OS) {
  186. // Emit the version number: little-endian uint64_t.
  187. std::array<char, 8> Version;
  188. support::endian::write64le(Version.data(), remarks::CurrentRemarkVersion);
  189. OS.write(Version.data(), Version.size());
  190. }
  191. static void emitStrTab(raw_ostream &OS, Optional<const StringTable *> StrTab) {
  192. // Emit the string table in the section.
  193. uint64_t StrTabSize = StrTab ? (*StrTab)->SerializedSize : 0;
  194. // Emit the total size of the string table (the size itself excluded):
  195. // little-endian uint64_t.
  196. // Note: even if no string table is used, emit 0.
  197. std::array<char, 8> StrTabSizeBuf;
  198. support::endian::write64le(StrTabSizeBuf.data(), StrTabSize);
  199. OS.write(StrTabSizeBuf.data(), StrTabSizeBuf.size());
  200. if (StrTab)
  201. (*StrTab)->serialize(OS);
  202. }
  203. static void emitExternalFile(raw_ostream &OS, StringRef Filename) {
  204. // Emit the null-terminated absolute path to the remark file.
  205. SmallString<128> FilenameBuf = Filename;
  206. sys::fs::make_absolute(FilenameBuf);
  207. assert(!FilenameBuf.empty() && "The filename can't be empty.");
  208. OS.write(FilenameBuf.data(), FilenameBuf.size());
  209. OS.write('\0');
  210. }
  211. void YAMLMetaSerializer::emit() {
  212. emitMagic(OS);
  213. emitVersion(OS);
  214. emitStrTab(OS, None);
  215. if (ExternalFilename)
  216. emitExternalFile(OS, *ExternalFilename);
  217. }
  218. void YAMLStrTabMetaSerializer::emit() {
  219. emitMagic(OS);
  220. emitVersion(OS);
  221. emitStrTab(OS, &StrTab);
  222. if (ExternalFilename)
  223. emitExternalFile(OS, *ExternalFilename);
  224. }