Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c180af9

Browse files
committed
Auto merge of rust-lang#139162 - scottmcm:always-pretty, r=<try>
What if we always run `ReorderBasicBlocks`? Obviously we don't *need* to run this, but I keep being sad when I look at MIR on the playground that it's really awkward to read because you have to jump around all over because whenever something's inlined it's put at the end. So what if we just do it when we're optimizing anyway? Can we afford it?
2 parents 3c0f722 + 701d9fa commit c180af9

21 files changed

+208
-203
lines changed

compiler/rustc_mir_transform/src/prettify.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ use rustc_session::Session;
1818
pub(super) struct ReorderBasicBlocks;
1919

2020
impl<'tcx> crate::MirPass<'tcx> for ReorderBasicBlocks {
21-
fn is_enabled(&self, _session: &Session) -> bool {
22-
false
21+
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
22+
sess.mir_opt_level() > 1
2323
}
2424

2525
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
26-
let rpo: IndexVec<BasicBlock, BasicBlock> =
27-
body.basic_blocks.reverse_postorder().iter().copied().collect();
26+
let rpo = IndexSlice::from_raw(body.basic_blocks.reverse_postorder());
2827
if rpo.iter().is_sorted() {
2928
return;
3029
}

tests/codegen/hint/cold_path.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ pub fn test1(x: bool) {
2626
}
2727

2828
// CHECK-LABEL: @test1(
29-
// CHECK: br i1 %x, label %bb1, label %bb2, !prof ![[NUM:[0-9]+]]
30-
// CHECK: bb2:
31-
// CHECK: path_b
29+
// CHECK: br i1 %x, label %bb3, label %bb1, !prof ![[NUM:[0-9]+]]
3230
// CHECK: bb1:
31+
// CHECK: path_b
32+
// CHECK: bb3:
3333
// CHECK: path_a
3434
}
3535

@@ -44,10 +44,11 @@ pub fn test2(x: i32) {
4444
}
4545

4646
// CHECK-LABEL: @test2(
47-
// CHECK: br i1 %_2, label %bb2, label %bb1, !prof ![[NUM]]
47+
// CHECK: %_2 = icmp sgt i32 %x, 0
48+
// CHECK: br i1 %_2, label %bb3, label %bb1, !prof ![[NUM]]
4849
// CHECK: bb1:
4950
// CHECK: path_b
50-
// CHECK: bb2:
51+
// CHECK: bb3:
5152
// CHECK: path_a
5253
}
5354

tests/codegen/hint/likely.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ pub fn test1(x: bool) {
2525
}
2626

2727
// CHECK-LABEL: @test1(
28-
// CHECK: br i1 %x, label %bb2, label %bb3, !prof ![[NUM:[0-9]+]]
29-
// CHECK: bb3:
28+
// CHECK: br i1 %x, label %bb3, label %bb1, !prof ![[NUM:[0-9]+]]
29+
// CHECK: bb1:
3030
// CHECK: path_b
31-
// CHECK: bb2:
31+
// CHECK: bb3:
3232
// CHECK: path_a
3333
}
3434

@@ -40,8 +40,8 @@ pub fn test2(x: i32) {
4040
}
4141

4242
// CHECK-LABEL: @test2(
43-
// CHECK: br i1 %_2, label %bb2, label %bb3, !prof ![[NUM]]
44-
// CHECK: bb3:
43+
// CHECK: br i1 %_2, label %bb2, label %bb1, !prof ![[NUM]]
44+
// CHECK: bb1:
4545
// CHECK: path_b
4646
// CHECK: bb2:
4747
// CHECK: path_a
@@ -55,10 +55,10 @@ pub fn test3(x: i8) {
5555
}
5656

5757
// CHECK-LABEL: @test3(
58-
// CHECK: br i1 %_2, label %bb2, label %bb3, !prof ![[NUM]]
59-
// CHECK: bb3:
58+
// CHECK: br i1 %_2, label %bb3, label %bb1, !prof ![[NUM]]
59+
// CHECK: bb1:
6060
// CHECK: path_b
61-
// CHECK: bb2:
61+
// CHECK: bb3:
6262
// CHECK: path_a
6363
}
6464

@@ -69,11 +69,14 @@ pub fn test4(x: u64) {
6969
_ => path_b(),
7070
}
7171

