xref: /freebsd-src/contrib/llvm-project/llvm/lib/ProfileData/PGOCtxProfWriter.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1*0fca6ea1SDimitry Andric //===- PGOCtxProfWriter.cpp - Contextual Instrumentation profile writer ---===//
2*0fca6ea1SDimitry Andric //
3*0fca6ea1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0fca6ea1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0fca6ea1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0fca6ea1SDimitry Andric //
7*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
8*0fca6ea1SDimitry Andric //
9*0fca6ea1SDimitry Andric // Write a contextual profile to bitstream.
10*0fca6ea1SDimitry Andric //
11*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
12*0fca6ea1SDimitry Andric 
13*0fca6ea1SDimitry Andric #include "llvm/ProfileData/PGOCtxProfWriter.h"
14*0fca6ea1SDimitry Andric #include "llvm/Bitstream/BitCodeEnums.h"
15*0fca6ea1SDimitry Andric 
16*0fca6ea1SDimitry Andric using namespace llvm;
17*0fca6ea1SDimitry Andric using namespace llvm::ctx_profile;
18*0fca6ea1SDimitry Andric 
19*0fca6ea1SDimitry Andric void PGOCtxProfileWriter::writeCounters(const ContextNode &Node) {
20*0fca6ea1SDimitry Andric   Writer.EmitCode(bitc::UNABBREV_RECORD);
21*0fca6ea1SDimitry Andric   Writer.EmitVBR(PGOCtxProfileRecords::Counters, VBREncodingBits);
22*0fca6ea1SDimitry Andric   Writer.EmitVBR(Node.counters_size(), VBREncodingBits);
23*0fca6ea1SDimitry Andric   for (uint32_t I = 0U; I < Node.counters_size(); ++I)
24*0fca6ea1SDimitry Andric     Writer.EmitVBR64(Node.counters()[I], VBREncodingBits);
25*0fca6ea1SDimitry Andric }
26*0fca6ea1SDimitry Andric 
27*0fca6ea1SDimitry Andric // recursively write all the subcontexts. We do need to traverse depth first to
28*0fca6ea1SDimitry Andric // model the context->subcontext implicitly, and since this captures call
29*0fca6ea1SDimitry Andric // stacks, we don't really need to be worried about stack overflow and we can
30*0fca6ea1SDimitry Andric // keep the implementation simple.
31*0fca6ea1SDimitry Andric void PGOCtxProfileWriter::writeImpl(std::optional<uint32_t> CallerIndex,
32*0fca6ea1SDimitry Andric                                     const ContextNode &Node) {
33*0fca6ea1SDimitry Andric   Writer.EnterSubblock(PGOCtxProfileBlockIDs::ContextNodeBlockID, CodeLen);
34*0fca6ea1SDimitry Andric   Writer.EmitRecord(PGOCtxProfileRecords::Guid,
35*0fca6ea1SDimitry Andric                     SmallVector<uint64_t, 1>{Node.guid()});
36*0fca6ea1SDimitry Andric   if (CallerIndex)
37*0fca6ea1SDimitry Andric     Writer.EmitRecord(PGOCtxProfileRecords::CalleeIndex,
38*0fca6ea1SDimitry Andric                       SmallVector<uint64_t, 1>{*CallerIndex});
39*0fca6ea1SDimitry Andric   writeCounters(Node);
40*0fca6ea1SDimitry Andric   for (uint32_t I = 0U; I < Node.callsites_size(); ++I)
41*0fca6ea1SDimitry Andric     for (const auto *Subcontext = Node.subContexts()[I]; Subcontext;
42*0fca6ea1SDimitry Andric          Subcontext = Subcontext->next())
43*0fca6ea1SDimitry Andric       writeImpl(I, *Subcontext);
44*0fca6ea1SDimitry Andric   Writer.ExitBlock();
45*0fca6ea1SDimitry Andric }
46*0fca6ea1SDimitry Andric 
47*0fca6ea1SDimitry Andric void PGOCtxProfileWriter::write(const ContextNode &RootNode) {
48*0fca6ea1SDimitry Andric   writeImpl(std::nullopt, RootNode);
49*0fca6ea1SDimitry Andric }
50