Skip to content

Commit f47e9af

Browse files
committed
Auto merge of rust-lang#102551 - bjorn3:cg_ssa_cleanup, r=davidtwco
Some more cleanup for rustc_codegen_ssa With the aim to make non-LLVM like backends, like Cranelift, easier to support using cg_ssa.
2 parents 6b139c5 + 268e02c commit f47e9af

File tree

22 files changed

+115
-131
lines changed

22 files changed

+115
-131
lines changed

compiler/rustc_codegen_gcc/src/abi.rs

-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ use crate::intrinsic::ArgAbiExt;
1111
use crate::type_of::LayoutGccExt;
1212

1313
impl<'a, 'gcc, 'tcx> AbiBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
14-
fn apply_attrs_callsite(&mut self, _fn_abi: &FnAbi<'tcx, Ty<'tcx>>, _callsite: Self::Value) {
15-
// TODO(antoyo)
16-
}
17-
1814
fn get_param(&mut self, index: usize) -> Self::Value {
1915
let func = self.current_func();
2016
let param = func.get_param(index as i32);

compiler/rustc_codegen_gcc/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
498498
if options.contains(InlineAsmOptions::NORETURN) {
499499
let builtin_unreachable = self.context.get_builtin_function("__builtin_unreachable");
500500
let builtin_unreachable: RValue<'gcc> = unsafe { std::mem::transmute(builtin_unreachable) };
501-
self.call(self.type_void(), builtin_unreachable, &[], None);
501+
self.call(self.type_void(), None, builtin_unreachable, &[], None);
502502
}
503503

504504
// Write results to outputs.

compiler/rustc_codegen_gcc/src/builder.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,23 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
444444
self.block.end_with_switch(None, value, default_block, &gcc_cases);
445445
}
446446

447-
fn invoke(&mut self, typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
447+
fn invoke(
448+
&mut self,
449+
typ: Type<'gcc>,
450+
fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
451+
func: RValue<'gcc>,
452+
args: &[RValue<'gcc>],
453+
then: Block<'gcc>,
454+
catch: Block<'gcc>,
455+
_funclet: Option<&Funclet>,
456+
) -> RValue<'gcc> {
448457
// TODO(bjorn3): Properly implement unwinding.
449-
let call_site = self.call(typ, func, args, None);
458+
let call_site = self.call(typ, None, func, args, None);
450459
let condition = self.context.new_rvalue_from_int(self.bool_type, 1);
451460
self.llbb().end_with_conditional(None, condition, then, catch);
461+
if let Some(_fn_abi) = fn_abi {
462+
// TODO(bjorn3): Apply function attributes
463+
}
452464
call_site
453465
}
454466

@@ -643,11 +655,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
643655
self.current_func().new_local(None, aligned_type, &format!("stack_var_{}", self.stack_var_count.get())).get_address(None)
644656
}
645657

646-
fn dynamic_alloca(&mut self, _ty: Type<'gcc>, _align: Align) -> RValue<'gcc> {
647-
unimplemented!();
648-
}
649-
650-
fn array_alloca(&mut self, _ty: Type<'gcc>, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
658+
fn byte_array_alloca(&mut self, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
651659
unimplemented!();
652660
}
653661

@@ -1227,16 +1235,27 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
12271235
// TODO(antoyo)
12281236
}
12291237

1230-
fn call(&mut self, _typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>], funclet: Option<&Funclet>) -> RValue<'gcc> {
1238+
fn call(
1239+
&mut self,
1240+
_typ: Type<'gcc>,
1241+
fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
1242+
func: RValue<'gcc>,
1243+
args: &[RValue<'gcc>],
1244+
funclet: Option<&Funclet>,
1245+
) -> RValue<'gcc> {
12311246
// FIXME(antoyo): remove when having a proper API.
12321247
let gcc_func = unsafe { std::mem::transmute(func) };
1233-
if self.functions.borrow().values().find(|value| **value == gcc_func).is_some() {
1248+
let call = if self.functions.borrow().values().find(|value| **value == gcc_func).is_some() {
12341249
self.function_call(func, args, funclet)
12351250
}
12361251
else {
12371252
// If it's a not function that was defined, it's a function pointer.
12381253
self.function_ptr_call(func, args, funclet)
1254+
};
1255+
if let Some(_fn_abi) = fn_abi {
1256+
// TODO(bjorn3): Apply function attributes
12391257
}
1258+
call
12401259
}
12411260

12421261
fn zext(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {

compiler/rustc_codegen_gcc/src/context.rs

-16
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,6 @@ impl<'gcc, 'tcx> MiscMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
416416
self.codegen_unit
417417
}
418418

419-
fn used_statics(&self) -> &RefCell<Vec<RValue<'gcc>>> {
420-
unimplemented!();
421-
}
422-
423419
fn set_frame_pointer_type(&self, _llfn: RValue<'gcc>) {
424420
// TODO(antoyo)
425421
}
@@ -428,10 +424,6 @@ impl<'gcc, 'tcx> MiscMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
428424
// TODO(antoyo)
429425
}
430426

