Skip to content

Move all LLVM externs into the rustc_llvm crate #142897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3521,7 +3521,6 @@ dependencies = [
name = "rustc_codegen_llvm"
version = "0.0.0"
dependencies = [
"bitflags",
"gimli 0.31.1",
"itertools",
"libc",
Expand Down Expand Up @@ -4101,8 +4100,10 @@ dependencies = [
name = "rustc_llvm"
version = "0.0.0"
dependencies = [
"bitflags",
"cc",
"libc",
"rustc_macros",
]

[[package]]
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ test = false

[dependencies]
# tidy-alphabetical-start
bitflags = "2.4.1"
# To avoid duplicate dependencies, this should match the version of gimli used
# by `rustc_codegen_ssa` via its `thorin-dwp` dependency.
gimli = "0.31"
Expand Down Expand Up @@ -38,7 +37,7 @@ rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
rustc_target = { path = "../rustc_target" }
serde = { version = "1", features = [ "derive" ]}
serde = { version = "1", features = ["derive"] }
serde_json = "1"
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
tracing = "0.1"
Expand Down
11 changes: 7 additions & 4 deletions compiler/rustc_codegen_llvm/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ use smallvec::SmallVec;
use crate::attributes::{self, llfn_attrs_from_instance};
use crate::builder::Builder;
use crate::context::CodegenCx;
use crate::llvm::{self, Attribute, AttributePlace};
use crate::llvm::{self, Attribute, AttributePlace, Value};
use crate::type_::Type;
use crate::type_of::LayoutLlvmExt;
use crate::value::Value;

trait ArgAttributesExt {
fn apply_attrs_to_llfn(&self, idx: AttributePlace, cx: &CodegenCx<'_, '_>, llfn: &Value);
Expand Down Expand Up @@ -644,8 +643,12 @@ impl AbiBuilderMethods for Builder<'_, '_, '_> {
}
}

impl llvm::CallConv {
pub(crate) fn from_conv(conv: CanonAbi, arch: &str) -> Self {
pub(crate) trait CallConvExt {
fn from_conv(conv: CanonAbi, arch: &str) -> llvm::CallConv;
}

impl CallConvExt for llvm::CallConv {
fn from_conv(conv: CanonAbi, arch: &str) -> Self {
match conv {
CanonAbi::C | CanonAbi::Rust => llvm::CCallConv,
CanonAbi::RustCold => llvm::PreserveMost,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_llvm/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn create_const_value_function(
name,
llvm::CallConv::CCallConv,
llvm::UnnamedAddr::Global,
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
llvm::visibility_from_generic(tcx.sess.default_visibility()),
ty,
);

Expand Down Expand Up @@ -144,7 +144,7 @@ fn create_wrapper_function(
from_name,
llvm::CallConv::CCallConv,
llvm::UnnamedAddr::Global,
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
llvm::visibility_from_generic(tcx.sess.default_visibility()),
ty,
);
let no_return = if no_return {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use tracing::debug;
use crate::builder::Builder;
use crate::common::Funclet;
use crate::context::CodegenCx;
use crate::llvm::Value;
use crate::type_::Type;
use crate::type_of::LayoutLlvmExt;
use crate::value::Value;
use crate::{attributes, llvm};

impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use smallvec::SmallVec;
use crate::context::CodegenCx;
use crate::errors::SanitizerMemtagRequiresMte;
use crate::llvm::AttributePlace::Function;
use crate::llvm::{self, AllocKindFlags, Attribute, AttributeKind, AttributePlace, MemoryEffects};
use crate::value::Value;
use crate::llvm::{
self, AllocKindFlags, Attribute, AttributeKind, AttributePlace, MemoryEffects, Value,
};
use crate::{attributes, llvm_util};

pub(crate) fn apply_to_llfn(llfn: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_codegen_llvm/src/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_codegen_ssa::back::archive::{
use rustc_session::Session;

use crate::llvm::archive_ro::{ArchiveRO, Child};
use crate::llvm::{self, ArchiveKind, last_error};
use crate::llvm::{self, last_error, parse_archive_kind};

/// Helper for adding many files to an archive.
#[must_use = "must call build() to finish building the archive"]
Expand Down Expand Up @@ -182,8 +182,7 @@ fn llvm_is_ec_object_file(buf: &[u8]) -> bool {
impl<'a> LlvmArchiveBuilder<'a> {
fn build_with_llvm(&mut self, output: &Path) -> io::Result<bool> {
let kind = &*self.sess.target.archive_format;
let kind = kind
.parse::<ArchiveKind>()
let kind = parse_archive_kind(kind)
.map_err(|_| kind)
.unwrap_or_else(|kind| self.sess.dcx().emit_fatal(UnknownArchiveKind { kind }));

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::errors::{
};
use crate::llvm::diagnostic::OptimizationDiagnosticKind::*;
use crate::llvm::{self, DiagnosticInfo};
use crate::type_::Type;
use crate::type_::type_ptr_llcx;
use crate::{LlvmCodegenBackend, ModuleLlvm, base, common, llvm_util};

pub(crate) fn llvm_err<'a>(dcx: DiagCtxtHandle<'_>, err: LlvmError<'a>) -> FatalError {
Expand Down Expand Up @@ -1174,7 +1174,7 @@ fn create_msvc_imps(
// underscores added in front).
let prefix = if cgcx.target_arch == "x86" { "\x01__imp__" } else { "\x01__imp_" };

let ptr_ty = Type::ptr_llcx(llcx);
let ptr_ty = type_ptr_llcx(llcx);
let globals = base::iter_globals(llmod)
.filter(|&val| {
llvm::get_linkage(val) == llvm::Linkage::ExternalLinkage && !llvm::is_declaration(val)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use rustc_target::spec::SanitizerSet;
use super::ModuleLlvm;
use crate::builder::Builder;
use crate::context::CodegenCx;
use crate::value::Value;
use crate::llvm::Value;
use crate::{attributes, llvm};

pub(crate) struct ValueIter<'ll> {
Expand Down
23 changes: 10 additions & 13 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ use crate::abi::FnAbiLlvmExt;
use crate::attributes;
use crate::common::Funclet;
use crate::context::{CodegenCx, FullCx, GenericCx, SCx};
use crate::llvm::{
self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, GEPNoWrapFlags, Metadata, True,
};
use crate::llvm::{self, BasicBlock, False, GEPNoWrapFlags, Metadata, True, Value};
use crate::type_::Type;
use crate::type_of::LayoutLlvmExt;
use crate::value::Value;

#[must_use]
pub(crate) struct GenericBuilder<'a, 'll, CX: Borrow<SCx<'ll>>> {
Expand Down Expand Up @@ -568,7 +565,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
ty,
ptr,
UNNAMED,
AtomicOrdering::from_generic(order),
llvm::atomic_ordering_from_generic(order),
);
// LLVM requires the alignment of atomic loads to be at least the size of the type.
llvm::LLVMSetAlignment(load, size.bytes() as c_uint);
Expand Down Expand Up @@ -808,7 +805,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
self.llbuilder,
val,
ptr,
AtomicOrdering::from_generic(order),
llvm::atomic_ordering_from_generic(order),
);
// LLVM requires the alignment of atomic stores to be at least the size of the type.
llvm::LLVMSetAlignment(store, size.bytes() as c_uint);
Expand Down Expand Up @@ -1005,12 +1002,12 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {

/* Comparisons */
fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
let op = llvm::IntPredicate::from_generic(op);
let op = llvm::int_predicate_from_generic(op);
unsafe { llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
}

fn fcmp(&mut self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
let op = llvm::RealPredicate::from_generic(op);
let op = llvm::real_predicate_from_generic(op);
unsafe { llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
}

Expand Down Expand Up @@ -1246,8 +1243,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
dst,
cmp,
src,
AtomicOrdering::from_generic(order),
AtomicOrdering::from_generic(failure_order),
llvm::atomic_ordering_from_generic(order),
llvm::atomic_ordering_from_generic(failure_order),
llvm::False, // SingleThreaded
);
llvm::LLVMSetWeak(value, weak);
Expand All @@ -1273,10 +1270,10 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
let mut res = unsafe {
llvm::LLVMBuildAtomicRMW(
self.llbuilder,
AtomicRmwBinOp::from_generic(op),
llvm::atomic_rmw_bin_op_from_generic(op),
dst,
src,
AtomicOrdering::from_generic(order),
llvm::atomic_ordering_from_generic(order),
llvm::False, // SingleThreaded
)
};
Expand All @@ -1298,7 +1295,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
unsafe {
llvm::LLVMBuildFence(
self.llbuilder,
AtomicOrdering::from_generic(order),
llvm::atomic_ordering_from_generic(order),
single_threaded,
UNNAMED,
);
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/src/builder/autodiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use crate::context::SimpleCx;
use crate::declare::declare_simple_fn;
use crate::errors::{AutoDiffWithoutEnable, LlvmError};
use crate::llvm::AttributePlace::Function;
use crate::llvm::{Metadata, True};
use crate::value::Value;
use crate::llvm::{Metadata, True, Value};
use crate::{CodegenContext, LlvmCodegenBackend, ModuleLlvm, attributes, llvm};

fn get_params(fnc: &Value) -> Vec<&Value> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tracing::debug;

use crate::context::CodegenCx;
use crate::llvm;
use crate::value::Value;
use crate::llvm::Value;

/// Codegens a reference to a fn/method item, monomorphizing and
/// inlining as it goes.
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ use tracing::debug;
use crate::consts::const_alloc_to_llvm;
pub(crate) use crate::context::CodegenCx;
use crate::context::{GenericCx, SCx};
use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, Metadata, True};
use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, Metadata, True, Value};
use crate::type_::Type;
use crate::value::Value;

/*
* A note on nomenclature of linking: "extern", "foreign", and "upcall".
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ use tracing::{debug, instrument, trace};

use crate::common::{AsCCharPtr, CodegenCx};
use crate::errors::SymbolAlreadyDefined;
use crate::llvm::{self, True};
use crate::llvm::{self, True, Value};
use crate::type_::Type;
use crate::type_of::LayoutLlvmExt;
use crate::value::Value;
use crate::{base, debuginfo};

pub(crate) fn const_alloc_to_llvm<'ll>(
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ use rustc_symbol_mangling::mangle_internal_symbol;
use rustc_target::spec::{HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel};
use smallvec::SmallVec;

use crate::abi::CallConvExt as _;
use crate::back::write::to_llvm_code_model;
use crate::callee::get_fn;
use crate::common::AsCCharPtr;
use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
use crate::llvm::Metadata;
use crate::type_::Type;
use crate::value::Value;
use crate::llvm::{Metadata, Value};
use crate::type_::{Type, type_ix_llcx};
use crate::{attributes, common, coverageinfo, debuginfo, llvm, llvm_util};

/// `TyCtxt` (and related cache datastructures) can't be move between threads.
Expand Down Expand Up @@ -669,7 +669,7 @@ impl<'ll> SimpleCx<'ll> {
llcx: &'ll llvm::Context,
pointer_size: Size,
) -> Self {
let isize_ty = llvm::Type::ix_llcx(llcx, pointer_size.bits());
let isize_ty = type_ix_llcx(llcx, pointer_size.bits());
Self(SCx { llmod, llcx, isize_ty }, PhantomData)
}
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

use std::ffi::CString;

use rustc_llvm::ffi;

use crate::common::AsCCharPtr;
use crate::coverageinfo::ffi;
use crate::llvm;

pub(crate) fn covmap_var_name() -> CString {
Expand Down
17 changes: 9 additions & 8 deletions compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::sync::Arc;

use rustc_abi::Align;
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods as _, ConstCodegenMethods};
use rustc_llvm::ffi;
use rustc_middle::mir::coverage::{
BasicCoverageBlock, CovTerm, CoverageIdsInfo, Expression, FunctionCoverageInfo, Mapping,
MappingKind, Op,
Expand All @@ -19,8 +20,8 @@ use rustc_target::spec::HasTargetSpec;
use tracing::debug;

use crate::common::CodegenCx;
use crate::coverageinfo::llvm_cov;
use crate::coverageinfo::mapgen::{GlobalFileTable, VirtualFileMapping, spans};
use crate::coverageinfo::{ffi, llvm_cov};
use crate::llvm;

/// Intermediate coverage metadata for a single function, used to help build
Expand Down Expand Up @@ -75,8 +76,6 @@ pub(crate) fn prepare_covfun_record<'tcx>(

/// Convert the function's coverage-counter expressions into a form suitable for FFI.
fn prepare_expressions(ids_info: &CoverageIdsInfo) -> Vec<ffi::CounterExpression> {
let counter_for_term = ffi::Counter::from_term;

// We know that LLVM will optimize out any unused expressions before
// producing the final coverage map, so there's no need to do the same
// thing on the Rust side unless we're confident we can do much better.
Expand All @@ -85,12 +84,12 @@ fn prepare_expressions(ids_info: &CoverageIdsInfo) -> Vec<ffi::CounterExpression
.expressions
.iter()
.map(move |&Expression { lhs, op, rhs }| ffi::CounterExpression {
lhs: counter_for_term(lhs),
lhs: llvm::counter_from_term(lhs),
kind: match op {
Op::Add => ffi::ExprKind::Add,
Op::Subtract => ffi::ExprKind::Subtract,
},
rhs: counter_for_term(rhs),
rhs: llvm::counter_from_term(rhs),
})
.collect::<Vec<_>>()
}
Expand Down Expand Up @@ -140,7 +139,7 @@ fn fill_region_tables<'tcx>(
} else {
CovTerm::Zero
};
ffi::Counter::from_term(term)
llvm::counter_from_term(term)
};

let Some(coords) = make_coords(span) else { continue };
Expand All @@ -162,13 +161,15 @@ fn fill_region_tables<'tcx>(
cov_span,
true_counter: counter_for_bcb(true_bcb),
false_counter: counter_for_bcb(false_bcb),
mcdc_branch_params: ffi::mcdc::BranchParameters::from(mcdc_params),
mcdc_branch_params: llvm::branch_parameters_from_condition_info(mcdc_params),
});
}
MappingKind::MCDCDecision(mcdc_decision_params) => {
mcdc_decision_regions.push(ffi::MCDCDecisionRegion {
cov_span,
mcdc_decision_params: ffi::mcdc::DecisionParameters::from(mcdc_decision_params),
mcdc_decision_params: llvm::decision_parameters_from_decision_info(
mcdc_decision_params,
),
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rustc_llvm::ffi;
use rustc_span::source_map::SourceMap;
use rustc_span::{BytePos, Pos, SourceFile, Span};
use tracing::debug;

use crate::coverageinfo::ffi;
use crate::coverageinfo::mapgen::LocalFileId;

/// Line and byte-column coordinates of a source code span within some file.
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::builder::Builder;
use crate::common::CodegenCx;
use crate::llvm;

pub(crate) mod ffi;
mod llvm_cov;
mod mapgen;

Expand Down
Loading
Loading