about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-08-21 06:40:20 +0000
committerbors <bors@rust-lang.org>2018-08-21 06:40:20 +0000
commit70c33bb8e2649a019e48515128a0b447dfe66f6b (patch)
tree14d1b86a3a2203429aa468a70de5f783b74caaeb /src
parent1558ae7cfd5e1190d3388dcc6f0f734589e4e478 (diff)
parent79a905ef305b1c3048ad2535887951721ab65f5c (diff)
downloadrust-70c33bb8e2649a019e48515128a0b447dfe66f6b.tar.gz
rust-70c33bb8e2649a019e48515128a0b447dfe66f6b.zip
Auto merge of #53080 - hermord:rc-opt, r=alexcrichton
Change `Rc::inc_{weak,strong}` to better hint optimization to LLVM

As discussed in #13018, `Rc::inc_strong` and `Rc::inc_weak` are changed to allow compositions of `clone` and `drop` to be better optimized. Almost entirely as in [this comment](https://github.com/rust-lang/rust/issues/13018#issuecomment-408642184), except that `abort` on zero is added so that a `drop(t.clone())` does not produce a zero check followed by conditional deallocation.

This is different from #21418 in that it doesn't rely on `assume`, avoiding the prohibitive compilation slowdown.

[Before and after IR](https://gist.github.com/hermord/266e55451b7fe0bb8caa6e35d17c86e1).
Diffstat (limited to 'src')
-rw-r--r--src/liballoc/rc.rs18
-rw-r--r--src/test/codegen/issue-13018.rs21
2 files changed, 37 insertions, 2 deletions
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs
index be049eb6e5e..82e1c92359c 100644
--- a/src/liballoc/rc.rs
+++ b/src/liballoc/rc.rs
@@ -1359,7 +1359,14 @@ trait RcBoxPtr<T: ?Sized> {
 
     #[inline]
     fn inc_strong(&self) {
-        self.inner().strong.set(self.strong().checked_add(1).unwrap_or_else(|| unsafe { abort() }));
+        // We want to abort on overflow instead of dropping the value.
+        // The reference count will never be zero when this is called;
+        // nevertheless, we insert an abort here to hint LLVM at
+        // an otherwise missed optimization.
+        if self.strong() == 0 || self.strong() == usize::max_value() {
+            unsafe { abort(); }
+        }
+        self.inner().strong.set(self.strong() + 1);
     }
 
     #[inline]
@@ -1374,7 +1381,14 @@ trait RcBoxPtr<T: ?Sized> {
 
     #[inline]
     fn inc_weak(&self) {
-        self.inner().weak.set(self.weak().checked_add(1).unwrap_or_else(|| unsafe { abort() }));
+        // We want to abort on overflow instead of dropping the value.
+        // The reference count will never be zero when this is called;
+        // nevertheless, we insert an abort here to hint LLVM at
+        // an otherwise missed optimization.
+        if self.weak() == 0 || self.weak() == usize::max_value() {
+            unsafe { abort(); }
+        }
+        self.inner().weak.set(self.weak() + 1);
     }
 
     #[inline]
diff --git a/src/test/codegen/issue-13018.rs b/src/test/codegen/issue-13018.rs
new file mode 100644
index 00000000000..702b9545794
--- /dev/null
+++ b/src/test/codegen/issue-13018.rs
@@ -0,0 +1,21 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: -O
+
+// A drop([...].clone()) sequence on an Rc should be a no-op
+// In particular, no call to __rust_dealloc should be emitted
+#![crate_type = "lib"]
+use std::rc::Rc;
+
+pub fn foo(t: &Rc<Vec<usize>>) {
+// CHECK-NOT: __rust_dealloc
+    drop(t.clone());
+}