Skip to content

Commit 9bf2867

Browse files
committed
Codegen **non-overloaded** LLVM intrinsics using their name
1 parent 8afd710 commit 9bf2867

File tree

13 files changed

+242
-46
lines changed

13 files changed

+242
-46
lines changed

compiler/rustc_codegen_gcc/src/type_of.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt::Write;
22

3-
use gccjit::{Struct, Type};
3+
use gccjit::{RValue, Struct, Type};
44
use rustc_abi as abi;
55
use rustc_abi::Primitive::*;
66
use rustc_abi::{
@@ -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

Lines changed: 140 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::borrow::Borrow;
2-
use std::cmp;
2+
use std::{cmp, iter};
33

44
use libc::c_uint;
55
use rustc_abi::{BackendRepr, HasDataLayout, Primitive, Reg, RegKind, Size};
@@ -307,8 +307,39 @@ impl<'ll, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
307307
}
308308
}
309309

310+
pub(crate) enum FunctionSignature<'ll> {
311+
/// The signature is obtained directly from LLVM, and **may not match the Rust signature**
312+
Intrinsic(&'ll Type),
313+
/// The name starts with `llvm.`, but can't obtain the intrinsic ID. May be invalid or upgradable
314+
MaybeInvalidIntrinsic(&'ll Type),
315+
/// Just the Rust signature
316+
Rust(&'ll Type),
317+
}
318+
319+
impl<'ll> FunctionSignature<'ll> {
320+
pub(crate) fn fn_ty(&self) -> &'ll Type {
321+
match self {
322+
FunctionSignature::Intrinsic(fn_ty)
323+
| FunctionSignature::MaybeInvalidIntrinsic(fn_ty)
324+
| FunctionSignature::Rust(fn_ty) => fn_ty,
325+
}
326+
}
327+
}
328+
310329
pub(crate) trait FnAbiLlvmExt<'ll, 'tcx> {
311-
fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
330+
fn llvm_return_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
331+
fn llvm_argument_types(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec<&'ll Type>;
332+
/// When `do_verify` is set, this function performs checks for the signature of LLVM intrinsics
333+
/// and emits a fatal error if it doesn't match. These checks are important,but somewhat expensive
334+
/// So they are only used at function definitions, not at callsites
335+
fn llvm_type(
336+
&self,
337+
cx: &CodegenCx<'ll, 'tcx>,
338+
name: &[u8],
339+
do_verify: bool,
340+
) -> FunctionSignature<'ll>;
341+
/// **If this function is an LLVM intrinsic** checks if the LLVM signature provided matches with this
342+
fn verify_intrinsic_signature(&self, cx: &CodegenCx<'ll, 'tcx>, llvm_ty: &'ll Type) -> bool;
312343
fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
313344
fn llvm_cconv(&self, cx: &CodegenCx<'ll, 'tcx>) -> llvm::CallConv;
314345

@@ -321,30 +352,38 @@ pub(crate) trait FnAbiLlvmExt<'ll, 'tcx> {
321352
);
322353

323354
/// Apply attributes to a function call.
324-
fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value);
355+
fn apply_attrs_callsite(
356+
&self,
357+
bx: &mut Builder<'_, 'll, 'tcx>,
358+
callsite: &'ll Value,
359+
llfn: &'ll Value,
360+
);
325361
}
326362

327363
impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
328-
fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
364+
fn llvm_return_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
365+
match &self.ret.mode {
366+
PassMode::Ignore => cx.type_void(),
367+
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_llvm_type(cx),
368+
PassMode::Cast { cast, pad_i32: _ } => cast.llvm_type(cx),
369+
PassMode::Indirect { .. } => cx.type_void(),
370+
}
371+
}
372+
373+
fn llvm_argument_types(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec<&'ll Type> {
374+
let indirect_return = matches!(self.ret.mode, PassMode::Indirect { .. });
375+
329376
// Ignore "extra" args from the call site for C variadic functions.
330377
// Only the "fixed" args are part of the LLVM function signature.
331378
let args =
332379
if self.c_variadic { &self.args[..self.fixed_count as usize] } else { &self.args };
333380

334-
// This capacity calculation is approximate.
335-
let mut llargument_tys = Vec::with_capacity(
336-
self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 },
337-
);
381+
let mut llargument_tys =
382+
Vec::with_capacity(args.len() + if indirect_return { 1 } else { 0 });
338383

339-
let llreturn_ty = match &self.ret.mode {
340-
PassMode::Ignore => cx.type_void(),
341-
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_llvm_type(cx),
342-
PassMode::Cast { cast, pad_i32: _ } => cast.llvm_type(cx),
343-
PassMode::Indirect { .. } => {
344-
llargument_tys.push(cx.type_ptr());
345-
cx.type_void()
346-
}
347-
};
384+
if indirect_return {
385+
llargument_tys.push(cx.type_ptr());
386+
}
348387

349388
for arg in args {
350389
// Note that the exact number of arguments pushed here is carefully synchronized with
@@ -391,10 +430,74 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
391430
llargument_tys.push(llarg_ty);
392431
}
393432

394-
if self.c_variadic {
395-
cx.type_variadic_func(&llargument_tys, llreturn_ty)
433+
llargument_tys
434+
}
435+
436+
fn verify_intrinsic_signature(&self, cx: &CodegenCx<'ll, 'tcx>, llvm_fn_ty: &'ll Type) -> bool {
437+
let rust_return_ty = self.llvm_return_type(cx);
438+
let rust_argument_tys = self.llvm_argument_types(cx);
439+
440+
let llvm_return_ty = cx.get_return_type(llvm_fn_ty);
441+
let llvm_argument_tys = cx.func_params_types(llvm_fn_ty);
442+
let llvm_is_variadic = cx.func_is_variadic(llvm_fn_ty);
443+
444+
if self.c_variadic != llvm_is_variadic || rust_argument_tys.len() != llvm_argument_tys.len()
445+
{
446+
return false;
447+
}
448+
449+
// todo: add bypasses for types not accessible from Rust here
450+
iter::once((rust_return_ty, llvm_return_ty))
451+
.chain(iter::zip(rust_argument_tys, llvm_argument_tys))
452+
.all(|(rust_ty, llvm_ty)| rust_ty == llvm_ty)
453+
}
454+
455+
fn llvm_type(
456+
&self,
457+
cx: &CodegenCx<'ll, 'tcx>,
458+
name: &[u8],
459+
do_verify: bool,
460+
) -> FunctionSignature<'ll> {
461+
let mut maybe_invalid = false;
462+
463+
if name.starts_with(b"llvm.") {
464+
if let Some(intrinsic) = llvm::Intrinsic::lookup(name) {
465+
if !intrinsic.is_overloaded() {
466+
// FIXME: also do this for overloaded intrinsics
467+
let llvm_fn_ty = cx.intrinsic_type(intrinsic, &[]);
468+
if do_verify {
469+
if !self.verify_intrinsic_signature(cx, llvm_fn_ty) {
470+
cx.tcx.dcx().fatal(format!(
471+
"Intrinsic signature mismatch for `{}`: expected signature `{llvm_fn_ty:?}`",
472+
str::from_utf8(name).unwrap()
473+
));
474+
}
475+
}
476+
return FunctionSignature::Intrinsic(llvm_fn_ty);
477+
}
478+
} else {
479+
// it's one of 2 cases,
480+
// - either the base name is invalid
481+
// - it has been superceded by something else, so the intrinsic was removed entirely
482+
// to check for upgrades, we need the `llfn`, so we defer it for now
483+
484+
maybe_invalid = true;
485+
}
486+
}
487+
488+
let return_ty = self.llvm_return_type(cx);
489+
let argument_tys = self.llvm_argument_types(cx);
490+
491+
let fn_ty = if self.c_variadic {
492+
cx.type_variadic_func(&argument_tys, return_ty)
396493
} else {
397-
cx.type_func(&llargument_tys, llreturn_ty)
494+
cx.type_func(&argument_tys, return_ty)
495+
};
496+
497+
if maybe_invalid {
498+
FunctionSignature::MaybeInvalidIntrinsic(fn_ty)
499+
} else {
500+
FunctionSignature::Rust(fn_ty)
398501
}
399502
}
400503

