1
Fork 0

Rollup merge of #137549 - oli-obk:llvm-ffi, r=davidtwco

Clean up various LLVM FFI things in codegen_llvm

cc ```@ZuseZ4``` I touched some autodiff parts

The major change of this PR is [bfd88ce](https://github.com/rust-lang/rust/pull/137549/commits/bfd88cead0dd79717f123ad7e9a26ecad88653cb) which makes `CodegenCx` generic just like `GenericBuilder`

The other commits mostly took advantage of the new feature of making extern functions safe, but also just used some wrappers that were already there and shrunk unsafe blocks.

best reviewed commit-by-commit
This commit is contained in:
Matthias Krüger 2025-03-07 19:15:34 +01:00 committed by GitHub
commit 63c548d82c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 336 additions and 368 deletions

View file

@ -6,6 +6,7 @@ use std::sync::Arc;
use std::sync::mpsc::{Receiver, Sender, channel};
use std::{fs, io, mem, str, thread};
use rustc_abi::Size;
use rustc_ast::attr;
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
@ -355,6 +356,7 @@ pub struct CodegenContext<B: WriteBackendMethods> {
pub target_is_like_aix: bool,
pub split_debuginfo: rustc_target::spec::SplitDebuginfo,
pub split_dwarf_kind: rustc_session::config::SplitDwarfKind,
pub pointer_size: Size,
/// All commandline args used to invoke the compiler, with @file args fully expanded.
/// This will only be used within debug info, e.g. in the pdb file on windows
@ -1216,6 +1218,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
split_debuginfo: tcx.sess.split_debuginfo(),
split_dwarf_kind: tcx.sess.opts.unstable_opts.split_dwarf_kind,
parallel: backend.supports_parallel() && !sess.opts.unstable_opts.no_parallel_backend,
pointer_size: tcx.data_layout.pointer_size,
};
// This is the "main loop" of parallel work happening for parallel codegen.

View file

@ -133,7 +133,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
Self::alloca(bx, ptr_layout)
}
pub fn len<Cx: ConstCodegenMethods<'tcx, Value = V>>(&self, cx: &Cx) -> V {
pub fn len<Cx: ConstCodegenMethods<Value = V>>(&self, cx: &Cx) -> V {
if let FieldsShape::Array { count, .. } = self.layout.fields {
if self.layout.is_unsized() {
assert_eq!(count, 0);

View file

@ -1,5 +1,5 @@
use super::BackendTypes;
pub trait AbiBuilderMethods<'tcx>: BackendTypes {
pub trait AbiBuilderMethods: BackendTypes {
fn get_param(&mut self, index: usize) -> Self::Value;
}

View file

@ -40,7 +40,7 @@ pub trait BuilderMethods<'a, 'tcx>:
+ CoverageInfoBuilderMethods<'tcx>
+ DebugInfoBuilderMethods
+ ArgAbiBuilderMethods<'tcx>
+ AbiBuilderMethods<'tcx>
+ AbiBuilderMethods
+ IntrinsicCallBuilderMethods<'tcx>
+ AsmBuilderMethods<'tcx>
+ StaticBuilderMethods

View file

@ -3,7 +3,7 @@ use rustc_middle::mir::interpret::{ConstAllocation, Scalar};
use super::BackendTypes;
pub trait ConstCodegenMethods<'tcx>: BackendTypes {
pub trait ConstCodegenMethods: BackendTypes {
// Constant constructors
fn const_null(&self, t: Self::Type) -> Self::Value;
/// Generate an uninitialized value (matching uninitialized memory in MIR).
@ -37,7 +37,7 @@ pub trait ConstCodegenMethods<'tcx>: BackendTypes {
fn const_to_opt_uint(&self, v: Self::Value) -> Option<u64>;
fn const_to_opt_u128(&self, v: Self::Value, sign_ext: bool) -> Option<u128>;
fn const_data_from_alloc(&self, alloc: ConstAllocation<'tcx>) -> Self::Value;
fn const_data_from_alloc(&self, alloc: ConstAllocation<'_>) -> Self::Value;
fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, llty: Self::Type) -> Self::Value;

View file

@ -55,7 +55,7 @@ pub trait CodegenObject = Copy + PartialEq + fmt::Debug;
pub trait CodegenMethods<'tcx> = LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>>
+ FnAbiOf<'tcx, FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>>
+ TypeCodegenMethods<'tcx>
+ ConstCodegenMethods<'tcx>
+ ConstCodegenMethods
+ StaticCodegenMethods
+ DebugInfoCodegenMethods<'tcx>
+ AsmCodegenMethods<'tcx>

View file

@ -9,7 +9,7 @@ use super::misc::MiscCodegenMethods;
use crate::common::TypeKind;
use crate::mir::place::PlaceRef;
pub trait BaseTypeCodegenMethods<'tcx>: BackendTypes {
pub trait BaseTypeCodegenMethods: BackendTypes {
fn type_i8(&self) -> Self::Type;
fn type_i16(&self) -> Self::Type;
fn type_i32(&self) -> Self::Type;
@ -41,7 +41,7 @@ pub trait BaseTypeCodegenMethods<'tcx>: BackendTypes {
}
pub trait DerivedTypeCodegenMethods<'tcx>:
BaseTypeCodegenMethods<'tcx> + MiscCodegenMethods<'tcx> + HasTyCtxt<'tcx> + HasTypingEnv<'tcx>
BaseTypeCodegenMethods + MiscCodegenMethods<'tcx> + HasTyCtxt<'tcx> + HasTypingEnv<'tcx>
{
fn type_int(&self) -> Self::Type {
match &self.sess().target.c_int_width[..] {
@ -87,10 +87,7 @@ pub trait DerivedTypeCodegenMethods<'tcx>:
}
impl<'tcx, T> DerivedTypeCodegenMethods<'tcx> for T where
Self: BaseTypeCodegenMethods<'tcx>
+ MiscCodegenMethods<'tcx>
+ HasTyCtxt<'tcx>
+ HasTypingEnv<'tcx>
Self: BaseTypeCodegenMethods + MiscCodegenMethods<'tcx> + HasTyCtxt<'tcx> + HasTypingEnv<'tcx>
{
}