Skip to content
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

Set writable and dead_on_unwind attributes for sret arguments #121298

Merged
merged 3 commits into from
Apr 25, 2024
Merged
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
13 changes: 13 additions & 0 deletions compiler/rustc_codegen_llvm/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::attributes;
use crate::builder::Builder;
use crate::context::CodegenCx;
use crate::llvm::{self, Attribute, AttributePlace};
use crate::llvm_util;
use crate::type_::Type;
use crate::type_of::LayoutLlvmExt;
use crate::value::Value;
Expand Down Expand Up @@ -420,6 +421,18 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
cx.type_array(cx.type_i8(), self.ret.layout.size.bytes()),
);
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Argument(i), &[sret]);
if cx.sess().opts.optimize != config::OptLevel::No
&& llvm_util::get_version() >= (18, 0, 0)
{
attributes::apply_to_llfn(
llfn,
llvm::AttributePlace::Argument(i),
&[
llvm::AttributeKind::Writable.create_attr(cx.llcx),
llvm::AttributeKind::DeadOnUnwind.create_attr(cx.llcx),
],
);
}
}
PassMode::Cast { cast, pad_i32: _ } => {
cast.attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, cx, llfn);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ pub enum AttributeKind {
AllocAlign = 39,
SanitizeSafeStack = 40,
FnRetThunkExtern = 41,
Writable = 42,
DeadOnUnwind = 43,
}

/// LLVMIntPredicate
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/LLVMWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ enum LLVMRustAttribute {
AllocAlign = 39,
SanitizeSafeStack = 40,
FnRetThunkExtern = 41,
Writable = 42,
DeadOnUnwind = 43,
};

typedef struct OpaqueRustString *RustStringRef;
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,16 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
return Attribute::SafeStack;
case FnRetThunkExtern:
return Attribute::FnRetThunkExtern;
#if LLVM_VERSION_GE(18, 0)
case Writable:
return Attribute::Writable;
case DeadOnUnwind:
return Attribute::DeadOnUnwind;
#else
case Writable:
case DeadOnUnwind:
report_fatal_error("Not supported on this LLVM version");
#endif
}
report_fatal_error("bad AttributeKind");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/function-arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub fn notunpin_box(x: Box<NotUnpin>) -> Box<NotUnpin> {
x
}

// CHECK: @struct_return(ptr noalias nocapture noundef sret([32 x i8]) align 4 dereferenceable(32){{( %_0)?}})
// CHECK: @struct_return(ptr{{( dead_on_unwind)?}} noalias nocapture noundef{{( writable)?}} sret([32 x i8]) align 4 dereferenceable(32){{( %_0)?}})
#[no_mangle]
pub fn struct_return() -> S {
S {
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/loongarch-abi/loongarch64-lp64d-abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ pub struct IntDoubleInt {
#[no_mangle]
pub extern "C" fn f_int_double_int_s_arg(a: IntDoubleInt) {}

// CHECK: define void @f_ret_int_double_int_s(ptr noalias nocapture noundef sret([24 x i8]) align 8 dereferenceable(24) %_0)
// 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)
#[no_mangle]
pub extern "C" fn f_ret_int_double_int_s() -> IntDoubleInt {
IntDoubleInt { a: 1, b: 2., c: 3 }
Expand Down
6 changes: 4 additions & 2 deletions tests/codegen/maybeuninit-rvo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//@ compile-flags: -O
//@ needs-unwind
//@ min-llvm-version: 18
#![feature(c_unwind)]
#![crate_type = "lib"]

Expand All @@ -23,8 +25,8 @@ extern "C-unwind" {
}

pub fn new_from_uninit_unwind() -> Foo {
// CHECK-LABEL: new_from_uninit
// CHECK: call void @llvm.memcpy.
// CHECK-LABEL: new_from_uninit_unwind
// CHECK-NOT: call void @llvm.memcpy.
let mut x = std::mem::MaybeUninit::uninit();
unsafe {
init_unwind(x.as_mut_ptr());
Expand Down
Loading