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