-
Notifications
You must be signed in to change notification settings - Fork 13.6k
add const_allocate intrinsic #79594
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
Changes from 2 commits
528355c
b5b811a
a6c4cbd
1b7fe09
899a59e
bc6eb6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,7 +104,7 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>>( | |
// This match is just a canary for future changes to `MemoryKind`, which most likely need | ||
// changes in this function. | ||
match kind { | ||
MemoryKind::Stack | MemoryKind::Vtable | MemoryKind::CallerLocation => {} | ||
MemoryKind::Stack | MemoryKind::Heap | MemoryKind::Vtable | MemoryKind::CallerLocation => {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should Miri also use this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, good point. I think miri will need its own heap variant to prevent us from accidentally mixing the two somewhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @oli-obk so what do you think about using a non- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you are worried about miri breakage, we can also do that change to an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No I think this is fine (but please leave a FIXME in the |
||
} | ||
// Set allocation mutability as appropriate. This is used by LLVM to put things into | ||
// read-only memory, and also by Miri when evaluating other globals that | ||
|
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() {} |
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||||||||
#![feature(core_intrinsics)] | ||||||||||||
#![feature(const_heap)] | ||||||||||||
#![feature(const_raw_ptr_deref)] | ||||||||||||
#![feature(const_mut_refs)] | ||||||||||||
use std::intrinsics; | ||||||||||||
|
||||||||||||
const FOO: *const i32 = foo(); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh... I think I know why... rust/compiler/rustc_mir/src/const_eval/machine.rs Lines 40 to 44 in 75f1db1
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test memoizes function alls to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, lol... yeah, |
||||||||||||
//~^ error: untyped pointers are not allowed in constant | ||||||||||||
|
||||||||||||
const fn foo() -> &'static i32 { | ||||||||||||
let t = unsafe { | ||||||||||||
let i = intrinsics::const_allocate(4, 4) as * mut i32; | ||||||||||||
*i = 20; | ||||||||||||
i | ||||||||||||
}; | ||||||||||||
unsafe { &*t } | ||||||||||||
} | ||||||||||||
fn main() { | ||||||||||||
assert_eq!(unsafe { *FOO }, 20) | ||||||||||||
} |
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// run-pass | ||
#![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 { | ||
let t = unsafe { | ||
let i = intrinsics::const_allocate(4, 4) as * mut i32; | ||
*i = 20; | ||
i | ||
}; | ||
unsafe { *t } | ||
} | ||
fn main() { | ||
assert_eq!(FOO, 20); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// compile-test | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(const_raw_ptr_deref)] | ||
#![feature(const_mut_refs)] | ||
use std::intrinsics; | ||
|
||
const BAR: &i32 = unsafe { &*(intrinsics::const_allocate(4, 4) as *mut i32) }; | ||
//~^ error: it is undefined behavior to use this value | ||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error[E0080]: it is undefined behavior to use this value | ||
--> $DIR/alloc_intrinsic_uninit.rs:8:1 | ||
| | ||
LL | const BAR: &i32 = unsafe { &*(intrinsics::const_allocate(4, 4) as *mut i32) }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes at .<deref>, but expected initialized plain (non-pointer) bytes | ||
| | ||
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(const_raw_ptr_deref)] | ||
#![feature(const_mut_refs)] | ||
use std::intrinsics; | ||
|
||
const BAR: *mut i32 = unsafe { intrinsics::const_allocate(4, 4) as *mut i32}; | ||
//~^ error: untyped pointers are not allowed in constant | ||
|
||
fn main() {} |
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_untyped.rs:7:1 | ||
| | ||
LL | const BAR: *mut i32 = unsafe { intrinsics::const_allocate(4, 4) as *mut i32}; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
There was a problem hiding this comment.
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 returnOk(false)
from it if it's not an intrinsicThere was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
andalign_of
I thinkThere was a problem hiding this comment.
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...