[AllocToken] Fix and clarify -falloc-token-max=0 (#168689)

The option -falloc-token-max=0 is supposed to be usable to override
previous settings back to the target default max tokens (SIZE_MAX).

This did not work for the builtin:
```
| executed command: clang -cc1 [..] -nostdsysteminc -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify clang/test/SemaCXX/alloc-token.cpp -falloc-token-max=0
| clang: llvm/lib/Support/AllocToken.cpp:38: std::optional<uint64_t> llvm::getAllocToken(AllocTokenMode, const AllocTokenMetadata &, uint64_t): Assertion `MaxTokens && "Must provide non-zero max tokens"' failed.
```

Fix it by also picking the default if "0" is passed.

Improve the documentation to be clearer what the value of "0" means.
This commit is contained in:
Marco Elver 2025-11-19 12:15:15 +01:00 committed by GitHub
parent ed7f2a459a
commit 150053627d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 15 additions and 11 deletions

View File

@ -52,8 +52,8 @@ change or removal. These may (experimentally) be selected with ``-Xclang
The following command-line options affect generated token IDs:
* ``-falloc-token-max=<N>``
Configures the maximum number of tokens. No max by default (tokens bounded
by ``SIZE_MAX``).
Configures the maximum number of token IDs. By default the number of tokens
is bounded by ``SIZE_MAX``.
Querying Token IDs with ``__builtin_infer_alloc_token``
=======================================================
@ -129,7 +129,7 @@ Fast ABI
--------
An alternative ABI can be enabled with ``-fsanitize-alloc-token-fast-abi``,
which encodes the token ID hint in the allocation function name.
which encodes the token ID in the allocation function name.
.. code-block:: c

View File

@ -566,8 +566,8 @@ public:
bool AtomicFineGrainedMemory = false;
bool AtomicIgnoreDenormalMode = false;
/// Maximum number of allocation tokens (0 = no max), nullopt if none set (use
/// target default).
/// Maximum number of allocation tokens (0 = target SIZE_MAX), nullopt if none
/// set (use target SIZE_MAX).
std::optional<uint64_t> AllocTokenMax;
/// The allocation token mode.

View File

@ -2758,7 +2758,7 @@ defm sanitize_alloc_token_extended : BoolOption<"f", "sanitize-alloc-token-exten
def falloc_token_max_EQ : Joined<["-"], "falloc-token-max=">,
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
MetaVarName<"<N>">,
HelpText<"Limit to maximum N allocation tokens (0 = no max)">;
HelpText<"Limit to maximum N allocation tokens (0 = target SIZE_MAX)">;
def falloc_token_mode_EQ : Joined<["-"], "falloc-token-mode=">,
Group<f_Group>, Visibility<[CC1Option]>,

View File

@ -1317,8 +1317,9 @@ static bool interp__builtin_infer_alloc_token(InterpState &S, CodePtr OpPC,
uint64_t BitWidth = ASTCtx.getTypeSize(ASTCtx.getSizeType());
auto Mode =
ASTCtx.getLangOpts().AllocTokenMode.value_or(llvm::DefaultAllocTokenMode);
auto MaxTokensOpt = ASTCtx.getLangOpts().AllocTokenMax;
uint64_t MaxTokens =
ASTCtx.getLangOpts().AllocTokenMax.value_or(~0ULL >> (64 - BitWidth));
MaxTokensOpt.value_or(0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth));
// We do not read any of the arguments; discard them.
for (int I = Call->getNumArgs() - 1; I >= 0; --I)

View File

@ -15559,8 +15559,9 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
auto Mode =
Info.getLangOpts().AllocTokenMode.value_or(llvm::DefaultAllocTokenMode);
uint64_t BitWidth = Info.Ctx.getTypeSize(Info.Ctx.getSizeType());
auto MaxTokensOpt = Info.getLangOpts().AllocTokenMax;
uint64_t MaxTokens =
Info.getLangOpts().AllocTokenMax.value_or(~0ULL >> (64 - BitWidth));
MaxTokensOpt.value_or(0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth));
auto MaybeToken = llvm::getAllocToken(Mode, *ATMD, MaxTokens);
if (!MaybeToken)
return Error(E, diag::note_constexpr_infer_alloc_token_stateful_mode);

View File

@ -1,4 +1,5 @@
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s -falloc-token-max=0
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s -falloc-token-mode=typehash -DMODE_TYPEHASH
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s -falloc-token-max=2 -DTOKEN_MAX=2

View File

@ -67,9 +67,10 @@ cl::opt<std::string> ClFuncPrefix("alloc-token-prefix",
cl::desc("The allocation function prefix"),
cl::Hidden, cl::init("__alloc_token_"));
cl::opt<uint64_t> ClMaxTokens("alloc-token-max",
cl::desc("Maximum number of tokens (0 = no max)"),
cl::Hidden, cl::init(0));
cl::opt<uint64_t>
ClMaxTokens("alloc-token-max",
cl::desc("Maximum number of tokens (0 = target SIZE_MAX)"),
cl::Hidden, cl::init(0));
cl::opt<bool>
ClFastABI("alloc-token-fast-abi",