diff options
| author | bors <bors@rust-lang.org> | 2020-10-04 21:08:06 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-10-04 21:08:06 +0000 |
| commit | beb5ae474d2835962ebdf7416bd1c9ad864fe101 (patch) | |
| tree | 0e85c63f96c094cb01ece1687dac8c91ae96b4b1 /src/test/codegen | |
| parent | 4ccf5f731bb71db3470002d6baf5ab4792b821d9 (diff) | |
| parent | e44784b8750016a695361c990024750e037d8f9f (diff) | |
| download | rust-beb5ae474d2835962ebdf7416bd1c9ad864fe101.tar.gz rust-beb5ae474d2835962ebdf7416bd1c9ad864fe101.zip | |
Auto merge of #77023 - HeroicKatora:len-missed-optimization, r=Mark-Simulacrum
Hint the maximum length permitted by invariant of slices One of the safety invariants of references, and in particular of references to slices, is that they may not cover more than `isize::MAX` bytes. The unsafe `from_raw_parts` constructors of slices explicitly requires the caller to guarantee this fact. Violating it would also be UB with regards to the semantics of generated llvm code. This effectively bounds the length of a (non-ZST) slice from above by a compile time constant. But when the length is loaded from a function argument it appears llvm is not aware of this requirement. The additional value range assertions allow some further elision of code branches, including overflow checks, especially in the presence of artithmetic on the indices. This may have a performance impact, adding more code to a common method but allowing more optimization. I'm not quite sure, is the Rust side of const-prop strong enough to elide the irrelevant match branches? Fixes: #67186
Diffstat (limited to 'src/test/codegen')
| -rw-r--r-- | src/test/codegen/len-is-bounded.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/test/codegen/len-is-bounded.rs b/src/test/codegen/len-is-bounded.rs new file mode 100644 index 00000000000..bb74fc3b275 --- /dev/null +++ b/src/test/codegen/len-is-bounded.rs @@ -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() +} |
