Skip to content
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

add const_allocate intrinsic #79594

Merged
merged 6 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions compiler/rustc_mir/src/const_eval/machine.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustc_middle::mir;
use rustc_middle::ty::layout::HasTyCtxt;
use rustc_middle::ty::InstanceDef;
use rustc_middle::ty::{self, Ty};
use std::borrow::Borrow;
use std::collections::hash_map::Entry;
Expand Down Expand Up @@ -231,8 +232,13 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
if ecx.tcx.is_const_fn_raw(def.did) {
// If this function is a `const fn` then under certain circumstances we
// can evaluate call via the query system, thus memoizing all future calls.
if ecx.try_eval_const_fn_call(instance, ret, args)? {
return Ok(None);
match instance.def {
InstanceDef::Intrinsic(_) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

ah sorry, I meant to run this check within try_eval_const_fn_call and return Ok(false) from it if it's not an intrinsic

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand this new code. Don't we have to just entirely remove memoization?

Copy link
Contributor

Choose a reason for hiding this comment

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

we still memoize calls to size_of and align_of I think

Copy link
Member

Choose a reason for hiding this comment

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

Is that worth it all the code complexity here? Those functions are trivial to evaluate...

if ecx.try_eval_const_fn_call(instance, ret, args)? {
return Ok(None);
}
}
_ => {}
}
} else {
// Some functions we support even if they are non-const -- but avoid testing
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {

let align = match Align::from_bytes(align) {
Ok(a) => a,
Err(err) => bug!("align has to power of 2, {}", err),
Err(err) => throw_ub_format!("align has to be a power of 2, {}", err),
};

let ptr =
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir/src/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<T: MayLeak> MayLeak for MemoryKind<T> {
fn may_leak(self) -> bool {
match self {
MemoryKind::Stack => false,
MemoryKind::Heap => true,
MemoryKind::Heap => false,
MemoryKind::Vtable => true,
MemoryKind::CallerLocation => true,
MemoryKind::Machine(k) => k.may_leak(),
Expand All @@ -54,7 +54,7 @@ impl<T: fmt::Display> fmt::Display for MemoryKind<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MemoryKind::Stack => write!(f, "stack variable"),
MemoryKind::Heap => write!(f, "heap variable"),
MemoryKind::Heap => write!(f, "heap allocation"),
MemoryKind::Vtable => write!(f, "vtable"),
MemoryKind::CallerLocation => write!(f, "caller location"),
MemoryKind::Machine(m) => write!(f, "{}", m),
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1734,7 +1734,7 @@ extern "rust-intrinsic" {
pub fn ptr_guaranteed_ne<T>(ptr: *const T, other: *const T) -> bool;

/// Allocate at compile time. Should not be called at runtime.
#[rustc_const_unstable(feature = "const_heap", issue = "none")]
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
#[cfg(not(bootstrap))]
pub fn const_allocate(size: usize, align: usize) -> *mut u8;
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/consts/const-eval/heap/alloc_intrinsic_errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![feature(core_intrinsics)]
#![feature(const_heap)]
#![feature(const_raw_ptr_deref)]
#![feature(const_mut_refs)]
use std::intrinsics;

const FOO: i32 = foo();
const fn foo() -> i32 {
unsafe {
let _ = intrinsics::const_allocate(4, 3) as * mut i32;
//~^ error: any use of this value will cause an error [const_err]
}
1

}

fn main() {}
17 changes: 17 additions & 0 deletions src/test/ui/consts/const-eval/heap/alloc_intrinsic_errors.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: any use of this value will cause an error
--> $DIR/alloc_intrinsic_errors.rs:10:17
|
LL | const FOO: i32 = foo();
| -----------------------
...
LL | let _ = intrinsics::const_allocate(4, 3) as * mut i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| align has to be a power of 2, `3` is not a power of 2
| inside `foo` at $DIR/alloc_intrinsic_errors.rs:10:17
| inside `FOO` at $DIR/alloc_intrinsic_errors.rs:7:18
|
= note: `#[deny(const_err)]` on by default

error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// run-pass
#![feature(core_intrinsics)]
#![feature(const_heap)]
#![feature(const_raw_ptr_deref)]
#![feature(const_mut_refs)]
use std::intrinsics;

const FOO: *const i32 = foo();
Copy link
Contributor

Choose a reason for hiding this comment

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

It's super curious that this doesn't error with the same error that src/test/ui/consts/const-eval/heap/alloc_intrinsic_untyped.rs emits: "untyped pointer".

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh... I think I know why... foo is a function without any arguments, and is thus not actually called but treated as a constant itself and its body is thus evaluated directly:

// For the moment we only do this for functions which take no arguments
// (or all arguments are ZSTs) so that we don't memoize too much.
if args.iter().any(|a| !a.layout.is_zst()) {
return Ok(false);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we'll need to disable that optimization for function items now

Copy link
Member

Choose a reason for hiding this comment

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

I guess we'll need to disable that optimization for function items now

Why that?

Copy link
Contributor

Choose a reason for hiding this comment

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

This test memoizes function alls to foo(), even though every call should be producing a new heap allocation. Otherwise all calls to Box::new_uninit() will end up with the same heap allocation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Basically if you used

const fn foo() -> &'static i32 {
    const fn bar() -> *mut i32 {
        unsafe { intrinsics::const_allocate(4, 4) as *mut i32 }
    }
    unsafe {
        let i = bar();
        *i = 20;
        ...
    }
}

the *i = 20 will fail to interpret because you'd be modifying interned memory

Copy link
Member

Choose a reason for hiding this comment

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

Oh, lol... yeah, const fn without inputs are no longer pure with this change, That's kind of a big deal actually. We certainly need T-lang approval before stabilizing this.

//~^ error: untyped pointers are not allowed in constant

const fn foo() -> &'static i32 {
let t = unsafe {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: untyped pointers are not allowed in constant
--> $DIR/alloc_intrinsic_nontransient.rs:7:1
|
LL | const FOO: *const i32 = foo();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error