Skip to content

Commit c822e91

Browse files
authored
Rollup merge of rust-lang#76973 - lzutao:unstably-const-assume, r=oli-obk
Unstably allow assume intrinsic in const contexts Not sure much about this usage because there are concerns about [blocking optimization][1] and [slowing down LLVM][2] when using `assme` intrinsic in inline functions. But since Oli suggested in rust-lang#76960 (comment), here we are. [1]: rust-lang#54995 (comment) [2]: rust-lang#49572 (comment)
2 parents 7fe4750 + 382d724 commit c822e91

File tree

10 files changed

+39
-15
lines changed

10 files changed

+39
-15
lines changed

compiler/rustc_codegen_ssa/src/traits/intrinsic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use rustc_span::Span;
55
use rustc_target::abi::call::FnAbi;
66

77
pub trait IntrinsicCallMethods<'tcx>: BackendTypes {
8-
/// Remember to add all intrinsics here, in librustc_typeck/check/mod.rs,
9-
/// and in libcore/intrinsics.rs; if you need access to any llvm intrinsics,
10-
/// add them to librustc_codegen_llvm/context.rs
8+
/// Remember to add all intrinsics here, in `compiler/rustc_typeck/src/check/mod.rs`,
9+
/// and in `library/core/src/intrinsics.rs`; if you need access to any LLVM intrinsics,
10+
/// add them to `compiler/rustc_codegen_llvm/src/context.rs`.
1111
fn codegen_intrinsic_call(
1212
&mut self,
1313
instance: ty::Instance<'tcx>,

compiler/rustc_error_codes/src/error_codes/E0092.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ extern "rust-intrinsic" {
1212
```
1313

1414
Please check you didn't make a mistake in the function's name. All intrinsic
15-
functions are defined in `librustc_codegen_llvm/intrinsic.rs` and in
16-
`libcore/intrinsics.rs` in the Rust source code. Example:
15+
functions are defined in `compiler/rustc_codegen_llvm/src/intrinsic.rs` and in
16+
`library/core/src/intrinsics.rs` in the Rust source code. Example:
1717

1818
```
1919
#![feature(intrinsics)]

compiler/rustc_error_codes/src/error_codes/E0093.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ fn main() {
1717
```
1818

1919
Please check you didn't make a mistake in the function's name. All intrinsic
20-
functions are defined in `librustc_codegen_llvm/intrinsic.rs` and in
21-
`libcore/intrinsics.rs` in the Rust source code. Example:
20+
functions are defined in `compiler/rustc_codegen_llvm/src/intrinsic.rs` and in
21+
`library/core/src/intrinsics.rs` in the Rust source code. Example:
2222

2323
```
2424
#![feature(intrinsics)]

compiler/rustc_mir/src/interpret/intrinsics.rs

+6
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
435435
// These just return their argument
436436
self.copy_op(args[0], dest)?;
437437
}
438+
sym::assume => {
439+
let cond = self.read_scalar(args[0])?.check_init()?.to_bool()?;
440+
if !cond {
441+
throw_ub_format!("`assume` intrinsic called with `false`");
442+
}
443+
}
438444
_ => return Ok(false),
439445
}
440446

compiler/rustc_mir/src/interpret/terminator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
390390
ty::InstanceDef::Virtual(_, idx) => {
391391
let mut args = args.to_vec();
392392
// We have to implement all "object safe receivers". Currently we
393-
// support built-in pointers (&, &mut, Box) as well as unsized-self. We do
393+
// support built-in pointers `(&, &mut, Box)` as well as unsized-self. We do
394394
// not yet support custom self types.
395-
// Also see librustc_codegen_llvm/abi.rs and librustc_codegen_llvm/mir/block.rs.
395+
// Also see `compiler/rustc_codegen_llvm/src/abi.rs` and `compiler/rustc_codegen_ssa/src/mir/block.rs`.
396396
let receiver_place = match args[0].layout.ty.builtin_deref(true) {
397397
Some(_) => {
398398
// Built-in pointer.

compiler/rustc_typeck/src/check/intrinsic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ pub fn intrinsic_operation_unsafety(intrinsic: Symbol) -> hir::Unsafety {
106106
}
107107
}
108108

109-
/// Remember to add all intrinsics here, in librustc_codegen_llvm/intrinsic.rs,
110-
/// and in libcore/intrinsics.rs
109+
/// Remember to add all intrinsics here, in `compiler/rustc_codegen_llvm/src/intrinsic.rs`,
110+
/// and in `library/core/src/intrinsics.rs`.
111111
pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
112112
let param = |n| tcx.mk_ty_param(n, Symbol::intern(&format!("P{}", n)));
113113
let def_id = tcx.hir().local_def_id(it.hir_id).to_def_id();

library/core/src/intrinsics.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Compiler intrinsics.
22
//!
3-
//! The corresponding definitions are in `librustc_codegen_llvm/intrinsic.rs`.
4-
//! The corresponding const implementations are in `librustc_mir/interpret/intrinsics.rs`
3+
//! The corresponding definitions are in `compiler/rustc_codegen_llvm/src/intrinsic.rs`.
4+
//! The corresponding const implementations are in `compiler/rustc_mir/src/interpret/intrinsics.rs`
55
//!
66
//! # Const intrinsics
77
//!
@@ -10,7 +10,7 @@
1010
//!
1111
//! In order to make an intrinsic usable at compile-time, one needs to copy the implementation
1212
//! from https://github.com/rust-lang/miri/blob/master/src/shims/intrinsics.rs to
13-
//! `librustc_mir/interpret/intrinsics.rs` and add a
13+
//! `compiler/rustc_mir/src/interpret/intrinsics.rs` and add a
1414
//! `#[rustc_const_unstable(feature = "foo", issue = "01234")]` to the intrinsic.
1515
//!
1616
//! If an intrinsic is supposed to be used from a `const fn` with a `rustc_const_stable` attribute,
@@ -733,6 +733,7 @@ extern "rust-intrinsic" {
733733
/// own, or if it does not enable any significant optimizations.
734734
///
735735
/// This intrinsic does not have a stable counterpart.
736+
#[rustc_const_unstable(feature = "const_assume", issue = "76972")]
736737
pub fn assume(b: bool);
737738

738739
/// Hints to the compiler that branch condition is likely to be true.

library/core/tests/intrinsics.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::any::TypeId;
2+
use core::intrinsics::assume;
23

34
#[test]
45
fn test_typeid_sized_types() {
@@ -20,3 +21,17 @@ fn test_typeid_unsized_types() {
2021
assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
2122
assert!(TypeId::of::<X>() != TypeId::of::<Y>());
2223
}
24+
25+
// Check that `const_assume` feature allow `assume` intrinsic
26+
// to be used in const contexts.
27+
#[test]
28+
fn test_assume_can_be_in_const_contexts() {
29+
const unsafe fn foo(x: usize, y: usize) -> usize {
30+
// SAFETY: the entire function is not safe,
31+
// but it is just an example not used elsewhere.
32+
unsafe { assume(y != 0) };
33+
x / y
34+
}
35+
let rs = unsafe { foo(42, 97) };
36+
assert_eq!(rs, 0);
37+
}

library/core/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#![feature(bound_cloned)]
99
#![feature(box_syntax)]
1010
#![feature(cell_update)]
11+
#![feature(const_assume)]
12+
#![feature(core_intrinsics)]
1113
#![feature(core_private_bignum)]
1214
#![feature(core_private_diy_float)]
1315
#![feature(debug_non_exhaustive)]

library/panic_unwind/src/seh.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub struct _TypeDescriptor {
175175
// to be able to catch Rust panics by simply declaring a `struct rust_panic`.
176176
//
177177
// When modifying, make sure that the type name string exactly matches
178-
// the one used in src/librustc_codegen_llvm/intrinsic.rs.
178+
// the one used in `compiler/rustc_codegen_llvm/src/intrinsic.rs`.
179179
const TYPE_NAME: [u8; 11] = *b"rust_panic\0";
180180

181181
static mut THROW_INFO: _ThrowInfo = _ThrowInfo {

0 commit comments

Comments
 (0)