From f46fade8990f7ef24f3f35a1a5c7411268a9091d Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Tue, 26 May 2026 14:32:05 +0000 Subject: [PATCH 1/3] Emit uwtable annotation for modules --- compiler/rustc_codegen_llvm/src/attributes.rs | 2 ++ compiler/rustc_codegen_llvm/src/context.rs | 19 +++++++++++++++++++ compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 12 ++++++++++++ 3 files changed, 33 insertions(+) diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index f4a15b7b40267..d7d1e4966c252 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -165,6 +165,8 @@ pub(crate) fn uwtable_attr(llcx: &llvm::Context, use_sync_unwind: Option) // NOTE: We should determine if we even need async unwind tables, as they // take have more overhead and if we can use sync unwind tables we // probably should. + // + // Similar logic exists for the per-module uwtable annotation in `context.rs`. let async_unwind = !use_sync_unwind.unwrap_or(false); llvm::CreateUWTableAttr(llcx, async_unwind) } diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index a7b5c71b51285..cfc82dc5a4059 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -311,6 +311,25 @@ pub(crate) unsafe fn create_module<'ll>( ); } + if sess.must_emit_unwind_tables() { + // This assertion checks that Max is the correct merge behavior. + // Async unwind tables are strictly more useful than sync uwtables. + const { + assert!((llvm::UWTableKind::None as u32) < (llvm::UWTableKind::Sync as u32)); + assert!((llvm::UWTableKind::Sync as u32) < (llvm::UWTableKind::Async as u32)); + } + + llvm::add_module_flag_u32( + llmod, + llvm::ModuleFlagMergeBehavior::Max, + "uwtable", + match sess.opts.unstable_opts.use_sync_unwind { + Some(true) => llvm::UWTableKind::Sync as u32, + Some(false) | None => llvm::UWTableKind::Async as u32, + }, + ); + } + // Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.) if sess.is_sanitizer_kcfi_enabled() { llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Override, "kcfi", 1); diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index a240bca80955a..7855584cdd737 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -240,6 +240,18 @@ pub(crate) enum DLLStorageClass { DllExport = 2, // Function to be accessible from DLL. } +/// Must match the layout of `UWTableKind`. +#[derive(Copy, Clone)] +#[repr(C)] +pub(crate) enum UWTableKind { + /// No unwind table requested + None = 0, + /// "Synchronous" unwind tables + Sync = 1, + /// "Asynchronous" unwind tables (instr precise) + Async = 2, +} + /// Must match the layout of `LLVMRustAttributeKind`. /// Semantically a subset of the C++ enum llvm::Attribute::AttrKind, /// though it is not ABI compatible (since it's a C++ enum) From 581bf3c2f935c7e9916cbe0ca0183fd6e6bcc4c7 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Tue, 26 May 2026 14:32:28 +0000 Subject: [PATCH 2/3] Add tests for uwtable annotations on modules --- tests/codegen-llvm/force-no-unwind-tables.rs | 2 ++ tests/codegen-llvm/force-unwind-tables.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/codegen-llvm/force-no-unwind-tables.rs b/tests/codegen-llvm/force-no-unwind-tables.rs index 1de5e0858e0ba..a7d07df5ba6db 100644 --- a/tests/codegen-llvm/force-no-unwind-tables.rs +++ b/tests/codegen-llvm/force-no-unwind-tables.rs @@ -9,3 +9,5 @@ fn foo() { panic!(); } + +// CHECK-NOT: !"uwtable" diff --git a/tests/codegen-llvm/force-unwind-tables.rs b/tests/codegen-llvm/force-unwind-tables.rs index a2ef8a104543d..b406d493e843d 100644 --- a/tests/codegen-llvm/force-unwind-tables.rs +++ b/tests/codegen-llvm/force-unwind-tables.rs @@ -4,3 +4,5 @@ // CHECK: attributes #{{.*}} uwtable pub fn foo() {} + +// CHECK: !{{[0-9]+}} = !{i32 7, !"uwtable", i32 2} From 90fe8cc66f63070eeafed09ec683e95d820316c0 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 27 May 2026 09:27:35 +0000 Subject: [PATCH 3/3] Adjust UWTableKind comment ref to llvm type --- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 7855584cdd737..2728152b5d209 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -240,7 +240,7 @@ pub(crate) enum DLLStorageClass { DllExport = 2, // Function to be accessible from DLL. } -/// Must match the layout of `UWTableKind`. +/// Must match the layout of `llvm::UWTableKind`. #[derive(Copy, Clone)] #[repr(C)] pub(crate) enum UWTableKind {