|
| 1 | +//===- llvm/Support/AllocToken.h - Allocation Token Calculation -----*- 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 | +// Definition of AllocToken modes and shared calculation of stateless token IDs. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef LLVM_SUPPORT_ALLOCTOKEN_H |
| 14 | +#define LLVM_SUPPORT_ALLOCTOKEN_H |
| 15 | + |
| 16 | +#include "llvm/ADT/SmallString.h" |
| 17 | +#include <cstdint> |
| 18 | +#include <optional> |
| 19 | + |
| 20 | +namespace llvm { |
| 21 | + |
| 22 | +/// Modes for generating allocation token IDs. |
| 23 | +enum class AllocTokenMode { |
| 24 | + /// Incrementally increasing token ID. |
| 25 | + Increment, |
| 26 | + |
| 27 | + /// Simple mode that returns a statically-assigned random token ID. |
| 28 | + Random, |
| 29 | + |
| 30 | + /// Token ID based on allocated type hash. |
| 31 | + TypeHash, |
| 32 | + |
| 33 | + /// Token ID based on allocated type hash, where the top half ID-space is |
| 34 | + /// reserved for types that contain pointers and the bottom half for types |
| 35 | + /// that do not contain pointers. |
| 36 | + TypeHashPointerSplit, |
| 37 | +}; |
| 38 | + |
| 39 | +/// The default allocation token mode. |
| 40 | +inline constexpr AllocTokenMode DefaultAllocTokenMode = |
| 41 | + AllocTokenMode::TypeHashPointerSplit; |
| 42 | + |
| 43 | +/// Metadata about an allocation used to generate a token ID. |
| 44 | +struct AllocTokenMetadata { |
| 45 | + SmallString<64> TypeName; |
| 46 | + bool ContainsPointer; |
| 47 | +}; |
| 48 | + |
| 49 | +/// Calculates stable allocation token ID. Returns std::nullopt for stateful |
| 50 | +/// modes that are only available in the AllocToken pass. |
| 51 | +/// |
| 52 | +/// \param Mode The token generation mode. |
| 53 | +/// \param Metadata The metadata about the allocation. |
| 54 | +/// \param MaxTokens The maximum number of tokens (must not be 0) |
| 55 | +/// \return The calculated allocation token ID, or std::nullopt. |
| 56 | +std::optional<uint64_t> getAllocTokenHash(AllocTokenMode Mode, |
| 57 | + const AllocTokenMetadata &Metadata, |
| 58 | + uint64_t MaxTokens); |
| 59 | + |
| 60 | +} // end namespace llvm |
| 61 | + |
| 62 | +#endif // LLVM_SUPPORT_ALLOCTOKEN_H |
0 commit comments