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 .Default(std::nullopt);
28}
29
31 uint64_t MaxTokens) {
32 return getStableSipHash(Metadata.TypeName) % MaxTokens;
33}
34
35std::optional<uint64_t> llvm::getAllocToken(AllocTokenMode Mode,
37 uint64_t MaxTokens) {
38 assert(MaxTokens && "Must provide non-zero max tokens");
39
40 switch (Mode) {
43 // Stateful modes cannot be implemented as a pure function.
44 return std::nullopt;
45
47 return getStableHash(Metadata, MaxTokens);
48
50 if (MaxTokens == 1)
51 return 0;
52 const uint64_t HalfTokens = MaxTokens / 2;
53 uint64_t Hash = getStableHash(Metadata, HalfTokens);
54 if (Metadata.ContainsPointer)
55 Hash += HalfTokens;
56 return Hash;
57 }
58 }
59
61}
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.
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...
Metadata about an allocation used to generate a token ID.
Definition AllocToken.h:50