Skip to content

Commit fba722a

Browse files
authored
Rollup merge of rust-lang#107085 - tmiasko:custom-mir-operators, r=oli-obk
Custom MIR: Support binary and unary operations Lower binary and unary operations directly to corresponding unchecked MIR operations. Ultimately this might not be syntax we want, but it allows for experimentation in the meantime. r? ```@oli-obk``` ```@JakobDegen```
2 parents 24bcb15 + d3cfe97 commit fba722a

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

compiler/rustc_mir_build/src/build/custom/parse/instruction.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
147147
ExprKind::AddressOf { mutability, arg } => Ok(
148148
Rvalue::AddressOf(*mutability, self.parse_place(*arg)?)
149149
),
150+
ExprKind::Binary { op, lhs, rhs } => Ok(
151+
Rvalue::BinaryOp(*op, Box::new((self.parse_operand(*lhs)?, self.parse_operand(*rhs)?)))
152+
),
153+
ExprKind::Unary { op, arg } => Ok(
154+
Rvalue::UnaryOp(*op, self.parse_operand(*arg)?)
155+
),
150156
_ => self.parse_operand(expr_id).map(Rvalue::Use),
151157
)
152158
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// MIR for `f` after built
2+
3+
fn f(_1: i32, _2: bool) -> i32 {
4+
let mut _0: i32; // return place in scope 0 at $DIR/operators.rs:+0:30: +0:33
5+
6+
bb0: {
7+
_1 = Neg(_1); // scope 0 at $DIR/operators.rs:+2:9: +2:15
8+
_2 = Not(_2); // scope 0 at $DIR/operators.rs:+3:9: +3:15
9+
_1 = Add(_1, _1); // scope 0 at $DIR/operators.rs:+4:9: +4:18
10+
_1 = Sub(_1, _1); // scope 0 at $DIR/operators.rs:+5:9: +5:18
11+
_1 = Mul(_1, _1); // scope 0 at $DIR/operators.rs:+6:9: +6:18
12+
_1 = Div(_1, _1); // scope 0 at $DIR/operators.rs:+7:9: +7:18
13+
_1 = Rem(_1, _1); // scope 0 at $DIR/operators.rs:+8:9: +8:18
14+
_1 = BitXor(_1, _1); // scope 0 at $DIR/operators.rs:+9:9: +9:18
15+
_1 = BitAnd(_1, _1); // scope 0 at $DIR/operators.rs:+10:9: +10:18
16+
_1 = Shl(_1, _1); // scope 0 at $DIR/operators.rs:+11:9: +11:19
17+
_1 = Shr(_1, _1); // scope 0 at $DIR/operators.rs:+12:9: +12:19
18+
_2 = Eq(_1, _1); // scope 0 at $DIR/operators.rs:+13:9: +13:19
19+
_2 = Lt(_1, _1); // scope 0 at $DIR/operators.rs:+14:9: +14:18
20+
_2 = Le(_1, _1); // scope 0 at $DIR/operators.rs:+15:9: +15:19
21+
_2 = Ge(_1, _1); // scope 0 at $DIR/operators.rs:+16:9: +16:19
22+
_2 = Gt(_1, _1); // scope 0 at $DIR/operators.rs:+17:9: +17:18
23+
_0 = _1; // scope 0 at $DIR/operators.rs:+18:9: +18:16
24+
return; // scope 0 at $DIR/operators.rs:+19:9: +19:17
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// compile-flags: --crate-type=lib
2+
#![feature(custom_mir, core_intrinsics, inline_const)]
3+
use std::intrinsics::mir::*;
4+
5+
// EMIT_MIR operators.f.built.after.mir
6+
#[custom_mir(dialect = "built")]
7+
pub fn f(a: i32, b: bool) -> i32 {
8+
mir!({
9+
a = -a;
10+
b = !b;
11+
a = a + a;
12+
a = a - a;
13+
a = a * a;
14+
a = a / a;
15+
a = a % a;
16+
a = a ^ a;
17+
a = a & a;
18+
a = a << a;
19+
a = a >> a;
20+
b = a == a;
21+
b = a < a;
22+
b = a <= a;
23+
b = a >= a;
24+
b = a > a;
25+
RET = a;
26+
Return()
27+
})
28+
}

0 commit comments

Comments
 (0)