@@ -531,7 +634,23 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
531634
}
532635
}
533636

534-
fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value) {
637+
fn apply_attrs_callsite(
638+
&self,
639+
bx: &mut Builder<'_, 'll, 'tcx>,
640+
callsite: &'ll Value,
641+
llfn: &'ll Value,
642+
) {
643+
// if we are using the LLVM signature, use the LLVM attributes otherwise it might be problematic
644+
let name = llvm::get_value_name(llfn);
645+
if name.starts_with(b"llvm.")
646+
&& let Some(intrinsic) = llvm::Intrinsic::lookup(name)
647+
{
648+
// FIXME: also do this for overloaded intrinsics
649+
if !intrinsic.is_overloaded() {
650+
return;
651+
}
652+
}
653+
535654
let mut func_attrs = SmallVec::<[_; 2]>::new();
536655
if self.ret.layout.is_uninhabited() {
537656
func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(bx.cx.llcx));

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
380380
)
381381
};
382382
if let Some(fn_abi) = fn_abi {
383-
fn_abi.apply_attrs_callsite(self, invoke);
383+
fn_abi.apply_attrs_callsite(self, invoke, llfn);
384384
}
385385
invoke
386386
}
@@ -1433,7 +1433,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
14331433
)
14341434
};
14351435
if let Some(fn_abi) = fn_abi {
1436-
fn_abi.apply_attrs_callsite(self, call);
1436+
fn_abi.apply_attrs_callsite(self, call, llfn);
14371437
}
14381438
call
14391439
}
@@ -1769,7 +1769,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17691769
)
17701770
};
17711771
if let Some(fn_abi) = fn_abi {
1772-
fn_abi.apply_attrs_callsite(self, callbr);
1772+
fn_abi.apply_attrs_callsite(self, callbr, llfn);
17731773
}
17741774
callbr
17751775
}

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,21 @@ impl<'ll> CodegenCx<'ll, '_> {
12601260
self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));
12611261
eh_catch_typeinfo
12621262
}
1263+
1264+
pub(crate) fn intrinsic_type(
1265+
&self,
1266+
intrinsic: llvm::Intrinsic,
1267+
type_params: &[&'ll Type],
1268+
) -> &'ll Type {
1269+
unsafe {
1270+
llvm::LLVMIntrinsicGetType(
1271+
self.llcx(),
1272+
intrinsic.id(),
1273+
type_params.as_ptr(),
1274+
type_params.len(),
1275+
)
1276+
}
1277+
}
12631278
}
12641279

