-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Support call and drop terminators in custom mir #105814
Merged
Merged
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
17 changes: 17 additions & 0 deletions
17
src/test/mir-opt/building/custom/terminators.assert_nonzero.built.after.mir
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,17 @@ | ||
// MIR for `assert_nonzero` after built | ||
|
||
fn assert_nonzero(_1: i32) -> () { | ||
let mut _0: (); // return place in scope 0 at $DIR/terminators.rs:+0:27: +0:27 | ||
|
||
bb0: { | ||
switchInt(_1) -> [0: bb1, otherwise: bb2]; // scope 0 at $DIR/terminators.rs:+3:13: +6:14 | ||
} | ||
|
||
bb1: { | ||
unreachable; // scope 0 at $DIR/terminators.rs:+10:13: +10:26 | ||
} | ||
|
||
bb2: { | ||
return; // scope 0 at $DIR/terminators.rs:+14:13: +14:21 | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/test/mir-opt/building/custom/terminators.direct_call.built.after.mir
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,16 @@ | ||
// MIR for `direct_call` after built | ||
|
||
fn direct_call(_1: i32) -> i32 { | ||
let mut _0: i32; // return place in scope 0 at $DIR/terminators.rs:+0:27: +0:30 | ||
|
||
bb0: { | ||
_0 = ident::<i32>(_1) -> bb1; // scope 0 at $DIR/terminators.rs:+3:13: +3:42 | ||
// mir::Constant | ||
// + span: $DIR/terminators.rs:15:33: 15:38 | ||
// + literal: Const { ty: fn(i32) -> i32 {ident::<i32>}, val: Value(<ZST>) } | ||
} | ||
|
||
bb1: { | ||
return; // scope 0 at $DIR/terminators.rs:+7:13: +7:21 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/mir-opt/building/custom/terminators.drop_first.built.after.mir
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,13 @@ | ||
// MIR for `drop_first` after built | ||
|
||
fn drop_first(_1: WriteOnDrop<'_>, _2: WriteOnDrop<'_>) -> () { | ||
let mut _0: (); // return place in scope 0 at $DIR/terminators.rs:+0:59: +0:59 | ||
|
||
bb0: { | ||
replace(_1 <- move _2) -> bb1; // scope 0 at $DIR/terminators.rs:+3:13: +3:49 | ||
} | ||
|
||
bb1: { | ||
return; // scope 0 at $DIR/terminators.rs:+7:13: +7:21 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/mir-opt/building/custom/terminators.drop_second.built.after.mir
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,13 @@ | ||
// MIR for `drop_second` after built | ||
|
||
fn drop_second(_1: WriteOnDrop<'_>, _2: WriteOnDrop<'_>) -> () { | ||
let mut _0: (); // return place in scope 0 at $DIR/terminators.rs:+0:60: +0:60 | ||
|
||
bb0: { | ||
drop(_2) -> bb1; // scope 0 at $DIR/terminators.rs:+3:13: +3:30 | ||
} | ||
|
||
bb1: { | ||
return; // scope 0 at $DIR/terminators.rs:+7:13: +7:21 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/mir-opt/building/custom/terminators.indirect_call.built.after.mir
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,13 @@ | ||
// MIR for `indirect_call` after built | ||
|
||
fn indirect_call(_1: i32, _2: fn(i32) -> i32) -> i32 { | ||
let mut _0: i32; // return place in scope 0 at $DIR/terminators.rs:+0:48: +0:51 | ||
|
||
bb0: { | ||
_0 = _2(_1) -> bb1; // scope 0 at $DIR/terminators.rs:+3:13: +3:38 | ||
} | ||
|
||
bb1: { | ||
return; // scope 0 at $DIR/terminators.rs:+7:13: +7:21 | ||
} | ||
} |
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,108 @@ | ||
#![feature(custom_mir, core_intrinsics)] | ||
|
||
extern crate core; | ||
use core::intrinsics::mir::*; | ||
|
||
fn ident<T>(t: T) -> T { | ||
t | ||
} | ||
|
||
// EMIT_MIR terminators.direct_call.built.after.mir | ||
#[custom_mir(dialect = "built")] | ||
fn direct_call(x: i32) -> i32 { | ||
mir!( | ||
{ | ||
Call(RET, retblock, ident(x)) | ||
} | ||
|
||
retblock = { | ||
Return() | ||
} | ||
) | ||
} | ||
|
||
// EMIT_MIR terminators.indirect_call.built.after.mir | ||
#[custom_mir(dialect = "built")] | ||
fn indirect_call(x: i32, f: fn(i32) -> i32) -> i32 { | ||
mir!( | ||
{ | ||
Call(RET, retblock, f(x)) | ||
} | ||
|
||
retblock = { | ||
Return() | ||
} | ||
) | ||
} | ||
|
||
struct WriteOnDrop<'a>(&'a mut i32, i32); | ||
|
||
impl<'a> Drop for WriteOnDrop<'a> { | ||
fn drop(&mut self) { | ||
*self.0 = self.1; | ||
} | ||
} | ||
|
||
// EMIT_MIR terminators.drop_first.built.after.mir | ||
#[custom_mir(dialect = "built")] | ||
fn drop_first<'a>(a: WriteOnDrop<'a>, b: WriteOnDrop<'a>) { | ||
mir!( | ||
{ | ||
DropAndReplace(a, Move(b), retblock) | ||
} | ||
|
||
retblock = { | ||
Return() | ||
} | ||
) | ||
} | ||
|
||
// EMIT_MIR terminators.drop_second.built.after.mir | ||
#[custom_mir(dialect = "built")] | ||
fn drop_second<'a>(a: WriteOnDrop<'a>, b: WriteOnDrop<'a>) { | ||
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 can write mem::forget without an intrinsic now 😄 |
||
mir!( | ||
{ | ||
Drop(b, retblock) | ||
} | ||
|
||
retblock = { | ||
Return() | ||
} | ||
) | ||
} | ||
|
||
// EMIT_MIR terminators.assert_nonzero.built.after.mir | ||
#[custom_mir(dialect = "built")] | ||
fn assert_nonzero(a: i32) { | ||
mir!( | ||
{ | ||
match a { | ||
0 => unreachable, | ||
_ => retblock | ||
} | ||
} | ||
|
||
unreachable = { | ||
Unreachable() | ||
} | ||
|
||
retblock = { | ||
Return() | ||
} | ||
) | ||
} | ||
|
||
fn main() { | ||
assert_eq!(direct_call(5), 5); | ||
assert_eq!(indirect_call(5, ident), 5); | ||
|
||
let mut a = 0; | ||
let mut b = 0; | ||
drop_first(WriteOnDrop(&mut a, 1), WriteOnDrop(&mut b, 1)); | ||
assert_eq!((a, b), (1, 0)); | ||
|
||
let mut a = 0; | ||
let mut b = 0; | ||
drop_second(WriteOnDrop(&mut a, 1), WriteOnDrop(&mut b, 1)); | ||
assert_eq!((a, b), (0, 1)); | ||
} |
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.
I think it would be neat if we actually supported
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.
Yeah, I thought about this. There's a couple reasons I'm hesitant to do it:
=>
instead of->
, because of follow set rules. That's fine, although it's not the most intuitive syntax-> pop, Unreachable()
,-> pop, Abort()
, or-> pop, other_block
are fine though?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.
Oh yea, I don't think that syntax should happen in this PR and at best any such syntax should desugar to what you have now.