Skip to content

Commit 6816a17

Browse files
authored
Unrolled build for #141448
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 1bbd62e + 865c7b9 commit 6816a17

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>,

compiler/rustc_codegen_llvm/src/base.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,24 @@ pub(crate) fn compile_codegen_unit(
8686
let mut cx = CodegenCx::new(tcx, cgu, &llvm_module);
8787
let mono_items = cx.codegen_unit.items_in_deterministic_order(cx.tcx);
8888
for &(mono_item, data) in &mono_items {
89-
mono_item.predefine::<Builder<'_, '_, '_>>(&cx, data.linkage, data.visibility);
89+
mono_item.predefine::<Builder<'_, '_, '_>>(
90+
&mut cx,
91+
cgu_name.as_str(),
92+
data.linkage,
93+
data.visibility,
94+
);
9095
}
9196

9297
// ... and now that we have everything pre-defined, fill out those definitions.
9398
for &(mono_item, item_data) in &mono_items {
94-
mono_item.define::<Builder<'_, '_, '_>>(&mut cx, item_data);
99+
mono_item.define::<Builder<'_, '_, '_>>(&mut cx, cgu_name.as_str(), item_data);
95100
}
96101

97102
// If this codegen unit contains the main function, also create the
98103
// wrapper here
99-
if let Some(entry) = maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx) {
104+
if let Some(entry) =
105+
maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx, cx.codegen_unit)
106+
{
100107
let attrs = attributes::sanitize_attrs(&cx, SanitizerSet::empty());
101108
attributes::apply_to_llfn(entry, llvm::AttributePlace::Function, &attrs);
102109
}
@@ -108,14 +115,11 @@ pub(crate) fn compile_codegen_unit(
108115
}
109116

110117
// Create the llvm.used and llvm.compiler.used variables.
111-
if !cx.used_statics.borrow().is_empty() {
112-
cx.create_used_variable_impl(c"llvm.used", &*cx.used_statics.borrow());
118+
if !cx.used_statics.is_empty() {
119+
cx.create_used_variable_impl(c"llvm.used", &cx.used_statics);
113120
}
114-
if !cx.compiler_used_statics.borrow().is_empty() {
115-
cx.create_used_variable_impl(
116-
c"llvm.compiler.used",
117-
&*cx.compiler_used_statics.borrow(),
118-
);
121+
if !cx.compiler_used_statics.is_empty() {
122+
cx.create_used_variable_impl(c"llvm.compiler.used", &cx.compiler_used_statics);
119123
}
120124

121125
// Run replace-all-uses-with for statics that need it. This must

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ impl<'ll> CodegenCx<'ll, '_> {
411411
g
412412
}
413413

414-
fn codegen_static_item(&self, def_id: DefId) {
414+
fn codegen_static_item(&mut self, def_id: DefId) {
415415
unsafe {
416416
assert!(
417417
llvm::LLVMGetInitializer(
@@ -557,6 +557,17 @@ impl<'ll> CodegenCx<'ll, '_> {
557557
}
558558
}
559559
}
560+
561+
/// Add a global value to a list to be stored in the `llvm.used` variable, an array of ptr.
562+
pub(crate) fn add_used_global(&mut self, global: &'ll Value) {
563+
self.used_statics.push(global);
564+
}
565+
566+
/// Add a global value to a list to be stored in the `llvm.compiler.used` variable,
567+
/// an array of ptr.
568+
pub(crate) fn add_compiler_used_global(&mut self, global: &'ll Value) {
569+
self.compiler_used_statics.push(global);
570+
}
560571
}
561572

562573
impl<'ll> StaticCodegenMethods for CodegenCx<'ll, '_> {
@@ -571,18 +582,7 @@ impl<'ll> StaticCodegenMethods for CodegenCx<'ll, '_> {
571582
self.const_pointercast(gv, self.type_ptr())
572583
}
573584

574-
fn codegen_static(&self, def_id: DefId) {
585+
fn codegen_static(&mut self, def_id: DefId) {
575586
self.codegen_static_item(def_id)
576587
}
577-
578-
/// Add a global value to a list to be stored in the `llvm.used` variable, an array of ptr.
579-
fn add_used_global(&self, global: &'ll Value) {
580-
self.used_statics.borrow_mut().push(global);
581-
}
582-
583-
/// Add a global value to a list to be stored in the `llvm.compiler.used` variable,
584-
/// an array of ptr.
585-
fn add_compiler_used_global(&self, global: &'ll Value) {
586-
self.compiler_used_statics.borrow_mut().push(global);
587-
}
588588
}

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::Borrow;
22
use std::cell::{Cell, RefCell};
33
use std::ffi::{CStr, c_char, c_uint};
44
use std::marker::PhantomData;
5-
use std::ops::Deref;
5+
use std::ops::{Deref, DerefMut};
66
use std::str;
77

88
use rustc_abi::{HasDataLayout, Size, TargetDataLayout, VariantIdx};
@@ -77,6 +77,13 @@ impl<'ll, T: Borrow<SCx<'ll>>> Deref for GenericCx<'ll, T> {
7777
}
7878
}
7979

80+
impl<'ll, T: Borrow<SCx<'ll>>> DerefMut for GenericCx<'ll, T> {
81+
#[inline]
82+
fn deref_mut(&mut self) -> &mut Self::Target {
83+
&mut self.0
84+
}
85+
}
86+
8087
pub(crate) type SimpleCx<'ll> = GenericCx<'ll, SCx<'ll>>;
8188

8289
/// There is one `CodegenCx` per codegen unit. Each one has its own LLVM
@@ -110,11 +117,11 @@ pub(crate) struct FullCx<'ll, 'tcx> {
110117

111118
/// Statics that will be placed in the llvm.used variable
112119
/// See <https://llvm.org/docs/LangRef.html#the-llvm-used-global-variable> for details
113-
pub used_statics: RefCell<Vec<&'ll Value>>,
120+
pub used_statics: Vec<&'ll Value>,
114121

115122
/// Statics that will be placed in the llvm.compiler.used variable
116123
/// See <https://llvm.org/docs/LangRef.html#the-llvm-compiler-used-global-variable> for details
117-
pub compiler_used_statics: RefCell<Vec<&'ll Value>>,
124+
pub compiler_used_statics: Vec<&'ll Value>,
118125

119126
/// Mapping of non-scalar types to llvm types.
120127
pub type_lowering: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'ll Type>>,
@@ -606,8 +613,8 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
606613
const_str_cache: Default::default(),
607614
const_globals: Default::default(),
608615
statics_to_rauw: RefCell::new(Vec::new()),
609-
used_statics: RefCell::new(Vec::new()),
610-
compiler_used_statics: RefCell::new(Vec::new()),
616+
used_statics: Vec::new(),
617+
compiler_used_statics: Vec::new(),
611618
type_lowering: Default::default(),
612619
scalar_lltypes: Default::default(),
613620
coverage_cx,
@@ -801,10 +808,6 @@ impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
801808
self.tcx.sess
802809
}
803810

804-
fn codegen_unit(&self) -> &'tcx CodegenUnit<'tcx> {
805-
self.codegen_unit
806-
}
807-
808811
fn set_frame_pointer_type(&self, llfn: &'ll Value) {
809812
if let Some(attr) = attributes::frame_pointer_type_attr(self) {
810813
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr]);

0 commit comments

Comments
 (0)