Path: blob/main/contrib/llvm-project/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
35233 views
//===--- CtxInstrContextNode.h - Contextual Profile Node --------*- C++ -*-===//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//9// NOTE!10// llvm/lib/ProfileData/CtxInstrContextNode.h and11// compiler-rt/lib/ctx_profile/CtxInstrContextNode.h12// must be exact copies of eachother13//14// compiler-rt creates these objects as part of the instrumentation runtime for15// contextual profiling. LLVM only consumes them to convert a contextual tree16// to a bitstream.17//18//==============================================================================1920/// The contextual profile is a directed tree where each node has one parent. A21/// node (ContextNode) corresponds to a function activation. The root of the22/// tree is at a function that was marked as entrypoint to the compiler. A node23/// stores counter values for edges and a vector of subcontexts. These are the24/// contexts of callees. The index in the subcontext vector corresponds to the25/// index of the callsite (as was instrumented via llvm.instrprof.callsite). At26/// that index we find a linked list, potentially empty, of ContextNodes. Direct27/// calls will have 0 or 1 values in the linked list, but indirect callsites may28/// have more.29///30/// The ContextNode has a fixed sized header describing it - the GUID of the31/// function, the size of the counter and callsite vectors. It is also an32/// (intrusive) linked list for the purposes of the indirect call case above.33///34/// Allocation is expected to happen on an Arena. The allocation lays out inline35/// the counter and subcontexts vectors. The class offers APIs to correctly36/// reference the latter.37///38/// The layout is as follows:39///40/// [[declared fields][counters vector][vector of ptrs to subcontexts]]41///42/// See also documentation on the counters and subContexts members below.43///44/// The structure of the ContextNode is known to LLVM, because LLVM needs to:45/// (1) increment counts, and46/// (2) form a GEP for the position in the subcontext list of a callsite47/// This means changes to LLVM contextual profile lowering and changes here48/// must be coupled.49/// Note: the header content isn't interesting to LLVM (other than its size)50///51/// Part of contextual collection is the notion of "scratch contexts". These are52/// buffers that are "large enough" to allow for memory-safe acceses during53/// counter increments - meaning the counter increment code in LLVM doesn't need54/// to be concerned with memory safety. Their subcontexts never get populated,55/// though. The runtime code here produces and recognizes them.5657#ifndef LLVM_PROFILEDATA_CTXINSTRCONTEXTNODE_H58#define LLVM_PROFILEDATA_CTXINSTRCONTEXTNODE_H5960#include <stdint.h>61#include <stdlib.h>6263namespace llvm {64namespace ctx_profile {65using GUID = uint64_t;6667class ContextNode final {68const GUID Guid;69ContextNode *const Next;70const uint32_t NrCounters;71const uint32_t NrCallsites;7273public:74ContextNode(GUID Guid, uint32_t NrCounters, uint32_t NrCallsites,75ContextNode *Next = nullptr)76: Guid(Guid), Next(Next), NrCounters(NrCounters),77NrCallsites(NrCallsites) {}7879static inline size_t getAllocSize(uint32_t NrCounters, uint32_t NrCallsites) {80return sizeof(ContextNode) + sizeof(uint64_t) * NrCounters +81sizeof(ContextNode *) * NrCallsites;82}8384// The counters vector starts right after the static header.85uint64_t *counters() {86ContextNode *addr_after = &(this[1]);87return reinterpret_cast<uint64_t *>(addr_after);88}8990uint32_t counters_size() const { return NrCounters; }91uint32_t callsites_size() const { return NrCallsites; }9293const uint64_t *counters() const {94return const_cast<ContextNode *>(this)->counters();95}9697// The subcontexts vector starts right after the end of the counters vector.98ContextNode **subContexts() {99return reinterpret_cast<ContextNode **>(&(counters()[NrCounters]));100}101102ContextNode *const *subContexts() const {103return const_cast<ContextNode *>(this)->subContexts();104}105106GUID guid() const { return Guid; }107ContextNode *next() const { return Next; }108109size_t size() const { return getAllocSize(NrCounters, NrCallsites); }110111uint64_t entrycount() const { return counters()[0]; }112};113} // namespace ctx_profile114} // namespace llvm115#endif116117