Skip to content

improve va_arg assembly on arm targets #144549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions compiler/rustc_codegen_llvm/src/va_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ fn round_pointer_up_to_alignment<'ll>(
align: Align,
ptr_ty: &'ll Type,
) -> &'ll Value {
let mut ptr_as_int = bx.ptrtoint(addr, bx.cx().type_isize());
ptr_as_int = round_up_to_alignment(bx, ptr_as_int, align);
bx.inttoptr(ptr_as_int, ptr_ty)
let ptr = bx.inbounds_ptradd(addr, bx.const_i32(align.bytes() as i32 - 1));
bx.call_intrinsic(
"llvm.ptrmask",
&[ptr_ty, bx.type_i32()],
&[ptr, bx.const_int(bx.isize_ty, -(align.bytes() as isize) as i64)],
)
}

fn emit_direct_ptr_va_arg<'ll, 'tcx>(
Expand Down Expand Up @@ -905,6 +908,21 @@ pub(super) fn emit_va_arg<'ll, 'tcx>(
)
}
"aarch64" => emit_aapcs_va_arg(bx, addr, target_ty),
"arm" => {
// Types wider than 16 bytes are not currently supported. Clang has special logic for
// such types, but `VaArgSafe` is not implemented for any type that is this large.
assert!(bx.cx.size_of(target_ty).bytes() <= 16);

emit_ptr_va_arg(
bx,
addr,
target_ty,
PassMode::Direct,
SlotSize::Bytes4,
AllowHigherAlign::Yes,
ForceRightAdjust::No,
)
}
"s390x" => emit_s390x_va_arg(bx, addr, target_ty),
"powerpc" => emit_powerpc_va_arg(bx, addr, target_ty),
"powerpc64" | "powerpc64le" => emit_ptr_va_arg(
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
match self.locals[mir::Local::from_usize(1 + va_list_arg_idx)] {
LocalRef::Place(va_list) => {
bx.va_end(va_list.val.llval);

// Explicitly end the lifetime of the `va_list`, this matters for LLVM.
bx.lifetime_end(va_list.val.llval, va_list.layout.size);
}
_ => bug!("C-variadic function must have a `VaList` place"),
}
Expand Down
26 changes: 26 additions & 0 deletions tests/assembly-llvm/c-variadic-arm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//@ assembly-output: emit-asm
//@ compile-flags: -Copt-level=3
//@ only-arm
//@ ignore-thumb
#![no_std]
#![crate_type = "lib"]
#![feature(c_variadic)]

// Check that the assembly that rustc generates matches what clang emits.

#[unsafe(no_mangle)]
unsafe extern "C" fn variadic(a: f64, mut args: ...) -> f64 {
// CHECK-LABEL: variadic
// CHECK: sub sp, sp, #12

// CHECK: vldr
let b = args.arg::<f64>();
// CHECK: vldr
let c = args.arg::<f64>();

// CHECK: vadd.f64
// CHECK: vadd.f64
a + b + c

// CHECK: add sp, sp, #12
}
Loading