Skip to content
Closed
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
18 changes: 6 additions & 12 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,16 +986,6 @@ impl<'a> MethodDef<'a> {
f(cx, span, &substructure)
}

fn get_ret_ty(
&self,
cx: &ExtCtxt<'_>,
trait_: &TraitDef<'_>,
generics: &Generics,
type_ident: Ident,
) -> Box<ast::Ty> {
self.ret_ty.to_ty(cx, trait_.span, type_ident, generics)
}

fn is_static(&self) -> bool {
!self.explicit_self
}
Expand Down Expand Up @@ -1068,10 +1058,14 @@ impl<'a> MethodDef<'a> {
self_arg.into_iter().chain(nonself_args).collect()
};

let ret_type = self.get_ret_ty(cx, trait_, generics, type_ident);
let ret_type = if let Ty::Unit = &self.ret_ty {
ast::FnRetTy::Default(span)
} else {
ast::FnRetTy::Ty(self.ret_ty.to_ty(cx, span, type_ident, generics))
};

let method_ident = Ident::new(self.name, span);
let fn_decl = cx.fn_decl(args, ast::FnRetTy::Ty(ret_type));
let fn_decl = cx.fn_decl(args, ret_type);
let body_block = body.into_block(cx, span);

let trait_lo_sp = span.shrink_to_lo();
Expand Down
27 changes: 14 additions & 13 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1397,12 +1397,12 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
fn call(
&mut self,
llty: &'ll Type,
fn_call_attrs: Option<&CodegenFnAttrs>,
caller_attrs: Option<&CodegenFnAttrs>,
fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
llfn: &'ll Value,
args: &[&'ll Value],
funclet: Option<&Funclet<'ll>>,
instance: Option<Instance<'tcx>>,
callee_instance: Option<Instance<'tcx>>,
) -> &'ll Value {
debug!("call {:?} with args ({:?})", llfn, args);

Expand All @@ -1414,10 +1414,10 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
}

// Emit CFI pointer type membership test
self.cfi_type_test(fn_call_attrs, fn_abi, instance, llfn);
self.cfi_type_test(caller_attrs, fn_abi, callee_instance, llfn);

// Emit KCFI operand bundle
let kcfi_bundle = self.kcfi_operand_bundle(fn_call_attrs, fn_abi, instance, llfn);
let kcfi_bundle = self.kcfi_operand_bundle(caller_attrs, fn_abi, callee_instance, llfn);
if let Some(kcfi_bundle) = kcfi_bundle.as_ref().map(|b| b.as_ref()) {
bundles.push(kcfi_bundle);
}
Expand All @@ -1435,17 +1435,17 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
)
};

