about summary refs log tree commit diff
path: root/src/test/codegen
diff options
context:
space:
mode:
authorAndreas Molzer <andreas.molzer@gmx.de>2020-09-20 17:22:17 +0200
committerAndreas Molzer <andreas.molzer@gmx.de>2020-10-04 20:43:36 +0200
commite44784b8750016a695361c990024750e037d8f9f (patch)
tree60652ccb46cce808024badc56173593400be44a4 /src/test/codegen
parentd92d28e523bf056ab4eb752510ec52fe4f1c6311 (diff)
downloadrust-e44784b8750016a695361c990024750e037d8f9f.tar.gz
rust-e44784b8750016a695361c990024750e037d8f9f.zip
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 #67186
Diffstat (limited to 'src/test/codegen')
-rw-r--r--src/test/codegen/len-is-bounded.rs24
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()
+}