12651280
impl CodegenCx<'_, '_> {

compiler/rustc_codegen_llvm/src/declare.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_target::callconv::FnAbi;
2222
use smallvec::SmallVec;
2323
use tracing::debug;
2424

25-
use crate::abi::FnAbiLlvmExt;
25+
use crate::abi::{FnAbiLlvmExt, FunctionSignature};
2626
use crate::common::AsCCharPtr;
2727
use crate::context::{CodegenCx, GenericCx, SCx, SimpleCx};
2828
use crate::llvm::AttributePlace::Function;
@@ -150,17 +150,34 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
150150
) -> &'ll Value {
151151
debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
152152

153-
// Function addresses in Rust are never significant, allowing functions to
154-
// be merged.
155-
let llfn = declare_raw_fn(
156-
self,
157-
name,
158-
fn_abi.llvm_cconv(self),
159-
llvm::UnnamedAddr::Global,
160-
llvm::Visibility::Default,
161-
fn_abi.llvm_type(self),
162-
);
163-
fn_abi.apply_attrs_llfn(self, llfn, instance);
153+
let signature = fn_abi.llvm_type(self, name.as_bytes(), true);
154+
let llfn;
155+
156+
if let FunctionSignature::Intrinsic(fn_ty) = signature {
157+
// intrinsics have a specified set of attributes, so we don't use the `FnAbi` set for them
158+
llfn = declare_simple_fn(
159+
self,
160+
name,
161+
fn_abi.llvm_cconv(self),
162+
llvm::UnnamedAddr::Global,
163+
llvm::Visibility::Default,
164+
fn_ty,
165+
);
166+
} else {
167+
// Function addresses in Rust are never significant, allowing functions to
168+
// be merged.
169+
llfn = declare_raw_fn(
170+
self,
171+
name,
172+
fn_abi.llvm_cconv(self),
173+
llvm::UnnamedAddr::Global,
174+
llvm::Visibility::Default,
175+
signature.fn_ty(),
176+
);
177+
fn_abi.apply_attrs_llfn(self, llfn, instance);
178+
}
179+
180+
// todo: check for upgrades, and emit error if not upgradable
164181

