-
Couldn't load subscription status.
- Fork 15k
[AllocToken] Refactor stateless token calculation into Support #163633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
melver
merged 8 commits into
main
from
users/melver/spr/alloctoken-refactor-stateless-token-calculation-into-support
Oct 22, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
676e8a0
[𝘀𝗽𝗿] initial version
melver da8cbd4
[𝘀𝗽𝗿] changes to main this commit is based on
melver 5947ec5
rebase
melver 9aea5dd
[𝘀𝗽𝗿] changes introduced through rebase
melver 5c71147
fixup! address comments
melver 8dd6689
[𝘀𝗽𝗿] changes introduced through rebase
melver c3395b9
address comments and rebase
melver f70c457
rebase
melver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| //===- llvm/Support/AllocToken.h - Allocation Token Calculation -----*- C++ -*// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Definition of AllocToken modes and shared calculation of stateless token IDs. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_SUPPORT_ALLOCTOKEN_H | ||
| #define LLVM_SUPPORT_ALLOCTOKEN_H | ||
|
|
||
| #include "llvm/ADT/SmallString.h" | ||
| #include <cstdint> | ||
| #include <optional> | ||
|
|
||
| namespace llvm { | ||
|
|
||
| /// Modes for generating allocation token IDs. | ||
| enum class AllocTokenMode { | ||
| /// Incrementally increasing token ID. | ||
| Increment, | ||
|
|
||
| /// Simple mode that returns a statically-assigned random token ID. | ||
| Random, | ||
|
|
||
| /// Token ID based on allocated type hash. | ||
| TypeHash, | ||
|
|
||
| /// Token ID based on allocated type hash, where the top half ID-space is | ||
| /// reserved for types that contain pointers and the bottom half for types | ||
| /// that do not contain pointers. | ||
| TypeHashPointerSplit, | ||
| }; | ||
|
|
||
| /// Metadata about an allocation used to generate a token ID. | ||
| struct AllocTokenMetadata { | ||
| SmallString<64> TypeName; | ||
| bool ContainsPointer; | ||
| }; | ||
|
|
||
| /// Calculates stable allocation token ID. Returns std::nullopt for stateful | ||
| /// modes that are only available in the AllocToken pass. | ||
| /// | ||
| /// \param Mode The token generation mode. | ||
| /// \param Metadata The metadata about the allocation. | ||
| /// \param MaxTokens The maximum number of tokens (must not be 0) | ||
| /// \return The calculated allocation token ID, or std::nullopt. | ||
| LLVM_ABI std::optional<uint64_t> | ||
| getAllocToken(AllocTokenMode Mode, const AllocTokenMetadata &Metadata, | ||
| uint64_t MaxTokens); | ||
|
|
||
| } // end namespace llvm | ||
|
|
||
| #endif // LLVM_SUPPORT_ALLOCTOKEN_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| //===- AllocToken.cpp - Allocation Token Calculation ----------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Definition of AllocToken modes and shared calculation of stateless token IDs. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "llvm/Support/AllocToken.h" | ||
| #include "llvm/Support/ErrorHandling.h" | ||
| #include "llvm/Support/SipHash.h" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| static uint64_t getStableHash(const AllocTokenMetadata &Metadata, | ||
| uint64_t MaxTokens) { | ||
| return getStableSipHash(Metadata.TypeName) % MaxTokens; | ||
| } | ||
|
|
||
| std::optional<uint64_t> llvm::getAllocToken(AllocTokenMode Mode, | ||
| const AllocTokenMetadata &Metadata, | ||
| uint64_t MaxTokens) { | ||
| assert(MaxTokens && "Must provide non-zero max tokens"); | ||
|
|
||
| switch (Mode) { | ||
| case AllocTokenMode::Increment: | ||
| case AllocTokenMode::Random: | ||
| // Stateful modes cannot be implemented as a pure function. | ||
| return std::nullopt; | ||
|
|
||
| case AllocTokenMode::TypeHash: | ||
| return getStableHash(Metadata, MaxTokens); | ||
|
|
||
| case AllocTokenMode::TypeHashPointerSplit: { | ||
| if (MaxTokens == 1) | ||
| return 0; | ||
| const uint64_t HalfTokens = MaxTokens / 2; | ||
| uint64_t Hash = getStableHash(Metadata, HalfTokens); | ||
| if (Metadata.ContainsPointer) | ||
| Hash += HalfTokens; | ||
| return Hash; | ||
| } | ||
| } | ||
|
|
||
| llvm_unreachable(""); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.