Skip to content

Commit 42b0b80

Browse files
committed
Replace ByVal attribute with on_stack field for Indirect
This makes it clearer that only PassMode::Indirect allows ByVal
1 parent 967a228 commit 42b0b80

File tree

5 files changed

+102
-57
lines changed

5 files changed

+102
-57
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

Lines changed: 79 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ impl ArgAttributeExt for ArgAttribute {
4141
}
4242

4343
pub trait ArgAttributesExt {
44-
fn apply_llfn(&self, idx: AttributePlace, llfn: &Value, ty: Option<&Type>);
45-
fn apply_callsite(&self, idx: AttributePlace, callsite: &Value, ty: Option<&Type>);
44+
fn apply_attrs_to_llfn(&self, idx: AttributePlace, llfn: &Value);
45+
fn apply_attrs_to_callsite(&self, idx: AttributePlace, callsite: &Value);
4646
}
4747

4848
impl ArgAttributesExt for ArgAttributes {
49-
fn apply_llfn(&self, idx: AttributePlace, llfn: &Value, ty: Option<&Type>) {
49+
fn apply_attrs_to_llfn(&self, idx: AttributePlace, llfn: &Value) {
5050
let mut regular = self.regular;
5151
unsafe {
5252
let deref = self.pointee_size.bytes();
@@ -61,9 +61,6 @@ impl ArgAttributesExt for ArgAttributes {
6161
if let Some(align) = self.pointee_align {
6262
llvm::LLVMRustAddAlignmentAttr(llfn, idx.as_uint(), align.bytes() as u32);
6363
}
64-
if regular.contains(ArgAttribute::ByVal) {
65-
llvm::LLVMRustAddByValAttr(llfn, idx.as_uint(), ty.unwrap());
66-
}
6764
regular.for_each_kind(|attr| attr.apply_llfn(idx, llfn));
6865
match self.arg_ext {
6966
ArgExtension::None => {}
@@ -77,7 +74,7 @@ impl ArgAttributesExt for ArgAttributes {
7774
}
7875
}
7976

80-
fn apply_callsite(&self, idx: AttributePlace, callsite: &Value, ty: Option<&Type>) {
77+
fn apply_attrs_to_callsite(&self, idx: AttributePlace, callsite: &Value) {
8178
let mut regular = self.regular;
8279
unsafe {
8380
let deref = self.pointee_size.bytes();
@@ -100,9 +97,6 @@ impl ArgAttributesExt for ArgAttributes {
10097
align.bytes() as u32,
10198
);
10299
}
103-
if regular.contains(ArgAttribute::ByVal) {
104-
llvm::LLVMRustAddByValCallSiteAttr(callsite, idx.as_uint(), ty.unwrap());
105-
}
106100
regular.for_each_kind(|attr| attr.apply_callsite(idx, callsite));
107101
match self.arg_ext {
108102
ArgExtension::None => {}
@@ -285,10 +279,12 @@ impl ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
285279
PassMode::Pair(..) => {
286280
OperandValue::Pair(next(), next()).store(bx, dst);
287281
}
288-
PassMode::Indirect(_, Some(_)) => {
282+
PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
289283
OperandValue::Ref(next(), Some(next()), self.layout.align.abi).store(bx, dst);
290284
}
291-
PassMode::Direct(_) | PassMode::Indirect(_, None) | PassMode::Cast(_) => {
285+
PassMode::Direct(_)
286+
| PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ }
287+
| PassMode::Cast(_) => {
292288
let next_arg = next();
293289
self.store(bx, next_arg, dst);
294290
}
@@ -333,14 +329,14 @@ impl<'tcx> FnAbiLlvmExt<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
333329
if let PassMode::Pair(_, _) = arg.mode { 2 } else { 1 }
334330
).sum();
335331
let mut llargument_tys = Vec::with_capacity(
336-
if let PassMode::Indirect(..) = self.ret.mode { 1 } else { 0 } + args_capacity,
332+
if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 } + args_capacity,
337333
);
338334

339335
let llreturn_ty = match self.ret.mode {
340336
PassMode::Ignore => cx.type_void(),
341337
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_llvm_type(cx),
342338
PassMode::Cast(cast) => cast.llvm_type(cx),
343-
PassMode::Indirect(..) => {
339+
PassMode::Indirect { .. } => {
344340
llargument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
345341
cx.type_void()
346342
}
@@ -360,15 +356,17 @@ impl<'tcx> FnAbiLlvmExt<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
360356
llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 1, true));
361357
continue;
362358
}
363-
PassMode::Indirect(_, Some(_)) => {
359+
PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
364360
let ptr_ty = cx.tcx.mk_mut_ptr(arg.layout.ty);
365361
let ptr_layout = cx.layout_of(ptr_ty);
366362
llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 0, true));
367363
llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 1, true));
368364
continue;
369365
}
370366
PassMode::Cast(cast) => cast.llvm_type(cx),
371-
PassMode::Indirect(_, None) => cx.type_ptr_to(arg.memory_ty(cx)),
367+
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
368+
cx.type_ptr_to(arg.memory_ty(cx))
369+
}
372370
};
373371
llargument_tys.push(llarg_ty);
374372
}
@@ -420,35 +418,53 @@ impl<'tcx> FnAbiLlvmExt<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
420418
}
421419

