Skip to content

Commit 800d6a3

Browse files
authored
Unrolled build for #147327
Rollup merge of #147327 - Zalathar:inherent, r=workingjubilee cg_llvm: Remove inherent methods from several LLVM FFI types This is mainly motivated by #142897, which proposes to move the LLVM FFI bindings out of `rustc_codegen_llvm` and into `rustc_llvm`, which is arguably the more correct place for them from a linking perspective. --- In order to perform that migration, all of the types used in FFI signatures also need to be moved. However, several of those types have inherent methods that convert from backend-independent types to LLVM FFI types. Moving the inherent methods as-is would require adding a lot of otherwise-unnecessary dependencies to `rustc_llvm`. And we can't leave them behind as-is, because inherent methods can't be defined in another crate. Therefore, this PR replaces several of those inherent methods with either extension trait methods or free functions.
2 parents 981353c + f8c54d2 commit 800d6a3

File tree

14 files changed

+225
-225
lines changed

14 files changed

+225
-225
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::borrow::Borrow;
21
use std::cmp;
32

43
use libc::c_uint;
@@ -13,7 +12,7 @@ use rustc_codegen_ssa::traits::*;
1312
use rustc_middle::ty::Ty;
1413
use rustc_middle::ty::layout::LayoutOf;
1514
use rustc_middle::{bug, ty};
16-
use rustc_session::config;
15+
use rustc_session::{Session, config};
1716
use rustc_target::callconv::{
1817
ArgAbi, ArgAttribute, ArgAttributes, ArgExtension, CastTarget, FnAbi, PassMode,
1918
};
@@ -400,7 +399,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
400399
}
401400

402401
fn llvm_cconv(&self, cx: &CodegenCx<'ll, 'tcx>) -> llvm::CallConv {
403-
llvm::CallConv::from_conv(self.conv, cx.tcx.sess.target.arch.borrow())
402+
to_llvm_calling_convention(cx.tcx.sess, self.conv)
404403
}
405404

406405
fn apply_attrs_llfn(
@@ -663,43 +662,44 @@ impl AbiBuilderMethods for Builder<'_, '_, '_> {
663662
}
664663
}
665664