431-
fn create_used_variable(&self) {
432-
unimplemented!();
433-
}
434-
435427
fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
436428
if self.get_declared_value("main").is_none() {
437429
Some(self.declare_cfn("main", fn_type))
@@ -443,14 +435,6 @@ impl<'gcc, 'tcx> MiscMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
443435
None
444436
}
445437
}
446-
447-
fn compiler_used_statics(&self) -> &RefCell<Vec<RValue<'gcc>>> {
448-
unimplemented!()
449-
}
450-
451-
fn create_compiler_used_variable(&self) {
452-
unimplemented!()
453-
}
454438
}
455439

456440
impl<'gcc, 'tcx> HasTyCtxt<'tcx> for CodegenCx<'gcc, 'tcx> {

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
100100
_ if simple.is_some() => {
101101
// FIXME(antoyo): remove this cast when the API supports function.
102102
let func = unsafe { std::mem::transmute(simple.expect("simple")) };
103-
self.call(self.type_void(), func, &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(), None)
103+
self.call(self.type_void(), None, func, &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(), None)
104104
},
105105
sym::likely => {
106106
self.expect(args[0].immediate(), true)
@@ -341,7 +341,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
341341
fn abort(&mut self) {
342342
let func = self.context.get_builtin_function("abort");
343343
let func: RValue<'gcc> = unsafe { std::mem::transmute(func) };
344-
self.call(self.type_void(), func, &[], None);
344+
self.call(self.type_void(), None, func, &[], None);
345345
}
346346

347347
fn assume(&mut self, value: Self::Value) {
@@ -1124,7 +1124,7 @@ fn try_intrinsic<'gcc, 'tcx>(bx: &mut Builder<'_, 'gcc, 'tcx>, try_func: RValue<
11241124
// NOTE: the `|| true` here is to use the panic=abort strategy with panic=unwind too
11251125
if bx.sess().panic_strategy() == PanicStrategy::Abort || true {
11261126
// TODO(bjorn3): Properly implement unwinding and remove the `|| true` once this is done.
1127-
bx.call(bx.type_void(), try_func, &[data], None);
1127+
bx.call(bx.type_void(), None, try_func, &[data], None);
11281128
// Return 0 unconditionally from the intrinsic call;
11291129
// we can never unwind.
11301130
let ret_align = bx.tcx.data_layout.i32_align.abi;

compiler/rustc_codegen_gcc/src/intrinsic/simd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>,
461461
let llvm_name = &format!("llvm.{0}.v{1}{2}", intr_name, in_len, elem_ty_str);
462462
let function = intrinsic::llvm::intrinsic(llvm_name, &bx.cx);
463463
let function: RValue<'gcc> = unsafe { std::mem::transmute(function) };
464-
let c = bx.call(fn_ty, function, &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(), None);
464+
let c = bx.call(fn_ty, None, function, &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(), None);
465465
Ok(c)
466466
}
467467

compiler/rustc_codegen_gcc/src/lib.rs

-10
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,6 @@ impl ExtraBackendMethods for GccCodegenBackend {
171171
Ok(())
172172
})
173173
}
174-
175-
fn target_cpu<'b>(&self, _sess: &'b Session) -> &'b str {
176-
unimplemented!();
177-
}
178-
179-
fn tune_cpu<'b>(&self, _sess: &'b Session) -> Option<&'b str> {
180-
None
181-
// TODO(antoyo)
182-
}
183174
}
184175

