Skip to content

Commit 5616460

Browse files
authored
Rollup merge of #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``
2 parents e21e455 + e515526 commit 5616460

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

core/src/intrinsics/mir.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
//! if you want your MIR to be modified by the full MIR pipeline, or `#![custom_mir(dialect =
4545
//! "runtime", phase = "optimized")] if you don't.
4646
//!
47-
//! [dialect docs]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/enum.MirPhase.html
47+
//! [dialect docs]:
48+
//! https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/enum.MirPhase.html
4849
//!
4950
//! The input to the [`mir!`] macro is:
5051
//!
@@ -99,6 +100,30 @@
99100
//! Return()
100101
//! })
101102
//! }
103+
//!
104+
//! #[custom_mir(dialect = "runtime", phase = "optimized")]
105+
//! fn push_and_pop<T>(v: &mut Vec<T>, value: T) {
106+
//! mir!(
107+
//! let unused;
108+
//! let popped;
109+
//!
110+
//! {
111+
//! Call(unused, pop, Vec::push(v, value))
112+
//! }
113+
//!
114+
//! pop = {
115+
//! Call(popped, drop, Vec::pop(v))
116+
//! }
117+
//!
118+
//! drop = {
119+
//! Drop(popped, ret)
120+
//! }
121+
//!
122+
//! ret = {
123+
//! Return()
124+
//! }
125+
//! )
126+
//! }
102127
//! ```
103128
//!
104129
//! We can also set off compilation failures that happen in sufficiently late stages of the
@@ -195,10 +220,16 @@
195220
//!
196221
//! #### Terminators
197222
//!
198-
//! - [`Goto`] and [`Return`] have associated functions.
223+
//! Custom MIR does not currently support cleanup blocks or non-trivial unwind paths. As such, there
224+
//! are no resume and abort terminators, and terminators that might unwind do not have any way to
225+
//! indicate the unwind block.
226+
//!
227+
//! - [`Goto`], [`Return`], [`Unreachable`], [`Drop`](Drop()), and [`DropAndReplace`] have associated functions.
199228
//! - `match some_int_operand` becomes a `SwitchInt`. Each arm should be `literal => basic_block`
200229
//! - The exception is the last arm, which must be `_ => basic_block` and corresponds to the
201230
//! otherwise branch.
231+
//! - [`Call`] has an associated function as well. The third argument of this function is a normal
232+
//! function call expresion, for example `my_other_function(a, 5)`.
202233
//!
203234
204235
#![unstable(
@@ -223,6 +254,10 @@ macro_rules! define {
223254

224255
define!("mir_return", fn Return() -> BasicBlock);
225256
define!("mir_goto", fn Goto(destination: BasicBlock) -> BasicBlock);
257+
define!("mir_unreachable", fn Unreachable() -> BasicBlock);
258+
define!("mir_drop", fn Drop<T>(place: T, goto: BasicBlock));
259+
define!("mir_drop_and_replace", fn DropAndReplace<T>(place: T, value: T, goto: BasicBlock));
260+
define!("mir_call", fn Call<T>(place: T, goto: BasicBlock, call: T));
226261
define!("mir_retag", fn Retag<T>(place: T));
227262
define!("mir_retag_raw", fn RetagRaw<T>(place: T));
228263
define!("mir_move", fn Move<T>(place: T) -> T);

0 commit comments

Comments
 (0)