165182
if self.tcx.sess.is_sanitizer_cfi_enabled() {
166183
if let Some(instance) = instance {

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ fn gen_fn<'a, 'll, 'tcx>(
10861086
codegen: &mut dyn FnMut(Builder<'a, 'll, 'tcx>),
10871087
) -> (&'ll Type, &'ll Value) {
10881088
let fn_abi = cx.fn_abi_of_fn_ptr(rust_fn_sig, ty::List::empty());
1089-
let llty = fn_abi.llvm_type(cx);
1089+
let llty = fn_abi.llvm_type(cx, name.as_bytes(), true).fn_ty();
10901090
let llfn = cx.declare_fn(name, fn_abi, None);
10911091
cx.set_frame_pointer_type(llfn);
10921092
cx.apply_target_cpu_attr(llfn);

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ unsafe extern "C" {
10591059
) -> &'a Type;
10601060
pub(crate) fn LLVMCountParamTypes(FunctionTy: &Type) -> c_uint;
10611061
pub(crate) fn LLVMGetParamTypes<'a>(FunctionTy: &'a Type, Dest: *mut &'a Type);
1062+
pub(crate) fn LLVMIsFunctionVarArg(FunctionTy: &Type) -> Bool;
10621063

10631064
// Operations on struct types
10641065
pub(crate) fn LLVMStructTypeInContext<'a>(
@@ -1194,6 +1195,14 @@ unsafe extern "C" {
11941195

11951196
// Operations on functions
11961197
pub(crate) fn LLVMSetFunctionCallConv(Fn: &Value, CC: c_uint);
1198+
pub(crate) fn LLVMLookupIntrinsicID(Name: *const c_char, NameLen: size_t) -> c_uint;
1199+
pub(crate) fn LLVMIntrinsicGetType<'a>(
1200+
C: &'a Context,
1201+
ID: c_uint,
1202+
ParamTypes: *const &'a Type,
1203+
ParamCount: size_t,
1204+
) -> &'a Type;
1205+
pub(crate) fn LLVMIntrinsicIsOverloaded(ID: c_uint) -> Bool;
11971206

11981207
// Operations on parameters
11991208
pub(crate) fn LLVMIsAArgument(Val: &Value) -> Option<&Value>;

compiler/rustc_codegen_llvm/src/llvm/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,26 @@ pub(crate) fn get_value_name(value: &Value) -> &[u8] {
327327
}
328328
}
329329

330+
#[derive(Debug, Copy, Clone)]
331+
pub(crate) struct Intrinsic {
332+
id: c_uint,
333+
}
334+
335+
impl Intrinsic {
336+
pub(crate) fn lookup(name: &[u8]) -> Option<Self> {
337+
let id = unsafe { LLVMLookupIntrinsicID(name.as_c_char_ptr(), name.len()) };
338+
if id == 0 { None } else { Some(Self { id }) }
339+
}
340+
341+
pub(crate) fn id(self) -> c_uint {
342+
self.id
343+
}
344+
345+
pub(crate) fn is_overloaded(self) -> bool {
346+
unsafe { LLVMIntrinsicIsOverloaded(self.id) == True }
347+
}
348+
}
349+
330350
/// Safe wrapper for `LLVMSetValueName2` from a byte slice
331351
pub(crate) fn set_value_name(value: &Value, name: &[u8]) {
332352
unsafe {

0 commit comments

Comments
 (0)