| 1 | //===--- CtxInstrContextNode.h - Contextual Profile Node --------*- C++ -*-===// |
| 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 | // |
| 10 | // NOTE! |
| 11 | // llvm/include/llvm/ProfileData/CtxInstrContextNode.h and |
| 12 | // compiler-rt/lib/ctx_profile/CtxInstrContextNode.h |
| 13 | // must be exact copies of each other. |
| 14 | // |
| 15 | // compiler-rt creates these objects as part of the instrumentation runtime for |
| 16 | // contextual profiling. LLVM only consumes them to convert a contextual tree |
| 17 | // to a bitstream. |
| 18 | // |
| 19 | //============================================================================== |
| 20 | |
| 21 | /// The contextual profile is a directed tree where each node has one parent. A |
| 22 | /// node (ContextNode) corresponds to a function activation. The root of the |
| 23 | /// tree is at a function that was marked as entrypoint to the compiler. A node |
| 24 | /// stores counter values for edges and a vector of subcontexts. These are the |
| 25 | /// contexts of callees. The index in the subcontext vector corresponds to the |
| 26 | /// index of the callsite (as was instrumented via llvm.instrprof.callsite). At |
| 27 | /// that index we find a linked list, potentially empty, of ContextNodes. Direct |
| 28 | /// calls will have 0 or 1 values in the linked list, but indirect callsites may |
| 29 | /// have more. |
| 30 | /// |
| 31 | /// The ContextNode has a fixed sized header describing it - the GUID of the |
| 32 | /// function, the size of the counter and callsite vectors. It is also an |
| 33 | /// (intrusive) linked list for the purposes of the indirect call case above. |
| 34 | /// |
| 35 | /// Allocation is expected to happen on an Arena. The allocation lays out inline |
| 36 | /// the counter and subcontexts vectors. The class offers APIs to correctly |
| 37 | /// reference the latter. |
| 38 | /// |
| 39 | /// The layout is as follows: |
| 40 | /// |
| 41 | /// [[declared fields][counters vector][vector of ptrs to subcontexts]] |
| 42 | /// |
| 43 | /// See also documentation on the counters and subContexts members below. |
| 44 | /// |
| 45 | /// The structure of the ContextNode is known to LLVM, because LLVM needs to: |
| 46 | /// (1) increment counts, and |
| 47 | /// (2) form a GEP for the position in the subcontext list of a callsite |
| 48 | /// This means changes to LLVM contextual profile lowering and changes here |
| 49 | /// must be coupled. |
| 50 | /// Note: the header content isn't interesting to LLVM (other than its size) |
| 51 | /// |
| 52 | /// Part of contextual collection is the notion of "scratch contexts". These are |
| 53 | /// buffers that are "large enough" to allow for memory-safe acceses during |
| 54 | /// counter increments - meaning the counter increment code in LLVM doesn't need |
| 55 | /// to be concerned with memory safety. Their subcontexts never get populated, |
| 56 | /// though. The runtime code here produces and recognizes them. |
| 57 | |
| 58 | #ifndef LLVM_PROFILEDATA_CTXINSTRCONTEXTNODE_H |
| 59 | #define LLVM_PROFILEDATA_CTXINSTRCONTEXTNODE_H |
| 60 | |
| 61 | #include <stdint.h> |
| 62 | #include <stdlib.h> |
| 63 | |
| 64 | namespace llvm { |
| 65 | namespace ctx_profile { |
| 66 | using GUID = uint64_t; |
| 67 | |
| 68 | class ContextNode final { |
| 69 | const GUID Guid; |
| 70 | ContextNode *const Next; |
| 71 | const uint32_t NumCounters; |
| 72 | const uint32_t NumCallsites; |
| 73 | |
| 74 | public: |
| 75 | ContextNode(GUID Guid, uint32_t NumCounters, uint32_t NumCallsites, |
| 76 | ContextNode *Next = nullptr) |
| 77 | : Guid(Guid), Next(Next), NumCounters(NumCounters), |
| 78 | NumCallsites(NumCallsites) {} |
| 79 | |
| 80 | static inline size_t getAllocSize(uint32_t NumCounters, |
| 81 | uint32_t NumCallsites) { |
| 82 | return sizeof(ContextNode) + sizeof(uint64_t) * NumCounters + |
| 83 | sizeof(ContextNode *) * NumCallsites; |
| 84 | } |
| 85 | |
| 86 | // The counters vector starts right after the static header. |
| 87 | uint64_t *counters() { |
| 88 | ContextNode *addr_after = &(this[1]); |
| 89 | return reinterpret_cast<uint64_t *>(addr_after); |
| 90 | } |
| 91 | |
| 92 | uint32_t counters_size() const { return NumCounters; } |
| 93 | uint32_t callsites_size() const { return NumCallsites; } |
| 94 | |
| 95 | const uint64_t *counters() const { |
| 96 | return const_cast<ContextNode *>(this)->counters(); |
| 97 | } |
| 98 | |
| 99 | // The subcontexts vector starts right after the end of the counters vector. |
| 100 | ContextNode **subContexts() { |
| 101 | return reinterpret_cast<ContextNode **>(&(counters()[NumCounters])); |
| 102 | } |
| 103 | |
| 104 | ContextNode *const *subContexts() const { |
| 105 | return const_cast<ContextNode *>(this)->subContexts(); |
| 106 | } |
| 107 | |
| 108 | GUID guid() const { return Guid; } |
| 109 | ContextNode *next() const { return Next; } |
| 110 | |
| 111 | size_t size() const { return getAllocSize(NumCounters, NumCallsites); } |
| 112 | |
| 113 | uint64_t entrycount() const { return counters()[0]; } |
| 114 | }; |
| 115 | |
| 116 | /// The internal structure of FunctionData. This makes sure that changes to |
| 117 | /// the fields of FunctionData either get automatically captured on the llvm |
| 118 | /// side, or force a manual corresponding update. |
| 119 | /// |
| 120 | /// The macro arguments (see CtxInstrProfiling.h for example): |
| 121 | /// |
| 122 | /// PTRDECL is a macro taking 2 parameters: a type and the name of the field. |
| 123 | /// The field is a pointer of that type; |
| 124 | /// |
| 125 | /// VOLATILE_PTRDECL is the same as above, but for volatile pointers; |
| 126 | /// |
| 127 | /// MUTEXDECL takes one parameter, the name of a field that is a mutex. |
| 128 | #define CTXPROF_FUNCTION_DATA(PTRDECL, CONTEXT_PTR, VOLATILE_PTRDECL, \ |
| 129 | MUTEXDECL) \ |
| 130 | PTRDECL(FunctionData, Next) \ |
| 131 | VOLATILE_PTRDECL(void, EntryAddress) \ |
| 132 | CONTEXT_PTR \ |
| 133 | VOLATILE_PTRDECL(ContextNode, FlatCtx) \ |
| 134 | MUTEXDECL(Mutex) |
| 135 | |
| 136 | /// Abstraction for the parameter passed to `__llvm_ctx_profile_fetch`. |
| 137 | /// `startContextSection` is called before any context roots are sent for |
| 138 | /// writing. Then one or more `writeContextual` calls are made; finally, |
| 139 | /// `endContextSection` is called. |
| 140 | class ProfileWriter { |
| 141 | public: |
| 142 | virtual void startContextSection() = 0; |
| 143 | virtual void writeContextual(const ctx_profile::ContextNode &RootNode, |
| 144 | const ctx_profile::ContextNode *Unhandled, |
| 145 | uint64_t TotalRootEntryCount) = 0; |
| 146 | virtual void endContextSection() = 0; |
| 147 | |
| 148 | virtual void startFlatSection() = 0; |
| 149 | virtual void writeFlat(ctx_profile::GUID Guid, const uint64_t *Buffer, |
| 150 | size_t BufferSize) = 0; |
| 151 | virtual void endFlatSection() = 0; |
| 152 | |
| 153 | virtual ~ProfileWriter() = default; |
| 154 | }; |
| 155 | } // namespace ctx_profile |
| 156 | } // namespace llvm |
| 157 | #endif |
| 158 | |