-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add tests for slice bounds check optimization #139129
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
bors
merged 1 commit into
rust-lang:master
from
reez12g:add-tests-for-slice-bounds-check-optimization
Apr 2, 2025
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//@ compile-flags: -Copt-level=3 | ||
//@ only-x86_64 | ||
//@ min-llvm-version: 20 | ||
#![crate_type = "lib"] | ||
|
||
// This test verifies that LLVM 20 properly optimizes the bounds check | ||
// when accessing the last few elements of a slice with proper conditions. | ||
// Previously, this would generate an unreachable branch to | ||
// slice_start_index_len_fail even when the bounds check was provably safe. | ||
|
||
// CHECK-LABEL: @last_four_initial( | ||
#[no_mangle] | ||
pub fn last_four_initial(s: &[u8]) -> &[u8] { | ||
// Previously this would generate a branch to slice_start_index_len_fail | ||
// that is unreachable. The LLVM 20 fix should eliminate this branch. | ||
// CHECK-NOT: slice_start_index_len_fail | ||
// CHECK-NOT: unreachable | ||
let start = if s.len() <= 4 { 0 } else { s.len() - 4 }; | ||
&s[start..] | ||
} | ||
|
||
// CHECK-LABEL: @last_four_optimized( | ||
#[no_mangle] | ||
pub fn last_four_optimized(s: &[u8]) -> &[u8] { | ||
// This version was already correctly optimized before the fix in LLVM 20. | ||
// CHECK-NOT: slice_start_index_len_fail | ||
// CHECK-NOT: unreachable | ||
if s.len() <= 4 { &s[0..] } else { &s[s.len() - 4..] } | ||
} | ||
|
||
// Just to verify we're correctly checking for the right thing | ||
// CHECK-LABEL: @test_bounds_check_happens( | ||
#[no_mangle] | ||
pub fn test_bounds_check_happens(s: &[u8], i: usize) -> &[u8] { | ||
// CHECK: slice_start_index_len_fail | ||
&s[i..] | ||
} | ||
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.