-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lower assume(false) to an unreachable terminator #122610
Conversation
Unlucky roll, I think. r? nnethercote |
This comment has been minimized.
This comment has been minimized.
This comment was marked as duplicate.
This comment was marked as duplicate.
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Lower assume(false) to an unreachable terminator This turns the IR pattern: ``` call void `@llvm.assume(i1` false) call void `@core::hint::unreachable_unchecked::precondition_check()` rust-lang#7 br label %bb3 ``` Into just: ``` unreachable ``` I'm not sure why LLVM needs its hand held like this, but also this is a chance to emit less IR so even if the optimization is fixed in LLVM we still come out ahead. The only part of this PR I'm not happy with is the complexity in the loop that iterates over the MIR blocks. The structure of it was a bit subtle before, and now it's just out of control. Please help?
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
I can't pass this test locally. I don't know why this is happening. |
Perhaps it's time to add a post-mono optimization phase? We can know all the facts at the phase. |
Finished benchmarking commit (04d1404): comparison URL. Overall result: ❌✅ regressions and improvements - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 670.532s -> 669.93s (-0.09%) |
I don't know what this sentence means. What are you seeing locally? |
break; | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need the else
after break
.
&mut self, | ||
bx: &mut Bx, | ||
statement: &mir::Statement<'tcx>, | ||
) -> ControlFlow<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A brief comment about the meaning of the return value would be helpful.
@@ -0,0 +1,23 @@ | |||
//@ compile-flags: -O |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A brief comment about the point of this test would be helpful.
Looks ok to me, though I'm not an expert on this code even though I have modified it before :) Just a couple of nits, r=me after fixing them. Might be worth waiting for more information from @DianQK about the local test failure. |
This comment has been minimized.
This comment has been minimized.
Details
config.toml
Maybe it's just my local environment (NixOS?), I've had similar problems before. I don't think this should affect this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can ignore it if the CI is good. 🤯
Ah, the problem is that If you add
the test should pass. Setting |
It's not working, even If I set to `rustc +04d1404e95852ca1daf95ab4997467c9115ed67c discriminant-swap.rs -O --emit=llvm-ir -C no-prepopulate-passes`
; ModuleID = 'discriminant_swap.9e637175484fe1de-cgu.0'
source_filename = "discriminant_swap.9e637175484fe1de-cgu.0"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
%State = type { i8, [753 x i8] }
@alloc_1eb6f53a157dccb32488e066ad957e6d = private unnamed_addr constant <{ [82 x i8] }> <{ [82 x i8] c"unsafe precondition(s) violated: hint::unreachable_unchecked must never be reached" }>, align 1
; core::hint::unreachable_unchecked::precondition_check
; Function Attrs: inlinehint nounwind nonlazybind uwtable
define internal void @_ZN4core4hint21unreachable_unchecked18precondition_check17h10b11a7859d8bb49E() unnamed_addr #0 {
start:
; call core::panicking::panic_nounwind
call void @_ZN4core9panicking14panic_nounwind17hc33628e8262d0458E(ptr noalias noundef nonnull readonly align 1 @alloc_1eb6f53a157dccb32488e066ad957e6d, i64 noundef 82) #5
unreachable
}
; Function Attrs: nonlazybind uwtable
define void @init(ptr noundef %s) unnamed_addr #1 {
start:
%src = alloca %State, align 1
%_3 = alloca %State, align 1
%v = alloca [753 x i8], align 1
call void @llvm.lifetime.start.p0(i64 754, ptr %_3)
call void @llvm.memcpy.p0.p0.i64(ptr align 1 %_3, ptr align 1 %s, i64 754, i1 false)
%0 = load i8, ptr %_3, align 1, !range !3, !noundef !4
%1 = trunc i8 %0 to i1
%_5 = zext i1 %1 to i64
switch i64 %_5, label %bb3 [
i64 0, label %bb1
i64 1, label %bb2
]
bb3: ; preds = %start
unreachable
bb1: ; preds = %start
%2 = getelementptr inbounds i8, ptr %_3, i64 1
call void @llvm.memcpy.p0.p0.i64(ptr align 1 %v, ptr align 1 %2, i64 753, i1 false)
call void @llvm.lifetime.end.p0(i64 754, ptr %_3)
%3 = getelementptr inbounds i8, ptr %src, i64 1
call void @llvm.memcpy.p0.p0.i64(ptr align 1 %3, ptr align 1 %v, i64 753, i1 false)
store i8 1, ptr %src, align 1
call void @llvm.memcpy.p0.p0.i64(ptr align 1 %s, ptr align 1 %src, i64 754, i1 false)
ret void
bb2: ; preds = %start
call void @llvm.lifetime.end.p0(i64 754, ptr %_3)
unreachable
}
; core::panicking::panic_nounwind
; Function Attrs: cold noinline noreturn nounwind nonlazybind uwtable
declare void @_ZN4core9panicking14panic_nounwind17hc33628e8262d0458E(ptr noalias noundef nonnull readonly align 1, i64 noundef) unnamed_addr #2
; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #4
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #4
attributes #0 = { inlinehint nounwind nonlazybind uwtable "probe-stack"="inline-asm" "target-cpu"="x86-64" }
attributes #1 = { nonlazybind uwtable "probe-stack"="inline-asm" "target-cpu"="x86-64" }
attributes #2 = { cold noinline noreturn nounwind nonlazybind uwtable "probe-stack"="inline-asm" "target-cpu"="x86-64" }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { noreturn nounwind }
!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}
!0 = !{i32 8, !"PIC Level", i32 2}
!1 = !{i32 2, !"RtLibUseGOT", i32 1}
!2 = !{!"rustc version 1.79.0-nightly (04d1404e9 2024-03-17)"}
!3 = !{i8 0, i8 2}
!4 = !{} `rustc +04d1404e95852ca1daf95ab4997467c9115ed67c discriminant-swap.rs -O --emit=llvm-ir`
; ModuleID = 'discriminant_swap.9e637175484fe1de-cgu.0'
source_filename = "discriminant_swap.9e637175484fe1de-cgu.0"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
%State = type { i8, [753 x i8] }
; Function Attrs: mustprogress nofree norecurse nosync nounwind nonlazybind willreturn memory(argmem: readwrite) uwtable
define void @init(ptr nocapture noundef %s) unnamed_addr #0 {
start:
%src.sroa.2 = alloca [753 x i8], align 1
%_3 = alloca %State, align 1
call void @llvm.lifetime.start.p0(i64 754, ptr nonnull %_3)
call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(754) %_3, ptr noundef nonnull align 1 dereferenceable(754) %s, i64 754, i1 false)
%0 = getelementptr inbounds i8, ptr %_3, i64 1
call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(753) %src.sroa.2, ptr noundef nonnull align 1 dereferenceable(753) %0, i64 753, i1 false)
call void @llvm.lifetime.end.p0(i64 754, ptr nonnull %_3)
store i8 1, ptr %s, align 1
%src.sroa.2.0.s.sroa_idx = getelementptr inbounds i8, ptr %s, i64 1
call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(753) %src.sroa.2.0.s.sroa_idx, ptr noundef nonnull align 1 dereferenceable(753) %src.sroa.2, i64 753, i1 false)
ret void
}
; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1
; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2
; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2
attributes #0 = { mustprogress nofree norecurse nosync nounwind nonlazybind willreturn memory(argmem: readwrite) uwtable "probe-stack"="inline-asm" "target-cpu"="x86-64" }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}
!0 = !{i32 8, !"PIC Level", i32 2}
!1 = !{i32 2, !"RtLibUseGOT", i32 1}
!2 = !{!"rustc version 1.79.0-nightly (04d1404e9 2024-03-17)"} |
self.codegen_statement(bx, statement); | ||
if let ControlFlow::Break(()) = self.codegen_statement(bx, statement) { | ||
replaced_terminator = true; | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, can you use a loop label and use it to break out of the outer loop? Avoiding the need for replaced_terminator
entirely.
That's possible but I swear I got the test to pass locally. Now it seems to be failing, which is very strange. In any case, this should definitely be a better test that doesn't rely on so much other code. |
I think this is a fragile optimization per this result. |
Could you remove the switch instruction that has only one branch? The key point here is that the load instruction triggered the SROA transformation, which led to the final transformation. However, |
Eliminating the switch would eliminate the information that it is providing about the value of the switched-on value. If we eliminate the switch, we don't know that |
We can add assume on the switch-on value while eliminating the switch. |
Ah, I see! I think if you simplify |
Yes. We need to fine-tune the pipeline. |
As far as I can tell, there is no optimization value to this change; what I saw before was just poking around an unstable part of the LLVM pipeline and getting lucky. I'm wary of adding more cleverness to codegen that is not justified, so since there's no demonstrated upside at all to this change I'm closing this. |
This turns IR like:
Into just:
I'm not sure why LLVM needs its hand held like this, but also this is a chance to emit less IR so even if the optimization is fixed in LLVM we still come out ahead.
The only part of this PR I'm not happy with is the complexity in the loop that iterates over the MIR blocks. The structure of it was a bit subtle before, and now it's just out of control. Please help?