Skip to content

Commit 2fdaee2

Browse files
authored
Merge pull request #4794 from rust-lang/rustup-2025-12-28
Automatic Rustup
2 parents 8742d86 + 97af316 commit 2fdaee2

80 files changed

Lines changed: 1498 additions & 809 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3638,8 +3638,6 @@ dependencies = [
36383638
"rustc_span",
36393639
"rustc_symbol_mangling",
36403640
"rustc_target",
3641-
"serde",
3642-
"serde_json",
36433641
"smallvec",
36443642
"tracing",
36453643
]

compiler/rustc_codegen_gcc/src/builder.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,12 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
314314
self.block.get_function()
315315
}
316316

317-
fn function_call(
317+
pub fn function_call(
318318
&mut self,
319-
func: RValue<'gcc>,
319+
func: Function<'gcc>,
320320
args: &[RValue<'gcc>],
321321
_funclet: Option<&Funclet>,
322322
) -> RValue<'gcc> {
323-
// TODO(antoyo): remove when the API supports a different type for functions.
324-
let func: Function<'gcc> = self.cx.rvalue_as_function(func);
325323
let args = self.check_call("call", func, args);
326324

327325
// gccjit requires to use the result of functions, even when it's not used.
@@ -514,6 +512,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
514512
type CodegenCx = CodegenCx<'gcc, 'tcx>;
515513

516514
fn build(cx: &'a CodegenCx<'gcc, 'tcx>, block: Block<'gcc>) -> Builder<'a, 'gcc, 'tcx> {
515+
*cx.current_func.borrow_mut() = Some(block.get_function());
517516
Builder::with_cx(cx, block)
518517
}
519518

@@ -1765,6 +1764,8 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
17651764
// FIXME(antoyo): remove when having a proper API.
17661765
let gcc_func = unsafe { std::mem::transmute::<RValue<'gcc>, Function<'gcc>>(func) };
17671766
let call = if self.functions.borrow().values().any(|value| *value == gcc_func) {
1767+
// TODO(antoyo): remove when the API supports a different type for functions.
1768+
let func: Function<'gcc> = self.cx.rvalue_as_function(func);
17681769
self.function_call(func, args, funclet)
17691770
} else {
17701771
// If it's a not function that was defined, it's a function pointer.

compiler/rustc_codegen_gcc/src/context.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ pub struct CodegenCx<'gcc, 'tcx> {
9292
pub instances: RefCell<FxHashMap<Instance<'tcx>, LValue<'gcc>>>,
9393
/// Cache function instances of monomorphic and polymorphic items
9494
pub function_instances: RefCell<FxHashMap<Instance<'tcx>, Function<'gcc>>>,
95+
/// Cache function instances of intrinsics
96+
pub intrinsic_instances: RefCell<FxHashMap<Instance<'tcx>, Function<'gcc>>>,
9597
/// Cache generated vtables
9698
pub vtables:
9799
RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), RValue<'gcc>>>,
@@ -280,6 +282,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
280282
linkage: Cell::new(FunctionType::Internal),
281283
instances: Default::default(),
282284
function_instances: Default::default(),
285+
intrinsic_instances: Default::default(),
283286
on_stack_params: Default::default(),
284287
on_stack_function_params: Default::default(),
285288
vtables: Default::default(),
@@ -391,17 +394,13 @@ impl<'gcc, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
391394
}
392395

393396
fn get_fn(&self, instance: Instance<'tcx>) -> Function<'gcc> {
394-
let func = get_fn(self, instance);
395-
*self.current_func.borrow_mut() = Some(func);
396-
func
397+
get_fn(self, instance)
397398
}
398399

399400
fn get_fn_addr(&self, instance: Instance<'tcx>) -> RValue<'gcc> {
400401
let func_name = self.tcx.symbol_name(instance).name;
401402

402-
let func = if self.intrinsics.borrow().contains_key(func_name) {
403-
self.intrinsics.borrow()[func_name]
404-
} else if let Some(variable) = self.get_declared_value(func_name) {
403+
let func = if let Some(variable) = self.get_declared_value(func_name) {
405404
return variable;
406405
} else {
407406
get_fn(self, instance)

compiler/rustc_codegen_gcc/src/declare.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_target::callconv::FnAbi;
88

99
use crate::abi::{FnAbiGcc, FnAbiGccExt};
1010
use crate::context::CodegenCx;
11-
use crate::intrinsic::llvm;
1211

1312
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
1413
pub fn get_or_insert_global(
@@ -100,18 +99,14 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
10099
let return_type = self.type_i32();
101100
let variadic = false;
102101
self.linkage.set(FunctionType::Exported);
103-
let func = declare_raw_fn(
102+
declare_raw_fn(
104103
self,
105104
name,
106105
callconv,
107106
return_type,
108107
&[self.type_i32(), const_string],
109108
variadic,
110-
);
111-
// NOTE: it is needed to set the current_func here as well, because get_fn() is not called
112-
// for the main function.
113-
*self.current_func.borrow_mut() = Some(func);
114-
func
109+
)
115110
}
116111

117112
pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Function<'gcc> {
@@ -166,19 +161,6 @@ fn declare_raw_fn<'gcc>(
166161
param_types: &[Type<'gcc>],
167162
variadic: bool,
168163
) -> Function<'gcc> {
169-
if name.starts_with("llvm.") {
170-
let intrinsic = match name {
171-
"llvm.fma.f16" => {
172-
// fma is not a target builtin, but a normal builtin, so we handle it differently
173-
// here.
174-
cx.context.get_builtin_function("fma")
175-
}
176-
_ => llvm::intrinsic(name, cx),
177-
};
178-
179-
cx.intrinsics.borrow_mut().insert(name.to_string(), intrinsic);
180-
return intrinsic;
181-
}
182164
let func = if cx.functions.borrow().contains_key(name) {
183165
cx.functions.borrow()[name]
184166
} else {

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use gccjit::Type;
99
use gccjit::{ComparisonOp, Function, FunctionType, RValue, ToRValue, UnaryOp};
1010
#[cfg(feature = "master")]
1111
use rustc_abi::ExternAbi;
12-
use rustc_abi::{BackendRepr, HasDataLayout};
12+
use rustc_abi::{BackendRepr, HasDataLayout, WrappingRange};
1313
use rustc_codegen_ssa::MemFlags;
1414
use rustc_codegen_ssa::base::wants_msvc_seh;
1515
use rustc_codegen_ssa::common::IntPredicate;
@@ -20,19 +20,15 @@ use rustc_codegen_ssa::mir::place::{PlaceRef, PlaceValue};
2020
use rustc_codegen_ssa::traits::MiscCodegenMethods;
2121
use rustc_codegen_ssa::traits::{
2222
ArgAbiBuilderMethods, BaseTypeCodegenMethods, BuilderMethods, ConstCodegenMethods,
23-
IntrinsicCallBuilderMethods,
23+
IntrinsicCallBuilderMethods, LayoutTypeCodegenMethods,
2424
};
2525
use rustc_middle::bug;
26-
#[cfg(feature = "master")]
27-
use rustc_middle::ty::layout::FnAbiOf;
28-
use rustc_middle::ty::layout::LayoutOf;
26+
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
2927
use rustc_middle::ty::{self, Instance, Ty};
3028
use rustc_span::{Span, Symbol, sym};
3129
use rustc_target::callconv::{ArgAbi, PassMode};
3230

33-
#[cfg(feature = "master")]
34-
use crate::abi::FnAbiGccExt;
35-
use crate::abi::GccType;
31+
use crate::abi::{FnAbiGccExt, GccType};
3632
use crate::builder::Builder;
3733
use crate::common::{SignType, TypeReflection};
3834
use crate::context::CodegenCx;
@@ -609,6 +605,94 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
609605
Ok(())
610606
}
611607

608+
fn codegen_llvm_intrinsic_call(
609+
&mut self,
610+
instance: ty::Instance<'tcx>,
611+
args: &[OperandRef<'tcx, Self::Value>],
612+
is_cleanup: bool,
613+
) -> Self::Value {
614+
let func = if let Some(&func) = self.intrinsic_instances.borrow().get(&instance) {
615+
func
616+
} else {
617+
let sym = self.tcx.symbol_name(instance).name;
618+
619+
let func = if let Some(func) = self.intrinsics.borrow().get(sym) {
620+
*func
621+
} else {
622+
self.linkage.set(FunctionType::Extern);
623+
let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty());
624+
let fn_ty = fn_abi.gcc_type(self);
625+
626+
let func = match sym {
627+
"llvm.fma.f16" => {
628+
// fma is not a target builtin, but a normal builtin, so we handle it differently
629+
// here.
630+
self.context.get_builtin_function("fma")
631+
}
632+
_ => llvm::intrinsic(sym, self),
633+
};
634+
635+
self.intrinsics.borrow_mut().insert(sym.to_string(), func);
636+
637+
self.on_stack_function_params
638+
.borrow_mut()
639+
.insert(func, fn_ty.on_stack_param_indices);
640+
#[cfg(feature = "master")]
641+
for fn_attr in fn_ty.fn_attributes {
642+
func.add_attribute(fn_attr);
643+
}
644+
645+
crate::attributes::from_fn_attrs(self, func, instance);
646+
647+
func
648+
};
649+
650+
self.intrinsic_instances.borrow_mut().insert(instance, func);
651+
652+
func
653+
};
654+
let fn_ptr = func.get_address(None);
655+
let fn_ty = fn_ptr.get_type();
656+
657+
let mut llargs = vec![];
658+
659+
for arg in args {
660+
match arg.val {
661+
OperandValue::ZeroSized => {}
662+
OperandValue::Immediate(_) => llargs.push(arg.immediate()),
663+
OperandValue::Pair(a, b) => {
664+
llargs.push(a);
665+
llargs.push(b);
666+
}
667+
OperandValue::Ref(op_place_val) => {
668+
let mut llval = op_place_val.llval;
669+
// We can't use `PlaceRef::load` here because the argument
670+
// may have a type we don't treat as immediate, but the ABI
671+
// used for this call is passing it by-value. In that case,
672+
// the load would just produce `OperandValue::Ref` instead
673+
// of the `OperandValue::Immediate` we need for the call.
674+
llval = self.load(self.backend_type(arg.layout), llval, op_place_val.align);
675+
if let BackendRepr::Scalar(scalar) = arg.layout.backend_repr {
676+
if scalar.is_bool() {
677+
self.range_metadata(llval, WrappingRange { start: 0, end: 1 });
678+
}
679+
// We store bools as `i8` so we need to truncate to `i1`.
680+
llval = self.to_immediate_scalar(llval, scalar);
681+
}
682+
llargs.push(llval);
683+
}
684+
}
685+
}
686+
687+
// FIXME directly use the llvm intrinsic adjustment functions here
688+
let llret = self.call(fn_ty, None, None, fn_ptr, &llargs, None, None);
689+
if is_cleanup {
690+
self.apply_attrs_to_cleanup_callsite(llret);
691+
}
692+
693+
llret
694+
}
695+
612696
fn abort(&mut self) {
613697
let func = self.context.get_builtin_function("abort");
614698
let func: RValue<'gcc> = unsafe { std::mem::transmute(func) };

compiler/rustc_codegen_llvm/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ rustc_session = { path = "../rustc_session" }
3838
rustc_span = { path = "../rustc_span" }
3939
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
4040
rustc_target = { path = "../rustc_target" }
41-
serde = { version = "1", features = ["derive"] }
42-
serde_json = "1"
4341
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
4442
tracing = "0.1"
4543
# tidy-alphabetical-end

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ pub(crate) fn codegen(
11361136
EmitObj::None => {}
11371137
}
11381138

1139-
record_llvm_cgu_instructions_stats(&cgcx.prof, llmod);
1139+
record_llvm_cgu_instructions_stats(&cgcx.prof, &module.name, llmod);
11401140
}
11411141

11421142
// `.dwo` files are only emitted if:
@@ -1343,22 +1343,11 @@ fn record_artifact_size(
13431343
}
13441344
}
13451345

1346-
fn record_llvm_cgu_instructions_stats(prof: &SelfProfilerRef, llmod: &llvm::Module) {
1346+
fn record_llvm_cgu_instructions_stats(prof: &SelfProfilerRef, name: &str, llmod: &llvm::Module) {
13471347
if !prof.enabled() {
13481348
return;
13491349
}
13501350

1351-
let raw_stats =
1352-
llvm::build_string(|s| unsafe { llvm::LLVMRustModuleInstructionStats(llmod, s) })
1353-
.expect("cannot get module instruction stats");
1354-
1355-
#[derive(serde::Deserialize)]
1356-
struct InstructionsStats {
1357-
module: String,
1358-
total: u64,
1359-
}
1360-
1361-
let InstructionsStats { module, total } =
1362-
serde_json::from_str(&raw_stats).expect("cannot parse llvm cgu instructions stats");
1363-
prof.artifact_size("cgu_instructions", module, total);
1351+
let total = unsafe { llvm::LLVMRustModuleInstructionStats(llmod) };
1352+
prof.artifact_size("cgu_instructions", name, total);
13641353
}

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
17051705
ret.expect("LLVM does not have support for catchret")
17061706
}
17071707

1708-
fn check_call<'b>(
1708+
pub(crate) fn check_call<'b>(
17091709
&mut self,
17101710
typ: &str,
17111711
fn_ty: &'ll Type,

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ pub(crate) struct FullCx<'ll, 'tcx> {
101101

102102
/// Cache instances of monomorphic and polymorphic items
103103
pub instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
104+
/// Cache instances of intrinsics
105+
pub intrinsic_instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
104106
/// Cache generated vtables
105107
pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), &'ll Value>>,
106108
/// Cache of constant strings,
@@ -627,6 +629,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
627629
tls_model,
628630
codegen_unit,
629631
instances: Default::default(),
632+
intrinsic_instances: Default::default(),
630633
vtables: Default::default(),
631634
const_str_cache: Default::default(),
632635
const_globals: Default::default(),

0 commit comments

Comments
 (0)