72+
// Note that LLVM seems to flip this one, for some reason
73+
7274
// CHECK-LABEL: @test4(
73-
// CHECK: br i1 %0, label %bb3, label %bb2, !prof ![[NUM2:[0-9]+]]
75+
// CHECK: %0 = icmp eq i64 %x, 33
76+
// CHECK: br i1 %0, label %bb1, label %bb3, !prof ![[NUM2:[0-9]+]]
7477
// CHECK: bb3:
7578
// CHECK: path_a
76-
// CHECK: bb2:
79+
// CHECK: bb1:
7780
// CHECK: path_b
7881
}
7982

tests/codegen/hint/unlikely.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ pub fn test1(x: bool) {
2525
}
2626

2727
// CHECK-LABEL: @test1(
28-
// CHECK: br i1 %x, label %bb2, label %bb4, !prof ![[NUM:[0-9]+]]
29-
// CHECK: bb4:
28+
// CHECK: br i1 %x, label %bb2, label %bb1, !prof ![[NUM:[0-9]+]]
29+
// CHECK: bb1:
3030
// CHECK: path_b
3131
// CHECK: bb2:
3232
// CHECK: path_a
@@ -40,8 +40,8 @@ pub fn test2(x: i32) {
4040
}
4141

4242
// CHECK-LABEL: @test2(
43-
// CHECK: br i1 %_2, label %bb2, label %bb4, !prof ![[NUM]]
44-
// CHECK: bb4:
43+
// CHECK: br i1 %_2, label %bb2, label %bb1, !prof ![[NUM]]
44+
// CHECK: bb1:
4545
// CHECK: path_b
4646
// CHECK: bb2:
4747
// CHECK: path_a
@@ -55,8 +55,8 @@ pub fn test3(x: i8) {
5555
}
5656

5757
// CHECK-LABEL: @test3(
58-
// CHECK: br i1 %_2, label %bb2, label %bb4, !prof ![[NUM]]
59-
// CHECK: bb4:
58+
// CHECK: br i1 %_2, label %bb2, label %bb1, !prof ![[NUM]]
59+
// CHECK: bb1:
6060
// CHECK: path_b
6161
// CHECK: bb2:
6262
// CHECK: path_a
@@ -69,12 +69,16 @@ pub fn test4(x: u64) {
6969
_ => path_b(),
7070
}
7171

72+
// Note that LLVM seems to flip this one, for some reason
73+
7274
// CHECK-LABEL: @test4(
73-
// CHECK: br i1 %0, label %bb4, label %bb2, !prof ![[NUM2:[0-9]+]]
74-
// CHECK: bb4:
75+
// CHECK: %0 = icmp eq i64 %x, 33
76+
// CHECK: br i1 %0, label %bb1, label %bb2, !prof ![[NUM2:[0-9]+]]
77+
// CHECK: bb1:
7578
// CHECK: path_a
7679
// CHECK: bb2:
7780
// CHECK: path_b
7881
}
7982

8083
// CHECK: ![[NUM]] = !{!"branch_weights", {{(!"expected", )?}}i32 1, i32 2000}
84+
// CHECK: ![[NUM2]] = !{!"branch_weights", {{(!"expected", )?}}i32 2000, i32 1}

tests/codegen/intrinsics/cold_path3.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ pub fn test(x: Option<u32>) {
4545
}
4646

4747
// CHECK-LABEL: @test(
48-
// CHECK: switch i32 %1, label %bb1 [
49-
// CHECK: i32 0, label %bb6
50-
// CHECK: i32 1, label %bb5
51-
// CHECK: i32 2, label %bb4
52-
// CHECK: i32 3, label %bb3
48+
// CHECK: switch i32 %1, label %bb8 [
49+
// CHECK: i32 0, label %bb2
50+
// CHECK: i32 1, label %bb3
51+
// CHECK: i32 2, label %bb5
52+
// CHECK: i32 3, label %bb6
5353
// CHECK: ], !prof ![[NUM1:[0-9]+]]
5454
}
5555

@@ -76,10 +76,10 @@ pub fn test2(x: Option<u32>) {
7676
}
7777

