Skip to content

Commit 161cf3e

Browse files
Rollup merge of #141448 - bjorn3:codegen_refactors, r=WaffleLapkin
A variety of improvements to the codegen backends Some are just general improvements to cg_ssa or cg_llvm, while others will make it slightly easier to use cg_ssa in cg_clif in the future.
2 parents 08bbf24 + 865c7b9 commit 161cf3e

File tree

30 files changed

+121
-182
lines changed

30 files changed

+121
-182
lines changed

compiler/rustc_codegen_gcc/src/abi.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_target::callconv::{Conv, RiscvInterruptKind};
1515

1616
use crate::builder::Builder;
1717
use crate::context::CodegenCx;
18-
use crate::intrinsic::ArgAbiExt;
1918
use crate::type_of::LayoutGccExt;
2019

2120
impl AbiBuilderMethods for Builder<'_, '_, '_> {
@@ -125,7 +124,7 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
125124
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
126125
PassMode::Cast { ref cast, .. } => cast.gcc_type(cx),
127126
PassMode::Indirect { .. } => {
128-
argument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
127+
argument_tys.push(cx.type_ptr_to(self.ret.layout.gcc_type(cx)));
129128
cx.type_void()
130129
}
131130
};
@@ -176,13 +175,13 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
176175
PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: true } => {
177176
// This is a "byval" argument, so we don't apply the `restrict` attribute on it.
178177
on_stack_param_indices.insert(argument_tys.len());
179-
arg.memory_ty(cx)
178+
arg.layout.gcc_type(cx)
180179
}
181180
PassMode::Direct(attrs) => {
182181
apply_attrs(arg.layout.immediate_gcc_type(cx), &attrs, argument_tys.len())
183182
}
184183
PassMode::Indirect { attrs, meta_attrs: None, on_stack: false } => {
185-
apply_attrs(cx.type_ptr_to(arg.memory_ty(cx)), &attrs, argument_tys.len())
184+
apply_attrs(cx.type_ptr_to(arg.layout.gcc_type(cx)), &attrs, argument_tys.len())
186185
}
187186
PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), on_stack } => {
188187
assert!(!on_stack);

compiler/rustc_codegen_gcc/src/base.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,22 @@ pub fn compile_codegen_unit(
219219

220220
let mono_items = cgu.items_in_deterministic_order(tcx);
221221
for &(mono_item, data) in &mono_items {
222-
mono_item.predefine::<Builder<'_, '_, '_>>(&cx, data.linkage, data.visibility);
222+
mono_item.predefine::<Builder<'_, '_, '_>>(
223+
&mut cx,
224+
cgu_name.as_str(),
225+
data.linkage,
226+
data.visibility,
227+
);
223228
}
224229

225230
// ... and now that we have everything pre-defined, fill out those definitions.
226231
for &(mono_item, item_data) in &mono_items {
227-
mono_item.define::<Builder<'_, '_, '_>>(&mut cx, item_data);
232+
mono_item.define::<Builder<'_, '_, '_>>(&mut cx, cgu_name.as_str(), item_data);
228233
}
229234

230235
// If this codegen unit contains the main function, also create the
231236
// wrapper here
232-
maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx);
237+
maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx, cx.codegen_unit);
233238

234239
// Finalize debuginfo
235240
if cx.sess().opts.debuginfo != DebugInfo::None {

compiler/rustc_codegen_gcc/src/consts.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'gcc, 'tcx> StaticCodegenMethods for CodegenCx<'gcc, 'tcx> {
6767
}
6868

6969
#[cfg_attr(not(feature = "master"), allow(unused_mut))]
70-
fn codegen_static(&self, def_id: DefId) {
70+
fn codegen_static(&mut self, def_id: DefId) {
7171
let attrs = self.tcx.codegen_fn_attrs(def_id);
7272

7373
let Ok((value, alloc)) = codegen_static_initializer(self, def_id) else {
@@ -160,19 +160,14 @@ impl<'gcc, 'tcx> StaticCodegenMethods for CodegenCx<'gcc, 'tcx> {
160160
self.add_used_global(global.to_rvalue());
161161
}
162162
}
163+
}
163164

165+
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
164166
/// Add a global value to a list to be stored in the `llvm.used` variable, an array of i8*.
165-
fn add_used_global(&self, _global: RValue<'gcc>) {
167+
pub fn add_used_global(&mut self, _global: RValue<'gcc>) {
166168
// TODO(antoyo)
167169
}
168170

169-
fn add_compiler_used_global(&self, global: RValue<'gcc>) {
170-
// NOTE: seems like GCC does not make the distinction between compiler.used and used.
171-
self.add_used_global(global);
172-
}
173-
}
174-
175-
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
176171
#[cfg_attr(not(feature = "master"), allow(unused_variables))]
177172
pub fn add_used_function(&self, function: Function<'gcc>) {
178173
#[cfg(feature = "master")]

compiler/rustc_codegen_gcc/src/context.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,6 @@ impl<'gcc, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
470470
self.tcx.sess
471471
}
472472

473-
fn codegen_unit(&self) -> &'tcx CodegenUnit<'tcx> {
474-
self.codegen_unit
475-
}
476-
477473
fn set_frame_pointer_type(&self, _llfn: RValue<'gcc>) {
478474
// TODO(antoyo)
479475
}

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,9 @@ impl<'a, 'gcc, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
574574
) {
575575
arg_abi.store(self, val, dst)
576576
}
577-
578-
fn arg_memory_ty(&self, arg_abi: &ArgAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
579-
arg_abi.memory_ty(self)
580-
}
581577
}
582578

