LLVM 24.0.0git
AllocToken.cpp
Go to the documentation of this file.
1//===- AllocToken.cpp - Allocation Token Calculation ----------------------===//
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
18
19using namespace llvm;
20
21std::optional<AllocTokenMode>
24 .Case("increment", AllocTokenMode::Increment)
26 .Case("typehash", AllocTokenMode::TypeHash)
27 .Case("typehashpointersplit", AllocTokenMode::TypeHashPointerSplit)
28 .Case("typefunchash", AllocTokenMode::TypeFuncHash)
29 .Case("typefunchashpointersplit",
31 .Case("default", DefaultAllocTokenMode)
32 .Default(std::nullopt);
33}
34
36 switch (Mode) {
38 return "increment";
40 return "random";
42 return "typehash";
44 return "typehashpointersplit";
46 return "typefunchash";
48 return "typefunchashpointersplit";
49 }
50 llvm_unreachable("Unknown AllocTokenMode");
51}
52
54 uint64_t MaxTokens) {
55 return getStableSipHash(Metadata.TypeName) % MaxTokens;
56}
57
58/// Splits the Bits bits into: [pointer flag,] type name hash, function name
59/// hash. The function name hash gets Bits / 2 bits. Bits is k if MaxTokens is
60/// 2^k-1 (e.g. SIZE_MAX, tokens in [0, MaxTokens]), or Log2(MaxTokens)
61/// otherwise. With pointer split, the MSB is the pointer flag.
63 uint64_t MaxTokens, bool PointerSplit) {
64 if (MaxTokens == 1)
65 return 0;
66 const unsigned Bits =
67 isMask_64(MaxTokens) ? llvm::countr_one(MaxTokens) : Log2_64(MaxTokens);
68 const unsigned FuncBits = Bits / 2;
69 unsigned TypeBits = Bits - FuncBits;
70 uint64_t Token = 0;
71 if (PointerSplit) {
72 --TypeBits;
73 Token = uint64_t(Metadata.ContainsPointer) << (Bits - 1);
74 }
75 // An empty type name denotes an unknown type.
76 if (!Metadata.TypeName.empty())
77 Token |= (getStableSipHash(Metadata.TypeName) &
79 << FuncBits;
80 Token |= getStableSipHash(*Metadata.FunctionName) &
82 return Token;
83}
84
85std::optional<uint64_t> llvm::getAllocToken(AllocTokenMode Mode,
87 uint64_t MaxTokens) {
88 assert(MaxTokens && "Must provide non-zero max tokens");
89
90 switch (Mode) {
93 // Stateful modes cannot be implemented as a pure function.
94 return std::nullopt;
95
98 if (!Metadata.FunctionName)
99 return std::nullopt;
100 return getTypeFuncHash(Metadata, MaxTokens,
102
104 return getStableHash(Metadata, MaxTokens);
105
107 if (MaxTokens == 1)
108 return 0;
109 const uint64_t HalfTokens = MaxTokens / 2;
110 uint64_t Hash = getStableHash(Metadata, HalfTokens);
111 if (Metadata.ContainsPointer)
112 Hash += HalfTokens;
113 return Hash;
114 }
115 }
116
118}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
static uint64_t getStableHash(const AllocTokenMetadata &Metadata, uint64_t MaxTokens)
static uint64_t getTypeFuncHash(const AllocTokenMetadata &Metadata, uint64_t MaxTokens, bool PointerSplit)
Splits the Bits bits into: [pointer flag,] type name hash, function name hash.
Root of the metadata hierarchy.
Definition Metadata.h:64
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::optional< uint64_t > getAllocToken(AllocTokenMode Mode, const AllocTokenMetadata &Metadata, uint64_t MaxTokens)
Calculates stable allocation token ID.
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:315
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:332
constexpr AllocTokenMode DefaultAllocTokenMode
The default allocation token mode.
Definition AllocToken.h:50
constexpr bool isMask_64(uint64_t Value)
Return true if the argument is a non-empty sequence of ones starting at the least significant bit wit...
Definition MathExtras.h:262
LLVM_ABI uint64_t getStableSipHash(StringRef Str)
Compute a stable 64-bit hash of the given string.
Definition SipHash.cpp:39
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:78
AllocTokenMode
Modes for generating allocation token IDs.
Definition AllocToken.h:24
@ TypeFuncHashPointerSplit
Like TypeFuncHash, but for MaxTokens > 1 the most significant bit of the ID space is set for types th...
Definition AllocToken.h:46
@ TypeHash
Token ID based on allocated type hash.
Definition AllocToken.h:32
@ Random
Simple mode that returns a statically-assigned random token ID.
Definition AllocToken.h:29
@ Increment
Incrementally increasing token ID.
Definition AllocToken.h:26
@ TypeFuncHash
Token ID based on allocated type hash and containing function name hash.
Definition AllocToken.h:42
@ TypeHashPointerSplit
Token ID based on allocated type hash, where the top half ID-space is reserved for types that contain...
Definition AllocToken.h:37
LLVM_ABI std::optional< AllocTokenMode > getAllocTokenModeFromString(StringRef Name)
Returns the AllocTokenMode from its canonical string name; if an invalid name was provided returns nu...
LLVM_ABI StringRef getAllocTokenModeAsString(AllocTokenMode Mode)
Returns the canonical string name for the given AllocTokenMode.
Metadata about an allocation used to generate a token ID.
Definition AllocToken.h:62