Path: blob/main/contrib/llvm-project/llvm/lib/ProfileData/PGOCtxProfWriter.cpp
35233 views
//===- PGOCtxProfWriter.cpp - Contextual Instrumentation profile writer ---===//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// Write a contextual profile to bitstream.9//10//===----------------------------------------------------------------------===//1112#include "llvm/ProfileData/PGOCtxProfWriter.h"13#include "llvm/Bitstream/BitCodeEnums.h"1415using namespace llvm;16using namespace llvm::ctx_profile;1718void PGOCtxProfileWriter::writeCounters(const ContextNode &Node) {19Writer.EmitCode(bitc::UNABBREV_RECORD);20Writer.EmitVBR(PGOCtxProfileRecords::Counters, VBREncodingBits);21Writer.EmitVBR(Node.counters_size(), VBREncodingBits);22for (uint32_t I = 0U; I < Node.counters_size(); ++I)23Writer.EmitVBR64(Node.counters()[I], VBREncodingBits);24}2526// recursively write all the subcontexts. We do need to traverse depth first to27// model the context->subcontext implicitly, and since this captures call28// stacks, we don't really need to be worried about stack overflow and we can29// keep the implementation simple.30void PGOCtxProfileWriter::writeImpl(std::optional<uint32_t> CallerIndex,31const ContextNode &Node) {32Writer.EnterSubblock(PGOCtxProfileBlockIDs::ContextNodeBlockID, CodeLen);33Writer.EmitRecord(PGOCtxProfileRecords::Guid,34SmallVector<uint64_t, 1>{Node.guid()});35if (CallerIndex)36Writer.EmitRecord(PGOCtxProfileRecords::CalleeIndex,37SmallVector<uint64_t, 1>{*CallerIndex});38writeCounters(Node);39for (uint32_t I = 0U; I < Node.callsites_size(); ++I)40for (const auto *Subcontext = Node.subContexts()[I]; Subcontext;41Subcontext = Subcontext->next())42writeImpl(I, *Subcontext);43Writer.ExitBlock();44}4546void PGOCtxProfileWriter::write(const ContextNode &RootNode) {47writeImpl(std::nullopt, RootNode);48}495051