if let Some(instance) = instance {
if let Some(callee_instance) = callee_instance {
// Attributes on the function definition being called
let fn_defn_attrs = self.cx.tcx.codegen_fn_attrs(instance.def_id());
if let Some(fn_call_attrs) = fn_call_attrs
let callee_attrs = self.cx.tcx.codegen_fn_attrs(callee_instance.def_id());
if let Some(caller_attrs) = caller_attrs
// If there is an inline attribute and a target feature that matches
// we will add the attribute to the callsite otherwise we'll omit
// this and not add the attribute to prevent soundness issues.
&& let Some(inlining_rule) = attributes::inline_attr(&self.cx, self.cx.tcx, instance)
&& let Some(inlining_rule) = attributes::inline_attr(&self.cx, self.cx.tcx, callee_instance)
&& self.cx.tcx.is_target_feature_call_safe(
&fn_defn_attrs.target_features,
&fn_call_attrs.target_features.iter().cloned().chain(
&callee_attrs.target_features,
&caller_attrs.target_features.iter().cloned().chain(
self.cx.tcx.sess.target_features.iter().map(|feat| TargetFeature {
name: *feat,
kind: TargetFeatureKind::Implied,
Expand All @@ -1470,14 +1470,15 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
fn tail_call(
&mut self,
llty: Self::Type,
fn_attrs: Option<&CodegenFnAttrs>,
caller_attrs: Option<&CodegenFnAttrs>,
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
llfn: Self::Value,
args: &[Self::Value],
funclet: Option<&Self::Funclet>,
instance: Option<Instance<'tcx>>,
callee_instance: Option<Instance<'tcx>>,
) {
let call = self.call(llty, fn_attrs, Some(fn_abi), llfn, args, funclet, instance);
let call =
self.call(llty, caller_attrs, Some(fn_abi), llfn, args, funclet, callee_instance);
llvm::LLVMSetTailCallKind(call, llvm::TailCallKind::MustTail);

match &fn_abi.ret.mode {
Expand Down
19 changes: 13 additions & 6 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
// do an invoke, otherwise do a call.
let fn_ty = bx.fn_decl_backend_type(fn_abi);

let fn_attrs = if bx.tcx().def_kind(fx.instance.def_id()).has_codegen_attrs() {
let caller_attrs = if bx.tcx().def_kind(fx.instance.def_id()).has_codegen_attrs() {
Some(bx.tcx().codegen_instance_attrs(fx.instance.def))
} else {
None
};
let fn_attrs = fn_attrs.as_deref();
let caller_attrs = caller_attrs.as_deref();

if !fn_abi.can_unwind {
unwind = mir::UnwindAction::Unreachable;
Expand Down Expand Up @@ -233,7 +233,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
};

if kind == CallKind::Tail {
bx.tail_call(fn_ty, fn_attrs, fn_abi, fn_ptr, llargs, self.funclet(fx), instance);
bx.tail_call(fn_ty, caller_attrs, fn_abi, fn_ptr, llargs, self.funclet(fx), instance);
return MergingSucc::False;
}

Expand All @@ -245,7 +245,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
};
let invokeret = bx.invoke(
fn_ty,
fn_attrs,
caller_attrs,
Some(fn_abi),
fn_ptr,
llargs,
Expand All @@ -268,8 +268,15 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
}
MergingSucc::False
} else {
let llret =
bx.call(fn_ty, fn_attrs, Some(fn_abi), fn_ptr, llargs, self.funclet(fx), instance);
let llret = bx.call(
fn_ty,
caller_attrs,
Some(fn_abi),
fn_ptr,
llargs,
self.funclet(fx),
instance,
);
if fx.mir[self.bb].is_cleanup {
bx.apply_attrs_to_cleanup_callsite(llret);
}
Expand Down
19 changes: 11 additions & 8 deletions compiler/rustc_codegen_ssa/src/traits/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,13 @@ pub trait BuilderMethods<'a, 'tcx>:
///
/// ## Arguments
///
/// The `fn_attrs`, `fn_abi`, and `instance` arguments are Options because they are advisory.
/// They relate to optional codegen enhancements like LLVM CFI, and do not affect ABI per se.
/// Any ABI-related transformations should be handled by different, earlier stages of codegen.
/// For instance, in the caller of `BuilderMethods::call`.
/// `caller_attrs` are the attributes of the surrounding caller; they have nothing to do with
/// the callee.
///
/// The `caller_attrs`, `fn_abi`, and `callee_instance` arguments are Options because they are
/// advisory. They relate to optional codegen enhancements like LLVM CFI, and do not affect ABI
/// per se. Any ABI-related transformations should be handled by different, earlier stages of
/// codegen. For instance, in the caller of `BuilderMethods::call`.
///
/// This means that a codegen backend which disregards `fn_attrs`, `fn_abi`, and `instance`
/// should still do correct codegen, and code should not be miscompiled if they are omitted.
Expand All @@ -620,23 +623,23 @@ pub trait BuilderMethods<'a, 'tcx>:
fn call(
&mut self,
llty: Self::Type,
fn_attrs: Option<&CodegenFnAttrs>,
caller_attrs: Option<&CodegenFnAttrs>,
fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
fn_val: Self::Value,
args: &[Self::Value],
funclet: Option<&Self::Funclet>,
instance: Option<Instance<'tcx>>,
callee_instance: Option<Instance<'tcx>>,
) -> Self::Value;

fn tail_call(
&mut self,
llty: Self::Type,
fn_attrs: Option<&CodegenFnAttrs>,
caller_attrs: Option<&CodegenFnAttrs>,
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
llfn: Self::Value,
args: &[Self::Value],
funclet: Option<&Self::Funclet>,
instance: Option<Instance<'tcx>>,
callee_instance: Option<Instance<'tcx>>,
);

fn zext(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,12 @@ LLVMRustCreateAttrNoValue(LLVMContextRef C, LLVMRustAttributeKind RustAttr) {
*unwrap(C), CaptureInfo(CaptureComponents::Address |
CaptureComponents::ReadProvenance)));
}
#endif
#if LLVM_VERSION_GE(23, 0)
if (RustAttr == LLVMRustAttributeKind::DeadOnReturn) {
return wrap(Attribute::getWithDeadOnReturnInfo(*unwrap(C),
llvm::DeadOnReturnInfo()));
}
#endif
return wrap(Attribute::get(*unwrap(C), fromRust(RustAttr)));
}
Expand Down
15 changes: 9 additions & 6 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
)]

use crate::ffi::va_list::{VaArgSafe, VaList};
use crate::marker::{ConstParamTy, Destruct, DiscriminantKind, PointeeSized, Tuple};
use crate::marker::{ConstParamTy, DiscriminantKind, PointeeSized, Tuple};
use crate::{mem, ptr};

mod bounds;
Expand Down Expand Up @@ -483,11 +483,14 @@ pub const fn unlikely(b: bool) -> bool {
#[rustc_nounwind]
#[miri::intrinsic_fallback_is_spec]
#[inline]
pub const fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T
where
T: [const] Destruct,
{
if b { true_val } else { false_val }
pub const fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T {
if b {
forget(false_val);
true_val
} else {
forget(true_val);
false_val
}
}

/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
Expand Down
10 changes: 2 additions & 8 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2103,10 +2103,7 @@ impl<T> Option<&T> {
where
T: Clone,
{
match self {
Some(t) => Some(t.clone()),
None => None,
}
self.map(T::clone)
}
}

Expand Down Expand Up @@ -2154,10 +2151,7 @@ impl<T> Option<&mut T> {
where
T: Clone,
{
match self {
Some(t) => Some(t.clone()),
None => None,
}
self.as_deref().map(T::clone)
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/tools/miri/tests/pass/intrinsics/select-unpredictable-drop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Check that `select_unpredictable` properly forgets the value it does not select.
#![feature(core_intrinsics)]
use std::cell::Cell;
use std::intrinsics::select_unpredictable;

fn main() {
let (true_val, false_val) = (Cell::new(false), Cell::new(false));
_ = select_unpredictable(true, TraceDrop(&true_val), TraceDrop(&false_val));
assert!(true_val.get());
assert!(!false_val.get());
}

struct TraceDrop<'a>(&'a Cell<bool>);

impl<'a> Drop for TraceDrop<'a> {
fn drop(&mut self) {
self.0.set(true);
}
}
6 changes: 3 additions & 3 deletions tests/codegen-llvm/addr-of-mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Test for the absence of `readonly` on the argument when it is mutated via `&raw const`.
// See <https://github.com/rust-lang/rust/issues/111502>.

// CHECK: i8 @foo(ptr{{( dead_on_return)?}} noalias noundef align 1{{( captures\(address\))?}} dereferenceable(128) %x)
// CHECK: i8 @foo(ptr{{( dead_on_return)?}} noalias noundef align 1{{( captures\(address\))?}}{{( dead_on_return)?}} dereferenceable(128) %x)
#[no_mangle]
pub fn foo(x: [u8; 128]) -> u8 {
let ptr = core::ptr::addr_of!(x).cast_mut();
Expand All @@ -15,7 +15,7 @@ pub fn foo(x: [u8; 128]) -> u8 {
x[0]
}

// CHECK: i1 @second(ptr{{( dead_on_return)?}} noalias noundef align {{[0-9]+}}{{( captures\(address\))?}} dereferenceable({{[0-9]+}}) %a_ptr_and_b)
// CHECK: i1 @second(ptr{{( dead_on_return)?}} noalias noundef align {{[0-9]+}}{{( captures\(address\))?}}{{( dead_on_return)?}} dereferenceable({{[0-9]+}}) %a_ptr_and_b)
#[no_mangle]
pub unsafe fn second(a_ptr_and_b: (*mut (i32, bool), (i64, bool))) -> bool {
let b_bool_ptr = core::ptr::addr_of!(a_ptr_and_b.1.1).cast_mut();
Expand All @@ -24,7 +24,7 @@ pub unsafe fn second(a_ptr_and_b: (*mut (i32, bool), (i64, bool))) -> bool {
}

// If going through a deref (and there are no other mutating accesses), then `readonly` is fine.
// CHECK: i1 @third(ptr{{( dead_on_return)?}} noalias noundef readonly align {{[0-9]+}}{{( captures\(none\))?}} dereferenceable({{[0-9]+}}) %a_ptr_and_b)
// CHECK: i1 @third(ptr{{( dead_on_return)?}} noalias noundef readonly align {{[0-9]+}}{{( captures\(none\))?}}{{( dead_on_return)?}} dereferenceable({{[0-9]+}}) %a_ptr_and_b)
#[no_mangle]
pub unsafe fn third(a_ptr_and_b: (*mut (i32, bool), (i64, bool))) -> bool {
let b_bool_ptr = core::ptr::addr_of!((*a_ptr_and_b.0).1).cast_mut();
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen-llvm/function-arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub fn mutable_notunpin_borrow(_: &mut NotUnpin) {}
#[no_mangle]
pub fn notunpin_borrow(_: &NotUnpin) {}

// CHECK: @indirect_struct(ptr{{( dead_on_return)?}} noalias noundef readonly align 4{{( captures\(none\))?}} dereferenceable(32) %_1)
// CHECK: @indirect_struct(ptr{{( dead_on_return)?}} noalias noundef readonly align 4{{( captures\(none\))?}}{{( dead_on_return)?}} dereferenceable(32) %_1)
#[no_mangle]
pub fn indirect_struct(_: S) {}

Expand Down
2 changes: 1 addition & 1 deletion tests/codegen-llvm/loongarch-abi/loongarch64-lp64d-abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ pub struct IntDoubleInt {
c: i32,
}

// CHECK: define void @f_int_double_int_s_arg(ptr{{( dead_on_return)?}} noalias noundef align 8{{( captures\(address\))?}} dereferenceable(24) %a)
// CHECK: define void @f_int_double_int_s_arg(ptr{{( dead_on_return)?}} noalias noundef align 8{{( captures\(address\))?}}{{( dead_on_return)?}} dereferenceable(24) %a)
#[no_mangle]
pub extern "C" fn f_int_double_int_s_arg(a: IntDoubleInt) {}

Expand Down
Loading
Loading