-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Allow reifying intrinsics to fn pointers. (rebase of #86699) #126595
Open
GrigorenkoPV
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
GrigorenkoPV:reify-intrinsics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+264
−65
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/mir-opt/lower_intrinsics.core.intrinsics-#1-discriminant_value.LowerIntrinsics.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
- // MIR for `discriminant_value` before LowerIntrinsics | ||
+ // MIR for `discriminant_value` after LowerIntrinsics | ||
|
||
fn discriminant_value(_1: &T) -> <T as DiscriminantKind>::Discriminant { | ||
let mut _0: <T as std::marker::DiscriminantKind>::Discriminant; // return place in scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
|
||
bb0: { | ||
- _0 = discriminant_value::<T>(move _1) -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
- // mir::Constant | ||
- // + span: $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
- // + literal: Const { ty: for<'r> extern "rust-intrinsic" fn(&'r T) -> <T as std::marker::DiscriminantKind>::Discriminant {std::intrinsics::discriminant_value::<T>}, val: Value(Scalar(<ZST>)) } | ||
+ _0 = discriminant((*_1)); // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
+ goto -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
|
||
bb1: { | ||
return; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
|
||
bb2 (cleanup): { | ||
resume; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/mir-opt/lower_intrinsics.core.intrinsics-#1-forget.LowerIntrinsics.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
- // MIR for `std::intrinsics::forget` before LowerIntrinsics | ||
+ // MIR for `std::intrinsics::forget` after LowerIntrinsics | ||
|
||
fn std::intrinsics::forget(_1: T) -> () { | ||
let mut _0: (); // return place in scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
|
||
bb0: { | ||
- _0 = std::intrinsics::forget::<T>(move _1) -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
- // mir::Constant | ||
- // + span: $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
- // + literal: Const { ty: extern "rust-intrinsic" fn(T) {std::intrinsics::forget::<T>}, val: Value(Scalar(<ZST>)) } | ||
+ _0 = const (); // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
+ goto -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
|
||
bb1: { | ||
return; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
|
||
bb2 (cleanup): { | ||
resume; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/mir-opt/lower_intrinsics.core.intrinsics-#1-size_of.LowerIntrinsics.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
- // MIR for `std::intrinsics::size_of` before LowerIntrinsics | ||
+ // MIR for `std::intrinsics::size_of` after LowerIntrinsics | ||
|
||
fn std::intrinsics::size_of() -> usize { | ||
let mut _0: usize; // return place in scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
|
||
bb0: { | ||
- _0 = std::intrinsics::size_of::<T>() -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
- // mir::Constant | ||
- // + span: $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
- // + literal: Const { ty: extern "rust-intrinsic" fn() -> usize {std::intrinsics::size_of::<T>}, val: Value(Scalar(<ZST>)) } | ||
+ _0 = SizeOf(T); // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
+ goto -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
|
||
bb1: { | ||
return; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
|
||
bb2 (cleanup): { | ||
resume; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/mir-opt/lower_intrinsics.core.intrinsics-#1-unreachable.LowerIntrinsics.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
- // MIR for `std::intrinsics::unreachable` before LowerIntrinsics | ||
+ // MIR for `std::intrinsics::unreachable` after LowerIntrinsics | ||
|
||
fn std::intrinsics::unreachable() -> ! { | ||
let mut _0: !; // return place in scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
|
||
bb0: { | ||
- _0 = std::intrinsics::unreachable() -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
- // mir::Constant | ||
- // + span: $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
- // + literal: Const { ty: unsafe extern "rust-intrinsic" fn() -> ! {std::intrinsics::unreachable}, val: Value(Scalar(<ZST>)) } | ||
+ unreachable; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
|
||
bb1: { | ||
return; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
|
||
bb2 (cleanup): { | ||
resume; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/mir-opt/lower_intrinsics.core.intrinsics-#1-wrapping_add.LowerIntrinsics.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
- // MIR for `wrapping_add` before LowerIntrinsics | ||
+ // MIR for `wrapping_add` after LowerIntrinsics | ||
|
||
fn wrapping_add(_1: T, _2: T) -> T { | ||
let mut _0: T; // return place in scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
|
||
bb0: { | ||
- _0 = wrapping_add::<T>(move _1, move _2) -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
- // mir::Constant | ||
- // + span: $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
- // + literal: Const { ty: extern "rust-intrinsic" fn(T, T) -> T {std::intrinsics::wrapping_add::<T>}, val: Value(Scalar(<ZST>)) } | ||
+ _0 = Add(move _1, move _2); // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
+ goto -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
|
||
bb1: { | ||
return; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
|
||
bb2 (cleanup): { | ||
resume; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -267,3 +267,15 @@ pub fn get_metadata(a: *const i32, b: *const [u8], c: *const dyn std::fmt::Debug | |
let _usize = ptr_metadata(b); | ||
let _vtable = ptr_metadata(c); | ||
} | ||
|
||
// Check that the MIR shims used for reifying intrinsics to `fn` pointers, | ||
// also go through the lowering pass. | ||
pub fn reify_intrinsics() -> impl Copy { | ||
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. Maybe
? 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. Well, I think this should be a |
||
( | ||
core::intrinsics::wrapping_add::<u32> as unsafe fn(_, _) -> _, | ||
core::intrinsics::size_of::<u8> as unsafe fn() -> _, | ||
core::intrinsics::unreachable as unsafe fn() -> !, | ||
core::intrinsics::forget::<E> as unsafe fn(_), | ||
core::intrinsics::discriminant_value::<E> as unsafe fn(_) -> _, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,46 @@ | ||
//@ check-fail | ||
//@ run-pass | ||
|
||
#![feature(core_intrinsics, intrinsics)] | ||
|
||
fn a() { | ||
let _: unsafe fn(isize) -> usize = std::mem::transmute; | ||
//~^ ERROR cannot coerce | ||
// NOTE(eddyb) `#[inline(never)]` and returning `fn` pointers from functions is | ||
// done to force codegen (of the reification-to-`fn`-ptr shims around intrinsics). | ||
|
||
#[inline(never)] | ||
fn a() -> unsafe fn(isize) -> usize { | ||
let f: unsafe fn(isize) -> usize = std::mem::transmute; | ||
f | ||
} | ||
|
||
fn b() { | ||
let _ = std::mem::transmute as unsafe fn(isize) -> usize; | ||
//~^ ERROR casting | ||
#[inline(never)] | ||
fn b() -> unsafe fn(isize) -> usize { | ||
let f = std::mem::transmute as unsafe fn(isize) -> usize; | ||
f | ||
} | ||
|
||
fn c() { | ||
let _: [unsafe fn(f32) -> f32; 2] = [ | ||
std::intrinsics::floorf32, //~ ERROR cannot coerce | ||
#[inline(never)] | ||
fn c() -> [unsafe fn(f32) -> f32; 2] { | ||
let fs = [ | ||
std::intrinsics::floorf32, | ||
std::intrinsics::log2f32, | ||
]; | ||
fs | ||
} | ||
|
||
fn call_by_ptr() { | ||
let ptr: fn(u8, u8) -> u8 = std::intrinsics::wrapping_add::<u8>; | ||
let res = ptr(u8::MAX, 1); | ||
assert_eq!(res, 0); | ||
} | ||
|
||
fn main() {} | ||
fn main() { | ||
unsafe { | ||
assert_eq!(a()(-1), !0); | ||
assert_eq!(b()(-1), !0); | ||
|
||
let [floorf32_ptr, log2f32_ptr] = c(); | ||
assert_eq!(floorf32_ptr(1.5), 1.0); | ||
assert_eq!(log2f32_ptr(2.0), 1.0); | ||
} | ||
|
||
call_by_ptr(); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why was this added?
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.
Actually, I know why. I wonder if it's better to place this in
build_call_shim
, or even make our own function likebuild_reified_intrinsic
.