583579
pub trait ArgAbiExt<'gcc, 'tcx> {
584-
fn memory_ty(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
585580
fn store(
586581
&self,
587582
bx: &mut Builder<'_, 'gcc, 'tcx>,
@@ -597,12 +592,6 @@ pub trait ArgAbiExt<'gcc, 'tcx> {
597592
}
598593

599594
impl<'gcc, 'tcx> ArgAbiExt<'gcc, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
600-
/// Gets the LLVM type for a place of the original Rust type of
601-
/// this argument/return, i.e., the result of `type_of::type_of`.
602-
fn memory_ty(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
603-
self.layout.gcc_type(cx)
604-
}
605-
606595
/// Stores a direct/indirect value described by this ArgAbi into a
607596
/// place for the original Rust type of this argument/return.
608597
/// Can be used for both storing formal arguments into Rust variables

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl WriteBackendMethods for GccCodegenBackend {
391391
unimplemented!()
392392
}
393393

394-
unsafe fn optimize(
394+
fn optimize(
395395
_cgcx: &CodegenContext<Self>,
396396
_dcx: DiagCtxtHandle<'_>,
397397
module: &mut ModuleCodegen<Self::Module>,
@@ -409,14 +409,14 @@ impl WriteBackendMethods for GccCodegenBackend {
409409
Ok(())
410410
}
411411

412-
unsafe fn optimize_thin(
412+
fn optimize_thin(
413413
cgcx: &CodegenContext<Self>,
414414
thin: ThinModule<Self>,
415415
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
416416
back::lto::optimize_thin_module(thin, cgcx)
417417
}
418418

419-
unsafe fn codegen(
419+
fn codegen(
420420
cgcx: &CodegenContext<Self>,
421421
dcx: DiagCtxtHandle<'_>,
422422
module: ModuleCodegen<Self::Module>,

compiler/rustc_codegen_gcc/src/mono_item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{attributes, base};
1616
impl<'gcc, 'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
1717
#[cfg_attr(not(feature = "master"), allow(unused_variables))]
1818
fn predefine_static(
19-
&self,
19+
&mut self,
2020
def_id: DefId,
2121
_linkage: Linkage,
2222
visibility: Visibility,
@@ -42,7 +42,7 @@ impl<'gcc, 'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
4242

4343
#[cfg_attr(not(feature = "master"), allow(unused_variables))]
4444
fn predefine_fn(
45-
&self,
45+
&mut self,
4646
instance: Instance<'tcx>,
4747
linkage: Linkage,
4848
visibility: Visibility,

compiler/rustc_codegen_llvm/src/abi.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ impl LlvmType for CastTarget {
172172
}
173173

174174
trait ArgAbiExt<'ll, 'tcx> {
175-
fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
176175
fn store(
177176
&self,
178177
bx: &mut Builder<'_, 'll, 'tcx>,
@@ -188,12 +187,6 @@ trait ArgAbiExt<'ll, 'tcx> {
188187
}
189188

190189
impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
191-
/// Gets the LLVM type for a place of the original Rust type of
192-
/// this argument/return, i.e., the result of `type_of::type_of`.
193-
fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
194-
self.layout.llvm_type(cx)
195-
}
196-
197190
/// Stores a direct/indirect value described by this ArgAbi into a
198191
/// place for the original Rust type of this argument/return.
199192
/// Can be used for both storing formal arguments into Rust variables
@@ -302,9 +295,6 @@ impl<'ll, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
302295
) {
303296
arg_abi.store(self, val, dst)
304297
}
305-
fn arg_memory_ty(&self, arg_abi: &ArgAbi<'tcx, Ty<'tcx>>) -> &'ll Type {
306-
arg_abi.memory_ty(self)
307-
}
308298
}
309299

310300
pub(crate) trait FnAbiLlvmExt<'ll, 'tcx> {

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ impl Drop for ThinBuffer {
799799
}
800800
}
801801

802-
pub(crate) unsafe fn optimize_thin_module(
802+
pub(crate) fn optimize_thin_module(
803803
thin_module: ThinModule<LlvmCodegenBackend>,
804804
cgcx: &CodegenContext<LlvmCodegenBackend>,
805805
) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> {

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ pub(crate) unsafe fn llvm_optimize(
704704
}
705705

706706
// Unsafe due to LLVM calls.
707-
pub(crate) unsafe fn optimize(
707+
pub(crate) fn optimize(
708708
cgcx: &CodegenContext<LlvmCodegenBackend>,
709709
dcx: DiagCtxtHandle<'_>,
710710
module: &mut ModuleCodegen<ModuleLlvm>,
@@ -815,7 +815,7 @@ pub(crate) fn link(
815815
Ok(modules.remove(0))
816816
}
817817

818-
pub(crate) unsafe fn codegen(
818+
pub(crate) fn codegen(
819819
cgcx: &CodegenContext<LlvmCodegenBackend>,
820820
dcx: DiagCtxtHandle<'_>,
821821
module: ModuleCodegen<ModuleLlvm>,

0 commit comments

Comments
 (0)