Skip to content

Commit 4fe6c09

Browse files
committed
Select functions through their names insead of only ABI
1 parent 0ea0fb5 commit 4fe6c09

File tree

9 files changed

+32
-18
lines changed

9 files changed

+32
-18
lines changed

compiler/rustc_codegen_gcc/src/type_of.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,11 @@ impl<'gcc, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
373373
unimplemented!();
374374
}
375375

376-
fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
376+
fn fn_decl_backend_type(
377+
&self,
378+
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
379+
_fn_ptr: RValue<'gcc>,
380+
) -> Type<'gcc> {
377381
// FIXME(antoyo): Should we do something with `FnAbiGcc::fn_attributes`?
378382
let FnAbiGcc { return_type, arguments_type, is_c_variadic, .. } = fn_abi.gcc_type(self);
379383
self.context.new_function_pointer_type(None, return_type, &arguments_type, is_c_variadic)

compiler/rustc_codegen_llvm/src/abi.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<'ll, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
309309
}
310310

311311
pub(crate) trait FnAbiLlvmExt<'ll, 'tcx> {
312-
fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
312+
fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>, name: &[u8]) -> &'ll Type;
313313
fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
314314
fn llvm_cconv(&self, cx: &CodegenCx<'ll, 'tcx>) -> llvm::CallConv;
315315

@@ -326,20 +326,22 @@ pub(crate) trait FnAbiLlvmExt<'ll, 'tcx> {
326326
}
327327

328328
impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
329-
fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
329+
fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>, name: &[u8]) -> &'ll Type {
330330
// Ignore "extra" args from the call site for C variadic functions.
331331
// Only the "fixed" args are part of the LLVM function signature.
332332
let args =
333333
if self.c_variadic { &self.args[..self.fixed_count as usize] } else { &self.args };
334334

