Skip to content

Tell LLVM about NonZero integer types #54995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
#[stable(feature = "nonzero", since = "1.28.0")]
#[inline]
pub fn get(self) -> $Int {
self.0 .0
let r = self.0 .0;
unsafe { intrinsics::assume(r != 0) };
r
}

}
Expand Down
23 changes: 23 additions & 0 deletions src/test/codegen/nonzero-assume.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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

#![crate_type = "lib"]

use std::num::NonZeroU64;

// CHECK-LABEL: @assume_nonzero
#[no_mangle]
pub fn assume_nonzero(x: u64, y: NonZeroU64) -> u64 {
x / y.get()
// CHECK: icmp ne i64 %y, 0
// CHECK: @llvm.assume
Copy link
Member

@scottmcm scottmcm Oct 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth a CHECK-NOT of some sort that there's no branch/select/panic/whatever?

Also, cc @rkruppe who might have thoughts here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the assume is just a hint and might get removed at some stage of the pipeline in the future. Checking for lack of the branch is the more correct way to test this here.

}