1 //===- PGOCtxProfWriter.cpp - Contextual Instrumentation profile writer ---===//
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
7 //===----------------------------------------------------------------------===//
9 // Write a contextual profile to bitstream.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ProfileData/PGOCtxProfWriter.h"
14 #include "llvm/Bitstream/BitCodeEnums.h"
17 using namespace llvm::ctx_profile
;
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
);
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.
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()});
37 Writer
.EmitRecord(PGOCtxProfileRecords::CalleeIndex
,
38 SmallVector
<uint64_t, 1>{*CallerIndex
});
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
);
47 void PGOCtxProfileWriter::write(const ContextNode
&RootNode
) {
48 writeImpl(std::nullopt
, RootNode
);