Skip to content

Commit 415e61c

Browse files
authored
Rollup merge of #131418 - coolreader18:wasm-exc-use-stdarch, r=bjorn3
Use throw intrinsic from stdarch in wasm libunwind Tracking issue: #118168 This is a very belated followup to #121438; now that rust-lang/stdarch#1542 is merged, we can use the intrinsic exported from `core::arch` instead of defining it inline. I also cleaned up the cfgs a bit and added a more detailed comment.
2 parents ef96679 + 35d9bdb commit 415e61c

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

library/unwind/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
#![feature(link_cfg)]
44
#![feature(staged_api)]
55
#![feature(strict_provenance)]
6-
#![cfg_attr(target_arch = "wasm64", feature(simd_wasm64))]
76
#![cfg_attr(not(target_env = "msvc"), feature(libc))]
87
#![cfg_attr(
98
all(target_family = "wasm", not(target_os = "emscripten")),
10-
feature(link_llvm_intrinsics)
9+
feature(simd_wasm64, wasm_exception_handling_intrinsics)
1110
)]
1211
#![allow(internal_features)]
1312

library/unwind/src/wasm.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,25 @@ pub unsafe fn _Unwind_DeleteException(exception: *mut _Unwind_Exception) {
4040
}
4141

4242
pub unsafe fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
43-
#[cfg(panic = "unwind")]
44-
extern "C" {
45-
/// LLVM lowers this intrinsic to the `throw` instruction.
46-
// FIXME(coolreader18): move to stdarch
47-
#[link_name = "llvm.wasm.throw"]
48-
fn wasm_throw(tag: i32, ptr: *mut u8) -> !;
49-
}
50-
5143
// The wasm `throw` instruction takes a "tag", which differentiates certain
5244
// types of exceptions from others. LLVM currently just identifies these
5345
// via integers, with 0 corresponding to C++ exceptions and 1 to C setjmp()/longjmp().
5446
// Ideally, we'd be able to choose something unique for Rust, but for now,
5547
// we pretend to be C++ and implement the Itanium exception-handling ABI.
5648
cfg_if::cfg_if! {
57-
// for now, unless we're -Zbuild-std with panic=unwind, never codegen a throw.
49+
// panic=abort is default for wasm targets. Because an unknown instruction is a load-time
50+
// error on wasm, instead of a runtime error like on traditional architectures, we never
51+
// want to codegen a `throw` instruction, as that would break users using runtimes that
52+
// don't yet support exceptions. The only time this first branch would be selected is if
53+
// the user explicitly opts in to wasm exceptions, via -Zbuild-std with -Cpanic=unwind.
5854
if #[cfg(panic = "unwind")] {
59-
wasm_throw(0, exception.cast())
55+
// corresponds with llvm::WebAssembly::Tag::CPP_EXCEPTION
56+
// in llvm-project/llvm/include/llvm/CodeGen/WasmEHFuncInfo.h
57+
const CPP_EXCEPTION_TAG: i32 = 0;
58+
core::arch::wasm::throw::<CPP_EXCEPTION_TAG>(exception.cast())
6059
} else {
6160
let _ = exception;
62-
#[cfg(target_arch = "wasm32")]
63-
core::arch::wasm32::unreachable();
64-
#[cfg(target_arch = "wasm64")]
65-
core::arch::wasm64::unreachable();
61+
core::arch::wasm::unreachable()
6662
}
6763
}
6864
}

0 commit comments

Comments
 (0)