Lower assume(false) to an unreachable terminator by saethlin · Pull Request #122610 · rust-lang/rust (original) (raw)

Ah, the problem is that -Cdebuginfo=2 which is the default for the compiler profile, disables some optimizations. In this case, ptr::read has been denied some useful MIR optimizations.

If you add

[rust]
debuginfo-level = 1

the test should pass. Setting -Cdebuginfo=1 in the test doesn't help, because the optimization problem is in the precompiled standard library.

It's not working, even If I set to debuginfo-level=0. I downloaded 04d1404 still getting the same result.

`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)"}