Skip to content

Commit b14b8db

Browse files
authored
Rollup merge of rust-lang#67502 - Mark-Simulacrum:opt-catch, r=alexcrichton
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 rust-lang#64224, and resolves rust-lang#64222.
2 parents 1389494 + da5b1a4 commit b14b8db

File tree

16 files changed

+110
-107
lines changed

16 files changed

+110
-107
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,7 @@ dependencies = [
23012301
name = "panic_abort"
23022302
version = "0.0.0"
23032303
dependencies = [
2304+
"cfg-if",
23042305
"compiler_builtins",
23052306
"core",
23062307
"libc",

src/libpanic_abort/Cargo.toml

Lines changed: 1 addition & 0 deletions
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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@
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+
23+
// We need the definition of TryPayload for __rust_panic_cleanup.
24+
include!("../libpanic_unwind/payload.rs");
25+
2326
#[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
27+
pub unsafe extern "C" fn __rust_panic_cleanup(_: TryPayload) -> *mut (dyn Any + Send + 'static) {
28+
unreachable!()
3229
}
3330

3431
// "Leak" the payload and shim to the relevant abort on the platform in

src/libpanic_unwind/dummy.rs

Lines changed: 0 additions & 4 deletions
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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ 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
pub unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
5652
assert!(!ptr.is_null());
5753
let adjusted_ptr = __cxa_begin_catch(ptr as *mut libc::c_void);

src/libpanic_unwind/gcc.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848

4949
use alloc::boxed::Box;
5050
use core::any::Any;
51-
use core::ptr;
5251

5352
use crate::dwarf::eh::{self, EHAction, EHContext};
5453
use libc::{c_int, uintptr_t};
@@ -82,10 +81,6 @@ pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
8281
}
8382
}
8483

85-
pub fn payload() -> *mut u8 {
86-
ptr::null_mut()
87-
}
88-
8984
pub unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
9085
let my_ep = ptr as *mut Exception;
9186
let cause = (*my_ep).cause.take();

src/libpanic_unwind/hermit.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ use alloc::boxed::Box;
66
use core::any::Any;
77
use core::ptr;
88

9-
pub fn payload() -> *mut u8 {
10-
ptr::null_mut()
11-
}
12-
139
pub unsafe fn cleanup(_ptr: *mut u8) -> Box<dyn Any + Send> {
1410
extern "C" {
1511
pub fn __rust_abort() -> !;

src/libpanic_unwind/lib.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@
2222
#![feature(libc)]
2323
#![feature(nll)]
2424
#![feature(panic_unwind)]
25-
#![feature(raw)]
2625
#![feature(staged_api)]
2726
#![feature(std_internals)]
2827
#![feature(unwind_attributes)]
28+
#![feature(rustc_attrs)]
29+
#![feature(raw)]
2930
#![panic_runtime]
3031
#![feature(panic_runtime)]
3132

3233
use alloc::boxed::Box;
33-
use core::intrinsics;
34-
use core::mem;
34+
use core::any::Any;
3535
use core::panic::BoxMeUp;
36-
use core::raw;
3736

37+
// If adding to this list, you should also look at the list of TryPayload types
38+
// defined in payload.rs and likely add to there as well.
3839
cfg_if::cfg_if! {
3940
if #[cfg(target_os = "emscripten")] {
4041
#[path = "emcc.rs"]
@@ -60,30 +61,15 @@ cfg_if::cfg_if! {
6061
}
6162
}
6263

64+
include!("payload.rs");
65+
6366
mod dwarf;
6467

65-
// Entry point for catching an exception, implemented using the `try` intrinsic
66-
// in the compiler.
67-
//
68-
// The interaction between the `payload` function and the compiler is pretty
69-
// hairy and tightly coupled, for more information see the compiler's
70-
// implementation of this.
7168
#[no_mangle]
72-
pub unsafe extern "C" fn __rust_maybe_catch_panic(
73-
f: fn(*mut u8),
74-
data: *mut u8,
75-
data_ptr: *mut usize,
76-
vtable_ptr: *mut usize,
77-
) -> u32 {
78-
let mut payload = imp::payload();
79-
if intrinsics::r#try(f, data, &mut payload as *mut _ as *mut _) == 0 {
80-
0
81-
} else {
82-
let obj = mem::transmute::<_, raw::TraitObject>(imp::cleanup(payload));
83-
*data_ptr = obj.data as usize;
84-
*vtable_ptr = obj.vtable as usize;
85-
1
86-
}
69+
pub unsafe extern "C" fn __rust_panic_cleanup(
70+
payload: TryPayload,
71+
) -> *mut (dyn Any + Send + 'static) {
72+
Box::into_raw(imp::cleanup(payload))
8773
}
8874

8975
// Entry point for raising an exception, just delegates to the platform-specific

src/libpanic_unwind/payload.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Type definition for the payload argument of the try intrinsic.
2+
//
3+
// This must be kept in sync with the implementations of the try intrinsic.
4+
//
5+
// This file is included by both panic runtimes and libstd. It is part of the
6+
// panic runtime ABI.
7+
cfg_if::cfg_if! {
8+
if #[cfg(target_os = "emscripten")] {
9+
type TryPayload = *mut u8;
10+
} else if #[cfg(target_arch = "wasm32")] {
11+
type TryPayload = *mut u8;
12+
} else if #[cfg(target_os = "hermit")] {
13+
type TryPayload = *mut u8;
14+
} else if #[cfg(all(target_env = "msvc", target_arch = "aarch64"))] {
15+
type TryPayload = *mut u8;
16+
} else if #[cfg(target_env = "msvc")] {
17+
type TryPayload = [u64; 2];
18+
} else {
19+
type TryPayload = *mut u8;
20+
}
21+
}

src/libpanic_unwind/seh.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,6 @@ pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
264264
_CxxThrowException(throw_ptr, &mut THROW_INFO as *mut _ as *mut _);
265265
}
266266

267-
pub fn payload() -> [u64; 2] {
268-
[0; 2]
269-
}
270-
271267
pub unsafe fn cleanup(payload: [u64; 2]) -> Box<dyn Any + Send> {
272268
mem::transmute(raw::TraitObject { data: payload[0] as *mut _, vtable: payload[1] as *mut _ })
273269
}

0 commit comments

Comments
 (0)