forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assume slice len is bounded by allocation size
Uses assume to check the length against a constant upper bound. The inlined result then informs the optimizer of the sound value range. This was tried with unreachable_unchecked before which introduces a branch. This has the advantage of not being executed in sound code but complicates basic blocks. It resulted in ~2% increased compile time in some worst cases. Add a codegen test for the assumption, testing the issue from rust-lang#67186
- Loading branch information
1 parent
d92d28e
commit e44784b
Showing
3 changed files
with
51 additions
and
3 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
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,24 @@ | ||
// min-llvm-version: 11.0 | ||
// compile-flags: -O -C panic=abort | ||
#![crate_type = "lib"] | ||
|
||
#[no_mangle] | ||
pub fn len_range(a: &[u8], b: &[u8]) -> usize { | ||
// CHECK-NOT: panic | ||
a.len().checked_add(b.len()).unwrap() | ||
} | ||
|
||
#[no_mangle] | ||
pub fn len_range_on_non_byte(a: &[u16], b: &[u16]) -> usize { | ||
// CHECK-NOT: panic | ||
a.len().checked_add(b.len()).unwrap() | ||
} | ||
|
||
pub struct Zst; | ||
|
||
#[no_mangle] | ||
pub fn zst_range(a: &[Zst], b: &[Zst]) -> usize { | ||
// Zsts may be arbitrarily large. | ||
// CHECK: panic | ||
a.len().checked_add(b.len()).unwrap() | ||
} |