Skip to content

Commit db11faf

Browse files
committed
Set writable and dead_on_unwind attributes for sret arguments
1 parent 6639672 commit db11faf

File tree

7 files changed

+31
-3
lines changed

7 files changed

+31
-3
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::attributes;
22
use crate::builder::Builder;
33
use crate::context::CodegenCx;
44
use crate::llvm::{self, Attribute, AttributePlace};
5+
use crate::llvm_util;
56
use crate::type_::Type;
67
use crate::type_of::LayoutLlvmExt;
78
use crate::value::Value;
@@ -429,6 +430,18 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
429430
cx.type_array(cx.type_i8(), self.ret.layout.size.bytes()),
430431
);
431432
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Argument(i), &[sret]);
433+
if cx.sess().opts.optimize != config::OptLevel::No
434+
&& llvm_util::get_version() >= (18, 0, 0)
435+
{
436+
attributes::apply_to_llfn(
437+
llfn,
438+
llvm::AttributePlace::Argument(i),
439+
&[
440+
llvm::AttributeKind::Writable.create_attr(cx.llcx),
441+
llvm::AttributeKind::DeadOnUnwind.create_attr(cx.llcx),
442+
],
443+
);
444+
}
432445
}
433446
PassMode::Cast { cast, pad_i32: _ } => {
434447
cast.attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, cx, llfn);

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ pub enum AttributeKind {
200200
AllocAlign = 39,
201201
SanitizeSafeStack = 40,
202202
FnRetThunkExtern = 41,
203+
Writable = 42,
204+
DeadOnUnwind = 43,
203205
}
204206

205207
/// LLVMIntPredicate

compiler/rustc_llvm/llvm-wrapper/LLVMWrapper.h

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ enum LLVMRustAttribute {
9191
AllocAlign = 39,
9292
SanitizeSafeStack = 40,
9393
FnRetThunkExtern = 41,
94+
Writable = 42,
95+
DeadOnUnwind = 43,
9496
};
9597

9698
typedef struct OpaqueRustString *RustStringRef;

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,16 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
288288
return Attribute::SafeStack;
289289
case FnRetThunkExtern:
290290
return Attribute::FnRetThunkExtern;
291+
#if LLVM_VERSION_GE(18, 0)
292+
case Writable:
293+
return Attribute::Writable;
294+
case DeadOnUnwind:
295+
return Attribute::DeadOnUnwind;
296+
#else
297+
case Writable:
298+
case DeadOnUnwind:
299+
report_fatal_error("Not supported on this LLVM version");
300+
#endif
291301
}
292302
report_fatal_error("bad AttributeKind");
293303
}

tests/codegen/function-arguments.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub fn notunpin_box(x: Box<NotUnpin>) -> Box<NotUnpin> {
198198
x
199199
}
200200

201-
// CHECK: @struct_return(ptr noalias nocapture noundef sret([32 x i8]) align 4 dereferenceable(32){{( %_0)?}})
201+
// CHECK: @struct_return(ptr{{( dead_on_unwind)?}} noalias nocapture noundef{{( writable)?}} sret([32 x i8]) align 4 dereferenceable(32){{( %_0)?}})
202202
#[no_mangle]
203203
pub fn struct_return() -> S {
204204
S {

tests/codegen/loongarch-abi/loongarch64-lp64d-abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ pub struct IntDoubleInt {
260260
#[no_mangle]
261261
pub extern "C" fn f_int_double_int_s_arg(a: IntDoubleInt) {}
262262

263-
// CHECK: define void @f_ret_int_double_int_s(ptr noalias nocapture noundef sret([24 x i8]) align 8 dereferenceable(24) %_0)
263+
// CHECK: define void @f_ret_int_double_int_s(ptr{{( dead_on_unwind)?}} noalias nocapture noundef{{( writable)?}} sret([24 x i8]) align 8 dereferenceable(24) %_0)
264264
#[no_mangle]
265265
pub extern "C" fn f_ret_int_double_int_s() -> IntDoubleInt {
266266
IntDoubleInt { a: 1, b: 2., c: 3 }

tests/codegen/maybeuninit-rvo.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ compile-flags: -O
2+
//@ min-llvm-version: 18
23
#![feature(c_unwind)]
34
#![crate_type = "lib"]
45

@@ -24,7 +25,7 @@ extern "C-unwind" {
2425

2526
pub fn new_from_uninit_unwind() -> Foo {
2627
// CHECK-LABEL: new_from_uninit
27-
// CHECK: call void @llvm.memcpy.
28+
// CHECK-NOT: call void @llvm.memcpy.
2829
let mut x = std::mem::MaybeUninit::uninit();
2930
unsafe {
3031
init_unwind(x.as_mut_ptr());

0 commit comments

Comments
 (0)