7878
// CHECK-LABEL: @test2(
79-
// CHECK: switch i32 %1, label %bb1 [
80-
// CHECK: i32 10, label %bb5
81-
// CHECK: i32 11, label %bb4
82-
// CHECK: i32 13, label %bb3
79+
// CHECK: switch i32 %1, label %bb7 [
80+
// CHECK: i32 10, label %bb2
81+
// CHECK: i32 11, label %bb3
82+
// CHECK: i32 13, label %bb5
8383
// CHECK: ], !prof ![[NUM2:[0-9]+]]
8484
}
8585

tests/codegen/intrinsics/likely.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ pub fn test_likely(x: bool) {
2626
}
2727

2828
// CHECK-LABEL: @test_likely(
29-
// CHECK: br i1 %x, label %bb2, label %bb3, !prof ![[NUM:[0-9]+]]
30-
// CHECK: bb3:
29+
// CHECK: br i1 %x, label %bb3, label %bb1, !prof ![[NUM:[0-9]+]]
30+
// CHECK: bb1:
3131
// CHECK-NOT: cold_path
3232
// CHECK: path_b
33-
// CHECK: bb2:
33+
// CHECK: bb3:
3434
// CHECK: path_a
3535
// CHECK: ![[NUM]] = !{!"branch_weights", {{(!"expected", )?}}i32 2000, i32 1}

tests/codegen/intrinsics/unlikely.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub fn test_unlikely(x: bool) {
2626
}
2727

2828
// CHECK-LABEL: @test_unlikely(
29-
// CHECK: br i1 %x, label %bb2, label %bb4, !prof ![[NUM:[0-9]+]]
30-
// CHECK: bb4:
29+
// CHECK: br i1 %x, label %bb2, label %bb1, !prof ![[NUM:[0-9]+]]
30+
// CHECK: bb1:
3131
// CHECK: path_b
3232
// CHECK: bb2:
3333
// CHECK-NOT: cold_path

tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.32bit.panic-abort.diff

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@
8484
StorageLive(_17);
8585
StorageLive(_19);
8686
_19 = const false;
87-
- switchInt(move _19) -> [0: bb6, otherwise: bb5];
88-
+ switchInt(const false) -> [0: bb6, otherwise: bb5];
87+
- switchInt(move _19) -> [0: bb3, otherwise: bb2];
88+
+ switchInt(const false) -> [0: bb3, otherwise: bb2];
8989
}
9090

9191
bb1: {
@@ -95,15 +95,29 @@
9595
}
9696

9797
bb2: {
98-
unreachable;
98+
- _20 = Layout::from_size_align_unchecked::precondition_check(copy _11, copy _12) -> [return: bb3, unwind unreachable];
99+
+ _20 = Layout::from_size_align_unchecked::precondition_check(const 0_usize, const 1_usize) -> [return: bb3, unwind unreachable];
99100
}
100101

101102
bb3: {
102-
- _18 = handle_alloc_error(move _14) -> unwind unreachable;
103-
+ _18 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}) -> unwind unreachable;
103+
StorageDead(_19);
104+
StorageLive(_21);
105+
- _21 = copy _12 as std::ptr::Alignment (Transmute);
106+
- _14 = Layout { size: copy _11, align: move _21 };
107+
+ _21 = const std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0);
108+
+ _14 = const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }};
109+
StorageDead(_21);
110+
StorageLive(_15);
111+
- _15 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], copy _14, const false) -> [return: bb4, unwind unreachable];
112+
+ _15 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}, const false) -> [return: bb4, unwind unreachable];
104113
}
105114

106115
bb4: {
116+
_16 = discriminant(_15);
117+
switchInt(move _16) -> [0: bb5, 1: bb6, otherwise: bb7];
118+
}
119+
120+
bb5: {
107121
_17 = copy ((_15 as Ok).0: std::ptr::NonNull<[u8]>);
108122
StorageLive(_22);
109123
_22 = copy _17 as *mut [u8] (Transmute);
@@ -150,27 +164,13 @@
150164
drop(_3) -> [return: bb1, unwind unreachable];
151165
}
152166

