Path: blob/main/contrib/llvm-project/llvm/lib/Remarks/YAMLRemarkSerializer.cpp
35262 views
//===- YAMLRemarkSerializer.cpp -------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file provides the implementation of the YAML remark serializer using9// LLVM's YAMLTraits.10//11//===----------------------------------------------------------------------===//1213#include "llvm/Remarks/YAMLRemarkSerializer.h"14#include "llvm/Remarks/Remark.h"15#include "llvm/Support/FileSystem.h"16#include <optional>1718using namespace llvm;19using namespace llvm::remarks;2021// Use the same keys whether we use a string table or not (respectively, T is an22// unsigned or a StringRef).23template <typename T>24static void mapRemarkHeader(yaml::IO &io, T PassName, T RemarkName,25std::optional<RemarkLocation> RL, T FunctionName,26std::optional<uint64_t> Hotness,27ArrayRef<Argument> Args) {28io.mapRequired("Pass", PassName);29io.mapRequired("Name", RemarkName);30io.mapOptional("DebugLoc", RL);31io.mapRequired("Function", FunctionName);32io.mapOptional("Hotness", Hotness);33io.mapOptional("Args", Args);34}3536namespace llvm {37namespace yaml {3839template <> struct MappingTraits<remarks::Remark *> {40static void mapping(IO &io, remarks::Remark *&Remark) {41assert(io.outputting() && "input not yet implemented");4243if (io.mapTag("!Passed", (Remark->RemarkType == Type::Passed)))44;45else if (io.mapTag("!Missed", (Remark->RemarkType == Type::Missed)))46;47else if (io.mapTag("!Analysis", (Remark->RemarkType == Type::Analysis)))48;49else if (io.mapTag("!AnalysisFPCommute",50(Remark->RemarkType == Type::AnalysisFPCommute)))51;52else if (io.mapTag("!AnalysisAliasing",53(Remark->RemarkType == Type::AnalysisAliasing)))54;55else if (io.mapTag("!Failure", (Remark->RemarkType == Type::Failure)))56;57else58llvm_unreachable("Unknown remark type");5960if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(61reinterpret_cast<RemarkSerializer *>(io.getContext()))) {62assert(Serializer->StrTab && "YAMLStrTabSerializer with no StrTab.");63StringTable &StrTab = *Serializer->StrTab;64unsigned PassID = StrTab.add(Remark->PassName).first;65unsigned NameID = StrTab.add(Remark->RemarkName).first;66unsigned FunctionID = StrTab.add(Remark->FunctionName).first;67mapRemarkHeader(io, PassID, NameID, Remark->Loc, FunctionID,68Remark->Hotness, Remark->Args);69} else {70mapRemarkHeader(io, Remark->PassName, Remark->RemarkName, Remark->Loc,71Remark->FunctionName, Remark->Hotness, Remark->Args);72}73}74};7576template <> struct MappingTraits<RemarkLocation> {77static void mapping(IO &io, RemarkLocation &RL) {78assert(io.outputting() && "input not yet implemented");7980StringRef File = RL.SourceFilePath;81unsigned Line = RL.SourceLine;82unsigned Col = RL.SourceColumn;8384if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(85reinterpret_cast<RemarkSerializer *>(io.getContext()))) {86assert(Serializer->StrTab && "YAMLStrTabSerializer with no StrTab.");87StringTable &StrTab = *Serializer->StrTab;88unsigned FileID = StrTab.add(File).first;89io.mapRequired("File", FileID);90} else {91io.mapRequired("File", File);92}9394io.mapRequired("Line", Line);95io.mapRequired("Column", Col);96}9798static const bool flow = true;99};100101/// Helper struct for multiline string block literals. Use this type to preserve102/// newlines in strings.103struct StringBlockVal {104StringRef Value;105StringBlockVal(StringRef R) : Value(R) {}106};107108template <> struct BlockScalarTraits<StringBlockVal> {109static void output(const StringBlockVal &S, void *Ctx, raw_ostream &OS) {110return ScalarTraits<StringRef>::output(S.Value, Ctx, OS);111}112113static StringRef input(StringRef Scalar, void *Ctx, StringBlockVal &S) {114return ScalarTraits<StringRef>::input(Scalar, Ctx, S.Value);115}116};117118/// ArrayRef is not really compatible with the YAMLTraits. Everything should be119/// immutable in an ArrayRef, while the SequenceTraits expect a mutable version120/// for inputting, but we're only using the outputting capabilities here.121/// This is a hack, but still nicer than having to manually call the YAMLIO122/// internal methods.123/// Keep this in this file so that it doesn't get misused from YAMLTraits.h.124template <typename T> struct SequenceTraits<ArrayRef<T>> {125static size_t size(IO &io, ArrayRef<T> &seq) { return seq.size(); }126static Argument &element(IO &io, ArrayRef<T> &seq, size_t index) {127assert(io.outputting() && "input not yet implemented");128// The assert above should make this "safer" to satisfy the YAMLTraits.129return const_cast<T &>(seq[index]);130}131};132133/// Implement this as a mapping for now to get proper quotation for the value.134template <> struct MappingTraits<Argument> {135static void mapping(IO &io, Argument &A) {136assert(io.outputting() && "input not yet implemented");137138if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(139reinterpret_cast<RemarkSerializer *>(io.getContext()))) {140assert(Serializer->StrTab && "YAMLStrTabSerializer with no StrTab.");141StringTable &StrTab = *Serializer->StrTab;142auto ValueID = StrTab.add(A.Val).first;143io.mapRequired(A.Key.data(), ValueID);144} else if (StringRef(A.Val).count('\n') > 1) {145StringBlockVal S(A.Val);146io.mapRequired(A.Key.data(), S);147} else {148io.mapRequired(A.Key.data(), A.Val);149}150io.mapOptional("DebugLoc", A.Loc);151}152};153154} // end namespace yaml155} // end namespace llvm156157LLVM_YAML_IS_SEQUENCE_VECTOR(Argument)158159YAMLRemarkSerializer::YAMLRemarkSerializer(raw_ostream &OS, SerializerMode Mode,160std::optional<StringTable> StrTabIn)161: YAMLRemarkSerializer(Format::YAML, OS, Mode, std::move(StrTabIn)) {}162163YAMLRemarkSerializer::YAMLRemarkSerializer(Format SerializerFormat,164raw_ostream &OS, SerializerMode Mode,165std::optional<StringTable> StrTabIn)166: RemarkSerializer(SerializerFormat, OS, Mode),167YAMLOutput(OS, reinterpret_cast<void *>(this)) {168StrTab = std::move(StrTabIn);169}170171void YAMLRemarkSerializer::emit(const Remark &Remark) {172// Again, YAMLTraits expect a non-const object for inputting, but we're not173// using that here.174auto R = const_cast<remarks::Remark *>(&Remark);175YAMLOutput << R;176}177178std::unique_ptr<MetaSerializer> YAMLRemarkSerializer::metaSerializer(179raw_ostream &OS, std::optional<StringRef> ExternalFilename) {180return std::make_unique<YAMLMetaSerializer>(OS, ExternalFilename);181}182183void YAMLStrTabRemarkSerializer::emit(const Remark &Remark) {184// In standalone mode, for the serializer with a string table, emit the185// metadata first and set DidEmitMeta to avoid emitting it again.186if (Mode == SerializerMode::Standalone && !DidEmitMeta) {187std::unique_ptr<MetaSerializer> MetaSerializer =188metaSerializer(OS, /*ExternalFilename=*/std::nullopt);189MetaSerializer->emit();190DidEmitMeta = true;191}192193// Then do the usual remark emission.194YAMLRemarkSerializer::emit(Remark);195}196197std::unique_ptr<MetaSerializer> YAMLStrTabRemarkSerializer::metaSerializer(198raw_ostream &OS, std::optional<StringRef> ExternalFilename) {199assert(StrTab);200return std::make_unique<YAMLStrTabMetaSerializer>(OS, ExternalFilename,201*StrTab);202}203204static void emitMagic(raw_ostream &OS) {205// Emit the magic number.206OS << remarks::Magic;207// Explicitly emit a '\0'.208OS.write('\0');209}210211static void emitVersion(raw_ostream &OS) {212// Emit the version number: little-endian uint64_t.213std::array<char, 8> Version;214support::endian::write64le(Version.data(), remarks::CurrentRemarkVersion);215OS.write(Version.data(), Version.size());216}217218static void emitStrTab(raw_ostream &OS,219std::optional<const StringTable *> StrTab) {220// Emit the string table in the section.221uint64_t StrTabSize = StrTab ? (*StrTab)->SerializedSize : 0;222// Emit the total size of the string table (the size itself excluded):223// little-endian uint64_t.224// Note: even if no string table is used, emit 0.225std::array<char, 8> StrTabSizeBuf;226support::endian::write64le(StrTabSizeBuf.data(), StrTabSize);227OS.write(StrTabSizeBuf.data(), StrTabSizeBuf.size());228if (StrTab)229(*StrTab)->serialize(OS);230}231232static void emitExternalFile(raw_ostream &OS, StringRef Filename) {233// Emit the null-terminated absolute path to the remark file.234SmallString<128> FilenameBuf = Filename;235sys::fs::make_absolute(FilenameBuf);236assert(!FilenameBuf.empty() && "The filename can't be empty.");237OS.write(FilenameBuf.data(), FilenameBuf.size());238OS.write('\0');239}240241void YAMLMetaSerializer::emit() {242emitMagic(OS);243emitVersion(OS);244emitStrTab(OS, std::nullopt);245if (ExternalFilename)246emitExternalFile(OS, *ExternalFilename);247}248249void YAMLStrTabMetaSerializer::emit() {250emitMagic(OS);251emitVersion(OS);252emitStrTab(OS, &StrTab);253if (ExternalFilename)254emitExternalFile(OS, *ExternalFilename);255}256257258