-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][CodeGen][UBSan] Fixing shift-exponent generation for _BitInt #80515
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
AdamMagierFOSS
merged 1 commit into
llvm:main
from
AdamMagierFOSS:ubsan-shift-exponent-fix
Feb 6, 2024
+51
−7
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// RUN: %clang_cc1 %s -O0 -fsanitize=shift-exponent -emit-llvm -std=c2x -triple=x86_64-unknown-linux -o - | FileCheck %s | ||
|
||
// Checking that the code generation is using the unextended/untruncated | ||
// exponent values and capping the values accordingly | ||
|
||
// CHECK-LABEL: define{{.*}} i32 @test_left_variable | ||
int test_left_variable(unsigned _BitInt(5) b, unsigned _BitInt(2) e) { | ||
// CHECK: [[E_REG:%.+]] = load [[E_SIZE:i2]] | ||
// CHECK: icmp ule [[E_SIZE]] [[E_REG]], -1 | ||
return b << e; | ||
} | ||
|
||
// CHECK-LABEL: define{{.*}} i32 @test_right_variable | ||
int test_right_variable(unsigned _BitInt(2) b, unsigned _BitInt(3) e) { | ||
// CHECK: [[E_REG:%.+]] = load [[E_SIZE:i3]] | ||
// CHECK: icmp ule [[E_SIZE]] [[E_REG]], 1 | ||
return b >> e; | ||
} | ||
|
||
// Old code generation would give false positives on left shifts when: | ||
// value(e) > (width(b) - 1 % 2 ** width(e)) | ||
// CHECK-LABEL: define{{.*}} i32 @test_left_literal | ||
int test_left_literal(unsigned _BitInt(5) b) { | ||
// CHECK-NOT: br i1 false, label %cont, label %handler.shift_out_of_bounds | ||
// CHECK: br i1 true, label %cont, label %handler.shift_out_of_bounds | ||
return b << 3uwb; | ||
} | ||
|
||
// Old code generation would give false positives on right shifts when: | ||
// (value(e) % 2 ** width(b)) < width(b) | ||
// CHECK-LABEL: define{{.*}} i32 @test_right_literal | ||
int test_right_literal(unsigned _BitInt(2) b) { | ||
// CHECK-NOT: br i1 true, label %cont, label %handler.shift_out_of_bounds | ||
// CHECK: br i1 false, label %cont, label %handler.shift_out_of_bounds | ||
return b >> 4uwb; | ||
} |
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.
So this test case show something that we perhaps should follow up an optimize later.
Not sure if the idea is to make the frontend simple at let the middle end optimize it away, but it is a bit stupid to emit the check here when comparing with -1.
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 tried to include this type of optimization in this patch but there's an assert that expects a check to be generated when the shift-exponent check is enabled. I suppose it wouldn't be too difficult to refactor this but from what I can tell none of the UBSan checks are really optimized in this way and instead rely on the middle end to optimize this out (e.g. the array-bounds check still generates a check on an array of size 256 when indexing with
uint8_t
). Don't know how much of an impact this type of pre-emptive optimization would have either.