Skip to content

Commit d77a4b0

Browse files
authored
Rollup merge of rust-lang#120171 - cjgillot:jump-threading-assume-assert, r=tmiasko
Fix assume and assert in jump threading r? `@tmiasko`
2 parents 6a534f0 + afaac75 commit d77a4b0

11 files changed

+209
-15
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
2020
@call(mir_storage_dead, args) => {
2121
Ok(StatementKind::StorageDead(self.parse_local(args[0])?))
2222
},
23+
@call(mir_assume, args) => {
24+
let op = self.parse_operand(args[0])?;
25+
Ok(StatementKind::Intrinsic(Box::new(NonDivergingIntrinsic::Assume(op))))
26+
},
2327
@call(mir_deinit, args) => {
2428
Ok(StatementKind::Deinit(Box::new(self.parse_place(args[0])?)))
2529
},

compiler/rustc_mir_transform/src/jump_threading.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -566,11 +566,6 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
566566
cost: &CostChecker<'_, 'tcx>,
567567
depth: usize,
568568
) {
569-
let register_opportunity = |c: Condition| {
570-
debug!(?bb, ?c.target, "register");
571-
self.opportunities.push(ThreadingOpportunity { chain: vec![bb], target: c.target })
572-
};
573-
574569
let term = self.body.basic_blocks[bb].terminator();
575570
let place_to_flood = match term.kind {
576571
// We come from a target, so those are not possible.
@@ -592,16 +587,8 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
592587
// Flood the overwritten place, and progress through.
593588
TerminatorKind::Drop { place: destination, .. }
594589
| TerminatorKind::Call { destination, .. } => Some(destination),
595-
// Treat as an `assume(cond == expected)`.
596-
TerminatorKind::Assert { ref cond, expected, .. } => {
597-
if let Some(place) = cond.place()
598-
&& let Some(conditions) = state.try_get(place.as_ref(), self.map)
599-
{
600-
let expected = if expected { ScalarInt::TRUE } else { ScalarInt::FALSE };
601-
conditions.iter_matches(expected).for_each(register_opportunity);
602-
}
603-
None
604-
}
590+
// Ignore, as this can be a no-op at codegen time.
591+
TerminatorKind::Assert { .. } => None,
605592
};
606593

607594
// We can recurse through this terminator.

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,7 @@ symbols! {
10281028
minnumf32,
10291029
minnumf64,
10301030
mips_target_feature,
1031+
mir_assume,
10311032
mir_basic_block,
10321033
mir_call,
10331034
mir_cast_transmute,

library/core/src/intrinsics/mir.rs

+2
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ define!("mir_unwind_resume",
357357

358358
define!("mir_storage_live", fn StorageLive<T>(local: T));
359359
define!("mir_storage_dead", fn StorageDead<T>(local: T));
360+
#[cfg(not(bootstrap))]
361+
define!("mir_assume", fn Assume(operand: bool));
360362
define!("mir_deinit", fn Deinit<T>(place: T));
361363
define!("mir_checked", fn Checked<T>(binop: T) -> (T, bool));
362364
define!("mir_len", fn Len<T>(place: T) -> usize);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MIR for `assume_constant` after built
2+
3+
fn assume_constant() -> () {
4+
let mut _0: ();
5+
6+
bb0: {
7+
assume(const true);
8+
return;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MIR for `assume_local` after built
2+
3+
fn assume_local(_1: bool) -> () {
4+
let mut _0: ();
5+
6+
bb0: {
7+
assume(_1);
8+
return;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MIR for `assume_place` after built
2+
3+
fn assume_place(_1: (bool, u8)) -> () {
4+
let mut _0: ();
5+
6+
bb0: {
7+
assume((_1.0: bool));
8+
return;
9+
}
10+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// skip-filecheck
2+
#![feature(custom_mir, core_intrinsics)]
3+
4+
extern crate core;
5+
use core::intrinsics::mir::*;
6+
7+
// EMIT_MIR assume.assume_local.built.after.mir
8+
#[custom_mir(dialect = "built")]
9+
fn assume_local(x: bool) {
10+
mir!(
11+
{
12+
Assume(x);
13+
Return()
14+
}
15+
)
16+
}
17+
18+
// EMIT_MIR assume.assume_place.built.after.mir
19+
#[custom_mir(dialect = "built")]
20+
fn assume_place(p: (bool, u8)) {
21+
mir!(
22+
{
23+
Assume(p.0);
24+
Return()
25+
}
26+
)
27+
}
28+
29+
// EMIT_MIR assume.assume_constant.built.after.mir
30+
#[custom_mir(dialect = "built")]
31+
fn assume_constant() {
32+
mir!(
33+
{
34+
Assume(true);
35+
Return()
36+
}
37+
)
38+
}
39+
40+
fn main() {
41+
assume_local(true);
42+
assume_place((true, 50));
43+
assume_constant();
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
- // MIR for `assume` before JumpThreading
2+
+ // MIR for `assume` after JumpThreading
3+
4+
fn assume(_1: u8, _2: bool) -> u8 {
5+
let mut _0: u8;
6+
7+
bb0: {
8+
switchInt(_1) -> [7: bb1, otherwise: bb2];
9+
}
10+
11+
bb1: {
12+
assume(_2);
13+
- goto -> bb3;
14+
+ goto -> bb6;
15+
}
16+
17+
bb2: {
18+
goto -> bb3;
19+
}
20+
21+
bb3: {
22+
switchInt(_2) -> [0: bb4, otherwise: bb5];
23+
}
24+
25+
bb4: {
26+
_0 = const 4_u8;
27+
return;
28+
}
29+
30+
bb5: {
31+
_0 = const 5_u8;
32+
return;
33+
+ }
34+
+
35+
+ bb6: {
36+
+ goto -> bb5;
37+
}
38+
}
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
- // MIR for `assume` before JumpThreading
2+
+ // MIR for `assume` after JumpThreading
3+
4+
fn assume(_1: u8, _2: bool) -> u8 {
5+
let mut _0: u8;
6+
7+
bb0: {
8+
switchInt(_1) -> [7: bb1, otherwise: bb2];
9+
}
10+
11+
bb1: {
12+
assume(_2);
13+
- goto -> bb3;
14+
+ goto -> bb6;
15+
}
16+
17+
bb2: {
18+
goto -> bb3;
19+
}
20+
21+
bb3: {
22+
switchInt(_2) -> [0: bb4, otherwise: bb5];
23+
}
24+
25+
bb4: {
26+
_0 = const 4_u8;
27+
return;
28+
}
29+
30+
bb5: {
31+
_0 = const 5_u8;
32+
return;
33+
+ }
34+
+
35+
+ bb6: {
36+
+ goto -> bb5;
37+
}
38+
}
39+

tests/mir-opt/jump_threading.rs

+48
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,52 @@ fn aggregate(x: u8) -> u8 {
468468
}
469469
}
470470

471+
/// Verify that we can leverage the existence of an `Assume` terminator.
472+
#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
473+
fn assume(a: u8, b: bool) -> u8 {
474+
// CHECK-LABEL: fn assume(
475+
mir!(
476+
{
477+
// CHECK: bb0: {
478+
// CHECK-NEXT: switchInt(_1) -> [7: bb1, otherwise: bb2]
479+
match a { 7 => bb1, _ => bb2 }
480+
}
481+
bb1 = {
482+
// CHECK: bb1: {
483+
// CHECK-NEXT: assume(_2);
484+
// CHECK-NEXT: goto -> bb6;
485+
Assume(b);
486+
Goto(bb3)
487+
}
488+
bb2 = {
489+
// CHECK: bb2: {
490+
// CHECK-NEXT: goto -> bb3;
491+
Goto(bb3)
492+
}
493+
bb3 = {
494+
// CHECK: bb3: {
495+
// CHECK-NEXT: switchInt(_2) -> [0: bb4, otherwise: bb5];
496+
match b { false => bb4, _ => bb5 }
497+
}
498+
bb4 = {
499+
// CHECK: bb4: {
500+
// CHECK-NEXT: _0 = const 4_u8;
501+
// CHECK-NEXT: return;
502+
RET = 4;
503+
Return()
504+
}
505+
bb5 = {
506+
// CHECK: bb5: {
507+
// CHECK-NEXT: _0 = const 5_u8;
508+
// CHECK-NEXT: return;
509+
RET = 5;
510+
Return()
511+
}
512+
// CHECK: bb6: {
513+
// CHECK-NEXT: goto -> bb5;
514+
)
515+
}
516+
471517
fn main() {
472518
// CHECK-LABEL: fn main(
473519
too_complex(Ok(0));
@@ -481,6 +527,7 @@ fn main() {
481527
renumbered_bb(true);
482528
disappearing_bb(7);
483529
aggregate(7);
530+
assume(7, false);
484531
}
485532

486533
// EMIT_MIR jump_threading.too_complex.JumpThreading.diff
@@ -494,3 +541,4 @@ fn main() {
494541
// EMIT_MIR jump_threading.renumbered_bb.JumpThreading.diff
495542
// EMIT_MIR jump_threading.disappearing_bb.JumpThreading.diff
496543
// EMIT_MIR jump_threading.aggregate.JumpThreading.diff
544+
// EMIT_MIR jump_threading.assume.JumpThreading.diff

0 commit comments

Comments
 (0)