-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[Clang][CodeGen] Implement code generation for __builtin_infer_alloc_token() #156842
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 23 commits into
main
from
users/melver/spr/alloctoken-clang-implement-__builtin_alloc_token_infer-and-llvmalloctokenid
Oct 28, 2025
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ebdc34f
[𝘀𝗽𝗿] changes introduced through rebase
melver 019499f
force rebase
melver 3c1e721
[𝘀𝗽𝗿] changes introduced through rebase
melver 87eb971
fixup! address comments
melver 3a6e112
[𝘀𝗽𝗿] changes introduced through rebase
melver f1dc6eb
address comments and rebase
melver 2a4274c
[𝘀𝗽𝗿] changes introduced through rebase
melver dd6a4ef
rebase
melver f760bca
[𝘀𝗽𝗿] changes introduced through rebase
melver 6f5d488
rebase
melver 0c02ec9
[𝘀𝗽𝗿] changes introduced through rebase
melver 92ea57d
rebase
melver 6e64338
[𝘀𝗽𝗿] changes introduced through rebase
melver b5294fd
rebase
melver a25e7ff
[𝘀𝗽𝗿] changes introduced through rebase
melver 8150bba
rebase
melver 58102c6
[𝘀𝗽𝗿] changes introduced through rebase
melver 872b019
rebase
melver fc5d0db
[𝘀𝗽𝗿] changes introduced through rebase
melver b18c091
rebase
melver fd64555
rebase
melver 7813272
rebase
melver 31fd2b0
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
Some comments aren't visible on the classic Files Changed page.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // To test IR generation of the builtin without evaluating the LLVM intrinsic, | ||
| // we set the mode to a stateful mode, which prohibits constant evaluation. | ||
| // RUN: %clang_cc1 -triple x86_64-linux-gnu -Werror -std=c++20 -emit-llvm -falloc-token-mode=random -disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-CODEGEN | ||
| // RUN: %clang_cc1 -triple x86_64-linux-gnu -Werror -std=c++20 -emit-llvm -falloc-token-max=2 %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-LOWER | ||
|
|
||
| extern "C" void *my_malloc(unsigned long, unsigned long); | ||
|
|
||
| struct NoPtr { | ||
| int x; | ||
| long y; | ||
| }; | ||
|
|
||
| struct WithPtr { | ||
| int a; | ||
| char *buf; | ||
| }; | ||
|
|
||
| int unevaluated_fn(); | ||
|
|
||
| // CHECK-LABEL: @_Z16test_builtin_intv( | ||
| // CHECK-CODEGEN: call i64 @llvm.alloc.token.id.i64(metadata ![[META_INT:[0-9]+]]) | ||
| // CHECK-LOWER: ret i64 0 | ||
| unsigned long test_builtin_int() { | ||
| return __builtin_infer_alloc_token(sizeof(1)); | ||
| } | ||
|
|
||
| // CHECK-LABEL: @_Z16test_builtin_ptrv( | ||
| // CHECK-CODEGEN: call i64 @llvm.alloc.token.id.i64(metadata ![[META_PTR:[0-9]+]]) | ||
| // CHECK-LOWER: ret i64 1 | ||
| unsigned long test_builtin_ptr() { | ||
| return __builtin_infer_alloc_token(sizeof(int *)); | ||
| } | ||
|
|
||
| // CHECK-LABEL: @_Z25test_builtin_struct_noptrv( | ||
| // CHECK-CODEGEN: call i64 @llvm.alloc.token.id.i64(metadata ![[META_NOPTR:[0-9]+]]) | ||
| // CHECK-LOWER: ret i64 0 | ||
| unsigned long test_builtin_struct_noptr() { | ||
| return __builtin_infer_alloc_token(sizeof(NoPtr)); | ||
| } | ||
|
|
||
| // CHECK-LABEL: @_Z25test_builtin_struct_w_ptrv( | ||
| // CHECK-CODEGEN: call i64 @llvm.alloc.token.id.i64(metadata ![[META_WITHPTR:[0-9]+]]) | ||
| // CHECK-LOWER: ret i64 1 | ||
| unsigned long test_builtin_struct_w_ptr() { | ||
| return __builtin_infer_alloc_token(sizeof(WithPtr), 123); | ||
| } | ||
|
|
||
| // CHECK-LABEL: @_Z24test_builtin_unevaluatedv( | ||
| // CHECK-NOT: call{{.*}}unevaluated_fn | ||
| // CHECK-CODEGEN: call i64 @llvm.alloc.token.id.i64(metadata ![[META_INT:[0-9]+]]) | ||
| // CHECK-LOWER: ret i64 0 | ||
| unsigned long test_builtin_unevaluated() { | ||
| return __builtin_infer_alloc_token(sizeof(int) * unevaluated_fn()); | ||
| } | ||
|
|
||
| // CHECK-LABEL: @_Z36test_builtin_unsequenced_unevaluatedi( | ||
| // CHECK: add nsw | ||
| // CHECK-NOT: add nsw | ||
| // CHECK-CODEGEN: %[[REG:[0-9]+]] = call i64 @llvm.alloc.token.id.i64(metadata ![[META_UNKNOWN:[0-9]+]]) | ||
| // CHECK-CODEGEN: call{{.*}}@my_malloc({{.*}}, i64 noundef %[[REG]]) | ||
| // CHECK-LOWER: call{{.*}}@my_malloc({{.*}}, i64 noundef 0) | ||
| void test_builtin_unsequenced_unevaluated(int x) { | ||
| my_malloc(++x, __builtin_infer_alloc_token(++x)); | ||
| } | ||
|
|
||
| // CHECK-LABEL: @_Z20test_builtin_unknownv( | ||
| // CHECK-CODEGEN: call i64 @llvm.alloc.token.id.i64(metadata ![[META_UNKNOWN:[0-9]+]]) | ||
| // CHECK-LOWER: ret i64 0 | ||
| unsigned long test_builtin_unknown() { | ||
| return __builtin_infer_alloc_token(4096); | ||
| } | ||
|
|
||
| // Test template instantiation. | ||
| template <typename T> | ||
| constexpr unsigned long get_token() { | ||
| return __builtin_infer_alloc_token(sizeof(T)); | ||
| } | ||
|
|
||
| // CHECK-LABEL: @_Z13get_token_intv() | ||
| // CHECK-CODEGEN: call i64 @llvm.alloc.token.id.i64(metadata ![[META_INT]]) | ||
| // CHECK-LOWER: ret i64 0 | ||
| unsigned long get_token_int() { | ||
| return get_token<int>(); | ||
| } | ||
|
|
||
| // CHECK-LABEL: @_Z13get_token_ptrv() | ||
| // CHECK-CODEGEN: call i64 @llvm.alloc.token.id.i64(metadata ![[META_PTR]]) | ||
| // CHECK-LOWER: ret i64 1 | ||
| unsigned long get_token_ptr() { | ||
| return get_token<int *>(); | ||
| } | ||
|
|
||
| // CHECK-CODEGEN: ![[META_INT]] = !{!"int", i1 false} | ||
| // CHECK-CODEGEN: ![[META_PTR]] = !{!"int *", i1 true} | ||
| // CHECK-CODEGEN: ![[META_NOPTR]] = !{!"NoPtr", i1 false} | ||
| // CHECK-CODEGEN: ![[META_WITHPTR]] = !{!"WithPtr", i1 true} | ||
| // CHECK-CODEGEN: ![[META_UNKNOWN]] = !{} | ||
Oops, something went wrong.
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.