Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:23

0001 //===--- CtxInstrContextNode.h - Contextual Profile Node --------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //==============================================================================
0009 //
0010 // NOTE!
0011 // llvm/include/llvm/ProfileData/CtxInstrContextNode.h and
0012 //   compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
0013 // must be exact copies of each other.
0014 //
0015 // compiler-rt creates these objects as part of the instrumentation runtime for
0016 // contextual profiling. LLVM only consumes them to convert a contextual tree
0017 // to a bitstream.
0018 //
0019 //==============================================================================
0020 
0021 /// The contextual profile is a directed tree where each node has one parent. A
0022 /// node (ContextNode) corresponds to a function activation. The root of the
0023 /// tree is at a function that was marked as entrypoint to the compiler. A node
0024 /// stores counter values for edges and a vector of subcontexts. These are the
0025 /// contexts of callees. The index in the subcontext vector corresponds to the
0026 /// index of the callsite (as was instrumented via llvm.instrprof.callsite). At
0027 /// that index we find a linked list, potentially empty, of ContextNodes. Direct
0028 /// calls will have 0 or 1 values in the linked list, but indirect callsites may
0029 /// have more.
0030 ///
0031 /// The ContextNode has a fixed sized header describing it - the GUID of the
0032 /// function, the size of the counter and callsite vectors. It is also an
0033 /// (intrusive) linked list for the purposes of the indirect call case above.
0034 ///
0035 /// Allocation is expected to happen on an Arena. The allocation lays out inline
0036 /// the counter and subcontexts vectors. The class offers APIs to correctly
0037 /// reference the latter.
0038 ///
0039 /// The layout is as follows:
0040 ///
0041 /// [[declared fields][counters vector][vector of ptrs to subcontexts]]
0042 ///
0043 /// See also documentation on the counters and subContexts members below.
0044 ///
0045 /// The structure of the ContextNode is known to LLVM, because LLVM needs to:
0046 ///   (1) increment counts, and
0047 ///   (2) form a GEP for the position in the subcontext list of a callsite
0048 /// This means changes to LLVM contextual profile lowering and changes here
0049 /// must be coupled.
0050 /// Note: the header content isn't interesting to LLVM (other than its size)
0051 ///
0052 /// Part of contextual collection is the notion of "scratch contexts". These are
0053 /// buffers that are "large enough" to allow for memory-safe acceses during
0054 /// counter increments - meaning the counter increment code in LLVM doesn't need
0055 /// to be concerned with memory safety. Their subcontexts never get populated,
0056 /// though. The runtime code here produces and recognizes them.
0057 
0058 #ifndef LLVM_PROFILEDATA_CTXINSTRCONTEXTNODE_H
0059 #define LLVM_PROFILEDATA_CTXINSTRCONTEXTNODE_H
0060 
0061 #include <stdint.h>
0062 #include <stdlib.h>
0063 
0064 namespace llvm {
0065 namespace ctx_profile {
0066 using GUID = uint64_t;
0067 
0068 class ContextNode final {
0069   const GUID Guid;
0070   ContextNode *const Next;
0071   const uint32_t NumCounters;
0072   const uint32_t NumCallsites;
0073 
0074 public:
0075   ContextNode(GUID Guid, uint32_t NumCounters, uint32_t NumCallsites,
0076               ContextNode *Next = nullptr)
0077       : Guid(Guid), Next(Next), NumCounters(NumCounters),
0078         NumCallsites(NumCallsites) {}
0079 
0080   static inline size_t getAllocSize(uint32_t NumCounters,
0081                                     uint32_t NumCallsites) {
0082     return sizeof(ContextNode) + sizeof(uint64_t) * NumCounters +
0083            sizeof(ContextNode *) * NumCallsites;
0084   }
0085 
0086   // The counters vector starts right after the static header.
0087   uint64_t *counters() {
0088     ContextNode *addr_after = &(this[1]);
0089     return reinterpret_cast<uint64_t *>(addr_after);
0090   }
0091 
0092   uint32_t counters_size() const { return NumCounters; }
0093   uint32_t callsites_size() const { return NumCallsites; }
0094 
0095   const uint64_t *counters() const {
0096     return const_cast<ContextNode *>(this)->counters();
0097   }
0098 
0099   // The subcontexts vector starts right after the end of the counters vector.
0100   ContextNode **subContexts() {
0101     return reinterpret_cast<ContextNode **>(&(counters()[NumCounters]));
0102   }
0103 
0104   ContextNode *const *subContexts() const {
0105     return const_cast<ContextNode *>(this)->subContexts();
0106   }
0107 
0108   GUID guid() const { return Guid; }
0109   ContextNode *next() const { return Next; }
0110 
0111   size_t size() const { return getAllocSize(NumCounters, NumCallsites); }
0112 
0113   uint64_t entrycount() const { return counters()[0]; }
0114 };
0115 } // namespace ctx_profile
0116 } // namespace llvm
0117 #endif