Skip to content

Commit ad05d5d

Browse files
committed
Auto merge of #67502 - Mark-Simulacrum:opt-catch, r=<try>
Optimize catch_unwind to match C++ try/catch This refactors the implementation of catching unwinds to allow LLVM to inline the "try" closure directly into the happy path, avoiding indirection. This means that the catch_unwind implementation is (after this PR) zero-cost unless a panic is thrown. https://rust.godbolt.org/z/cZcUSB is an example of the current codegen in a simple case. Notably, the codegen is *exactly the same* if `-Cpanic=abort` is passed, which is clearly not great. This PR, on the other hand, generates the following assembly: ```asm # -Cpanic=unwind: push rbx mov ebx,0x2a call QWORD PTR [rip+0x1c53c] # <happy> mov eax,ebx pop rbx ret mov rdi,rax call QWORD PTR [rip+0x1c537] # cleanup function call call QWORD PTR [rip+0x1c539] # <unfortunate> mov ebx,0xd mov eax,ebx pop rbx ret # -Cpanic=abort: push rax call QWORD PTR [rip+0x20a1] # <happy> mov eax,0x2a pop rcx ret ``` Fixes #64224, and resolves #64222.
2 parents 75cf41a + 6f9c9c2 commit ad05d5d

File tree

43 files changed

