Skip to content

Commit f974617

Browse files
committed
Move ArgAbi::pad_i32 into PassMode::Cast.
Because it's only needed for that variant. This shrinks the types and clarifies the logic.
1 parent b853e8a commit f974617

File tree

14 files changed

+80
-88
lines changed

14 files changed

+80
-88
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/abi/pass_mode.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
100100
}
101101
_ => unreachable!("{:?}", self.layout.abi),
102102
},
103-
PassMode::Cast(ref 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,7 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
145148
}
146149
_ => unreachable!("{:?}", self.layout.abi),
147150
},
148-
PassMode::Cast(ref cast) => {
151+
PassMode::Cast(ref cast, _) => {
149152
(None, cast_target_to_abi_params(cast).into_iter().collect())
150153
}
151154
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack } => {
@@ -226,7 +229,7 @@ pub(super) fn adjust_arg_for_abi<'tcx>(
226229
let (a, b) = arg.load_scalar_pair(fx);
227230
smallvec![a, b]
228231
}
229-
PassMode::Cast(ref cast) => to_casted_value(fx, arg, cast),
232+
PassMode::Cast(ref cast, _) => to_casted_value(fx, arg, cast),
230233
PassMode::Indirect { .. } => {
231234
if is_owned {
232235
match arg.force_stack(fx) {
@@ -284,7 +287,7 @@ pub(super) fn cvalue_for_param<'tcx>(
284287
assert_eq!(block_params.len(), 2, "{:?}", block_params);
285288
Some(CValue::by_val_pair(block_params[0], block_params[1], arg_abi.layout))
286289
}
287-
PassMode::Cast(ref cast) => {
290+
PassMode::Cast(ref cast, _) => {
288291
Some(from_casted_value(fx, &block_params, arg_abi.layout, cast))
289292
}
290293
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {

Diff for: compiler/rustc_codegen_cranelift/src/abi/returning.rs

+4-4
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(
@@ -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(ref 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(ref 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);

Diff for: compiler/rustc_codegen_gcc/src/abi.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,14 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
117117
match self.ret.mode {
118118
PassMode::Ignore => cx.type_void(),
119119
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
120-
PassMode::Cast(ref cast) => cast.gcc_type(cx),
120+
PassMode::Cast(ref cast, _) => cast.gcc_type(cx),
121121
PassMode::Indirect { .. } => {
122122
argument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
123123
cx.type_void()
124124
}
125125
};
126126

127127
for arg in self.args.iter() {
128-
// add padding
129-
if arg.pad_i32 {
130-
argument_tys.push(Reg::i32().gcc_type(cx));
131-
}
132-
133128
let arg_ty = match arg.mode {
134129
PassMode::Ignore => continue,
135130
PassMode::Direct(_) => arg.layout.immediate_gcc_type(cx),
@@ -141,7 +136,13 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
141136
PassMode::Indirect { extra_attrs: Some(_), .. } => {
142137
unimplemented!();
143138
}
144-
PassMode::Cast(ref 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+
}
145146
PassMode::Indirect { extra_attrs: None, on_stack: true, .. } => {
146147
on_stack_param_indices.insert(argument_tys.len());
147148
arg.memory_ty(cx)

Diff for: 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(ref 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
},

Diff for: compiler/rustc_codegen_llvm/src/abi.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
213213
OperandValue::Ref(val, None, self.layout.align.abi).store(bx, dst)
214214
} else if self.is_unsized_indirect() {
215215
bug!("unsized `ArgAbi` must be handled through `store_fn_arg`");
216-
} else if let PassMode::Cast(cast) = &self.mode {
216+
} else if let PassMode::Cast(cast, _) = &self.mode {
217217
// FIXME(eddyb): Figure out when the simpler Store is safe, clang
218218
// uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
219219
let can_store_through_cast_ptr = false;
@@ -283,7 +283,7 @@ impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
283283
}
284284
PassMode::Direct(_)
285285
| PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ }
286-
| PassMode::Cast(_) => {
286+
| PassMode::Cast(..) => {
287287
let next_arg = next();
288288
self.store(bx, next_arg, dst);
289289
}
@@ -336,19 +336,14 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
336336
let llreturn_ty = match &self.ret.mode {
337337
PassMode::Ignore => cx.type_void(),
338338
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_llvm_type(cx),
339-
PassMode::Cast(cast) => cast.llvm_type(cx),
339+
PassMode::Cast(cast, _) => cast.llvm_type(cx),
340340
PassMode::Indirect { .. } => {
341341
llargument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
342342
cx.type_void()
343343
}
344344
};
345345

346346
for arg in args {
347-
// add padding
348-
if arg.pad_i32 {
349-
llargument_tys.push(Reg::i32().llvm_type(cx));
350-
}
351-
352347
let llarg_ty = match &arg.mode {
353348
PassMode::Ignore => continue,
354349
PassMode::Direct(_) => arg.layout.immediate_llvm_type(cx),
@@ -364,7 +359,13 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
364359
llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 1, true));
365360
continue;
366361
}
367-
PassMode::Cast(cast) => cast.llvm_type(cx),
362+
PassMode::Cast(cast, pad_i32) => {
363+
// add padding
364+
if *pad_i32 {
365+
llargument_tys.push(Reg::i32().llvm_type(cx));
366+
}
367+
cast.llvm_type(cx)
368+
}
368369
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
369370
cx.type_ptr_to(arg.memory_ty(cx))
370371
}
@@ -434,15 +435,12 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
434435
let sret = llvm::CreateStructRetAttr(cx.llcx, self.ret.layout.llvm_type(cx));
435436
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Argument(i), &[sret]);
436437
}
437-
PassMode::Cast(cast) => {
438+
PassMode::Cast(cast, _) => {
438439
cast.attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, cx, llfn);
439440
}
440441
_ => {}
441442
}
442443
for arg in self.args.iter() {
443-
if arg.pad_i32 {
444-
apply(&ArgAttributes::new());
445-
}
446444
match &arg.mode {
447445
PassMode::Ignore => {}
448446
PassMode::Indirect { attrs, extra_attrs: None, on_stack: true } => {
@@ -463,7 +461,10 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
463461
apply(a);
464462
apply(b);
465463
}
466-
PassMode::Cast(cast) => {
464+
PassMode::Cast(cast, pad_i32) => {
465+
if *pad_i32 {
466+
apply(&ArgAttributes::new());
467+
}
467468
apply(&cast.attrs);
468469
}
469470
}
@@ -496,7 +497,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
496497
let sret = llvm::CreateStructRetAttr(bx.cx.llcx, self.ret.layout.llvm_type(bx));
497498
attributes::apply_to_callsite(callsite, llvm::AttributePlace::Argument(i), &[sret]);
498499
}
499-
PassMode::Cast(cast) => {
500+
PassMode::Cast(cast, _) => {
500501
cast.attrs.apply_attrs_to_callsite(
501502
llvm::AttributePlace::ReturnValue,
502503
&bx.cx,
@@ -516,9 +517,6 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
516517
}
517518
}
518519
for arg in self.args.iter() {
519-
if arg.pad_i32 {
520-
apply(bx.cx, &ArgAttributes::new());
521-
}
522520
match &arg.mode {
523521
PassMode::Ignore => {}
524522
PassMode::Indirect { attrs, extra_attrs: None, on_stack: true } => {
@@ -542,7 +540,10 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
542540
apply(bx.cx, a);
543541
apply(bx.cx, b);
544542
}
545-
PassMode::Cast(cast) => {
543+
PassMode::Cast(cast, pad_i32) => {
544+
if *pad_i32 {
545+
apply(bx.cx, &ArgAttributes::new());
546+
}
546547
apply(bx.cx, &cast.attrs);
547548
}
548549
}

Diff for: compiler/rustc_codegen_llvm/src/intrinsic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
161161
sym::volatile_load | sym::unaligned_volatile_load => {
162162
let tp_ty = substs.type_at(0);
163163
let ptr = args[0].immediate();
164-
let load = if let PassMode::Cast(ty) = &fn_abi.ret.mode {
164+
let load = if let PassMode::Cast(ty, _) = &fn_abi.ret.mode {
165165
let llty = ty.llvm_type(self);
166166
let ptr = self.pointercast(ptr, self.type_ptr_to(llty));
167167
self.volatile_load(llty, ptr)
@@ -374,7 +374,7 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
374374
};
375375

376376
if !fn_abi.ret.is_ignore() {
377-
if let PassMode::Cast(ty) = &fn_abi.ret.mode {
377+
if let PassMode::Cast(ty, _) = &fn_abi.ret.mode {
378378
let ptr_llty = self.type_ptr_to(ty.llvm_type(self));
379379
let ptr = self.pointercast(result.llval, ptr_llty);
380380
self.store(llval, ptr, result.align);

Diff for: compiler/rustc_codegen_ssa/src/mir/block.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
339339
}
340340
}
341341

342-
PassMode::Cast(cast_ty) => {
342+
PassMode::Cast(cast_ty, _) => {
343343
let op = match self.locals[mir::RETURN_PLACE] {
344344
LocalRef::Operand(Some(op)) => op,
345345
LocalRef::Operand(None) => bug!("use of return before def"),
@@ -1158,39 +1158,35 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11581158
llargs: &mut Vec<Bx::Value>,
11591159
arg: &ArgAbi<'tcx, Ty<'tcx>>,
11601160
) {
1161-
// Fill padding with undef value, where applicable.
1162-
if arg.pad_i32 {
1163-
llargs.push(bx.const_undef(bx.reg_backend_type(&Reg::i32())))
1164-
}
1165-
1166-
if arg.is_ignore() {
1167-
return;
1168-
}
1169-
1170-
if let PassMode::Pair(..) = arg.mode {
1171-
match op.val {
1161+
match arg.mode {
1162+
PassMode::Ignore => return,
1163+
PassMode::Cast(_, true) => {
1164+
// Fill padding with undef value, where applicable.
1165+
llargs.push(bx.const_undef(bx.reg_backend_type(&Reg::i32())));
1166+
}
1167+
PassMode::Pair(..) => match op.val {
11721168
Pair(a, b) => {
11731169
llargs.push(a);
11741170
llargs.push(b);
11751171
return;
11761172
}
11771173
_ => bug!("codegen_argument: {:?} invalid for pair argument", op),
1178-
}
1179-
} else if arg.is_unsized_indirect() {
1180-
match op.val {
1174+
},
1175+
PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => match op.val {
11811176
Ref(a, Some(b), _) => {
11821177
llargs.push(a);
11831178
llargs.push(b);
11841179
return;
11851180
}
11861181
_ => bug!("codegen_argument: {:?} invalid for unsized indirect argument", op),
1187-
}
1182+
},
1183+
_ => {}
11881184
}
11891185

11901186
// Force by-ref if we have to load through a cast pointer.
11911187
let (mut llval, align, by_ref) = match op.val {
11921188
Immediate(_) | Pair(..) => match arg.mode {
1193-
PassMode::Indirect { .. } | PassMode::Cast(_) => {
1189+
PassMode::Indirect { .. } | PassMode::Cast(..) => {
11941190
let scratch = PlaceRef::alloca(bx, arg.layout);
11951191
op.val.store(bx, scratch);
11961192
(scratch.llval, scratch.align, true)
@@ -1222,7 +1218,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12221218

12231219
if by_ref && !arg.is_indirect() {
12241220
// Have to load the argument, maybe while casting it.
1225-
if let PassMode::Cast(ty) = &arg.mode {
1221+
if let PassMode::Cast(ty, _) = &arg.mode {
12261222
let llty = bx.cast_backend_type(ty);
12271223
let addr = bx.pointercast(llval, bx.type_ptr_to(llty));
12281224
llval = bx.load(llty, addr, align.min(arg.layout.align.abi));
@@ -1622,7 +1618,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16221618
}
16231619
DirectOperand(index) => {
16241620
// If there is a cast, we have to store and reload.
1625-
let op = if let PassMode::Cast(_) = ret_abi.mode {
1621+
let op = if let PassMode::Cast(..) = ret_abi.mode {
16261622
let tmp = PlaceRef::alloca(bx, ret_abi.layout);
16271623
tmp.storage_live(bx);
16281624
bx.store_arg(&ret_abi, llval, tmp);

Diff for: compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
597597
};
598598

599599
if !fn_abi.ret.is_ignore() {
600-
if let PassMode::Cast(ty) = &fn_abi.ret.mode {
600+
if let PassMode::Cast(ty, _) = &fn_abi.ret.mode {
601601
let ptr_llty = bx.type_ptr_to(bx.cast_backend_type(ty));
602602
let ptr = bx.pointercast(result.llval, ptr_llty);
603603
bx.store(llval, ptr, result.align);

Diff for: compiler/rustc_codegen_ssa/src/mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
283283
for i in 0..tupled_arg_tys.len() {
284284
let arg = &fx.fn_abi.args[idx];
285285
idx += 1;
286-
if arg.pad_i32 {
286+
if let PassMode::Cast(_, true) = arg.mode {
287287
llarg_idx += 1;
288288
}
289289
let pr_field = place.project_field(bx, i);
@@ -309,7 +309,7 @@ fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
309309

310310
let arg = &fx.fn_abi.args[idx];
311311
idx += 1;
312-
if arg.pad_i32 {
312+
if let PassMode::Cast(_, true) = arg.mode {
313313
llarg_idx += 1;
314314
}
315315

0 commit comments

Comments
 (0)