666-
impl llvm::CallConv {
667-
pub(crate) fn from_conv(conv: CanonAbi, arch: &str) -> Self {
668-
match conv {
669-
CanonAbi::C | CanonAbi::Rust => llvm::CCallConv,
670-
CanonAbi::RustCold => llvm::PreserveMost,
671-
// Functions with this calling convention can only be called from assembly, but it is
672-
// possible to declare an `extern "custom"` block, so the backend still needs a calling
673-
// convention for declaring foreign functions.
674-
CanonAbi::Custom => llvm::CCallConv,
675-
CanonAbi::GpuKernel => {
676-
if arch == "amdgpu" {
677-
llvm::AmdgpuKernel
678-
} else if arch == "nvptx64" {
679-
llvm::PtxKernel
680-
} else {
681-
panic!("Architecture {arch} does not support GpuKernel calling convention");
682-
}
665+
/// Determines the appropriate [`llvm::CallConv`] to use for a given function
666+
/// ABI, for the current target.
667+
pub(crate) fn to_llvm_calling_convention(sess: &Session, abi: CanonAbi) -> llvm::CallConv {
668+
match abi {
669+
CanonAbi::C | CanonAbi::Rust => llvm::CCallConv,
670+
CanonAbi::RustCold => llvm::PreserveMost,
671+
// Functions with this calling convention can only be called from assembly, but it is
672+
// possible to declare an `extern "custom"` block, so the backend still needs a calling
673+
// convention for declaring foreign functions.
674+
CanonAbi::Custom => llvm::CCallConv,
675+
CanonAbi::GpuKernel => {
676+
let arch = sess.target.arch.as_ref();
677+
if arch == "amdgpu" {
678+
llvm::AmdgpuKernel
679+
} else if arch == "nvptx64" {
680+
llvm::PtxKernel
681+
} else {
682+
panic!("Architecture {arch} does not support GpuKernel calling convention");
683683
}
684-
CanonAbi::Interrupt(interrupt_kind) => match interrupt_kind {
685-
InterruptKind::Avr => llvm::AvrInterrupt,
686-
InterruptKind::AvrNonBlocking => llvm::AvrNonBlockingInterrupt,
687-
InterruptKind::Msp430 => llvm::Msp430Intr,
688-
InterruptKind::RiscvMachine | InterruptKind::RiscvSupervisor => llvm::CCallConv,
689-
InterruptKind::X86 => llvm::X86_Intr,
690-
},
691-
CanonAbi::Arm(arm_call) => match arm_call {
692-
ArmCall::Aapcs => llvm::ArmAapcsCallConv,
693-
ArmCall::CCmseNonSecureCall | ArmCall::CCmseNonSecureEntry => llvm::CCallConv,
694-
},
695-
CanonAbi::X86(x86_call) => match x86_call {
696-
X86Call::Fastcall => llvm::X86FastcallCallConv,
697-
X86Call::Stdcall => llvm::X86StdcallCallConv,
698-
X86Call::SysV64 => llvm::X86_64_SysV,
699-
X86Call::Thiscall => llvm::X86_ThisCall,
700-
X86Call::Vectorcall => llvm::X86_VectorCall,
701-
X86Call::Win64 => llvm::X86_64_Win64,
702-
},
703684
}
685+
CanonAbi::Interrupt(interrupt_kind) => match interrupt_kind {
686+
InterruptKind::Avr => llvm::AvrInterrupt,
687+
InterruptKind::AvrNonBlocking => llvm::AvrNonBlockingInterrupt,
688+
InterruptKind::Msp430 => llvm::Msp430Intr,
689+
InterruptKind::RiscvMachine | InterruptKind::RiscvSupervisor => llvm::CCallConv,
690+
InterruptKind::X86 => llvm::X86_Intr,
691+
},
692+
CanonAbi::Arm(arm_call) => match arm_call {
693+
ArmCall::Aapcs => llvm::ArmAapcsCallConv,
694+
ArmCall::CCmseNonSecureCall | ArmCall::CCmseNonSecureEntry => llvm::CCallConv,
695+
},
696+
CanonAbi::X86(x86_call) => match x86_call {
697+
X86Call::Fastcall => llvm::X86FastcallCallConv,
698+
X86Call::Stdcall => llvm::X86StdcallCallConv,
699+
X86Call::SysV64 => llvm::X86_64_SysV,
700+
X86Call::Thiscall => llvm::X86_ThisCall,
701+
X86Call::Vectorcall => llvm::X86_VectorCall,
702+
X86Call::Win64 => llvm::X86_64_Win64,
703+
},
704704
}
705705
}

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_symbol_mangling::mangle_internal_symbol;
1414
use crate::attributes::llfn_attrs_from_instance;
1515
use crate::builder::SBuilder;
1616
use crate::declare::declare_simple_fn;
17-
use crate::llvm::{self, FALSE, TRUE, Type, Value};
17+
use crate::llvm::{self, FALSE, FromGeneric, TRUE, Type, Value};
1818
use crate::{SimpleCx, attributes, debuginfo};
1919

2020
pub(crate) unsafe fn codegen(

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::errors::{
4444
};
4545
use crate::llvm::diagnostic::OptimizationDiagnosticKind::*;
4646
use crate::llvm::{self, DiagnosticInfo};
47-
use crate::type_::Type;
47+
use crate::type_::llvm_type_ptr;
4848
use crate::{LlvmCodegenBackend, ModuleLlvm, base, common, llvm_util};
4949

5050
pub(crate) fn llvm_err<'a>(dcx: DiagCtxtHandle<'_>, err: LlvmError<'a>) -> ! {
@@ -1160,7 +1160,7 @@ fn create_msvc_imps(
11601160
// underscores added in front).
11611161
let prefix = if cgcx.target_arch == "x86" { "\x01__imp__" } else { "\x01__imp_" };
11621162

1163-
let ptr_ty = Type::ptr_llcx(llcx);
1163+
let ptr_ty = llvm_type_ptr(llcx);
11641164
let globals = base::iter_globals(llmod)
11651165
.filter(|&val| {
11661166
llvm::get_linkage(val) == llvm::Linkage::ExternalLinkage && !llvm::is_declaration(val)

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ use crate::attributes;
3636
use crate::common::Funclet;
3737
use crate::context::{CodegenCx, FullCx, GenericCx, SCx};
3838
use crate::llvm::{
39-
self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, GEPNoWrapFlags, Metadata, TRUE, ToLlvmBool,
39+
self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, FromGeneric, GEPNoWrapFlags, Metadata, TRUE,
40+
ToLlvmBool,
4041
};
4142
use crate::type_::Type;
4243
use crate::type_of::LayoutLlvmExt;

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use rustc_symbol_mangling::mangle_internal_symbol;
3131
use rustc_target::spec::{HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel};
3232
use smallvec::SmallVec;
3333

34+
use crate::abi::to_llvm_calling_convention;
3435
use crate::back::write::to_llvm_code_model;
3536
use crate::callee::get_fn;
3637
use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
@@ -740,7 +741,7 @@ impl<'ll> SimpleCx<'ll> {
740741
llcx: &'ll llvm::Context,
741742
pointer_size: Size,
742743
) -> Self {
743-
let isize_ty = llvm::Type::ix_llcx(llcx, pointer_size.bits());
744+
let isize_ty = llvm::LLVMIntTypeInContext(llcx, pointer_size.bits() as c_uint);
744745
Self(SCx { llmod, llcx, isize_ty }, PhantomData)
745746
}
746747
}
@@ -901,10 +902,7 @@ impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
901902
if self.get_declared_value(entry_name).is_none() {
902903
let llfn = self.declare_entry_fn(
903904
entry_name,
904-
llvm::CallConv::from_conv(
905-
self.sess().target.entry_abi,
906-
self.sess().target.arch.borrow(),
907-
),
905+
to_llvm_calling_convention(self.sess(), self.sess().target.entry_abi),
908906
llvm::UnnamedAddr::Global,
909907
fn_type,
910908
);

compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use rustc_middle::mir::coverage::{CounterId, CovTerm, ExpressionId};
2-
31
/// Must match the layout of `LLVMRustCounterKind`.
42
#[derive(Copy, Clone, Debug)]
53
#[repr(C)]
@@ -26,30 +24,12 @@ pub(crate) enum CounterKind {
2624
pub(crate) struct Counter {
2725
// Important: The layout (order and types of fields) must match its C++ counterpart.
2826
pub(crate) kind: CounterKind,
29-
id: u32,
27+
pub(crate) id: u32,
3028
}
3129

3230
impl Counter {
3331
/// A `Counter` of kind `Zero`. For this counter kind, the `id` is not used.
3432
pub(crate) const ZERO: Self = Self { kind: CounterKind::Zero, id: 0 };
35-
36-
/// Constructs a new `Counter` of kind `CounterValueReference`.
37-
pub(crate) fn counter_value_reference(counter_id: CounterId) -> Self {
38-
Self { kind: CounterKind::CounterValueReference, id: counter_id.as_u32() }
39-
}
40-
41-
/// Constructs a new `Counter` of kind `Expression`.
42-
pub(crate) fn expression(expression_id: ExpressionId) -> Self {
43-
Self { kind: CounterKind::Expression, id: expression_id.as_u32() }
44-
}
45-
46-
pub(crate) fn from_term(term: CovTerm) -> Self {
47-
match term {
48-
CovTerm::Zero => Self::ZERO,
49-
CovTerm::Counter(id) => Self::counter_value_reference(id),
50-
CovTerm::Expression(id) => Self::expression(id),
51-
}
52-
}
5333
}
5434

5535
/// Corresponds to enum `llvm::coverage::CounterExpression::ExprKind`.
@@ -94,29 +74,6 @@ pub(crate) struct CoverageSpan {
9474
pub(crate) end_col: u32,
9575
}
9676

97-
/// Holds tables of the various region types in one struct.
98-
///
99-
/// Don't pass this struct across FFI; pass the individual region tables as
100-
/// pointer/length pairs instead.
101-
///
102-
/// Each field name has a `_regions` suffix for improved readability after
103-
/// exhaustive destructing, which ensures that all region types are handled.
104-
#[derive(Clone, Debug, Default)]
105-
pub(crate) struct Regions {
106-
pub(crate) code_regions: Vec<CodeRegion>,
107-
pub(crate) expansion_regions: Vec<ExpansionRegion>,
108-
pub(crate) branch_regions: Vec<BranchRegion>,
109-
}
110-
111-
impl Regions {
112-
/// Returns true if none of this structure's tables contain any regions.
113-
pub(crate) fn has_no_regions(&self) -> bool {
114-
let Self { code_regions, expansion_regions, branch_regions } = self;
115-
116-
code_regions.is_empty() && expansion_regions.is_empty() && branch_regions.is_empty()
117-
}
118-
}
119-
12077
/// Must match the layout of `LLVMRustCoverageCodeRegion`.
12178
#[derive(Clone, Debug)]
12279
#[repr(C)]

compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,35 @@ pub(crate) fn write_filenames_to_buffer(filenames: &[impl AsRef<str>]) -> Vec<u8
5757
})
5858
}
5959

60+
/// Holds tables of the various region types in one struct.
61+
///
62+
/// Don't pass this struct across FFI; pass the individual region tables as
63+
/// pointer/length pairs instead.
64+
///
65+
/// Each field name has a `_regions` suffix for improved readability after
66+
/// exhaustive destructing, which ensures that all region types are handled.
67+
#[derive(Clone, Debug, Default)]
68+
pub(crate) struct Regions {
69+
pub(crate) code_regions: Vec<ffi::CodeRegion>,
70+
pub(crate) expansion_regions: Vec<ffi::ExpansionRegion>,
71+
pub(crate) branch_regions: Vec<ffi::BranchRegion>,
72+
}
73+
74+
impl Regions {
75+
/// Returns true if none of this structure's tables contain any regions.
76+
pub(crate) fn has_no_regions(&self) -> bool {
77+
let Self { code_regions, expansion_regions, branch_regions } = self;
78+
79+
code_regions.is_empty() && expansion_regions.is_empty() && branch_regions.is_empty()
80+
}
81+
}
82+
6083
pub(crate) fn write_function_mappings_to_buffer(
6184
virtual_file_mapping: &[u32],
6285
expressions: &[ffi::CounterExpression],
63-
regions: &ffi::Regions,
86+
regions: &Regions,
6487
) -> Vec<u8> {
65-
let ffi::Regions { code_regions, expansion_regions, branch_regions } = regions;
88+
let Regions { code_regions, expansion_regions, branch_regions } = regions;
6689

6790
// SAFETY:
6891
// - All types are FFI-compatible and have matching representations in Rust/C++.

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::sync::Arc;
1010
use rustc_abi::Align;
1111
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods as _, ConstCodegenMethods};
1212
use rustc_middle::mir::coverage::{
13-
BasicCoverageBlock, CovTerm, CoverageIdsInfo, Expression, FunctionCoverageInfo, Mapping,
14-
MappingKind, Op,
13+
BasicCoverageBlock, CounterId, CovTerm, CoverageIdsInfo, Expression, ExpressionId,
14+
FunctionCoverageInfo, Mapping, MappingKind, Op,
1515
};
1616
use rustc_middle::ty::{Instance, TyCtxt};
1717
use rustc_span::{SourceFile, Span};
@@ -36,7 +36,7 @@ pub(crate) struct CovfunRecord<'tcx> {
3636

3737
virtual_file_mapping: VirtualFileMapping,
3838
expressions: Vec<ffi::CounterExpression>,
39-
regions: ffi::Regions,
39+
regions: llvm_cov::Regions,
4040
}
4141

4242
impl<'tcx> CovfunRecord<'tcx> {
@@ -64,7 +64,7 @@ pub(crate) fn prepare_covfun_record<'tcx>(
6464
is_used,
6565
virtual_file_mapping: VirtualFileMapping::default(),
6666
expressions,
67-
regions: ffi::Regions::default(),
67+
regions: llvm_cov::Regions::default(),
6868
};
6969

7070
fill_region_tables(tcx, fn_cov_info, ids_info, &mut covfun);
@@ -77,10 +77,21 @@ pub(crate) fn prepare_covfun_record<'tcx>(
7777
Some(covfun)
7878
}
7979

80+
pub(crate) fn counter_for_term(term: CovTerm) -> ffi::Counter {
81+
use ffi::Counter;
82+
match term {
83+
CovTerm::Zero => Counter::ZERO,
84+
CovTerm::Counter(id) => {
85+
Counter { kind: ffi::CounterKind::CounterValueReference, id: CounterId::as_u32(id) }
86+
}
87+
CovTerm::Expression(id) => {
88+
Counter { kind: ffi::CounterKind::Expression, id: ExpressionId::as_u32(id) }
89+
}
90+
}
91+
}
92+
8093
/// Convert the function's coverage-counter expressions into a form suitable for FFI.
8194
fn prepare_expressions(ids_info: &CoverageIdsInfo) -> Vec<ffi::CounterExpression> {
82-
let counter_for_term = ffi::Counter::from_term;
83-
8495
// We know that LLVM will optimize out any unused expressions before
8596
// producing the final coverage map, so there's no need to do the same
8697
// thing on the Rust side unless we're confident we can do much better.
@@ -113,7 +124,7 @@ fn fill_region_tables<'tcx>(
113124
} else {
114125
CovTerm::Zero
115126
};
116-
ffi::Counter::from_term(term)
127+
counter_for_term(term)
117128
};
118129

119130
// Currently a function's mappings must all be in the same file, so use the
@@ -136,7 +147,7 @@ fn fill_region_tables<'tcx>(
136147
if discard_all { None } else { spans::make_coords(source_map, &source_file, span) }
137148
};
138149

139-
let ffi::Regions {
150+
let llvm_cov::Regions {
140151
code_regions,
141152
expansion_regions: _, // FIXME(Zalathar): Fill out support for expansion regions
142153
branch_regions,

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ use crate::common::{AsCCharPtr, CodegenCx};
3737
use crate::debuginfo::dwarf_const;
3838
use crate::debuginfo::metadata::type_map::build_type_with_children;
3939
use crate::debuginfo::utils::{WidePtrKind, wide_pointer_kind};
40-
use crate::llvm;
4140
use crate::llvm::debuginfo::{
4241
DIBasicType, DIBuilder, DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock,
4342
DIScope, DIType, DebugEmissionKind, DebugNameTableKind,
4443
};
44+
use crate::llvm::{self, FromGeneric};
4545
use crate::value::Value;
4646

4747
impl PartialEq for llvm::Metadata {

compiler/rustc_codegen_llvm/src/declare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::abi::FnAbiLlvmExt;
2626
use crate::common::AsCCharPtr;
2727
use crate::context::{CodegenCx, GenericCx, SCx, SimpleCx};
2828
use crate::llvm::AttributePlace::Function;
29-
use crate::llvm::Visibility;
29+
use crate::llvm::{FromGeneric, Visibility};
3030
use crate::type_::Type;
3131
use crate::value::Value;
3232
use crate::{attributes, llvm};

0 commit comments

Comments
 (0)