422420
let mut i = 0;
423-
let mut apply = |attrs: &ArgAttributes, ty: Option<&Type>| {
424-
attrs.apply_llfn(llvm::AttributePlace::Argument(i), llfn, ty);
421+
let mut apply = |attrs: &ArgAttributes| {
422+
attrs.apply_attrs_to_llfn(llvm::AttributePlace::Argument(i), llfn);
425423
i += 1;
424+
i - 1
426425
};
427426
match self.ret.mode {
428427
PassMode::Direct(ref attrs) => {
429-
attrs.apply_llfn(llvm::AttributePlace::ReturnValue, llfn, None);
428+
attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, llfn);
429+
}
430+
PassMode::Indirect { ref attrs, extra_attrs: _, on_stack } => {
431+
assert!(!on_stack);
432+
apply(attrs);
430433
}
431-
PassMode::Indirect(ref attrs, _) => apply(attrs, Some(self.ret.layout.llvm_type(cx))),
432434
_ => {}
433435
}
434436
for arg in &self.args {
435437
if arg.pad.is_some() {
436-
apply(&ArgAttributes::new(), None);
438+
apply(&ArgAttributes::new());
437439
}
438440
match arg.mode {
439441
PassMode::Ignore => {}
440-
PassMode::Direct(ref attrs) | PassMode::Indirect(ref attrs, None) => {
441-
apply(attrs, Some(arg.layout.llvm_type(cx)))
442+
PassMode::Indirect { ref attrs, extra_attrs: None, on_stack: true } => {
443+
let i = apply(attrs);
444+
unsafe {
445+
llvm::LLVMRustAddByValAttr(
446+
llfn,
447+
llvm::AttributePlace::Argument(i).as_uint(),
448+
arg.layout.llvm_type(cx),
449+
);
450+
}
451+
}
452+
PassMode::Direct(ref attrs)
453+
| PassMode::Indirect { ref attrs, extra_attrs: None, on_stack: false } => {
454+
apply(attrs);
442455
}
443-
PassMode::Indirect(ref attrs, Some(ref extra_attrs)) => {
444-
apply(attrs, None);
445-
apply(extra_attrs, None);
456+
PassMode::Indirect { ref attrs, extra_attrs: Some(ref extra_attrs), on_stack } => {
457+
assert!(!on_stack);
458+
apply(attrs);
459+
apply(extra_attrs);
446460
}
447461
PassMode::Pair(ref a, ref b) => {
448-
apply(a, None);
449-
apply(b, None);
462+
apply(a);
463+
apply(b);
464+
}
465+
PassMode::Cast(_) => {
466+
apply(&ArgAttributes::new());
450467
}
451-
PassMode::Cast(_) => apply(&ArgAttributes::new(), None),
452468
}
453469
}
454470
}
@@ -457,15 +473,19 @@ impl<'tcx> FnAbiLlvmExt<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
457473
// FIXME(wesleywiser, eddyb): We should apply `nounwind` and `noreturn` as appropriate to this callsite.
458474

459475
let mut i = 0;
460-
let mut apply = |attrs: &ArgAttributes, ty: Option<&Type>| {
461-
attrs.apply_callsite(llvm::AttributePlace::Argument(i), callsite, ty);
476+
let mut apply = |attrs: &ArgAttributes| {
477+
attrs.apply_attrs_to_callsite(llvm::AttributePlace::Argument(i), callsite);
462478
i += 1;
479+
i - 1
463480
};
464481
match self.ret.mode {
465482
PassMode::Direct(ref attrs) => {
466-
attrs.apply_callsite(llvm::AttributePlace::ReturnValue, callsite, None);
483+
attrs.apply_attrs_to_callsite(llvm::AttributePlace::ReturnValue, callsite);
484+
}
485+
PassMode::Indirect { ref attrs, extra_attrs: _, on_stack } => {
486+
assert!(!on_stack);
487+
apply(attrs);
467488
}
468-
PassMode::Indirect(ref attrs, _) => apply(attrs, Some(self.ret.layout.llvm_type(bx))),
469489
_ => {}
470490
}
471491
if let abi::Abi::Scalar(ref scalar) = self.ret.layout.abi {
@@ -483,22 +503,39 @@ impl<'tcx> FnAbiLlvmExt<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
483503
}
484504
for arg in &self.args {
485505
if arg.pad.is_some() {
486-
apply(&ArgAttributes::new(), None);
506+
apply(&ArgAttributes::new());
487507
}
488508
match arg.mode {
489509
PassMode::Ignore => {}
490-
PassMode::Direct(ref attrs) | PassMode::Indirect(ref attrs, None) => {
491-
apply(attrs, Some(arg.layout.llvm_type(bx)))
510+
PassMode::Indirect { ref attrs, extra_attrs: None, on_stack: true } => {
511+
let i = apply(attrs);
512+
unsafe {
513+
llvm::LLVMRustAddByValCallSiteAttr(
514+
callsite,
515+
llvm::AttributePlace::Argument(i).as_uint(),
516+
arg.layout.llvm_type(bx),
517+
);
518+
}
492519
}
493-
PassMode::Indirect(ref attrs, Some(ref extra_attrs)) => {
494-
apply(attrs, None);
495-
apply(extra_attrs, None);
520+
PassMode::Direct(ref attrs)
521+
| PassMode::Indirect { ref attrs, extra_attrs: None, on_stack: false } => {
522+
apply(attrs);
523+
}
524+
PassMode::Indirect {
525+
ref attrs,
526+
extra_attrs: Some(ref extra_attrs),
527+
on_stack: _,
528+
} => {
529+
apply(attrs);
530+
apply(extra_attrs);
496531
}
497532
PassMode::Pair(ref a, ref b) => {
498-
apply(a, None);
499-
apply(b, None);
533+
apply(a);
534+
apply(b);
535+
}
536+
PassMode::Cast(_) => {
537+
apply(&ArgAttributes::new());
500538
}
501-
PassMode::Cast(_) => apply(&ArgAttributes::new(), None),
502539
}
503540
}
504541

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
255255
return;
256256
}
257257
let llval = match self.fn_abi.ret.mode {
258-
PassMode::Ignore | PassMode::Indirect(..) => {
258+
PassMode::Ignore | PassMode::Indirect { .. } => {
259259
bx.ret_void();
260260
return;
261261
}
@@ -1101,7 +1101,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11011101
// Force by-ref if we have to load through a cast pointer.
11021102
let (mut llval, align, by_ref) = match op.val {
11031103
Immediate(_) | Pair(..) => match arg.mode {
1104-
PassMode::Indirect(..) | PassMode::Cast(_) => {
1104+
PassMode::Indirect { .. } | PassMode::Cast(_) => {
11051105
let scratch = PlaceRef::alloca(bx, arg.layout);
11061106
op.val.store(bx, scratch);
11071107
(scratch.llval, scratch.align, true)

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2801,7 +2801,8 @@ where
28012801
for arg in &mut self.args {
28022802
fixup(arg, false);
28032803
}
2804-
if let PassMode::Indirect(ref mut attrs, _) = self.ret.mode {
2804+
if let PassMode::Indirect { ref mut attrs, extra_attrs: _, on_stack: _ } = self.ret.mode
2805+
{
28052806
attrs.set(ArgAttribute::StructRet);
28062807
}
28072808
return;

compiler/rustc_target/src/abi/call/mod.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ pub enum PassMode {
3636
/// a single uniform or a pair of registers.
3737
Cast(CastTarget),
3838
/// Pass the argument indirectly via a hidden pointer.
39-
/// The second value, if any, is for the extra data (vtable or length)
39+
/// The `extra_attrs` value, if any, is for the extra data (vtable or length)
4040
/// which indicates that it refers to an unsized rvalue.
41-
Indirect(ArgAttributes, Option<ArgAttributes>),
41+
/// `on_stack` defines that the the value should be passed at a fixed
42+
/// stack offset in accordance to the ABI rather than passed using a
43+
/// pointer. This corresponds to the `byval` LLVM argument attribute.
44+
Indirect { attrs: ArgAttributes, extra_attrs: Option<ArgAttributes>, on_stack: bool },
4245
}
4346

4447
// Hack to disable non_upper_case_globals only for the bitflags! and not for the rest
@@ -52,7 +55,6 @@ mod attr_impl {
5255
bitflags::bitflags! {
5356
#[derive(Default)]
5457
pub struct ArgAttribute: u16 {
55-
const ByVal = 1 << 0;
5658
const NoAlias = 1 << 1;
5759
const NoCapture = 1 << 2;
5860
const NonNull = 1 << 3;
@@ -460,14 +462,14 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
460462

461463
let extra_attrs = self.layout.is_unsized().then_some(ArgAttributes::new());
462464

463-
self.mode = PassMode::Indirect(attrs, extra_attrs);
465+
self.mode = PassMode::Indirect { attrs, extra_attrs, on_stack: false };
464466
}
465467

466468
pub fn make_indirect_byval(&mut self) {
467469
self.make_indirect();
468470
match self.mode {
469-
PassMode::Indirect(ref mut attrs, _) => {
470-
attrs.set(ArgAttribute::ByVal);
471+
PassMode::Indirect { attrs: _, extra_attrs: _, ref mut on_stack } => {
472+
*on_stack = true;
471473
}
472474
_ => unreachable!(),
473475
}
@@ -500,15 +502,15 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
500502
}
501503

502504
pub fn is_indirect(&self) -> bool {
503-
matches!(self.mode, PassMode::Indirect(..))
505+
matches!(self.mode, PassMode::Indirect {..})
504506
}
505507

506508
pub fn is_sized_indirect(&self) -> bool {
507-
matches!(self.mode, PassMode::Indirect(_, None))
509+
matches!(self.mode, PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ })
508510
}
509511

510512
pub fn is_unsized_indirect(&self) -> bool {
511-
matches!(self.mode, PassMode::Indirect(_, Some(_)))
513+
matches!(self.mode, PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ })
512514
}
513515

514516
pub fn is_ignore(&self) -> bool {
@@ -617,7 +619,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
617619
a => return Err(format!("unrecognized arch \"{}\" in target specification", a)),
618620
}
619621

620-
if let PassMode::Indirect(ref mut attrs, _) = self.ret.mode {
622+
if let PassMode::Indirect { ref mut attrs, extra_attrs: _, on_stack: _ } = self.ret.mode {
621623
attrs.set(ArgAttribute::StructRet);
622624
}
623625

compiler/rustc_target/src/abi/call/x86.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,14 @@ where
9292

9393
for arg in &mut fn_abi.args {
9494
let attrs = match arg.mode {
95-
PassMode::Ignore | PassMode::Indirect(_, None) => continue,
95+
PassMode::Ignore
96+
| PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
97+
continue;
98+
}
9699
PassMode::Direct(ref mut attrs) => attrs,
97-
PassMode::Pair(..) | PassMode::Indirect(_, Some(_)) | PassMode::Cast(_) => {
100+
PassMode::Pair(..)
101+
| PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ }
102+
| PassMode::Cast(_) => {
98103
unreachable!("x86 shouldn't be passing arguments by {:?}", arg.mode)
99104
}
100105
};

0 commit comments

Comments
 (0)