LLVM 22.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
17
18using namespace llvm;
19
20std::optional<AllocTokenMode>
23 .Case("increment", AllocTokenMode::Increment)
25 .Case("typehash", AllocTokenMode::TypeHash)
26 .Case("typehashpointersplit", AllocTokenMode::TypeHashPointerSplit)
27 .Case("default", DefaultAllocTokenMode)
28 .Default(std::nullopt);
29}
30
32 switch (Mode) {
34 return "increment";
36 return "random";
38 return "typehash";
40 return "typehashpointersplit";
41 }
42 llvm_unreachable("Unknown AllocTokenMode");
43}
44
46 uint64_t MaxTokens) {
47 return getStableSipHash(Metadata.TypeName) % MaxTokens;
48}
49
50std::optional<uint64_t> llvm::getAllocToken(AllocTokenMode Mode,
52 uint64_t MaxTokens) {
53 assert(MaxTokens && "Must provide non-zero max tokens");
54
55 switch (Mode) {
58 // Stateful modes cannot be implemented as a pure function.
59 return std::nullopt;
60
62 return getStableHash(Metadata, MaxTokens);
63
65 if (MaxTokens == 1)
66 return 0;
67 const uint64_t HalfTokens = MaxTokens / 2;
68 uint64_t Hash = getStableHash(Metadata, HalfTokens);
69 if (Metadata.ContainsPointer)
70 Hash += HalfTokens;
71 return Hash;
72 }
73 }
74
76}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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)
Root of the metadata hierarchy.
Definition Metadata.h:64
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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.
constexpr AllocTokenMode DefaultAllocTokenMode
The default allocation token mode.
Definition AllocToken.h:41
LLVM_ABI uint64_t getStableSipHash(StringRef Str)
Compute a stable 64-bit hash of the given string.
Definition SipHash.cpp:39
AllocTokenMode
Modes for generating allocation token IDs.
Definition AllocToken.h:24
@ 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
@ 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:53