-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][builtin] Implement __builtin_allow_runtime_check #87568
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
vitalybuka
merged 23 commits into
main
from
users/vitalybuka/spr/clangbuiltin-implement-__builtin_allow_runtime_check
Apr 17, 2024
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
4deb37a
[𝘀𝗽𝗿] initial version
vitalybuka cc11b5c
[𝘀𝗽𝗿] changes to main this commit is based on
vitalybuka 7ca1d43
undo random change
vitalybuka 8bb91d8
drop one IgnoreParenImpCasts
vitalybuka d9d1b5e
[𝘀𝗽𝗿] changes introduced through rebase
vitalybuka 5a5de6f
fix ---
vitalybuka 6843b63
[𝘀𝗽𝗿] changes introduced through rebase
vitalybuka 92efd42
rebase
vitalybuka fe3bc24
[𝘀𝗽𝗿] changes introduced through rebase
vitalybuka 21dd7a8
rebase
vitalybuka 3eb49e4
[𝘀𝗽𝗿] changes introduced through rebase
vitalybuka a70607a
rebase
vitalybuka b4f0b3c
[𝘀𝗽𝗿] changes introduced through rebase
goldsteinn f8ba0d5
remove MDNode
vitalybuka 7ba991c
[𝘀𝗽𝗿] changes introduced through rebase
vitalybuka 3ecfd6c
rebase
vitalybuka 93133e2
[𝘀𝗽𝗿] changes introduced through rebase
vitalybuka c6167c3
update
vitalybuka 3143c5b
[𝘀𝗽𝗿] changes introduced through rebase
vitalybuka e7d855b
rebase
vitalybuka 5806a5a
Update clang/docs/LanguageExtensions.rst
vitalybuka 2523e11
rebase
vitalybuka 1b678ba
rebase
vitalybuka 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
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,29 @@ | ||
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 4 | ||
// RUN: %clang_cc1 -cc1 -triple x86_64-pc-linux-gnu -emit-llvm -o - %s | FileCheck %s | ||
|
||
static_assert(__has_builtin(__builtin_allow_runtime_check), ""); | ||
|
||
// CHECK-LABEL: define dso_local noundef zeroext i1 @_Z4testv( | ||
// CHECK-SAME: ) #[[ATTR0:[0-9]+]] { | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP0:%.*]] = call i1 @llvm.allow.runtime.check(metadata !"mycheck") | ||
// CHECK-NEXT: ret i1 [[TMP0]] | ||
// | ||
bool test() { | ||
return __builtin_allow_runtime_check("mycheck"); | ||
} | ||
|
||
// CHECK-LABEL: define dso_local noundef zeroext i1 @_Z10test_twicev( | ||
// CHECK-SAME: ) #[[ATTR0]] { | ||
// CHECK-NEXT: entry: | ||
// CHECK-NEXT: [[TMP0:%.*]] = call i1 @llvm.allow.runtime.check(metadata !"mycheck") | ||
// CHECK-NEXT: [[CONV:%.*]] = zext i1 [[TMP0]] to i32 | ||
// CHECK-NEXT: [[TMP1:%.*]] = call i1 @llvm.allow.runtime.check(metadata !"mycheck") | ||
// CHECK-NEXT: [[CONV1:%.*]] = zext i1 [[TMP1]] to i32 | ||
// CHECK-NEXT: [[OR:%.*]] = or i32 [[CONV]], [[CONV1]] | ||
// CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i32 [[OR]], 0 | ||
// CHECK-NEXT: ret i1 [[TOBOOL]] | ||
// | ||
bool test_twice() { | ||
return __builtin_allow_runtime_check("mycheck") | __builtin_allow_runtime_check("mycheck"); | ||
} |
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,24 @@ | ||
// RUN: %clang_cc1 -fsyntax-only -triple x86_64-pc-linux-gnu -verify %s | ||
// RUN: %clang_cc1 -fsyntax-only -triple aarch64-linux-gnu -verify %s | ||
|
||
extern const char *str; | ||
|
||
int main(void) { | ||
int r = 0; | ||
|
||
r |= __builtin_allow_runtime_check(); // expected-error {{too few arguments to function call}} | ||
|
||
r |= __builtin_allow_runtime_check(str); // expected-error {{expression is not a string literal}} | ||
|
||
r |= __builtin_allow_runtime_check(5); // expected-error {{incompatible integer to pointer conversion}} expected-error {{expression is not a string literal}} | ||
|
||
r |= __builtin_allow_runtime_check("a", "b"); // expected-error {{too many arguments to function call}} | ||
|
||
r |= __builtin_allow_runtime_check(""); | ||
|
||
r |= __builtin_allow_runtime_check("check"); | ||
|
||
str = __builtin_allow_runtime_check("check2"); // expected-error {{incompatible integer to pointer conversion}} | ||
|
||
return r; | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs aren't particularly clear to me -- why would the check at the current program location ever NOT be executed? What strings can be passed in? What compiler options impact it? Will it only accept string literals or are runtime values fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've started RFC, as @efriedma-quic suggested
https://discourse.llvm.org/t/rfc-introduce-new-clang-builtin-builtin-allow-runtime-check/78281
It needs to be compiler time lowered, so I think it should literals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, I think we want constant expression not just literal, right? e.g.,
is still able to be lowered at compile time. The reason I ask is because I'm thinking about template metaprogramming cases where you might want to do something along the lines of:
but maybe this is not a compelling use case? I don't insist on constant expression support, more just trying to verify that we support the expected usage patterns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how to do that. I would expected that from
__builtin_nan
, but it can't do that:https://godbolt.org/z/hWx47Gqvn
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar __builtin_cpu_is, also works only with literals.
The closest thing I see is c++26
static_assert
https://en.cppreference.com/w/cpp/language/static_assertAnd it's processed in a complicated way
EvaluateStaticAssertMessageAsString
https://godbolt.org/z/Gcf74Ysjs
It can try to port that code, but I'd keep as-is. We have a way to improve if use-case is important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, let's just skip this for now then, thank you!