Skip to content

Commit e865bbb

Browse files
committed
Move all LLVM externs into the rustc_llvm crate in preparation for ctest
1 parent 111e9bc commit e865bbb

39 files changed

+1060
-1237
lines changed

Cargo.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3434,7 +3434,6 @@ dependencies = [
34343434
name = "rustc_codegen_llvm"
34353435
version = "0.0.0"
34363436
dependencies = [
3437-
"bitflags",
34383437
"gimli",
34393438
"itertools",
34403439
"libc",
@@ -4014,8 +4013,14 @@ dependencies = [
40144013
name = "rustc_llvm"
40154014
version = "0.0.0"
40164015
dependencies = [
4016+
"bitflags",
40174017
"cc",
40184018
"libc",
4019+
"rustc_codegen_ssa",
4020+
"rustc_macros",
4021+
"rustc_middle",
4022+
"rustc_session",
4023+
"rustc_target",
40194024
]
40204025

40214026
[[package]]

compiler/rustc_codegen_llvm/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ test = false
88

99
[dependencies]
1010
# tidy-alphabetical-start
11-
bitflags = "2.4.1"
1211
# To avoid duplicate dependencies, this should match the version of gimli used
1312
# by `rustc_codegen_ssa` via its `thorin-dwp` dependency.
1413
gimli = "0.31"
@@ -38,7 +37,7 @@ rustc_session = { path = "../rustc_session" }
3837
rustc_span = { path = "../rustc_span" }
3938
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
4039
rustc_target = { path = "../rustc_target" }
41-
serde = { version = "1", features = [ "derive" ]}
40+
serde = { version = "1", features = ["derive"] }
4241
serde_json = "1"
4342
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
4443
tracing = "0.1"

compiler/rustc_codegen_llvm/src/abi.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ use smallvec::SmallVec;
2323
use crate::attributes::{self, llfn_attrs_from_instance};
2424
use crate::builder::Builder;
2525
use crate::context::CodegenCx;
26-
use crate::llvm::{self, Attribute, AttributePlace};
26+
use crate::llvm::{self, Attribute, AttributeExt as _, AttributePlace, Value};
2727
use crate::type_::Type;
2828
use crate::type_of::LayoutLlvmExt;
29-
use crate::value::Value;
3029

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

647-
impl llvm::CallConv {
648-
pub(crate) fn from_conv(conv: CanonAbi, arch: &str) -> Self {
646+
pub(crate) trait CallConvExt {
647+
fn from_conv(conv: CanonAbi, arch: &str) -> llvm::CallConv;
648+
}
649+
650+
impl CallConvExt for llvm::CallConv {
651+
fn from_conv(conv: CanonAbi, arch: &str) -> Self {
649652
match conv {
650653
CanonAbi::C | CanonAbi::Rust => llvm::CCallConv,
651654
CanonAbi::RustCold => llvm::PreserveMost,

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_symbol_mangling::mangle_internal_symbol;
1111

1212
use crate::builder::SBuilder;
1313
use crate::declare::declare_simple_fn;
14-
use crate::llvm::{self, False, True, Type};
14+
use crate::llvm::{self, AttributeExt as _, False, True, Type};
1515
use crate::{SimpleCx, attributes, debuginfo};
1616

1717
pub(crate) unsafe fn codegen(

compiler/rustc_codegen_llvm/src/asm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use tracing::debug;
1616
use crate::builder::Builder;
1717
use crate::common::Funclet;
1818
use crate::context::CodegenCx;
19+
use crate::llvm::{AttributeExt as _, Value};
1920
use crate::type_::Type;
2021
use crate::type_of::LayoutLlvmExt;
21-
use crate::value::Value;
2222
use crate::{attributes, llvm};
2323

2424
impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ use smallvec::SmallVec;
1212
use crate::context::CodegenCx;
1313
use crate::errors::SanitizerMemtagRequiresMte;
1414
use crate::llvm::AttributePlace::Function;
15-
use crate::llvm::{self, AllocKindFlags, Attribute, AttributeKind, AttributePlace, MemoryEffects};
16-
use crate::value::Value;
15+
use crate::llvm::{
16+
self, AllocKindFlags, Attribute, AttributeExt as _, AttributeKind, AttributePlace,
17+
MemoryEffects, Value,
18+
};
1719
use crate::{attributes, llvm_util};
1820

1921
pub(crate) fn apply_to_llfn(llfn: &Value, idx: AttributePlace, attrs: &[&Attribute]) {

compiler/rustc_codegen_llvm/src/back/archive.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_codegen_ssa::back::archive::{
1111
use rustc_session::Session;
1212

1313
use crate::llvm::archive_ro::{ArchiveRO, Child};
14-
use crate::llvm::{self, ArchiveKind, last_error};
14+
use crate::llvm::{self, LLVMRustResultExt as _, last_error, parse_archive_kind};
1515

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

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::errors::{
2929
DynamicLinkingWithLTO, LlvmError, LtoBitcodeFromRlib, LtoDisallowed, LtoDylib, LtoProcMacro,
3030
};
3131
use crate::llvm::AttributePlace::Function;
32-
use crate::llvm::{self, build_string};
32+
use crate::llvm::{self, AttributeExt as _, build_string};
3333
use crate::{LlvmCodegenBackend, ModuleLlvm, SimpleCx, attributes};
3434

3535
/// We keep track of the computed LTO cache keys from the previous

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ use crate::errors::{
4141
WithLlvmError, WriteBytecode,
4242
};
4343
use crate::llvm::diagnostic::OptimizationDiagnosticKind::*;
44-
use crate::llvm::{self, DiagnosticInfo};
45-
use crate::type_::Type;
44+
use crate::llvm::{self, DiagnosticInfo, LLVMRustResultExt as _};
45+
use crate::type_::type_ptr_llcx;
4646
use crate::{LlvmCodegenBackend, ModuleLlvm, base, common, llvm_util};
4747

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

1177-
let ptr_ty = Type::ptr_llcx(llcx);
1177+
let ptr_ty = type_ptr_llcx(llcx);
11781178
let globals = base::iter_globals(llmod)
11791179
.filter(|&val| {
11801180
llvm::get_linkage(val) == llvm::Linkage::ExternalLinkage && !llvm::is_declaration(val)

compiler/rustc_codegen_llvm/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_target::spec::SanitizerSet;
2929
use super::ModuleLlvm;
3030
use crate::builder::Builder;
3131
use crate::context::CodegenCx;
32-
use crate::value::Value;
32+
use crate::llvm::Value;
3333
use crate::{attributes, llvm};
3434

3535
pub(crate) struct ValueIter<'ll> {

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ use crate::attributes;
3333
use crate::common::Funclet;
3434
use crate::context::{CodegenCx, FullCx, GenericCx, SCx};
3535
use crate::llvm::{
36-
self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, GEPNoWrapFlags, Metadata, True,
36+
self, AtomicOrdering, AtomicRmwBinOp, AttributeExt as _, BasicBlock, False, GEPNoWrapFlags,
37+
Metadata, True, Value,
3738
};
3839
use crate::type_::Type;
3940
use crate::type_of::LayoutLlvmExt;
40-
use crate::value::Value;
4141

4242
#[must_use]
4343
pub(crate) struct GenericBuilder<'a, 'll, CX: Borrow<SCx<'ll>>> {

compiler/rustc_codegen_llvm/src/builder/autodiff.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use crate::context::SimpleCx;
1515
use crate::declare::declare_simple_fn;
1616
use crate::errors::{AutoDiffWithoutEnable, LlvmError};
1717
use crate::llvm::AttributePlace::Function;
18-
use crate::llvm::{Metadata, True};
19-
use crate::value::Value;
18+
use crate::llvm::{AttributeExt as _, Metadata, True, Value};
2019
use crate::{CodegenContext, LlvmCodegenBackend, ModuleLlvm, attributes, llvm};
2120

2221
fn get_params(fnc: &Value) -> Vec<&Value> {

compiler/rustc_codegen_llvm/src/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use tracing::debug;
1111

1212
use crate::context::CodegenCx;
1313
use crate::llvm;
14-
use crate::value::Value;
14+
use crate::llvm::Value;
1515

1616
/// Codegens a reference to a fn/method item, monomorphizing and
1717
/// inlining as it goes.

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ use tracing::debug;
2121
use crate::consts::const_alloc_to_llvm;
2222
pub(crate) use crate::context::CodegenCx;
2323
use crate::context::{GenericCx, SCx};
24-
use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, Metadata, True};
24+
use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, Metadata, True, Value};
2525
use crate::type_::Type;
26-
use crate::value::Value;
2726

2827
/*
2928
* A note on nomenclature of linking: "extern", "foreign", and "upcall".

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ use tracing::{debug, instrument, trace};
1919

2020
use crate::common::{AsCCharPtr, CodegenCx};
2121
use crate::errors::SymbolAlreadyDefined;
22-
use crate::llvm::{self, True};
22+
use crate::llvm::{self, True, Value};
2323
use crate::type_::Type;
2424
use crate::type_of::LayoutLlvmExt;
25-
use crate::value::Value;
2625
use crate::{base, debuginfo};
2726

2827
pub(crate) fn const_alloc_to_llvm<'ll>(

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ use rustc_symbol_mangling::mangle_internal_symbol;
3232
use rustc_target::spec::{HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel};
3333
use smallvec::SmallVec;
3434

35+
use crate::abi::CallConvExt as _;
3536
use crate::back::write::to_llvm_code_model;
3637
use crate::callee::get_fn;
3738
use crate::common::AsCCharPtr;
3839
use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
39-
use crate::llvm::Metadata;
40-
use crate::type_::Type;
41-
use crate::value::Value;
40+
use crate::llvm::{Metadata, Value};
41+
use crate::type_::{Type, type_ix_llcx};
4242
use crate::{attributes, common, coverageinfo, debuginfo, llvm, llvm_util};
4343

4444
/// `TyCtxt` (and related cache datastructures) can't be move between threads.
@@ -669,7 +669,7 @@ impl<'ll> SimpleCx<'ll> {
669669
llcx: &'ll llvm::Context,
670670
pointer_size: Size,
671671
) -> Self {
672-
let isize_ty = llvm::Type::ix_llcx(llcx, pointer_size.bits());
672+
let isize_ty = type_ix_llcx(llcx, pointer_size.bits());
673673
Self(SCx { llmod, llcx, isize_ty }, PhantomData)
674674
}
675675
}

compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
33
use std::ffi::CString;
44

5+
use rustc_llvm::ffi;
6+
57
use crate::common::AsCCharPtr;
6-
use crate::coverageinfo::ffi;
78
use crate::llvm;
89

910
pub(crate) fn covmap_var_name() -> CString {
@@ -60,10 +61,10 @@ pub(crate) fn write_filenames_to_buffer(filenames: &[impl AsRef<str>]) -> Vec<u8
6061

6162
pub(crate) fn write_function_mappings_to_buffer(
6263
virtual_file_mapping: &[u32],
63-
expressions: &[ffi::CounterExpression],
64-
regions: &ffi::Regions,
64+
expressions: &[ffi::coverageinfo::CounterExpression],
65+
regions: &ffi::coverageinfo::Regions,
6566
) -> Vec<u8> {
66-
let ffi::Regions {
67+
let ffi::coverageinfo::Regions {
6768
code_regions,
6869
expansion_regions,
6970
branch_regions,

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::sync::Arc;
99

1010
use rustc_abi::Align;
1111
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods as _, ConstCodegenMethods};
12+
use rustc_llvm::ffi;
1213
use rustc_middle::mir::coverage::{
1314
BasicCoverageBlock, CovTerm, CoverageIdsInfo, Expression, FunctionCoverageInfo, Mapping,
1415
MappingKind, Op,
@@ -19,8 +20,8 @@ use rustc_target::spec::HasTargetSpec;
1920
use tracing::debug;
2021

2122
use crate::common::CodegenCx;
23+
use crate::coverageinfo::llvm_cov;
2224
use crate::coverageinfo::mapgen::{GlobalFileTable, VirtualFileMapping, spans};
23-
use crate::coverageinfo::{ffi, llvm_cov};
2425
use crate::llvm;
2526

2627
/// Intermediate coverage metadata for a single function, used to help build
@@ -32,8 +33,8 @@ pub(crate) struct CovfunRecord<'tcx> {
3233
is_used: bool,
3334

3435
virtual_file_mapping: VirtualFileMapping,
35-
expressions: Vec<ffi::CounterExpression>,
36-
regions: ffi::Regions,
36+
expressions: Vec<ffi::coverageinfo::CounterExpression>,
37+
regions: ffi::coverageinfo::Regions,
3738
}
3839

3940
impl<'tcx> CovfunRecord<'tcx> {
@@ -60,7 +61,7 @@ pub(crate) fn prepare_covfun_record<'tcx>(
6061
is_used,
6162
virtual_file_mapping: VirtualFileMapping::default(),
6263
expressions,
63-
regions: ffi::Regions::default(),
64+
regions: ffi::coverageinfo::Regions::default(),
6465
};
6566

6667
fill_region_tables(tcx, fn_cov_info, ids_info, &mut covfun);
@@ -74,8 +75,8 @@ pub(crate) fn prepare_covfun_record<'tcx>(
7475
}
7576

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

8081
// We know that LLVM will optimize out any unused expressions before
8182
// producing the final coverage map, so there's no need to do the same
@@ -84,11 +85,11 @@ fn prepare_expressions(ids_info: &CoverageIdsInfo) -> Vec<ffi::CounterExpression
8485
ids_info
8586
.expressions
8687
.iter()
87-
.map(move |&Expression { lhs, op, rhs }| ffi::CounterExpression {
88+
.map(move |&Expression { lhs, op, rhs }| ffi::coverageinfo::CounterExpression {
8889
lhs: counter_for_term(lhs),
8990
kind: match op {
90-
Op::Add => ffi::ExprKind::Add,
91-
Op::Subtract => ffi::ExprKind::Subtract,
91+
Op::Add => ffi::coverageinfo::ExprKind::Add,
92+
Op::Subtract => ffi::coverageinfo::ExprKind::Subtract,
9293
},
9394
rhs: counter_for_term(rhs),
9495
})
@@ -122,7 +123,7 @@ fn fill_region_tables<'tcx>(
122123
if discard_all { None } else { spans::make_coords(source_map, &source_file, span) }
123124
};
124125

125-
let ffi::Regions {
126+
let ffi::coverageinfo::Regions {
126127
code_regions,
127128
expansion_regions: _, // FIXME(Zalathar): Fill out support for expansion regions
128129
branch_regions,
@@ -134,41 +135,48 @@ fn fill_region_tables<'tcx>(
134135
// form suitable for FFI.
135136
for &Mapping { ref kind, span } in &fn_cov_info.mappings {
136137
// If this function is unused, replace all counters with zero.
137-
let counter_for_bcb = |bcb: BasicCoverageBlock| -> ffi::Counter {
138+
let counter_for_bcb = |bcb: BasicCoverageBlock| -> ffi::coverageinfo::Counter {
138139
let term = if covfun.is_used {
139140
ids_info.term_for_bcb[bcb].expect("every BCB in a mapping was given a term")
140141
} else {
141142
CovTerm::Zero
142143
};
143-
ffi::Counter::from_term(term)
144+
ffi::coverageinfo::Counter::from_term(term)
144145
};
145146

146147
let Some(coords) = make_coords(span) else { continue };
147148
let cov_span = coords.make_coverage_span(local_file_id);
148149

149150
match *kind {
150151
MappingKind::Code { bcb } => {
151-
code_regions.push(ffi::CodeRegion { cov_span, counter: counter_for_bcb(bcb) });
152+
code_regions.push(ffi::coverageinfo::CodeRegion {
153+
cov_span,
154+
counter: counter_for_bcb(bcb),
155+
});
152156
}
153157
MappingKind::Branch { true_bcb, false_bcb } => {
154-
branch_regions.push(ffi::BranchRegion {
158+
branch_regions.push(ffi::coverageinfo::BranchRegion {
155159
cov_span,
156160
true_counter: counter_for_bcb(true_bcb),
157161
false_counter: counter_for_bcb(false_bcb),
158162
});
159163
}
160164
MappingKind::MCDCBranch { true_bcb, false_bcb, mcdc_params } => {
161-
mcdc_branch_regions.push(ffi::MCDCBranchRegion {
165+
mcdc_branch_regions.push(ffi::coverageinfo::MCDCBranchRegion {
162166
cov_span,
163167
true_counter: counter_for_bcb(true_bcb),
164168
false_counter: counter_for_bcb(false_bcb),
165-
mcdc_branch_params: ffi::mcdc::BranchParameters::from(mcdc_params),
169+
mcdc_branch_params: ffi::coverageinfo::mcdc::BranchParameters::from(
170+
mcdc_params,
171+
),
166172
});
167173
}
168174
MappingKind::MCDCDecision(mcdc_decision_params) => {
169-
mcdc_decision_regions.push(ffi::MCDCDecisionRegion {
175+
mcdc_decision_regions.push(ffi::coverageinfo::MCDCDecisionRegion {
170176
cov_span,
171-
mcdc_decision_params: ffi::mcdc::DecisionParameters::from(mcdc_decision_params),
177+
mcdc_decision_params: ffi::coverageinfo::mcdc::DecisionParameters::from(
178+
mcdc_decision_params,
179+
),
172180
});
173181
}
174182
}

0 commit comments

Comments
 (0)