forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#105814 - JakobDegen:custom-mir-terms, r=oli…
…-obk Support call and drop terminators in custom mir The only caveat with this change is that cleanup blocks are not supported. I would like to add them, but it's not quite clear to me what the best way to do that is, so I'll have to think about it some more. r? ``@oli-obk``
- Loading branch information
Showing
8 changed files
with
266 additions
and
2 deletions.
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>) { | ||
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)); | ||
} |