185176
pub struct ModuleBuffer;
@@ -210,7 +201,6 @@ impl WriteBackendMethods for GccCodegenBackend {
210201
type Module = GccContext;
211202
type TargetMachine = ();
212203
type ModuleBuffer = ModuleBuffer;
213-
type Context = ();
214204
type ThinData = ();
215205
type ThinBuffer = ThinBuffer;
216206

compiler/rustc_codegen_llvm/src/abi.rs

-4
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,6 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
592592
}
593593

594594
impl<'tcx> AbiBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
595-
fn apply_attrs_callsite(&mut self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>, callsite: Self::Value) {
596-
fn_abi.apply_attrs_callsite(self, callsite)
597-
}
598-
599595
fn get_param(&mut self, index: usize) -> Self::Value {
600596
llvm::get_param(self.llfn(), index as c_uint)
601597
}

compiler/rustc_codegen_llvm/src/asm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,9 @@ pub(crate) fn inline_asm_call<'ll>(
430430
);
431431

432432
let call = if let Some((dest, catch, funclet)) = dest_catch_funclet {
433-
bx.invoke(fty, v, inputs, dest, catch, funclet)
433+
bx.invoke(fty, None, v, inputs, dest, catch, funclet)
434434
} else {
435-
bx.call(fty, v, inputs, None)
435+
bx.call(fty, None, v, inputs, None)
436436
};
437437

438438
// Store mark in a metadata node so we can map LLVM errors

compiler/rustc_codegen_llvm/src/base.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use crate::context::CodegenCx;
1919
use crate::llvm;
2020
use crate::value::Value;
2121

22+
use cstr::cstr;
23+
2224
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
2325
use rustc_codegen_ssa::mono_item::MonoItemExt;
2426
use rustc_codegen_ssa::traits::*;
@@ -107,11 +109,14 @@ pub fn compile_codegen_unit(tcx: TyCtxt<'_>, cgu_name: Symbol) -> (ModuleCodegen
107109
}
108110

