Files
rust/tests/codegen-llvm
Jonathan Brouwer 3e443d8ad0 Rollup merge of #164059 - jnkel:outline-result-unwrap-drop, r=scottmcm
perf: drop error in `unwrap_failed` instead of `unwrap`

Context: https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/40-byte.20copy.20of.20unwrapped.20Result.3CT.2C.20.28.29.3E.20payload.20survives.20-O3/with/630360641

This PR changes `Result::unwrap` to pass the error by value into `unwrap_failed` rather than by reference. This makes `unwrap` no longer responsible for dropping the error after `unwrap_failed` panics, which reduces code size and compilation times and enables  MIR-inlining of `unwrap`.

The downside is that it introduces another function that must be monomorphized per error type (though the formatting part stays polymorphic); but it turned out to be a net perf win. I tried keeping the `unwrap_failed` fully polymorphic by passing the error as `&mut ManuallyDrop<dyn Debug>`, but this turned out to be worse for performance.

[The first perf run](https://github.com/rust-lang/rust/pull/164059#issuecomment-6090859813) matches the current state of this PR ([the second one](https://github.com/rust-lang/rust/pull/164059#issuecomment-6093489095) is for the ManuallyDrop implementation that turned out to be slower).

Generated code comparison (with an example of an error that has a destructor so that the landing pad shows up in the assembly post-LLVM): https://rust.godbolt.org/z/ajjYMTc7Y

Old:
```asm
        push    r14
        push    rbx
        sub     rsp, 24
        cmp     qword ptr [rsi], -1
        je      .LBB5_1
        mov     rax, qword ptr [rsi + 16]
        mov     qword ptr [rdi + 16], rax
        movups  xmm0, xmmword ptr [rsi]
        movups  xmmword ptr [rdi], xmm0
        mov     rax, rdi
        add     rsp, 24
        pop     rbx
        pop     r14
        ret
.LBB5_1:
        mov     eax, dword ptr [rsi + 8]
        mov     dword ptr [rsp + 4], eax
        lea     rdi, [rip + .Lanon.99b5833966418e083b8bdda9f0bfd3cb.1]
        lea     rcx, [rip + .Lanon.99b5833966418e083b8bdda9f0bfd3cb.0]
        lea     r8, [rip + .Lanon.99b5833966418e083b8bdda9f0bfd3cb.8]
        lea     r14, [rsp + 4]
        mov     esi, 43
        mov     rdx, r14
        call    qword ptr [rip + core[a9090a07a7fa7517]::result::unwrap_failed@GOTPCREL]
        ud2
        mov     rbx, rax
        mov     qword ptr [rsp + 8], r14
        mov     rax, qword ptr [rip + <i32 as core[a9090a07a7fa7517]::fmt::Display>::fmt@GOTPCREL]
        mov     qword ptr [rsp + 16], rax
        lea     rdi, [rip + .Lanon.99b5833966418e083b8bdda9f0bfd3cb.11]
        lea     rsi, [rsp + 8]
        call    qword ptr [rip + std[c29fa2d9dbea2b0e]::io::stdio::_print@GOTPCREL]
        mov     rdi, rbx
        call    _Unwind_Resume@PLT
        call    qword ptr [rip + core[a9090a07a7fa7517]::panicking::panic_in_cleanup@GOTPCREL]
```

New:
```asm
example[32c4274de85e6574]::generic::unwrap:
        cmp     qword ptr [rsi], -1
        je      .LBB7_2
        mov     rax, qword ptr [rsi + 16]
        mov     qword ptr [rdi + 16], rax
        movups  xmm0, xmmword ptr [rsi]
        movups  xmmword ptr [rdi], xmm0
        mov     rax, rdi
        ret
.LBB7_2:
        push    rax
        mov     edx, dword ptr [rsi + 8]
        lea     rdi, [rip + .Lanon.99b5833966418e083b8bdda9f0bfd3cb.1]
        lea     rcx, [rip + .Lanon.99b5833966418e083b8bdda9f0bfd3cb.10]
        mov     esi, 43
        call    example[32c4274de85e6574]::generic::unwrap_failed::<example[32c4274de85e6574]::Error>
```
2026-10-13 23:00:41 +02:00
..
2026-07-16 16:35:37 +02:00
2026-07-22 11:42:09 +02:00
…
2026-09-30 16:59:28 +02:00
2026-08-05 13:45:49 +02:00
2026-10-07 15:05:58 +02:00
2026-10-06 14:50:31 +01:00
2026-09-14 10:41:39 +03:00
2026-07-22 11:42:09 +02:00
2026-07-22 11:42:09 +02:00
2026-07-13 10:54:53 +02:00
2026-10-08 10:58:29 +02:00
…
…
…
…
…
2026-07-13 10:14:58 -07:00
2026-07-13 10:14:58 -07:00
…
…
…
2026-07-13 10:54:53 +02:00
2026-07-23 12:22:51 +02:00
…
…
…
…

The files here use the LLVM FileCheck framework, documented at https://llvm.org/docs/CommandGuide/FileCheck.html.

One extension worth noting is the use of revisions as custom prefixes for FileCheck. If your codegen test has different behavior based on the chosen target or different compiler flags that you want to exercise, you can use a revisions annotation, like so:

// revisions: aaa bbb
// [bbb] compile-flags: -Csomething=true

After specifying those variations, you can write different expected, or explicitly unexpected output by using <prefix>-SAME: and <prefix>-NOT:, like so:

// CHECK: expected code
// aaa-SAME: emitted-only-for-aaa
// aaa-NOT:                        emitted-only-for-bbb
// bbb-NOT:  emitted-only-for-aaa
// bbb-SAME:                       emitted-only-for-bbb

If multiple test revisions want to share some annotations, you can use a setup like this:

// revisions: aaa bbb ccc
// [bbb] filecheck-flags: --check-prefix=both
// [ccc] filecheck-flags: --check-prefix=both

Now aaa:, bbb:, ccc: annotations are only checked for those specific revisions, and both: annotations are checked for revisions bbb and ccc.