153-
bb5: {
154-
- _20 = Layout::from_size_align_unchecked::precondition_check(copy _11, copy _12) -> [return: bb6, unwind unreachable];
155-
+ _20 = Layout::from_size_align_unchecked::precondition_check(const 0_usize, const 1_usize) -> [return: bb6, unwind unreachable];
156-
}
157-
158167
bb6: {
159-
StorageDead(_19);
160-
StorageLive(_21);
161-
- _21 = copy _12 as std::ptr::Alignment (Transmute);
162-
- _14 = Layout { size: copy _11, align: move _21 };
163-
+ _21 = const std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0);
164-
+ _14 = const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }};
165-
StorageDead(_21);
166-
StorageLive(_15);
167-
- _15 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], copy _14, const false) -> [return: bb7, unwind unreachable];
168-
+ _15 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}, const false) -> [return: bb7, unwind unreachable];
168+
- _18 = handle_alloc_error(move _14) -> unwind unreachable;
169+
+ _18 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}) -> unwind unreachable;
169170
}
170171

171172
bb7: {
172-
_16 = discriminant(_15);
173-
switchInt(move _16) -> [0: bb4, 1: bb3, otherwise: bb2];
173+
unreachable;
174174
}
175175
+ }
176176
+

tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.64bit.panic-abort.diff

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@
8484
StorageLive(_17);
8585
StorageLive(_19);
8686
_19 = const false;
87-
- switchInt(move _19) -> [0: bb6, otherwise: bb5];
88-
+ switchInt(const false) -> [0: bb6, otherwise: bb5];
87+
- switchInt(move _19) -> [0: bb3, otherwise: bb2];
88+
+ switchInt(const false) -> [0: bb3, otherwise: bb2];
8989
}
9090

9191
bb1: {
@@ -95,15 +95,29 @@
9595
}
9696

9797
bb2: {
98-
unreachable;
98+
- _20 = Layout::from_size_align_unchecked::precondition_check(copy _11, copy _12) -> [return: bb3, unwind unreachable];
99+
+ _20 = Layout::from_size_align_unchecked::precondition_check(const 0_usize, const 1_usize) -> [return: bb3, unwind unreachable];
99100
}
100101

101102
bb3: {
102-
- _18 = handle_alloc_error(move _14) -> unwind unreachable;
103-
+ _18 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}) -> unwind unreachable;
103+
StorageDead(_19);
104+
StorageLive(_21);
105+
- _21 = copy _12 as std::ptr::Alignment (Transmute);
106+
- _14 = Layout { size: copy _11, align: move _21 };
107+
+ _21 = const std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0);
108+
+ _14 = const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }};
109+
StorageDead(_21);
110+
StorageLive(_15);
111+
- _15 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], copy _14, const false) -> [return: bb4, unwind unreachable];
112+
+ _15 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}, const false) -> [return: bb4, unwind unreachable];
104113
}
105114

106115
bb4: {
116+
_16 = discriminant(_15);
117+
switchInt(move _16) -> [0: bb5, 1: bb6, otherwise: bb7];
118+
}
119+
120+
bb5: {
107121
_17 = copy ((_15 as Ok).0: std::ptr::NonNull<[u8]>);
108122
StorageLive(_22);
109123
_22 = copy _17 as *mut [u8] (Transmute);
@@ -150,27 +164,13 @@
150164
drop(_3) -> [return: bb1, unwind unreachable];
151165
}
152166

153-
bb5: {
154-
- _20 = Layout::from_size_align_unchecked::precondition_check(copy _11, copy _12) -> [return: bb6, unwind unreachable];
155-
+ _20 = Layout::from_size_align_unchecked::precondition_check(const 0_usize, const 1_usize) -> [return: bb6, unwind unreachable];
156-
}
157-
158167
bb6: {
159-
StorageDead(_19);
160-
StorageLive(_21);
161-
- _21 = copy _12 as std::ptr::Alignment (Transmute);
162-
- _14 = Layout { size: copy _11, align: move _21 };
163-
+ _21 = const std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0);
164-
+ _14 = const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }};
165-
StorageDead(_21);
166-
StorageLive(_15);
167-
- _15 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], copy _14, const false) -> [return: bb7, unwind unreachable];
168-
+ _15 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}, const false) -> [return: bb7, unwind unreachable];
168+
- _18 = handle_alloc_error(move _14) -> unwind unreachable;
169+
+ _18 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}) -> unwind unreachable;
169170
}
170171

171172
bb7: {
172-
_16 = discriminant(_15);
173-
switchInt(move _16) -> [0: bb4, 1: bb3, otherwise: bb2];
173+
unreachable;
174174
}
175175
+ }
176176
+

0 commit comments

Comments
 (0)