10b57cec5SDimitry Andric //===- YAMLRemarkSerializer.cpp -------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file provides the implementation of the YAML remark serializer using
100b57cec5SDimitry Andric // LLVM's YAMLTraits.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
148bcb0991SDimitry Andric #include "llvm/Remarks/YAMLRemarkSerializer.h"
1581ad6265SDimitry Andric #include "llvm/Remarks/Remark.h"
16fe6060f1SDimitry Andric #include "llvm/Support/FileSystem.h"
17*bdd1243dSDimitry Andric #include <optional>
180b57cec5SDimitry Andric
190b57cec5SDimitry Andric using namespace llvm;
200b57cec5SDimitry Andric using namespace llvm::remarks;
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric // Use the same keys whether we use a string table or not (respectively, T is an
230b57cec5SDimitry Andric // unsigned or a StringRef).
240b57cec5SDimitry Andric template <typename T>
mapRemarkHeader(yaml::IO & io,T PassName,T RemarkName,std::optional<RemarkLocation> RL,T FunctionName,std::optional<uint64_t> Hotness,ArrayRef<Argument> Args)250b57cec5SDimitry Andric static void mapRemarkHeader(yaml::IO &io, T PassName, T RemarkName,
26*bdd1243dSDimitry Andric std::optional<RemarkLocation> RL, T FunctionName,
27*bdd1243dSDimitry Andric std::optional<uint64_t> Hotness,
280b57cec5SDimitry Andric ArrayRef<Argument> Args) {
290b57cec5SDimitry Andric io.mapRequired("Pass", PassName);
300b57cec5SDimitry Andric io.mapRequired("Name", RemarkName);
310b57cec5SDimitry Andric io.mapOptional("DebugLoc", RL);
320b57cec5SDimitry Andric io.mapRequired("Function", FunctionName);
330b57cec5SDimitry Andric io.mapOptional("Hotness", Hotness);
340b57cec5SDimitry Andric io.mapOptional("Args", Args);
350b57cec5SDimitry Andric }
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric namespace llvm {
380b57cec5SDimitry Andric namespace yaml {
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric template <> struct MappingTraits<remarks::Remark *> {
mappingllvm::yaml::MappingTraits410b57cec5SDimitry Andric static void mapping(IO &io, remarks::Remark *&Remark) {
420b57cec5SDimitry Andric assert(io.outputting() && "input not yet implemented");
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric if (io.mapTag("!Passed", (Remark->RemarkType == Type::Passed)))
450b57cec5SDimitry Andric ;
460b57cec5SDimitry Andric else if (io.mapTag("!Missed", (Remark->RemarkType == Type::Missed)))
470b57cec5SDimitry Andric ;
480b57cec5SDimitry Andric else if (io.mapTag("!Analysis", (Remark->RemarkType == Type::Analysis)))
490b57cec5SDimitry Andric ;
500b57cec5SDimitry Andric else if (io.mapTag("!AnalysisFPCommute",
510b57cec5SDimitry Andric (Remark->RemarkType == Type::AnalysisFPCommute)))
520b57cec5SDimitry Andric ;
530b57cec5SDimitry Andric else if (io.mapTag("!AnalysisAliasing",
540b57cec5SDimitry Andric (Remark->RemarkType == Type::AnalysisAliasing)))
550b57cec5SDimitry Andric ;
560b57cec5SDimitry Andric else if (io.mapTag("!Failure", (Remark->RemarkType == Type::Failure)))
570b57cec5SDimitry Andric ;
580b57cec5SDimitry Andric else
590b57cec5SDimitry Andric llvm_unreachable("Unknown remark type");
600b57cec5SDimitry Andric
618bcb0991SDimitry Andric if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
628bcb0991SDimitry Andric reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
6381ad6265SDimitry Andric assert(Serializer->StrTab && "YAMLStrTabSerializer with no StrTab.");
648bcb0991SDimitry Andric StringTable &StrTab = *Serializer->StrTab;
658bcb0991SDimitry Andric unsigned PassID = StrTab.add(Remark->PassName).first;
668bcb0991SDimitry Andric unsigned NameID = StrTab.add(Remark->RemarkName).first;
678bcb0991SDimitry Andric unsigned FunctionID = StrTab.add(Remark->FunctionName).first;
680b57cec5SDimitry Andric mapRemarkHeader(io, PassID, NameID, Remark->Loc, FunctionID,
690b57cec5SDimitry Andric Remark->Hotness, Remark->Args);
700b57cec5SDimitry Andric } else {
710b57cec5SDimitry Andric mapRemarkHeader(io, Remark->PassName, Remark->RemarkName, Remark->Loc,
720b57cec5SDimitry Andric Remark->FunctionName, Remark->Hotness, Remark->Args);
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric };
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric template <> struct MappingTraits<RemarkLocation> {
mappingllvm::yaml::MappingTraits780b57cec5SDimitry Andric static void mapping(IO &io, RemarkLocation &RL) {
790b57cec5SDimitry Andric assert(io.outputting() && "input not yet implemented");
800b57cec5SDimitry Andric
810b57cec5SDimitry Andric StringRef File = RL.SourceFilePath;
820b57cec5SDimitry Andric unsigned Line = RL.SourceLine;
830b57cec5SDimitry Andric unsigned Col = RL.SourceColumn;
840b57cec5SDimitry Andric
858bcb0991SDimitry Andric if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
868bcb0991SDimitry Andric reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
8781ad6265SDimitry Andric assert(Serializer->StrTab && "YAMLStrTabSerializer with no StrTab.");
888bcb0991SDimitry Andric StringTable &StrTab = *Serializer->StrTab;
898bcb0991SDimitry Andric unsigned FileID = StrTab.add(File).first;
900b57cec5SDimitry Andric io.mapRequired("File", FileID);
910b57cec5SDimitry Andric } else {
920b57cec5SDimitry Andric io.mapRequired("File", File);
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric io.mapRequired("Line", Line);
960b57cec5SDimitry Andric io.mapRequired("Column", Col);
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric static const bool flow = true;
1000b57cec5SDimitry Andric };
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric /// Helper struct for multiline string block literals. Use this type to preserve
1030b57cec5SDimitry Andric /// newlines in strings.
1040b57cec5SDimitry Andric struct StringBlockVal {
1050b57cec5SDimitry Andric StringRef Value;
StringBlockValllvm::yaml::StringBlockVal1068bcb0991SDimitry Andric StringBlockVal(StringRef R) : Value(R) {}
1070b57cec5SDimitry Andric };
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric template <> struct BlockScalarTraits<StringBlockVal> {
outputllvm::yaml::BlockScalarTraits1100b57cec5SDimitry Andric static void output(const StringBlockVal &S, void *Ctx, raw_ostream &OS) {
1110b57cec5SDimitry Andric return ScalarTraits<StringRef>::output(S.Value, Ctx, OS);
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric
inputllvm::yaml::BlockScalarTraits1140b57cec5SDimitry Andric static StringRef input(StringRef Scalar, void *Ctx, StringBlockVal &S) {
1150b57cec5SDimitry Andric return ScalarTraits<StringRef>::input(Scalar, Ctx, S.Value);
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric };
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric /// ArrayRef is not really compatible with the YAMLTraits. Everything should be
1200b57cec5SDimitry Andric /// immutable in an ArrayRef, while the SequenceTraits expect a mutable version
1210b57cec5SDimitry Andric /// for inputting, but we're only using the outputting capabilities here.
1220b57cec5SDimitry Andric /// This is a hack, but still nicer than having to manually call the YAMLIO
1230b57cec5SDimitry Andric /// internal methods.
1240b57cec5SDimitry Andric /// Keep this in this file so that it doesn't get misused from YAMLTraits.h.
1250b57cec5SDimitry Andric template <typename T> struct SequenceTraits<ArrayRef<T>> {
sizellvm::yaml::SequenceTraits1260b57cec5SDimitry Andric static size_t size(IO &io, ArrayRef<T> &seq) { return seq.size(); }
elementllvm::yaml::SequenceTraits1270b57cec5SDimitry Andric static Argument &element(IO &io, ArrayRef<T> &seq, size_t index) {
1280b57cec5SDimitry Andric assert(io.outputting() && "input not yet implemented");
1290b57cec5SDimitry Andric // The assert above should make this "safer" to satisfy the YAMLTraits.
1300b57cec5SDimitry Andric return const_cast<T &>(seq[index]);
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric };
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andric /// Implement this as a mapping for now to get proper quotation for the value.
1350b57cec5SDimitry Andric template <> struct MappingTraits<Argument> {
mappingllvm::yaml::MappingTraits1360b57cec5SDimitry Andric static void mapping(IO &io, Argument &A) {
1370b57cec5SDimitry Andric assert(io.outputting() && "input not yet implemented");
1380b57cec5SDimitry Andric
1398bcb0991SDimitry Andric if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
1408bcb0991SDimitry Andric reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
14181ad6265SDimitry Andric assert(Serializer->StrTab && "YAMLStrTabSerializer with no StrTab.");
1428bcb0991SDimitry Andric StringTable &StrTab = *Serializer->StrTab;
1438bcb0991SDimitry Andric auto ValueID = StrTab.add(A.Val).first;
1440b57cec5SDimitry Andric io.mapRequired(A.Key.data(), ValueID);
1450b57cec5SDimitry Andric } else if (StringRef(A.Val).count('\n') > 1) {
1460b57cec5SDimitry Andric StringBlockVal S(A.Val);
1470b57cec5SDimitry Andric io.mapRequired(A.Key.data(), S);
1480b57cec5SDimitry Andric } else {
1490b57cec5SDimitry Andric io.mapRequired(A.Key.data(), A.Val);
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric io.mapOptional("DebugLoc", A.Loc);
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric };
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric } // end namespace yaml
1560b57cec5SDimitry Andric } // end namespace llvm
1570b57cec5SDimitry Andric
LLVM_YAML_IS_SEQUENCE_VECTOR(Argument)1580b57cec5SDimitry Andric LLVM_YAML_IS_SEQUENCE_VECTOR(Argument)
1590b57cec5SDimitry Andric
1608bcb0991SDimitry Andric YAMLRemarkSerializer::YAMLRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
161*bdd1243dSDimitry Andric std::optional<StringTable> StrTabIn)
1628bcb0991SDimitry Andric : YAMLRemarkSerializer(Format::YAML, OS, Mode, std::move(StrTabIn)) {}
1638bcb0991SDimitry Andric
YAMLRemarkSerializer(Format SerializerFormat,raw_ostream & OS,SerializerMode Mode,std::optional<StringTable> StrTabIn)1648bcb0991SDimitry Andric YAMLRemarkSerializer::YAMLRemarkSerializer(Format SerializerFormat,
1658bcb0991SDimitry Andric raw_ostream &OS, SerializerMode Mode,
166*bdd1243dSDimitry Andric std::optional<StringTable> StrTabIn)
1678bcb0991SDimitry Andric : RemarkSerializer(SerializerFormat, OS, Mode),
1688bcb0991SDimitry Andric YAMLOutput(OS, reinterpret_cast<void *>(this)) {
1698bcb0991SDimitry Andric StrTab = std::move(StrTabIn);
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric
emit(const Remark & Remark)1728bcb0991SDimitry Andric void YAMLRemarkSerializer::emit(const Remark &Remark) {
1730b57cec5SDimitry Andric // Again, YAMLTraits expect a non-const object for inputting, but we're not
1740b57cec5SDimitry Andric // using that here.
1750b57cec5SDimitry Andric auto R = const_cast<remarks::Remark *>(&Remark);
1760b57cec5SDimitry Andric YAMLOutput << R;
1770b57cec5SDimitry Andric }
1788bcb0991SDimitry Andric
metaSerializer(raw_ostream & OS,std::optional<StringRef> ExternalFilename)179*bdd1243dSDimitry Andric std::unique_ptr<MetaSerializer> YAMLRemarkSerializer::metaSerializer(
180*bdd1243dSDimitry Andric raw_ostream &OS, std::optional<StringRef> ExternalFilename) {
1818bcb0991SDimitry Andric return std::make_unique<YAMLMetaSerializer>(OS, ExternalFilename);
1828bcb0991SDimitry Andric }
1838bcb0991SDimitry Andric
emit(const Remark & Remark)1848bcb0991SDimitry Andric void YAMLStrTabRemarkSerializer::emit(const Remark &Remark) {
1858bcb0991SDimitry Andric // In standalone mode, for the serializer with a string table, emit the
1868bcb0991SDimitry Andric // metadata first and set DidEmitMeta to avoid emitting it again.
1878bcb0991SDimitry Andric if (Mode == SerializerMode::Standalone && !DidEmitMeta) {
1888bcb0991SDimitry Andric std::unique_ptr<MetaSerializer> MetaSerializer =
189*bdd1243dSDimitry Andric metaSerializer(OS, /*ExternalFilename=*/std::nullopt);
1908bcb0991SDimitry Andric MetaSerializer->emit();
1918bcb0991SDimitry Andric DidEmitMeta = true;
1928bcb0991SDimitry Andric }
1938bcb0991SDimitry Andric
1948bcb0991SDimitry Andric // Then do the usual remark emission.
1958bcb0991SDimitry Andric YAMLRemarkSerializer::emit(Remark);
1968bcb0991SDimitry Andric }
1978bcb0991SDimitry Andric
metaSerializer(raw_ostream & OS,std::optional<StringRef> ExternalFilename)1988bcb0991SDimitry Andric std::unique_ptr<MetaSerializer> YAMLStrTabRemarkSerializer::metaSerializer(
199*bdd1243dSDimitry Andric raw_ostream &OS, std::optional<StringRef> ExternalFilename) {
2008bcb0991SDimitry Andric assert(StrTab);
2018bcb0991SDimitry Andric return std::make_unique<YAMLStrTabMetaSerializer>(OS, ExternalFilename,
2028bcb0991SDimitry Andric *StrTab);
2038bcb0991SDimitry Andric }
2048bcb0991SDimitry Andric
emitMagic(raw_ostream & OS)2058bcb0991SDimitry Andric static void emitMagic(raw_ostream &OS) {
2068bcb0991SDimitry Andric // Emit the magic number.
2078bcb0991SDimitry Andric OS << remarks::Magic;
2088bcb0991SDimitry Andric // Explicitly emit a '\0'.
2098bcb0991SDimitry Andric OS.write('\0');
2108bcb0991SDimitry Andric }
2118bcb0991SDimitry Andric
emitVersion(raw_ostream & OS)2128bcb0991SDimitry Andric static void emitVersion(raw_ostream &OS) {
2138bcb0991SDimitry Andric // Emit the version number: little-endian uint64_t.
2148bcb0991SDimitry Andric std::array<char, 8> Version;
2158bcb0991SDimitry Andric support::endian::write64le(Version.data(), remarks::CurrentRemarkVersion);
2168bcb0991SDimitry Andric OS.write(Version.data(), Version.size());
2178bcb0991SDimitry Andric }
2188bcb0991SDimitry Andric
emitStrTab(raw_ostream & OS,std::optional<const StringTable * > StrTab)219*bdd1243dSDimitry Andric static void emitStrTab(raw_ostream &OS,
220*bdd1243dSDimitry Andric std::optional<const StringTable *> StrTab) {
2218bcb0991SDimitry Andric // Emit the string table in the section.
2228bcb0991SDimitry Andric uint64_t StrTabSize = StrTab ? (*StrTab)->SerializedSize : 0;
2238bcb0991SDimitry Andric // Emit the total size of the string table (the size itself excluded):
2248bcb0991SDimitry Andric // little-endian uint64_t.
2258bcb0991SDimitry Andric // Note: even if no string table is used, emit 0.
2268bcb0991SDimitry Andric std::array<char, 8> StrTabSizeBuf;
2278bcb0991SDimitry Andric support::endian::write64le(StrTabSizeBuf.data(), StrTabSize);
2288bcb0991SDimitry Andric OS.write(StrTabSizeBuf.data(), StrTabSizeBuf.size());
2298bcb0991SDimitry Andric if (StrTab)
2308bcb0991SDimitry Andric (*StrTab)->serialize(OS);
2318bcb0991SDimitry Andric }
2328bcb0991SDimitry Andric
emitExternalFile(raw_ostream & OS,StringRef Filename)2338bcb0991SDimitry Andric static void emitExternalFile(raw_ostream &OS, StringRef Filename) {
2348bcb0991SDimitry Andric // Emit the null-terminated absolute path to the remark file.
2358bcb0991SDimitry Andric SmallString<128> FilenameBuf = Filename;
2368bcb0991SDimitry Andric sys::fs::make_absolute(FilenameBuf);
2378bcb0991SDimitry Andric assert(!FilenameBuf.empty() && "The filename can't be empty.");
2388bcb0991SDimitry Andric OS.write(FilenameBuf.data(), FilenameBuf.size());
2398bcb0991SDimitry Andric OS.write('\0');
2408bcb0991SDimitry Andric }
2418bcb0991SDimitry Andric
emit()2428bcb0991SDimitry Andric void YAMLMetaSerializer::emit() {
2438bcb0991SDimitry Andric emitMagic(OS);
2448bcb0991SDimitry Andric emitVersion(OS);
245*bdd1243dSDimitry Andric emitStrTab(OS, std::nullopt);
2468bcb0991SDimitry Andric if (ExternalFilename)
2478bcb0991SDimitry Andric emitExternalFile(OS, *ExternalFilename);
2488bcb0991SDimitry Andric }
2498bcb0991SDimitry Andric
emit()2508bcb0991SDimitry Andric void YAMLStrTabMetaSerializer::emit() {
2518bcb0991SDimitry Andric emitMagic(OS);
2528bcb0991SDimitry Andric emitVersion(OS);
2538bcb0991SDimitry Andric emitStrTab(OS, &StrTab);
2548bcb0991SDimitry Andric if (ExternalFilename)
2558bcb0991SDimitry Andric emitExternalFile(OS, *ExternalFilename);
2568bcb0991SDimitry Andric }
257