109111
// Create the llvm.used and llvm.compiler.used variables.
110-
if !cx.used_statics().borrow().is_empty() {
111-
cx.create_used_variable()
112+
if !cx.used_statics.borrow().is_empty() {
113+
cx.create_used_variable_impl(cstr!("llvm.used"), &*cx.used_statics.borrow());
112114
}
113-
if !cx.compiler_used_statics().borrow().is_empty() {
114-
cx.create_compiler_used_variable()
115+
if !cx.compiler_used_statics.borrow().is_empty() {
116+
cx.create_used_variable_impl(
117+
cstr!("llvm.compiler.used"),
118+
&*cx.compiler_used_statics.borrow(),
119+
);
115120
}
116121

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

compiler/rustc_codegen_llvm/src/builder.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::FnAbiLlvmExt;
12
use crate::attributes;
23
use crate::common::Funclet;
34
use crate::context::CodegenCx;
@@ -214,6 +215,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
214215
fn invoke(
215216
&mut self,
216217
llty: &'ll Type,
218+
fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
217219
llfn: &'ll Value,
218220
args: &[&'ll Value],
219221
then: &'ll BasicBlock,
@@ -226,7 +228,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
226228
let bundle = funclet.map(|funclet| funclet.bundle());
227229
let bundle = bundle.as_ref().map(|b| &*b.raw);
228230

229-
unsafe {
231+
let invoke = unsafe {
230232
llvm::LLVMRustBuildInvoke(
231233
self.llbuilder,
232234
llty,
@@ -238,7 +240,11 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
238240
bundle,
239241
UNNAMED,
240242
)
243+
};
244+
if let Some(fn_abi) = fn_abi {
245+
fn_abi.apply_attrs_callsite(self, invoke);
241246
}
247+
invoke
242248
}
243249

244250
fn unreachable(&mut self) {
@@ -405,20 +411,17 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
405411
fn alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
406412
let mut bx = Builder::with_cx(self.cx);
407413
bx.position_at_start(unsafe { llvm::LLVMGetFirstBasicBlock(self.llfn()) });
408-
bx.dynamic_alloca(ty, align)
409-
}
410-
411-
fn dynamic_alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
412414
unsafe {
413-
let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
415+
let alloca = llvm::LLVMBuildAlloca(bx.llbuilder, ty, UNNAMED);
414416
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
415417
alloca
416418
}
417419
}
418420

419-
fn array_alloca(&mut self, ty: &'ll Type, len: &'ll Value, align: Align) -> &'ll Value {
421+
fn byte_array_alloca(&mut self, len: &'ll Value, align: Align) -> &'ll Value {
420422
unsafe {
421-
let alloca = llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, UNNAMED);
423+
let alloca =
424+
llvm::LLVMBuildArrayAlloca(self.llbuilder, self.cx().type_i8(), len, UNNAMED);
422425
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
423426
alloca
424427
}
@@ -1145,6 +1148,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11451148
fn call(
11461149
&mut self,
11471150
llty: &'ll Type,
1151+
fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
11481152
llfn: &'ll Value,
11491153
args: &[&'ll Value],
11501154
funclet: Option<&Funclet<'ll>>,
@@ -1155,7 +1159,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11551159
let bundle = funclet.map(|funclet| funclet.bundle());
11561160
let bundle = bundle.as_ref().map(|b| &*b.raw);
11571161

1158-
unsafe {
1162+
let call = unsafe {
11591163
llvm::LLVMRustBuildCall(
11601164
self.llbuilder,
11611165
llty,
@@ -1164,7 +1168,11 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11641168
args.len() as c_uint,
11651169
bundle,
11661170
)
1171+
};
1172+
if let Some(fn_abi) = fn_abi {
1173+
fn_abi.apply_attrs_callsite(self, call);
11671174
}
1175+
call
11681176
}
11691177

11701178
fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
@@ -1397,7 +1405,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
13971405

13981406
pub(crate) fn call_intrinsic(&mut self, intrinsic: &str, args: &[&'ll Value]) -> &'ll Value {
13991407
let (ty, f) = self.cx.get_intrinsic(intrinsic);
1400-
self.call(ty, f, args, None)
1408+
self.call(ty, None, f, args, None)
14011409
}
14021410

14031411
fn call_lifetime_intrinsic(&mut self, intrinsic: &str, ptr: &'ll Value, size: Size) {
@@ -1459,7 +1467,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
14591467
format!("llvm.{}.sat.i{}.f{}", instr, int_width, float_width)
14601468
};
14611469
let f = self.declare_cfn(&name, llvm::UnnamedAddr::No, self.type_func(&[src_ty], dest_ty));
1462-
self.call(self.type_func(&[src_ty], dest_ty), f, &[val], None)
1470+
self.call(self.type_func(&[src_ty], dest_ty), None, f, &[val], None)
14631471
}
14641472

14651473
pub(crate) fn landing_pad(

compiler/rustc_codegen_llvm/src/context.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
458458
self.coverage_cx.as_ref()
459459
}
460460

461-
fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
461+
pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
462462
let section = cstr!("llvm.metadata");
463463
let array = self.const_array(self.type_ptr_to(self.type_i8()), values);
464464

@@ -556,14 +556,6 @@ impl<'ll, 'tcx> MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
556556
self.codegen_unit
557557
}
558558

559-
fn used_statics(&self) -> &RefCell<Vec<&'ll Value>> {
560-
&self.used_statics
561-
}
562-
563-
fn compiler_used_statics(&self) -> &RefCell<Vec<&'ll Value>> {
564-
&self.compiler_used_statics
565-
}
566-
567559
fn set_frame_pointer_type(&self, llfn: &'ll Value) {
568560
if let Some(attr) = attributes::frame_pointer_type_attr(self) {
569561
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr]);
@@ -577,17 +569,6 @@ impl<'ll, 'tcx> MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
577569
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
578570
}
579571

580-
fn create_used_variable(&self) {
581-
self.create_used_variable_impl(cstr!("llvm.used"), &*self.used_statics.borrow());
582-
}
583-
584-
fn create_compiler_used_variable(&self) {
585-
self.create_used_variable_impl(
586-
cstr!("llvm.compiler.used"),
587-
&*self.compiler_used_statics.borrow(),
588-
);
589-
}
590-
591572
fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
592573
if self.get_declared_value("main").is_none() {
593574
Some(self.declare_cfn("main", llvm::UnnamedAddr::Global, fn_type))

0 commit comments

Comments
 (0)