Skip to content

Commit 332cc8f

Browse files
committed
Auto merge of #100999 - nnethercote:shrink-FnAbi, r=bjorn3
Shrink `FnAbi` Because they can take up a lot of memory in debug and release builds. r? `@bjorn3`
2 parents 3b3f3b7 + f974617 commit 332cc8f

File tree

35 files changed

+165
-181
lines changed

35 files changed

+165
-181
lines changed

compiler/rustc_codegen_cranelift/src/abi/comments.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(super) fn add_arg_comment<'tcx>(
2424
local: Option<mir::Local>,
2525
local_field: Option<usize>,
2626
params: &[Value],
27-
arg_abi_mode: PassMode,
27+
arg_abi_mode: &PassMode,
2828
arg_layout: TyAndLayout<'tcx>,
2929
) {
3030
if !fx.clif_comments.enabled() {

compiler/rustc_codegen_cranelift/src/abi/pass_mode.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn apply_arg_attrs_to_abi_param(mut param: AbiParam, arg_attrs: ArgAttributes) -
3838
param
3939
}
4040

41-
fn cast_target_to_abi_params(cast: CastTarget) -> SmallVec<[AbiParam; 2]> {
41+
fn cast_target_to_abi_params(cast: &CastTarget) -> SmallVec<[AbiParam; 2]> {
4242
let (rest_count, rem_bytes) = if cast.rest.unit.size.bytes() == 0 {
4343
(0, 0)
4444
} else {
@@ -100,7 +100,10 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
100100
}
101101
_ => unreachable!("{:?}", self.layout.abi),
102102
},
103-
PassMode::Cast(cast) => cast_target_to_abi_params(cast),
103+
PassMode::Cast(ref cast, pad_i32) => {
104+
assert!(!pad_i32, "padding support not yet implemented");
105+
cast_target_to_abi_params(cast)
106+
}
104107
PassMode::Indirect { attrs, extra_attrs: None, on_stack } => {
105108
if on_stack {
106109
// Abi requires aligning struct size to pointer size
@@ -145,7 +148,9 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
145148
}
146149
_ => unreachable!("{:?}", self.layout.abi),
147150
},
148-
PassMode::Cast(cast) => (None, cast_target_to_abi_params(cast).into_iter().collect()),
151+
PassMode::Cast(ref cast, _) => {
152+
(None, cast_target_to_abi_params(cast).into_iter().collect())
153+
}
149154
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack } => {
150155
assert!(!on_stack);
151156
(Some(AbiParam::special(pointer_ty(tcx), ArgumentPurpose::StructReturn)), vec![])
@@ -160,7 +165,7 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
160165
pub(super) fn to_casted_value<'tcx>(
161166
fx: &mut FunctionCx<'_, '_, 'tcx>,
162167
arg: CValue<'tcx>,
163-
cast: CastTarget,
168+
cast: &CastTarget,
164169
) -> SmallVec<[Value; 2]> {
165170
let (ptr, meta) = arg.force_stack(fx);
166171
assert!(meta.is_none());
@@ -179,7 +184,7 @@ pub(super) fn from_casted_value<'tcx>(
179184
fx: &mut FunctionCx<'_, '_, 'tcx>,
180185
block_params: &[Value],
181186
layout: TyAndLayout<'tcx>,
182-
cast: CastTarget,
187+
cast: &CastTarget,
183188
) -> CValue<'tcx> {
184189
let abi_params = cast_target_to_abi_params(cast);
185190
let abi_param_size: u32 = abi_params.iter().map(|param| param.value_type.bytes()).sum();
@@ -224,7 +229,7 @@ pub(super) fn adjust_arg_for_abi<'tcx>(
224229
let (a, b) = arg.load_scalar_pair(fx);
225230
smallvec![a, b]
226231
}
227-
PassMode::Cast(cast) => to_casted_value(fx, arg, cast),
232+
PassMode::Cast(ref cast, _) => to_casted_value(fx, arg, cast),
228233
PassMode::Indirect { .. } => {
229234
if is_owned {
230235
match arg.force_stack(fx) {
@@ -268,7 +273,7 @@ pub(super) fn cvalue_for_param<'tcx>(
268273
local,
269274
local_field,
270275
&block_params,
271-
arg_abi.mode,
276+
&arg_abi.mode,
272277
arg_abi.layout,
273278
);
274279

@@ -282,7 +287,9 @@ pub(super) fn cvalue_for_param<'tcx>(
282287
assert_eq!(block_params.len(), 2, "{:?}", block_params);
283288
Some(CValue::by_val_pair(block_params[0], block_params[1], arg_abi.layout))
284289
}
285-
PassMode::Cast(cast) => Some(from_casted_value(fx, &block_params, arg_abi.layout, cast)),
290+
PassMode::Cast(ref cast, _) => {
291+
Some(from_casted_value(fx, &block_params, arg_abi.layout, cast))
292+
}
286293
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
287294
assert_eq!(block_params.len(), 1, "{:?}", block_params);
288295
Some(CValue::by_ref(Pointer::new(block_params[0]), arg_abi.layout))

compiler/rustc_codegen_cranelift/src/abi/returning.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(super) fn codegen_return_param<'tcx>(
1313
block_params_iter: &mut impl Iterator<Item = Value>,
1414
) -> CPlace<'tcx> {
1515
let (ret_place, ret_param): (_, SmallVec<[_; 2]>) = match fx.fn_abi.as_ref().unwrap().ret.mode {
16-
PassMode::Ignore | PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(_) => {
16+
PassMode::Ignore | PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(..) => {
1717
let is_ssa = ssa_analyzed[RETURN_PLACE] == crate::analyze::SsaKind::Ssa;
1818
(
1919
super::make_local_place(
@@ -44,7 +44,7 @@ pub(super) fn codegen_return_param<'tcx>(
4444
Some(RETURN_PLACE),
4545
None,
4646
&ret_param,
47-
fx.fn_abi.as_ref().unwrap().ret.mode,
47+
&fx.fn_abi.as_ref().unwrap().ret.mode,
4848
fx.fn_abi.as_ref().unwrap().ret.layout,
4949
);
5050

@@ -75,7 +75,7 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
7575
PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
7676
unreachable!("unsized return value")
7777
}
78-
PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(_) => (None, None),
78+
PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(..) => (None, None),
7979
};
8080

8181
let call_inst = f(fx, return_ptr);
@@ -92,7 +92,7 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
9292
ret_place
9393
.write_cvalue(fx, CValue::by_val_pair(ret_val_a, ret_val_b, ret_arg_abi.layout));
9494
}
95-
PassMode::Cast(cast) => {
95+
PassMode::Cast(ref cast, _) => {
9696
let results =
9797
fx.bcx.inst_results(call_inst).iter().copied().collect::<SmallVec<[Value; 2]>>();
9898
let result =
@@ -131,7 +131,7 @@ pub(crate) fn codegen_return(fx: &mut FunctionCx<'_, '_, '_>) {
131131
let (ret_val_a, ret_val_b) = place.to_cvalue(fx).load_scalar_pair(fx);
132132
fx.bcx.ins().return_(&[ret_val_a, ret_val_b]);
133133
}
134-
PassMode::Cast(cast) => {
134+
PassMode::Cast(ref cast, _) => {
135135
let place = fx.get_local_place(RETURN_PLACE);
136136
let ret_val = place.to_cvalue(fx);
137137
let ret_vals = super::pass_mode::to_casted_value(fx, ret_val, cast);

compiler/rustc_codegen_gcc/src/abi.rs

+12-27
Original file line numberDiff line numberDiff line change
@@ -107,45 +107,24 @@ pub trait FnAbiGccExt<'gcc, 'tcx> {
107107
impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
108108
fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool, FxHashSet<usize>) {
109109
let mut on_stack_param_indices = FxHashSet::default();
110-
let args_capacity: usize = self.args.iter().map(|arg|
111-
if arg.pad.is_some() {
112-
1
113-
}
114-
else {
115-
0
116-
} +
117-
if let PassMode::Pair(_, _) = arg.mode {
118-
2
119-
} else {
120-
1
121-
}
122-
).sum();
110+
111+
// This capacity calculation is approximate.
123112
let mut argument_tys = Vec::with_capacity(
124-
if let PassMode::Indirect { .. } = self.ret.mode {
125-
1
126-
}
127-
else {
128-
0
129-
} + args_capacity,
113+
self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 }
130114
);
131115

132116
let return_ty =
133117
match self.ret.mode {
134118
PassMode::Ignore => cx.type_void(),
135119
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
136-
PassMode::Cast(cast) => cast.gcc_type(cx),
120+
PassMode::Cast(ref cast, _) => cast.gcc_type(cx),
137121
PassMode::Indirect { .. } => {
138122
argument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
139123
cx.type_void()
140124
}
141125
};
142126

143-
for arg in &self.args {
144-
// add padding
145-
if let Some(ty) = arg.pad {
146-
argument_tys.push(ty.gcc_type(cx));
147-
}
148-
127+
for arg in self.args.iter() {
149128
let arg_ty = match arg.mode {
150129
PassMode::Ignore => continue,
151130
PassMode::Direct(_) => arg.layout.immediate_gcc_type(cx),
@@ -157,7 +136,13 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
157136
PassMode::Indirect { extra_attrs: Some(_), .. } => {
158137
unimplemented!();
159138
}
160-
PassMode::Cast(cast) => cast.gcc_type(cx),
139+
PassMode::Cast(ref cast, pad_i32) => {
140+
// add padding
141+
if pad_i32 {
142+
argument_tys.push(Reg::i32().gcc_type(cx));
143+
}
144+
cast.gcc_type(cx)
145+
}
161146
PassMode::Indirect { extra_attrs: None, on_stack: true, .. } => {
162147
on_stack_param_indices.insert(argument_tys.len());
163148
arg.memory_ty(cx)

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
130130
sym::volatile_load | sym::unaligned_volatile_load => {
131131
let tp_ty = substs.type_at(0);
132132
let mut ptr = args[0].immediate();
133-
if let PassMode::Cast(ty) = fn_abi.ret.mode {
133+
if let PassMode::Cast(ty, _) = &fn_abi.ret.mode {
134134
ptr = self.pointercast(ptr, self.type_ptr_to(ty.gcc_type(self)));
135135
}
136136
let load = self.volatile_load(ptr.get_type(), ptr);
@@ -320,7 +320,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
320320
};
321321

322322
if !fn_abi.ret.is_ignore() {
323-
if let PassMode::Cast(ty) = fn_abi.ret.mode {
323+
if let PassMode::Cast(ty, _) = &fn_abi.ret.mode {
324324
let ptr_llty = self.type_ptr_to(ty.gcc_type(self));
325325
let ptr = self.pointercast(result.llval, ptr_llty);
326326
self.store(llval, ptr, result.align);
@@ -416,7 +416,7 @@ impl<'gcc, 'tcx> ArgAbiExt<'gcc, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
416416
else if self.is_unsized_indirect() {
417417
bug!("unsized `ArgAbi` must be handled through `store_fn_arg`");
418418
}
419-
else if let PassMode::Cast(cast) = self.mode {
419+
else if let PassMode::Cast(ref cast, _) = self.mode {
420420
// FIXME(eddyb): Figure out when the simpler Store is safe, clang
421421
// uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
422422
let can_store_through_cast_ptr = false;
@@ -481,7 +481,7 @@ impl<'gcc, 'tcx> ArgAbiExt<'gcc, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
481481
PassMode::Indirect { extra_attrs: Some(_), .. } => {
482482
OperandValue::Ref(next(), Some(next()), self.layout.align.abi).store(bx, dst);
483483
},
484-
PassMode::Direct(_) | PassMode::Indirect { extra_attrs: None, .. } | PassMode::Cast(_) => {
484+
PassMode::Direct(_) | PassMode::Indirect { extra_attrs: None, .. } | PassMode::Cast(..) => {
485485
let next_arg = next();
486486
self.store(bx, next_arg, dst);
487487
},

0 commit comments

Comments
 (0)