+463
-366
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+463
-366
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -2290,6 +2290,7 @@ dependencies = [
22902290
name = "panic_abort"
22912291
version = "0.0.0"
22922292
dependencies = [
2293+
"cfg-if",
22932294
"compiler_builtins",
22942295
"core",
22952296
"libc",

src/ci/azure-pipelines/pr.yml

+51
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,54 @@ jobs:
2222
mingw-check: {}
2323
x86_64-gnu-tools:
2424
CI_ONLY_WHEN_SUBMODULES_CHANGED: 1
25+
- job: Windows
26+
timeoutInMinutes: 600
27+
pool:
28+
vmImage: 'vs2017-win2016'
29+
steps:
30+
- template: steps/run.yml
31+
strategy:
32+
matrix:
33+
x86_64-msvc-1:
34+
RUST_CONFIGURE_ARGS: --build=x86_64-pc-windows-msvc --enable-profiler
35+
SCRIPT: make ci-subset-1
36+
# FIXME(#59637)
37+
NO_DEBUG_ASSERTIONS: 1
38+
NO_LLVM_ASSERTIONS: 1
39+
x86_64-msvc-2:
40+
RUST_CONFIGURE_ARGS: --build=x86_64-pc-windows-msvc --enable-profiler
41+
SCRIPT: make ci-subset-2
42+
i686-msvc-1:
43+
RUST_CONFIGURE_ARGS: --build=i686-pc-windows-msvc
44+
SCRIPT: make ci-subset-1
45+
# FIXME(#59637)
46+
NO_DEBUG_ASSERTIONS: 1
47+
NO_LLVM_ASSERTIONS: 1
48+
i686-msvc-2:
49+
RUST_CONFIGURE_ARGS: --build=i686-pc-windows-msvc
50+
SCRIPT: make ci-subset-2
51+
# FIXME(#59637)
52+
NO_DEBUG_ASSERTIONS: 1
53+
NO_LLVM_ASSERTIONS: 1
54+
i686-mingw-1:
55+
RUST_CONFIGURE_ARGS: --build=i686-pc-windows-gnu
56+
SCRIPT: make ci-mingw-subset-1
57+
CUSTOM_MINGW: 1
58+
# FIXME(#59637)
59+
NO_DEBUG_ASSERTIONS: 1
60+
NO_LLVM_ASSERTIONS: 1
61+
i686-mingw-2:
62+
RUST_CONFIGURE_ARGS: --build=i686-pc-windows-gnu
63+
SCRIPT: make ci-mingw-subset-2
64+
CUSTOM_MINGW: 1
65+
x86_64-mingw-1:
66+
SCRIPT: make ci-mingw-subset-1
67+
RUST_CONFIGURE_ARGS: --build=x86_64-pc-windows-gnu
68+
CUSTOM_MINGW: 1
69+
# FIXME(#59637)
70+
NO_DEBUG_ASSERTIONS: 1
71+
NO_LLVM_ASSERTIONS: 1
72+
x86_64-mingw-2:
73+
SCRIPT: make ci-mingw-subset-2
74+
RUST_CONFIGURE_ARGS: --build=x86_64-pc-windows-gnu
75+
CUSTOM_MINGW: 1

src/ci/azure-pipelines/try.yml

+28-33
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ variables:
66
- group: prod-credentials
77

88
jobs:
9-
- job: Linux
10-
timeoutInMinutes: 600
11-
pool:
12-
vmImage: ubuntu-16.04
13-
steps:
14-
- template: steps/run.yml
15-
strategy:
16-
matrix:
17-
dist-x86_64-linux: {}
18-
dist-x86_64-linux-alt:
19-
IMAGE: dist-x86_64-linux
9+
# - job: Linux
10+
# timeoutInMinutes: 600
11+
# pool:
12+
# vmImage: ubuntu-16.04
13+
# steps:
14+
# - template: steps/run.yml
15+
# strategy:
16+
# matrix:
17+
# dist-x86_64-linux: {}
18+
# dist-x86_64-linux-alt:
19+
# IMAGE: dist-x86_64-linux
2020

2121
# The macOS and Windows builds here are currently disabled due to them not being
2222
# overly necessary on `try` builds. We also don't actually have anything that
@@ -49,25 +49,20 @@ jobs:
4949
# NO_LLVM_ASSERTIONS: 1
5050
# NO_DEBUG_ASSERTIONS: 1
5151
#
52-
# - job: Windows
53-
# timeoutInMinutes: 600
54-
# pool:
55-
# vmImage: 'vs2017-win2016'
56-
# steps:
57-
# - template: steps/run.yml
58-
# strategy:
59-
# matrix:
60-
# dist-x86_64-msvc:
61-
# RUST_CONFIGURE_ARGS: >
62-
# --build=x86_64-pc-windows-msvc
63-
# --target=x86_64-pc-windows-msvc,aarch64-pc-windows-msvc
64-
# --enable-full-tools
65-
# --enable-profiler
66-
# SCRIPT: python x.py dist
67-
# DIST_REQUIRE_ALL_TOOLS: 1
68-
# DEPLOY: 1
69-
#
70-
# dist-x86_64-msvc-alt:
71-
# RUST_CONFIGURE_ARGS: --build=x86_64-pc-windows-msvc --enable-extended --enable-profiler
72-
# SCRIPT: python x.py dist
73-
# DEPLOY_ALT: 1
52+
- job: Windows
53+
timeoutInMinutes: 600
54+
pool:
55+
vmImage: 'vs2017-win2016'
56+
steps:
57+
- template: steps/run.yml
58+
strategy:
59+
matrix:
60+
i686-mingw-2:
61+
RUST_CONFIGURE_ARGS: --build=i686-pc-windows-gnu
62+
SCRIPT: make ci-mingw-subset-2
63+
CUSTOM_MINGW: 1
64+
dist-i686-mingw:
65+
RUST_CONFIGURE_ARGS: --build=i686-pc-windows-gnu --enable-debug-assertions --enable-optimize --debuginfo-level=1
66+
SCRIPT: python x.py dist
67+
CUSTOM_MINGW: 1
68+
N_DIST_REQUIRE_ALL_TOOLS: 1

src/doc/unstable-book/src/language-features/lang-items.md

+4-18
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ fn main(_argc: isize, _argv: *const *const u8) -> isize {
5252
5353
#[lang = "eh_personality"] extern fn rust_eh_personality() {}
5454
#[lang = "panic_impl"] extern fn rust_begin_panic(info: &PanicInfo) -> ! { unsafe { intrinsics::abort() } }
55-
#[lang = "eh_unwind_resume"] extern fn rust_eh_unwind_resume() {}
5655
#[no_mangle] pub extern fn rust_eh_register_frames () {}
5756
#[no_mangle] pub extern fn rust_eh_unregister_frames () {}
5857
```
@@ -67,7 +66,7 @@ Other features provided by lang items include:
6766
marked with lang items; those specific four are `eq`, `ord`,
6867
`deref`, and `add` respectively.
6968
- stack unwinding and general failure; the `eh_personality`,
70-
`eh_unwind_resume`, `fail` and `fail_bounds_checks` lang items.
69+
`fail` and `fail_bounds_checks` lang items.
7170
- the traits in `std::marker` used to indicate types of
7271
various kinds; lang items `send`, `sync` and `copy`.
7372
- the marker types and variance indicators found in
@@ -130,12 +129,6 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
130129
pub extern fn rust_eh_personality() {
131130
}
132131
133-
// This function may be needed based on the compilation target.
134-
#[lang = "eh_unwind_resume"]
135-
#[no_mangle]
136-
pub extern fn rust_eh_unwind_resume() {
137-
}
138-
139132
#[lang = "panic_impl"]
140133
#[no_mangle]
141134
pub extern fn rust_begin_panic(info: &PanicInfo) -> ! {
@@ -173,12 +166,6 @@ pub extern fn main(_argc: i32, _argv: *const *const u8) -> i32 {
173166
pub extern fn rust_eh_personality() {
174167
}
175168
176-
// This function may be needed based on the compilation target.
177-
#[lang = "eh_unwind_resume"]
178-
#[no_mangle]
179-
pub extern fn rust_eh_unwind_resume() {
180-
}
181-
182169
#[lang = "panic_impl"]
183170
#[no_mangle]
184171
pub extern fn rust_begin_panic(info: &PanicInfo) -> ! {
@@ -213,8 +200,8 @@ the screen. While the language item's name is `panic_impl`, the symbol name is
213200

214201
A third function, `rust_eh_unwind_resume`, is also needed if the `custom_unwind_resume`
215202
flag is set in the options of the compilation target. It allows customizing the
216-
process of resuming unwind at the end of the landing pads. The language item's name
217-
is `eh_unwind_resume`.
203+
process of resuming unwind at the end of the landing pads. Since this function
204+
must be defined in assembly code it does not have an associated language item.
218205

219206
## List of all language items
220207

@@ -247,9 +234,8 @@ the source code.
247234
- `eh_personality`: `libpanic_unwind/emcc.rs` (EMCC)
248235
- `eh_personality`: `libpanic_unwind/gcc.rs` (GNU)
249236
- `eh_personality`: `libpanic_unwind/seh.rs` (SEH)
250-
- `eh_unwind_resume`: `libpanic_unwind/gcc.rs` (GCC)
251-
- `eh_catch_typeinfo`: `libpanic_unwind/seh.rs` (SEH)
252237
- `eh_catch_typeinfo`: `libpanic_unwind/emcc.rs` (EMCC)
238+
- `rust_eh_unwind_resume`: `libpanic_unwind/gcc.rs` (GCC)
253239
- `panic`: `libcore/panicking.rs`
254240
- `panic_bounds_check`: `libcore/panicking.rs`
255241
- `panic_impl`: `libcore/panicking.rs`

src/libcore/intrinsics.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1854,14 +1854,16 @@ extern "rust-intrinsic" {
18541854
/// [`std::mem::discriminant`](../../std/mem/fn.discriminant.html)
18551855
pub fn discriminant_value<T>(v: &T) -> u64;
18561856

1857-
/// Rust's "try catch" construct which invokes the function pointer `f` with
1858-
/// the data pointer `data`.
1857+
/// Rust's "try catch" construct which invokes the function pointer `try_fn`
1858+
/// with the data pointer `data`.
18591859
///
1860-
/// The third pointer is a target-specific data pointer which is filled in
1861-
/// with the specifics of the exception that occurred. For examples on Unix
1862-
/// platforms this is a `*mut *mut T` which is filled in by the compiler and
1863-
/// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
1860+
/// The third argument is a function called if a panic occurs. This function
1861+
/// takes the data pointer and a pointer to the target-specific exception
1862+
/// object that was caught. For more information see the compiler's
18641863
/// source as well as std's catch implementation.
1864+
#[cfg(not(bootstrap))]
1865+
pub fn r#try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32;
1866+
#[cfg(bootstrap)]
18651867
pub fn r#try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;
18661868

18671869
/// Emits a `!nontemporal` store according to LLVM (see their docs).

src/libpanic_abort/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ doc = false
1414
core = { path = "../libcore" }
1515
libc = { version = "0.2", default-features = false }
1616
compiler_builtins = "0.1.0"
17+
cfg-if = "0.1.8"

src/libpanic_abort/lib.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,11 @@
1818
#![feature(staged_api)]
1919
#![feature(rustc_attrs)]
2020

21-
// Rust's "try" function, but if we're aborting on panics we just call the
22-
// function as there's nothing else we need to do here.
21+
use core::any::Any;
22+
2323
#[rustc_std_internal_symbol]
24-
pub unsafe extern "C" fn __rust_maybe_catch_panic(
25-
f: fn(*mut u8),
26-
data: *mut u8,
27-
_data_ptr: *mut usize,
28-
_vtable_ptr: *mut usize,
29-
) -> u32 {
30-
f(data);
31-
0
24+
pub unsafe extern "C" fn __rust_panic_cleanup(_: *mut u8) -> *mut (dyn Any + Send + 'static) {
25+
unreachable!()
3226
}
3327

3428
// "Leak" the payload and shim to the relevant abort on the platform in
@@ -92,7 +86,7 @@ pub unsafe extern "C" fn __rust_start_panic(_payload: usize) -> u32 {
9286
// binaries, but it should never be called as we don't link in an unwinding
9387
// runtime at all.
9488
pub mod personalities {
95-
#[no_mangle]
89+
#[rustc_std_internal_symbol]
9690
#[cfg(not(any(
9791
all(target_arch = "wasm32", not(target_os = "emscripten"),),
9892
all(target_os = "windows", target_env = "gnu", target_arch = "x86_64",),
@@ -101,7 +95,7 @@ pub mod personalities {
10195

10296
// On x86_64-pc-windows-gnu we use our own personality function that needs
10397
// to return `ExceptionContinueSearch` as we're passing on all our frames.
104-
#[no_mangle]
98+
#[rustc_std_internal_symbol]
10599
#[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86_64"))]
106100
pub extern "C" fn rust_eh_personality(
107101
_record: usize,
@@ -117,16 +111,16 @@ pub mod personalities {
117111
//
118112
// Note that we don't execute landing pads, so this is never called, so it's
119113
// body is empty.
120-
#[no_mangle]
114+
#[rustc_std_internal_symbol]
121115
#[cfg(all(target_os = "windows", target_env = "gnu"))]
122116
pub extern "C" fn rust_eh_unwind_resume() {}
123117

124118
// These two are called by our startup objects on i686-pc-windows-gnu, but
125119
// they don't need to do anything so the bodies are nops.
126-
#[no_mangle]
120+
#[rustc_std_internal_symbol]
127121
#[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
128122
pub extern "C" fn rust_eh_register_frames() {}
129-
#[no_mangle]
123+
#[rustc_std_internal_symbol]
130124
#[cfg(all(target_os = "windows", target_env = "gnu", target_arch = "x86"))]
131125
pub extern "C" fn rust_eh_unregister_frames() {}
132126
}

src/libpanic_unwind/dummy.rs

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ use alloc::boxed::Box;
66
use core::any::Any;
77
use core::intrinsics;
88

9-
pub fn payload() -> *mut u8 {
10-
core::ptr::null_mut()
11-
}
12-
139
pub unsafe fn cleanup(_ptr: *mut u8) -> Box<dyn Any + Send> {
1410
intrinsics::abort()
1511
}

src/libpanic_unwind/emcc.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,11 @@ static EXCEPTION_TYPE_INFO: TypeInfo = TypeInfo {
4848
name: b"rust_panic\0".as_ptr(),
4949
};
5050

51-
pub fn payload() -> *mut u8 {
52-
ptr::null_mut()
53-
}
54-
5551
struct Exception {
5652
// This needs to be an Option because the object's lifetime follows C++
5753
// semantics: when catch_unwind moves the Box out of the exception it must
5854
// still leave the exception object in a valid state because its destructor
59-
// is still going to be called by __cxa_end_catch..
55+
// is still going to be called by __cxa_end_catch.
6056
data: Option<Box<dyn Any + Send>>,
6157
}
6258

@@ -98,7 +94,6 @@ extern "C" fn exception_cleanup(ptr: *mut libc::c_void) -> DestructorRet {
9894
}
9995

10096
#[lang = "eh_personality"]
101-
#[no_mangle]
10297
unsafe extern "C" fn rust_eh_personality(
10398
version: c_int,
10499
actions: uw::_Unwind_Action,

0 commit comments

Comments
 (0)