-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #75529 - bugadani:bounds-check, r=nagisa
Eliminate some other bound checks when index comes from an enum #36962 introduced an assumption for the upper limit of the enum's value. This PR adds an assumption to the lower value as well. I've modified the original codegen test to show that derived (in that case, adding 1) values also don't generate bounds checks. However, this test is actually carefully crafted to not hit a bug: if the enum's variants are modified to 1 and 2 instead of 2 and 3, the test fails by adding a bounds check. I suppose this is an LLVM issue and #75525, while not exactly in this context should be tracking it. I'm not at all confident if this patch can be accepted, or even if it _should_ be accepted in this state. But I'm curious about what others think :) ~Improves~ Should improve #13926 but does not close it because it's not exactly predictable, where bounds checks may pop up against the assumptions.
- Loading branch information
Showing
4 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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,25 @@ | ||
// This test checks an optimization that is not guaranteed to work. This test case should not block | ||
// a future LLVM update. | ||
// compile-flags: -O | ||
// min-llvm-version: 11.0 | ||
|
||
#![crate_type = "lib"] | ||
|
||
pub enum Bar { | ||
A = 1, | ||
B = 3, | ||
} | ||
|
||
// CHECK-LABEL: @lookup_inc | ||
#[no_mangle] | ||
pub fn lookup_inc(buf: &[u8; 5], f: Bar) -> u8 { | ||
// CHECK-NOT: panic_bounds_check | ||
buf[f as usize + 1] | ||
} | ||
|
||
// CHECK-LABEL: @lookup_dec | ||
#[no_mangle] | ||
pub fn lookup_dec(buf: &[u8; 5], f: Bar) -> u8 { | ||
// CHECK-NOT: panic_bounds_check | ||
buf[f as usize - 1] | ||
} |
This file contains 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,19 @@ | ||
// This test checks an optimization that is not guaranteed to work. This test case should not block | ||
// a future LLVM update. | ||
// compile-flags: -O | ||
// min-llvm-version: 11.0 | ||
|
||
#![crate_type = "lib"] | ||
|
||
#[repr(u8)] | ||
pub enum Exception { | ||
Low = 5, | ||
High = 10, | ||
} | ||
|
||
// CHECK-LABEL: @access | ||
#[no_mangle] | ||
pub fn access(array: &[usize; 12], exc: Exception) -> usize { | ||
// CHECK-NOT: panic_bounds_check | ||
array[(exc as u8 - 4) as usize] | ||
} |
This file contains 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