335+
// todo(sayantn): a better way is to look at the `link_name` instead of the function name, because function name can be "faked" using `#[export_name]`
336+
let llvm_intrinsic = name.starts_with(b"llvm.")
337+
&& !self.c_variadic
338+
&& self.conv == Conv::C
339+
&& !self.can_unwind;
340+
let amx_intrinsic =
341+
llvm_intrinsic && name.starts_with(b"llvm.x86.") && name.ends_with(b".internal");
335342
let adjust_ty = |ty| {
336-
// todo: rectify this to be more selective (help wanted)
337-
let probably_unadjusted = self.conv == Conv::C && !self.can_unwind && !self.c_variadic;
338-
let probably_amx_intrinsic = probably_unadjusted && cx.tcx.sess.target.arch == "x86_64";
339343
// Change type to `x86amx` from `i32x256` for x86_64 AMX intrinsics
340-
if probably_amx_intrinsic
341-
&& cx.type_kind(ty) == TypeKind::Vector
342-
&& cx.vector_length(ty) == 256
344+
if amx_intrinsic && cx.type_kind(ty) == TypeKind::Vector && cx.vector_length(ty) == 256
343345
{
344346
let element_ty = cx.element_type(ty);
345347
if cx.type_kind(element_ty) == TypeKind::Integer && cx.int_width(element_ty) == 32 {

compiler/rustc_codegen_llvm/src/declare.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
158158
fn_abi.llvm_cconv(self),
159159
llvm::UnnamedAddr::Global,
160160
llvm::Visibility::Default,
161-
fn_abi.llvm_type(self),
161+
fn_abi.llvm_type(self, name.as_ref()),
162162
);
163163
fn_abi.apply_attrs_llfn(self, llfn, instance);
164164

compiler/rustc_codegen_llvm/src/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ fn gen_fn<'a, 'll, 'tcx>(
10901090
codegen: &mut dyn FnMut(Builder<'a, 'll, 'tcx>),
10911091
) -> (&'ll Type, &'ll Value) {
10921092
let fn_abi = cx.fn_abi_of_fn_ptr(rust_fn_sig, ty::List::empty());
1093-
let llty = fn_abi.llvm_type(cx);
1093+
let llty = fn_abi.llvm_type(cx, name.as_ref());
10941094
let llfn = cx.declare_fn(name, fn_abi, None);
10951095
cx.set_frame_pointer_type(llfn);
10961096
cx.apply_target_cpu_attr(llfn);

compiler/rustc_codegen_llvm/src/type_.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,12 @@ impl<'ll, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
288288
fn cast_backend_type(&self, ty: &CastTarget) -> &'ll Type {
289289
ty.llvm_type(self)
290290
}
291-
fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Type {
292-
fn_abi.llvm_type(self)
291+
fn fn_decl_backend_type(
292+
&self,
293+
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
294+
fn_ptr: &'ll Value,
295+
) -> &'ll Type {
296+
fn_abi.llvm_type(self, llvm::get_value_name(fn_ptr))
293297
}
294298
fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Type {
295299
fn_abi.ptr_to_llvm_type(self)

compiler/rustc_codegen_ssa/src/mir/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
187187

188188
// If there is a cleanup block and the function we're calling can unwind, then
189189
// do an invoke, otherwise do a call.
190-
let fn_ty = bx.fn_decl_backend_type(fn_abi);
190+
let fn_ty = bx.fn_decl_backend_type(fn_abi, fn_ptr);
191191

192192
let fn_attrs = if bx.tcx().def_kind(fx.instance.def_id()).has_codegen_attrs() {
193193
Some(bx.tcx().codegen_fn_attrs(fx.instance.def_id()))
@@ -1806,7 +1806,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
18061806
if is_call_from_compiler_builtins_to_upstream_monomorphization(bx.tcx(), instance) {
18071807
bx.abort();
18081808
} else {
1809-
let fn_ty = bx.fn_decl_backend_type(fn_abi);
1809+
let fn_ty = bx.fn_decl_backend_type(fn_abi, fn_ptr);
18101810

18111811
let llret = bx.call(fn_ty, None, Some(fn_abi), fn_ptr, &[], funclet.as_ref(), None);
18121812
bx.apply_attrs_to_cleanup_callsite(llret);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
779779
};
780780
let fn_ptr = bx.get_fn_addr(instance);
781781
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
782-
let fn_ty = bx.fn_decl_backend_type(fn_abi);
782+
let fn_ty = bx.fn_decl_backend_type(fn_abi, fn_ptr);
783783
let fn_attrs = if bx.tcx().def_kind(instance.def_id()).has_codegen_attrs() {
784784
Some(bx.tcx().codegen_fn_attrs(instance.def_id()))
785785
} else {

compiler/rustc_codegen_ssa/src/size_of_val.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
6767
// Generate the call. Cannot use `do_call` since we don't have a MIR terminator so we
6868
// can't create a `TerminationCodegenHelper`. (But we are in good company, this code is
6969
// duplicated plenty of times.)
70-
let fn_ty = bx.fn_decl_backend_type(fn_abi);
70+
let fn_ty = bx.fn_decl_backend_type(fn_abi, llfn);
7171

7272
bx.call(
7373
fn_ty,

compiler/rustc_codegen_ssa/src/traits/type_.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ pub trait LayoutTypeCodegenMethods<'tcx>: BackendTypes {
9696
/// such as when it's stack-allocated or when it's being loaded or stored.
9797
fn backend_type(&self, layout: TyAndLayout<'tcx>) -> Self::Type;
9898
fn cast_backend_type(&self, ty: &CastTarget) -> Self::Type;
99-
fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::Type;
99+
fn fn_decl_backend_type(
100+
&self,
101+
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
102+
fn_ptr: Self::Value,
103+
) -> Self::Type;
100104
fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::Type;
101105
fn reg_backend_type(&self, ty: &Reg) -> Self::Type;
102106
/// The backend type used for a rust type when it's in an SSA register.

0 commit comments

Comments
 (0)