From f03581a12b6cff3ed299bb7c977c86bea56f4122 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Tue, 24 Feb 2026 11:27:44 +0000 Subject: [PATCH] Introduce FunctionSignature associated type for BackendTypes In Cranelift the regular Type enum can't represent function signatures. Function pointers are represented as plain pointer sized integer. --- compiler/rustc_codegen_gcc/src/builder.rs | 1 + compiler/rustc_codegen_gcc/src/context.rs | 1 + compiler/rustc_codegen_llvm/src/builder.rs | 1 + compiler/rustc_codegen_llvm/src/common.rs | 1 + compiler/rustc_codegen_ssa/src/traits/backend.rs | 1 + compiler/rustc_codegen_ssa/src/traits/builder.rs | 7 ++++--- compiler/rustc_codegen_ssa/src/traits/misc.rs | 2 +- compiler/rustc_codegen_ssa/src/traits/type_.rs | 4 ++-- 8 files changed, 12 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_gcc/src/builder.rs b/compiler/rustc_codegen_gcc/src/builder.rs index 511d047a8031..0dfa9c09ff99 100644 --- a/compiler/rustc_codegen_gcc/src/builder.rs +++ b/compiler/rustc_codegen_gcc/src/builder.rs @@ -491,6 +491,7 @@ impl<'gcc, 'tcx> BackendTypes for Builder<'_, 'gcc, 'tcx> { type Value = as BackendTypes>::Value; type Type = as BackendTypes>::Type; + type FunctionSignature = as BackendTypes>::FunctionSignature; type DIScope = as BackendTypes>::DIScope; type DILocation = as BackendTypes>::DILocation; diff --git a/compiler/rustc_codegen_gcc/src/context.rs b/compiler/rustc_codegen_gcc/src/context.rs index e71963d9081b..ada3d73f612e 100644 --- a/compiler/rustc_codegen_gcc/src/context.rs +++ b/compiler/rustc_codegen_gcc/src/context.rs @@ -386,6 +386,7 @@ impl<'gcc, 'tcx> BackendTypes for CodegenCx<'gcc, 'tcx> { type Value = RValue<'gcc>; type Type = Type<'gcc>; + type FunctionSignature = Type<'gcc>; type DIScope = (); // TODO(antoyo) type DILocation = Location<'gcc>; diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 250d3d35ca6a..8d65c0d4e0b0 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -202,6 +202,7 @@ impl<'ll, CX: Borrow>> BackendTypes for GenericBuilder<'_, 'll, CX> { type Value = as BackendTypes>::Value; type Type = as BackendTypes>::Type; + type FunctionSignature = as BackendTypes>::FunctionSignature; type DIScope = as BackendTypes>::DIScope; type DILocation = as BackendTypes>::DILocation; diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs index 8db4e16b3e79..b1f03f58d925 100644 --- a/compiler/rustc_codegen_llvm/src/common.rs +++ b/compiler/rustc_codegen_llvm/src/common.rs @@ -89,6 +89,7 @@ impl<'ll, CX: Borrow>> BackendTypes for GenericCx<'ll, CX> { type Value = &'ll Value; type Type = &'ll Type; + type FunctionSignature = &'ll Type; type DIScope = &'ll llvm::debuginfo::DIScope; type DILocation = &'ll llvm::debuginfo::DILocation; diff --git a/compiler/rustc_codegen_ssa/src/traits/backend.rs b/compiler/rustc_codegen_ssa/src/traits/backend.rs index 8ca620ff4fb4..60f2d4155d4b 100644 --- a/compiler/rustc_codegen_ssa/src/traits/backend.rs +++ b/compiler/rustc_codegen_ssa/src/traits/backend.rs @@ -27,6 +27,7 @@ pub trait BackendTypes { type Value: CodegenObject + PartialEq; type Type: CodegenObject + PartialEq; + type FunctionSignature: CodegenObject + PartialEq; // FIXME(eddyb) find a common convention for all of the debuginfo-related // names (choose between `Dbg`, `Debug`, `DebugInfo`, `DI` etc.). diff --git a/compiler/rustc_codegen_ssa/src/traits/builder.rs b/compiler/rustc_codegen_ssa/src/traits/builder.rs index 9b045aa4a146..9d202effc28b 100644 --- a/compiler/rustc_codegen_ssa/src/traits/builder.rs +++ b/compiler/rustc_codegen_ssa/src/traits/builder.rs @@ -53,6 +53,7 @@ pub trait BuilderMethods<'a, 'tcx>: Function = Self::Function, BasicBlock = Self::BasicBlock, Type = Self::Type, + FunctionSignature = Self::FunctionSignature, Funclet = Self::Funclet, DIScope = Self::DIScope, DILocation = Self::DILocation, @@ -124,7 +125,7 @@ fn switch_with_weights( fn invoke( &mut self, - llty: Self::Type, + llty: Self::FunctionSignature, fn_attrs: Option<&CodegenFnAttrs>, fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>, llfn: Self::Value, @@ -621,7 +622,7 @@ fn atomic_rmw( /// assuming the function does not explicitly pass the destination as a pointer in `args`. fn call( &mut self, - llty: Self::Type, + llty: Self::FunctionSignature, caller_attrs: Option<&CodegenFnAttrs>, fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>, fn_val: Self::Value, @@ -632,7 +633,7 @@ fn call( fn tail_call( &mut self, - llty: Self::Type, + llty: Self::FunctionSignature, caller_attrs: Option<&CodegenFnAttrs>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>, llfn: Self::Value, diff --git a/compiler/rustc_codegen_ssa/src/traits/misc.rs b/compiler/rustc_codegen_ssa/src/traits/misc.rs index 710fab279016..6a0f88983349 100644 --- a/compiler/rustc_codegen_ssa/src/traits/misc.rs +++ b/compiler/rustc_codegen_ssa/src/traits/misc.rs @@ -25,5 +25,5 @@ fn apply_vcall_visibility_metadata( fn apply_target_cpu_attr(&self, llfn: Self::Function); /// Declares the extern "C" main function for the entry point. Returns None if the symbol /// already exists. - fn declare_c_main(&self, fn_type: Self::Type) -> Option; + fn declare_c_main(&self, fn_type: Self::FunctionSignature) -> Option; } diff --git a/compiler/rustc_codegen_ssa/src/traits/type_.rs b/compiler/rustc_codegen_ssa/src/traits/type_.rs index 3000f84e5e32..45ea8384b2d4 100644 --- a/compiler/rustc_codegen_ssa/src/traits/type_.rs +++ b/compiler/rustc_codegen_ssa/src/traits/type_.rs @@ -23,7 +23,7 @@ pub trait BaseTypeCodegenMethods: BackendTypes { fn type_f128(&self) -> Self::Type; fn type_array(&self, ty: Self::Type, len: u64) -> Self::Type; - fn type_func(&self, args: &[Self::Type], ret: Self::Type) -> Self::Type; + fn type_func(&self, args: &[Self::Type], ret: Self::Type) -> Self::FunctionSignature; fn type_kind(&self, ty: Self::Type) -> TypeKind; fn type_ptr(&self) -> Self::Type; fn type_ptr_ext(&self, address_space: AddressSpace) -> Self::Type; @@ -114,7 +114,7 @@ pub trait LayoutTypeCodegenMethods<'tcx>: BackendTypes { /// such as when it's stack-allocated or when it's being loaded or stored. fn backend_type(&self, layout: TyAndLayout<'tcx>) -> Self::Type; fn cast_backend_type(&self, ty: &CastTarget) -> Self::Type; - fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::Type; + fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::FunctionSignature; fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::Type; fn reg_backend_type(&self, ty: &Reg) -> Self::Type; /// The backend type used for a rust type when it's in an SSA register.