explicitly start va_list lifetime

This commit is contained in:
Folkert de Vries 2025-09-03 00:05:50 +02:00
parent 05abce5d05
commit 418900562c
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
3 changed files with 26 additions and 1 deletions

View file

@ -520,7 +520,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
LocalRef::Place(va_list) => {
bx.va_end(va_list.val.llval);
// Explicitly end the lifetime of the `va_list`, this matters for LLVM.
// Explicitly end the lifetime of the `va_list`, improves LLVM codegen.
bx.lifetime_end(va_list.val.llval, va_list.layout.size);
}
_ => bug!("C-variadic function must have a `VaList` place"),

View file

@ -438,6 +438,10 @@ fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
if fx.fn_abi.c_variadic && arg_index == fx.fn_abi.args.len() {
let va_list = PlaceRef::alloca(bx, bx.layout_of(arg_ty));
// Explicitly start the lifetime of the `va_list`, improves LLVM codegen.
bx.lifetime_start(va_list.val.llval, va_list.layout.size);
bx.va_start(va_list.val.llval);
return LocalRef::Place(va_list);

View file

@ -0,0 +1,21 @@
//@ add-core-stubs
//@ compile-flags: -Copt-level=3
#![feature(c_variadic)]
#![crate_type = "lib"]
// Check that `%args` explicitly has its lifetime start and end. Being explicit can improve
// instruction and register selection, see e.g. https://github.com/rust-lang/rust/pull/144549
#[unsafe(no_mangle)]
unsafe extern "C" fn variadic(a: f64, mut args: ...) -> f64 {
// CHECK: call void @llvm.lifetime.start.p0(i64 {{[0-9]+}}, ptr nonnull %args)
// CHECK: call void @llvm.va_start.p0(ptr nonnull %args)
let b = args.arg::<f64>();
let c = args.arg::<f64>();
a + b + c
// CHECK: call void @llvm.va_end.p0(ptr nonnull %args)
// CHECK: call void @llvm.lifetime.end.p0(i64 {{[0-9]+}}, ptr nonnull %args)
}