Change SwitchTargets to use ScalarInt - #161033
Conversation
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt This PR changes MIR cc @oli-obk, @RalfJung, @JakobDegen, @vakaras |
This comment has been minimized.
This comment has been minimized.
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Change SwitchTargets to use ScalarInt
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (2c29995): comparison URL. Overall result: ❌✅ regressions and improvements - please read:Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf. Next, please: If you can, justify the regressions found in this try perf run in writing along with @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary 1.8%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -1.6%, secondary -1.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary 0.7%, secondary 0.5%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 455.642s -> 455.678s (0.01%) |
| static_assert_size!(SourceScopeData<'_>, 64); | ||
| static_assert_size!(Statement<'_>, 40); | ||
| static_assert_size!(Terminator<'_>, 104); | ||
| static_assert_size!(Terminator<'_>, 112); |
There was a problem hiding this comment.
Terminator size changes had caused regressions before, so this is quite unfortunate, but it's also somewhat mysterious, because the effect is not always there.
I tried to reduce SwitchInt size by using slightly different representation in #159928, but it didn't have as much effect (but I also had to change other Terminator variants to make it smaller, and increase its size in metadata, so those changes could erase the wins from SwitchInt).
There's definitely a way to make SwitchInt represenation more optimal, because right now it store one len redundantly, and lot of accesses require redundant branching, but doing so requires some custom data structure and that might be an overkill.
|
Wow, these perf results are wild. What's going on here? I wonder where are the improvements coming from. |
| && targets.all_values().contains(&ScalarInt::from(0)) | ||
| && targets.all_values().contains(&ScalarInt::from(1)) |
There was a problem hiding this comment.
@panstromek This one is cursed. Instead it should be &ScalarInt::from(0_u128), otherwise the following optimization will never apply :((
Here's the llvm ir before and after the current change:
before:
; Function Attrs: nonlazybind uwtable
define noundef i16 @option_match(i32 noundef range(i32 0, 2) %x.0, i32 %x.1) unnamed_addr #0 !guid !4 {
start:
%_0 = alloca [2 x i8], align 2
%_2 = zext i32 %x.0 to i64
%0 = trunc nuw i64 %_2 to i1
br i1 %0, label %bb3, label %bb2
bb3: ; preds = %start
store i16 13, ptr %_0, align 2
br label %bb4
bb2: ; preds = %start
store i16 42, ptr %_0, align 2
br label %bb4
bb4: ; preds = %bb3, %bb2
%1 = load i16, ptr %_0, align 2, !noundef !5
ret i16 %1
bb1: ; No predecessors!
unreachable
}after:
; Function Attrs: nonlazybind uwtable
define noundef i16 @option_match(i32 noundef range(i32 0, 2) %x.0, i32 %x.1) unnamed_addr #0 !guid !4 {
start:
%_0 = alloca [2 x i8], align 2
%_2 = zext i32 %x.0 to i64
switch i64 %_2, label %bb1 [
i64 0, label %bb2
i64 1, label %bb3
]
bb1: ; preds = %start
unreachable
bb2: ; preds = %start
store i16 42, ptr %_0, align 2
br label %bb4
bb3: ; preds = %start
store i16 13, ptr %_0, align 2
br label %bb4
bb4: ; preds = %bb3, %bb2
%0 = load i16, ptr %_0, align 2, !noundef !5
ret i16 %0
}There was a problem hiding this comment.
I actually don't know why skipping match simplification causes perf results like this
Resolves: #124127
r? @ghost (let's see if the perf result is acceptable)