Skip to content

Commit

Permalink
custom mir: make it clear what the return block is
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Dec 26, 2023
1 parent e1fadb2 commit 066453e
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 46 deletions.
12 changes: 10 additions & 2 deletions compiler/rustc_mir_build/src/build/custom/parse/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
@call(mir_drop, args) => {
Ok(TerminatorKind::Drop {
place: self.parse_place(args[0])?,
target: self.parse_block(args[1])?,
target: self.parse_return_to(args[1])?,
unwind: self.parse_unwind_action(args[2])?,
replace: false,
})
Expand Down Expand Up @@ -104,6 +104,14 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
)
}

fn parse_return_to(&self, expr_id: ExprId) -> PResult<BasicBlock> {
parse_by_kind!(self, expr_id, _, "return block",
@call(mir_return_to, args) => {
self.parse_block(args[0])
},
)
}

fn parse_match(&self, arms: &[ArmId], span: Span) -> PResult<SwitchTargets> {
let Some((otherwise, rest)) = arms.split_last() else {
return Err(ParseError {
Expand Down Expand Up @@ -146,7 +154,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
ExprKind::Assign { lhs, rhs } => (*lhs, *rhs),
);
let destination = self.parse_place(destination)?;
let target = self.parse_block(args[1])?;
let target = self.parse_return_to(args[1])?;
let unwind = self.parse_unwind_action(args[2])?;

parse_by_kind!(self, call, _, "function call",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@ symbols! {
mir_offset,
mir_retag,
mir_return,
mir_return_to,
mir_set_discriminant,
mir_static,
mir_static_mut,
Expand Down
49 changes: 40 additions & 9 deletions library/core/src/intrinsics/mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,22 @@
//! }
//!
//! #[custom_mir(dialect = "runtime", phase = "optimized")]
#![cfg_attr(bootstrap, doc = "#[cfg(any())]")] // disable the following function in doctests when `bootstrap` is set
//! fn push_and_pop<T>(v: &mut Vec<T>, value: T) {
//! mir!(
//! let _unused;
//! let popped;
//!
//! {
//! Call(_unused = Vec::push(v, value), pop, UnwindContinue())
//! Call(_unused = Vec::push(v, value), ReturnTo(pop), UnwindContinue())
//! }
//!
//! pop = {
//! Call(popped = Vec::pop(v), drop, UnwindContinue())
//! Call(popped = Vec::pop(v), ReturnTo(drop), UnwindContinue())
//! }
//!
//! drop = {
//! Drop(popped, ret, UnwindContinue())
//! Drop(popped, ReturnTo(ret), UnwindContinue())
//! }
//!
//! ret = {
Expand Down Expand Up @@ -242,9 +243,8 @@
//! - `match some_int_operand` becomes a `SwitchInt`. Each arm should be `literal => basic_block`
//! - The exception is the last arm, which must be `_ => basic_block` and corresponds to the
//! otherwise branch.
//! - [`Call`] has an associated function as well. The third argument of this function is a normal
//! function call expression, for example `my_other_function(a, 5)`.
//!
//! - [`Call`] has an associated function as well, with special syntax:
//! `Call(ret_val = function(arg1, arg2, ...), ReturnTo(next_block), UnwindContinue())`.
#![unstable(
feature = "custom_mir",
Expand Down Expand Up @@ -295,7 +295,7 @@ define!(
define!(
"mir_unwind_unreachable",
/// An unwind action that triggers undefined behaviour.
fn UnwindUnreachable() -> BasicBlock
fn UnwindUnreachable()
);
define!(
"mir_unwind_terminate",
Expand All @@ -310,12 +310,43 @@ define!(
fn UnwindCleanup(goto: BasicBlock)
);

// Return destination for `Call`
define!("mir_return_to", fn ReturnTo(goto: BasicBlock));

// Terminators
define!("mir_return", fn Return() -> BasicBlock);
define!("mir_goto", fn Goto(destination: BasicBlock) -> BasicBlock);
define!("mir_unreachable", fn Unreachable() -> BasicBlock);
define!("mir_drop", fn Drop<T, U>(place: T, goto: BasicBlock, unwind_action: U));
define!("mir_call", fn Call<U>(call: (), goto: BasicBlock, unwind_action: U));
define!("mir_drop",
/// Drop the contents of a place.
///
/// The first argument must be a place.
///
/// The second argument must be of the form `ReturnTo(bb)`, where `bb` is the basic block that
/// will be jumped to after the destructor returns.
///
/// The third argument describes what happens on unwind. It can be one of:
/// - [`UnwindContinue`]
/// - [`UnwindUnreachable`]
/// - [`UnwindTerminate`]
/// - [`UnwindCleanup`]
fn Drop<T>(place: T, goto: (), unwind_action: ())
);
define!("mir_call",
/// Call a function.
///
/// The first argument must be of the form `ret_val = fun(arg1, arg2, ...)`.
///
/// The second argument must be of the form `ReturnTo(bb)`, where `bb` is the basic block that
/// will be jumped to after the function returns.
///
/// The third argument describes what happens on unwind. It can be one of:
/// - [`UnwindContinue`]
/// - [`UnwindUnreachable`]
/// - [`UnwindTerminate`]
/// - [`UnwindCleanup`]
fn Call(call: (), goto: (), unwind_action: ())
);
define!("mir_unwind_resume",
/// A terminator that resumes the unwinding.
fn UnwindResume()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn call(f: fn(NonZeroU32)) {
let tmp = ptr::addr_of!(c);
let ptr = tmp as *const NonZeroU32;
// The call site now is a NonZeroU32-to-u32 transmute.
Call(_res = f(*ptr), retblock, UnwindContinue()) //~ERROR: expected something greater or equal to 1
Call(_res = f(*ptr), ReturnTo(retblock), UnwindContinue()) //~ERROR: expected something greater or equal to 1
}
retblock = {
Return()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: Undefined Behavior: constructing invalid value: encountered 0, but expected something greater or equal to 1
--> $DIR/cast_fn_ptr_invalid_caller_arg.rs:LL:CC
|
LL | Call(_res = f(*ptr), retblock, UnwindContinue())
LL | Call(_res = f(*ptr), ReturnTo(retblock), UnwindContinue())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0, but expected something greater or equal to 1
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
Expand Down
8 changes: 4 additions & 4 deletions tests/mir-opt/building/custom/terminators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn ident<T>(t: T) -> T {
fn direct_call(x: i32) -> i32 {
mir!(
{
Call(RET = ident(x), retblock, UnwindContinue())
Call(RET = ident(x), ReturnTo(retblock), UnwindContinue())
}

retblock = {
Expand All @@ -27,7 +27,7 @@ fn direct_call(x: i32) -> i32 {
fn indirect_call(x: i32, f: fn(i32) -> i32) -> i32 {
mir!(
{
Call(RET = f(x), retblock, UnwindContinue())
Call(RET = f(x), ReturnTo(retblock), UnwindContinue())
}

retblock = {
Expand All @@ -49,7 +49,7 @@ impl<'a> Drop for WriteOnDrop<'a> {
fn drop_first<'a>(a: WriteOnDrop<'a>, b: WriteOnDrop<'a>) {
mir!(
{
Drop(a, retblock, UnwindContinue())
Drop(a, ReturnTo(retblock), UnwindContinue())
}

retblock = {
Expand All @@ -64,7 +64,7 @@ fn drop_first<'a>(a: WriteOnDrop<'a>, b: WriteOnDrop<'a>) {
fn drop_second<'a>(a: WriteOnDrop<'a>, b: WriteOnDrop<'a>) {
mir!(
{
Drop(b, retblock, UnwindContinue())
Drop(b, ReturnTo(retblock), UnwindContinue())
}

retblock = {
Expand Down
8 changes: 4 additions & 4 deletions tests/mir-opt/building/custom/unwind_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use core::intrinsics::mir::*;
pub fn a() {
mir!(
{
Call(RET = a(), bb1, UnwindUnreachable())
Call(RET = a(), ReturnTo(bb1), UnwindUnreachable())
}
bb1 = {
Return()
Expand All @@ -26,7 +26,7 @@ pub fn a() {
pub fn b() {
mir!(
{
Call(RET = b(), bb1, UnwindContinue())
Call(RET = b(), ReturnTo(bb1), UnwindContinue())
}
bb1 = {
Return()
Expand All @@ -41,7 +41,7 @@ pub fn b() {
pub fn c() {
mir!(
{
Call(RET = c(), bb1, UnwindTerminate(ReasonAbi))
Call(RET = c(), ReturnTo(bb1), UnwindTerminate(ReasonAbi))
}
bb1 = {
Return()
Expand All @@ -56,7 +56,7 @@ pub fn c() {
pub fn d() {
mir!(
{
Call(RET = d(), bb1, UnwindCleanup(bb2))
Call(RET = d(), ReturnTo(bb1), UnwindCleanup(bb2))
}
bb1 = {
Return()
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/copy-prop/borrowed_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ fn f() -> bool {
let b = a;
// We cannot propagate the place `a`.
let r2 = &b;
Call(RET = cmp_ref(r1, r2), next, UnwindContinue())
Call(RET = cmp_ref(r1, r2), ReturnTo(next), UnwindContinue())
}
next = {
// But we can propagate the value `a`.
Call(RET = opaque(b), ret, UnwindContinue())
Call(RET = opaque(b), ReturnTo(ret), UnwindContinue())
}
ret = {
Return()
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/copy-prop/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn multiple_edges(t: bool) -> u8 {
match t { true => bbt, _ => ret }
}
bbt = {
Call(x = dummy(13), ret, UnwindContinue())
Call(x = dummy(13), ReturnTo(ret), UnwindContinue())
}
ret = {
// `x` is not assigned on the `bb0 -> ret` edge,
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/copy-prop/custom_move_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ struct NotCopy(bool);
fn f(_1: NotCopy) {
mir!({
let _2 = _1;
Call(RET = opaque(Move(_1)), bb1, UnwindContinue())
Call(RET = opaque(Move(_1)), ReturnTo(bb1), UnwindContinue())
}
bb1 = {
let _3 = Move(_2);
Call(RET = opaque(_3), bb2, UnwindContinue())
Call(RET = opaque(_3), ReturnTo(bb2), UnwindContinue())
}
bb2 = {
Return()
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/copy-prop/move_projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ fn f(a: Foo) -> bool {
let b = a;
// This is a move out of a copy, so must become a copy of `a.0`.
let c = Move(b.0);
Call(RET = opaque(Move(a)), bb1, UnwindContinue())
Call(RET = opaque(Move(a)), ReturnTo(bb1), UnwindContinue())
}
bb1 = {
Call(RET = opaque(Move(c)), ret, UnwindContinue())
Call(RET = opaque(Move(c)), ReturnTo(ret), UnwindContinue())
}
ret = {
Return()
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/dead-store-elimination/call_arg_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Packed {
fn move_packed(packed: Packed) {
mir!(
{
Call(RET = use_both(0, packed.y), ret, UnwindContinue())
Call(RET = use_both(0, packed.y), ReturnTo(ret), UnwindContinue())
}
ret = {
Return()
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/dead-store-elimination/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn cycle(mut x: i32, mut y: i32, mut z: i32) {
mir!(
let condition: bool;
{
Call(condition = cond(), bb1, UnwindContinue())
Call(condition = cond(), ReturnTo(bb1), UnwindContinue())
}
bb1 = {
match condition { true => bb2, _ => ret }
Expand All @@ -30,7 +30,7 @@ fn cycle(mut x: i32, mut y: i32, mut z: i32) {
z = y;
y = x;
x = temp;
Call(condition = cond(), bb1, UnwindContinue())
Call(condition = cond(), ReturnTo(bb1), UnwindContinue())
}
ret = {
Return()
Expand Down
12 changes: 6 additions & 6 deletions tests/mir-opt/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,31 +529,31 @@ fn duplicate_slice() -> (bool, bool) {
// CHECK: [[a:_.*]] = (const "a",);
// CHECK: [[au:_.*]] = ([[a]].0: &str) as u128 (Transmute);
let a = ("a",);
Call(au = transmute::<_, u128>(a.0), bb1, UnwindContinue())
Call(au = transmute::<_, u128>(a.0), ReturnTo(bb1), UnwindContinue())
}
bb1 = {
// CHECK: [[c:_.*]] = identity::<&str>(([[a]].0: &str))
Call(c = identity(a.0), bb2, UnwindContinue())
Call(c = identity(a.0), ReturnTo(bb2), UnwindContinue())
}
bb2 = {
// CHECK: [[cu:_.*]] = [[c]] as u128 (Transmute);
Call(cu = transmute::<_, u128>(c), bb3, UnwindContinue())
Call(cu = transmute::<_, u128>(c), ReturnTo(bb3), UnwindContinue())
}
bb3 = {
// This slice is different from `a.0`. Hence `bu` is not `au`.
// CHECK: [[b:_.*]] = const "a";
// CHECK: [[bu:_.*]] = [[b]] as u128 (Transmute);
let b = "a";
Call(bu = transmute::<_, u128>(b), bb4, UnwindContinue())
Call(bu = transmute::<_, u128>(b), ReturnTo(bb4), UnwindContinue())
}
bb4 = {
// This returns a copy of `b`, which is not `a`.
// CHECK: [[d:_.*]] = identity::<&str>([[b]])
Call(d = identity(b), bb5, UnwindContinue())
Call(d = identity(b), ReturnTo(bb5), UnwindContinue())
}
bb5 = {
// CHECK: [[du:_.*]] = [[d]] as u128 (Transmute);
Call(du = transmute::<_, u128>(d), bb6, UnwindContinue())
Call(du = transmute::<_, u128>(d), ReturnTo(bb6), UnwindContinue())
}
bb6 = {
// `direct` must not fold to `true`, as `indirect` will not.
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/inline/indirect_destination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn f(a: *mut u8) {
Goto(bb1)
}
bb1 = {
Call(*a = g(), bb1, UnwindUnreachable())
Call(*a = g(), ReturnTo(bb1), UnwindUnreachable())
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/mir-opt/reference_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ fn multiple_storage() {
// As there are multiple `StorageLive` statements for `x`, we cannot know if this `z`'s
// pointer address is the address of `x`, so do nothing.
let y = *z;
Call(RET = opaque(y), retblock, UnwindContinue())
Call(RET = opaque(y), ReturnTo(retblock), UnwindContinue())
}

retblock = {
Expand Down Expand Up @@ -724,7 +724,7 @@ fn dominate_storage() {
}
bb1 = {
let c = *r;
Call(RET = opaque(c), bb2, UnwindContinue())
Call(RET = opaque(c), ReturnTo(bb2), UnwindContinue())
}
bb2 = {
StorageDead(x);
Expand Down Expand Up @@ -760,18 +760,18 @@ fn maybe_dead(m: bool) {
bb1 = {
StorageDead(x);
StorageDead(y);
Call(RET = opaque(u), bb2, UnwindContinue())
Call(RET = opaque(u), ReturnTo(bb2), UnwindContinue())
}
bb2 = {
// As `x` may be `StorageDead`, `a` may be dangling, so we do nothing.
let z = *a;
Call(RET = opaque(z), bb3, UnwindContinue())
Call(RET = opaque(z), ReturnTo(bb3), UnwindContinue())
}
bb3 = {
// As `y` may be `StorageDead`, `b` may be dangling, so we do nothing.
// This implies that we also do not substitute `b` in `bb0`.
let t = *b;
Call(RET = opaque(t), retblock, UnwindContinue())
Call(RET = opaque(t), ReturnTo(retblock), UnwindContinue())
}
retblock = {
Return()
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mir/ssa_call_ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn f() -> u32 {
mir!(
let a: u32;
{
Call(a = g(), bb1, UnwindCleanup(bb2))
Call(a = g(), ReturnTo(bb1), UnwindCleanup(bb2))
}
bb1 = {
RET = a;
Expand Down
Loading

0 comments on commit 066453e

Please sign in to comment.