//===- PGOCtxProfWriter.cpp - Contextual Instrumentation profile writer ---===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // Write a contextual profile to bitstream. // //===----------------------------------------------------------------------===// #include "llvm/ProfileData/PGOCtxProfWriter.h" #include "llvm/Bitstream/BitCodeEnums.h" #include "llvm/ProfileData/CtxInstrContextNode.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Error.h" #include "llvm/Support/YAMLTraits.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; using namespace llvm::ctx_profile; static cl::opt IncludeEmptyOpt("ctx-prof-include-empty", cl::init(false), cl::desc("Also write profiles with all-zero counters. " "Intended for testing/debugging.")); PGOCtxProfileWriter::PGOCtxProfileWriter( raw_ostream &Out, std::optional VersionOverride, bool IncludeEmpty) : Writer(Out, 0), IncludeEmpty(IncludeEmptyOpt.getNumOccurrences() > 0 ? IncludeEmptyOpt : IncludeEmpty) { static_assert(ContainerMagic.size() == 4); Out.write(ContainerMagic.data(), ContainerMagic.size()); Writer.EnterBlockInfoBlock(); { auto DescribeBlock = [&](unsigned ID, StringRef Name) { Writer.EmitRecord(bitc::BLOCKINFO_CODE_SETBID, SmallVector{ID}); Writer.EmitRecord(bitc::BLOCKINFO_CODE_BLOCKNAME, llvm::arrayRefFromStringRef(Name)); }; SmallVector Data; auto DescribeRecord = [&](unsigned RecordID, StringRef Name) { Data.clear(); Data.push_back(RecordID); llvm::append_range(Data, Name); Writer.EmitRecord(bitc::BLOCKINFO_CODE_SETRECORDNAME, Data); }; DescribeBlock(PGOCtxProfileBlockIDs::ProfileMetadataBlockID, "Metadata"); DescribeRecord(PGOCtxProfileRecords::Version, "Version"); DescribeBlock(PGOCtxProfileBlockIDs::ContextsSectionBlockID, "Contexts"); DescribeBlock(PGOCtxProfileBlockIDs::ContextRootBlockID, "Root"); DescribeRecord(PGOCtxProfileRecords::Guid, "GUID"); DescribeRecord(PGOCtxProfileRecords::Counters, "Counters"); DescribeBlock(PGOCtxProfileBlockIDs::ContextNodeBlockID, "Context"); DescribeRecord(PGOCtxProfileRecords::Guid, "GUID"); DescribeRecord(PGOCtxProfileRecords::CalleeIndex, "CalleeIndex"); DescribeRecord(PGOCtxProfileRecords::Counters, "Counters"); } Writer.ExitBlock(); Writer.EnterSubblock(PGOCtxProfileBlockIDs::ProfileMetadataBlockID, CodeLen); const auto Version = VersionOverride.value_or(CurrentVersion); Writer.EmitRecord(PGOCtxProfileRecords::Version, SmallVector({Version})); } void PGOCtxProfileWriter::writeCounters(ArrayRef Counters) { Writer.EmitCode(bitc::UNABBREV_RECORD); Writer.EmitVBR(PGOCtxProfileRecords::Counters, VBREncodingBits); Writer.EmitVBR(Counters.size(), VBREncodingBits); for (uint64_t C : Counters) Writer.EmitVBR64(C, VBREncodingBits); } void PGOCtxProfileWriter::writeGuid(ctx_profile::GUID Guid) { Writer.EmitRecord(PGOCtxProfileRecords::Guid, SmallVector{Guid}); } // recursively write all the subcontexts. We do need to traverse depth first to // model the context->subcontext implicitly, and since this captures call // stacks, we don't really need to be worried about stack overflow and we can // keep the implementation simple. void PGOCtxProfileWriter::writeImpl(std::optional CallerIndex, const ContextNode &Node) { // A node with no counters is an error. We don't expect this to happen from // the runtime, rather, this is interesting for testing the reader. if (!IncludeEmpty && (Node.counters_size() > 0 && Node.entrycount() == 0)) return; Writer.EnterSubblock(CallerIndex ? PGOCtxProfileBlockIDs::ContextNodeBlockID : PGOCtxProfileBlockIDs::ContextRootBlockID, CodeLen); writeGuid(Node.guid()); if (CallerIndex) Writer.EmitRecord(PGOCtxProfileRecords::CalleeIndex, SmallVector{*CallerIndex}); writeCounters({Node.counters(), Node.counters_size()}); for (uint32_t I = 0U; I < Node.callsites_size(); ++I) for (const auto *Subcontext = Node.subContexts()[I]; Subcontext; Subcontext = Subcontext->next()) writeImpl(I, *Subcontext); Writer.ExitBlock(); } void PGOCtxProfileWriter::startContextSection() { Writer.EnterSubblock(PGOCtxProfileBlockIDs::ContextsSectionBlockID, CodeLen); } void PGOCtxProfileWriter::endContextSection() { Writer.ExitBlock(); } void PGOCtxProfileWriter::writeContextual(const ContextNode &RootNode) { writeImpl(std::nullopt, RootNode); } namespace { /// Representation of the context node suitable for yaml serialization / /// deserialization. struct SerializableCtxRepresentation { ctx_profile::GUID Guid = 0; std::vector Counters; std::vector> Callsites; }; struct SerializableProfileRepresentation { std::vector Contexts; }; ctx_profile::ContextNode * createNode(std::vector> &Nodes, const std::vector &DCList); // Convert a DeserializableCtx into a ContextNode, potentially linking it to // its sibling (e.g. callee at same callsite) "Next". ctx_profile::ContextNode * createNode(std::vector> &Nodes, const SerializableCtxRepresentation &DC, ctx_profile::ContextNode *Next = nullptr) { auto AllocSize = ctx_profile::ContextNode::getAllocSize(DC.Counters.size(), DC.Callsites.size()); auto *Mem = Nodes.emplace_back(std::make_unique(AllocSize)).get(); std::memset(Mem, 0, AllocSize); auto *Ret = new (Mem) ctx_profile::ContextNode(DC.Guid, DC.Counters.size(), DC.Callsites.size(), Next); std::memcpy(Ret->counters(), DC.Counters.data(), sizeof(uint64_t) * DC.Counters.size()); for (const auto &[I, DCList] : llvm::enumerate(DC.Callsites)) Ret->subContexts()[I] = createNode(Nodes, DCList); return Ret; } // Convert a list of SerializableCtxRepresentation into a linked list of // ContextNodes. ctx_profile::ContextNode * createNode(std::vector> &Nodes, const std::vector &DCList) { ctx_profile::ContextNode *List = nullptr; for (const auto &DC : DCList) List = createNode(Nodes, DC, List); return List; } } // namespace LLVM_YAML_IS_SEQUENCE_VECTOR(SerializableCtxRepresentation) LLVM_YAML_IS_SEQUENCE_VECTOR(std::vector) template <> struct yaml::MappingTraits { static void mapping(yaml::IO &IO, SerializableCtxRepresentation &SCR) { IO.mapRequired("Guid", SCR.Guid); IO.mapRequired("Counters", SCR.Counters); IO.mapOptional("Callsites", SCR.Callsites); } }; template <> struct yaml::MappingTraits { static void mapping(yaml::IO &IO, SerializableProfileRepresentation &SPR) { IO.mapOptional("Contexts", SPR.Contexts); } }; Error llvm::createCtxProfFromYAML(StringRef Profile, raw_ostream &Out) { yaml::Input In(Profile); SerializableProfileRepresentation SPR; In >> SPR; if (In.error()) return createStringError(In.error(), "incorrect yaml content"); std::vector> Nodes; std::error_code EC; if (EC) return createStringError(EC, "failed to open output"); PGOCtxProfileWriter Writer(Out); if (!SPR.Contexts.empty()) { Writer.startContextSection(); for (const auto &DC : SPR.Contexts) { auto *TopList = createNode(Nodes, DC); if (!TopList) return createStringError( "Unexpected error converting internal structure to ctx profile"); Writer.writeContextual(*TopList); } Writer.endContextSection(); } if (EC) return createStringError(EC, "failed to write output"); return Error::success(); }