Skip to content

Commit 7aff6fc

Browse files
committed
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](#13018 (comment)), 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).
2 parents 1558ae7 + 79a905e commit 7aff6fc

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/liballoc/rc.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,14 @@ trait RcBoxPtr<T: ?Sized> {
13591359

13601360
#[inline]
13611361
fn inc_strong(&self) {
1362-
self.inner().strong.set(self.strong().checked_add(1).unwrap_or_else(|| unsafe { abort() }));
1362+
// We want to abort on overflow instead of dropping the value.
1363+
// The reference count will never be zero when this is called;
1364+
// nevertheless, we insert an abort here to hint LLVM at
1365+
// an otherwise missed optimization.
1366+
if self.strong() == 0 || self.strong() == usize::max_value() {
1367+
unsafe { abort(); }
1368+
}
1369+
self.inner().strong.set(self.strong() + 1);
13631370
}
13641371

13651372
#[inline]
@@ -1374,7 +1381,14 @@ trait RcBoxPtr<T: ?Sized> {
13741381

13751382
#[inline]
13761383
fn inc_weak(&self) {
1377-
self.inner().weak.set(self.weak().checked_add(1).unwrap_or_else(|| unsafe { abort() }));
1384+
// We want to abort on overflow instead of dropping the value.
1385+
// The reference count will never be zero when this is called;
1386+
// nevertheless, we insert an abort here to hint LLVM at
1387+
// an otherwise missed optimization.
1388+
if self.weak() == 0 || self.weak() == usize::max_value() {
1389+
unsafe { abort(); }
1390+
}
1391+
self.inner().weak.set(self.weak() + 1);
13781392
}
13791393

13801394
#[inline]

src/test/codegen/issue-13018.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -O
12+
13+
// A drop([...].clone()) sequence on an Rc should be a no-op
14+
// In particular, no call to __rust_dealloc should be emitted
15+
#![crate_type = "lib"]
16+
use std::rc::Rc;
17+
18+
pub fn foo(t: &Rc<Vec<usize>>) {
19+
// CHECK-NOT: __rust_dealloc
20+
drop(t.clone());
21+
}

0 commit comments

Comments
 (0)