From 11171762e8a69160c3433f77697c3f0103aed35e Mon Sep 17 00:00:00 2001 From: hkalbasi Date: Sun, 21 Sep 2025 02:05:01 +0330 Subject: [PATCH 1/4] [Experiment] Do not emit noalias for inline functions --- compiler/rustc_ty_utils/src/abi.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index e4ed084b073b6..0b30d7951565d 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -3,6 +3,7 @@ use std::iter; use rustc_abi::Primitive::Pointer; use rustc_abi::{BackendRepr, ExternAbi, PointerKind, Scalar, Size}; use rustc_hir as hir; +use rustc_hir::attrs::InlineAttr; use rustc_hir::lang_items::LangItem; use rustc_middle::bug; use rustc_middle::query::Providers; @@ -275,6 +276,7 @@ fn arg_attrs_for_rust_scalar<'tcx>( offset: Size, is_return: bool, drop_target_pointee: Option>, + is_inline: bool, ) -> ArgAttributes { let mut attrs = ArgAttributes::new(); @@ -348,9 +350,10 @@ fn arg_attrs_for_rust_scalar<'tcx>( PointerKind::MutableRef { unpin } => unpin && noalias_mut_ref, PointerKind::Box { unpin, global } => unpin && global && noalias_for_box, }; + // We can never add `noalias` in return position; that LLVM attribute has some very surprising semantics // (see ). - if no_alias && !is_return { + if no_alias && !is_inline && !is_return { attrs.set(ArgAttribute::NoAlias); } @@ -509,6 +512,11 @@ fn fn_abi_new_uncached<'tcx>( extra_args }; + let is_inline = determined_fn_def_id.is_some_and(|def_id| { + let inline_attrs = tcx.codegen_fn_attrs(def_id).inline; + inline_attrs == InlineAttr::Hint || inline_attrs == InlineAttr::Always + }); + let is_drop_in_place = determined_fn_def_id.is_some_and(|def_id| { tcx.is_lang_item(def_id, LangItem::DropInPlace) || tcx.is_lang_item(def_id, LangItem::AsyncDropInPlace) @@ -535,7 +543,15 @@ fn fn_abi_new_uncached<'tcx>( }; let mut arg = ArgAbi::new(cx, layout, |layout, scalar, offset| { - arg_attrs_for_rust_scalar(*cx, scalar, *layout, offset, is_return, drop_target_pointee) + arg_attrs_for_rust_scalar( + *cx, + scalar, + *layout, + offset, + is_return, + drop_target_pointee, + is_inline, + ) }); if arg.layout.is_zst() { From f089cf8da0e33fd6bbe8ee04534f26f34a763d44 Mon Sep 17 00:00:00 2001 From: hkalbasi Date: Sun, 21 Sep 2025 11:03:23 +0330 Subject: [PATCH 2/4] Do it only for #[inline(always)] --- compiler/rustc_ty_utils/src/abi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index 0b30d7951565d..a2d2e0135e432 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -514,7 +514,7 @@ fn fn_abi_new_uncached<'tcx>( let is_inline = determined_fn_def_id.is_some_and(|def_id| { let inline_attrs = tcx.codegen_fn_attrs(def_id).inline; - inline_attrs == InlineAttr::Hint || inline_attrs == InlineAttr::Always + inline_attrs == InlineAttr::Always }); let is_drop_in_place = determined_fn_def_id.is_some_and(|def_id| { From 8b85d8eb9fed35b3ee73d0a134c271de32eb987d Mon Sep 17 00:00:00 2001 From: hkalbasi Date: Mon, 22 Sep 2025 02:20:27 +0330 Subject: [PATCH 3/4] Do it for functions involving raw pointers --- compiler/rustc_ty_utils/src/abi.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index a2d2e0135e432..8f98b7ac27309 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -3,7 +3,6 @@ use std::iter; use rustc_abi::Primitive::Pointer; use rustc_abi::{BackendRepr, ExternAbi, PointerKind, Scalar, Size}; use rustc_hir as hir; -use rustc_hir::attrs::InlineAttr; use rustc_hir::lang_items::LangItem; use rustc_middle::bug; use rustc_middle::query::Providers; @@ -276,7 +275,7 @@ fn arg_attrs_for_rust_scalar<'tcx>( offset: Size, is_return: bool, drop_target_pointee: Option>, - is_inline: bool, + involves_raw_ptr: bool, ) -> ArgAttributes { let mut attrs = ArgAttributes::new(); @@ -353,7 +352,7 @@ fn arg_attrs_for_rust_scalar<'tcx>( // We can never add `noalias` in return position; that LLVM attribute has some very surprising semantics // (see ). - if no_alias && !is_inline && !is_return { + if no_alias && !involves_raw_ptr && !is_return { attrs.set(ArgAttribute::NoAlias); } @@ -512,10 +511,13 @@ fn fn_abi_new_uncached<'tcx>( extra_args }; - let is_inline = determined_fn_def_id.is_some_and(|def_id| { - let inline_attrs = tcx.codegen_fn_attrs(def_id).inline; - inline_attrs == InlineAttr::Always - }); + let involves_raw_ptr = inputs + .iter() + .copied() + .chain(extra_args.iter().copied()) + .chain(caller_location) + .chain(Some(sig.output())) + .any(|ty| matches!(ty.kind(), ty::RawPtr(_, _))); let is_drop_in_place = determined_fn_def_id.is_some_and(|def_id| { tcx.is_lang_item(def_id, LangItem::DropInPlace) @@ -550,7 +552,7 @@ fn fn_abi_new_uncached<'tcx>( offset, is_return, drop_target_pointee, - is_inline, + involves_raw_ptr, ) }); From 60d03030884a04ae241ef0d58a31dd82bd94cde3 Mon Sep 17 00:00:00 2001 From: hkalbasi Date: Wed, 24 Sep 2025 02:19:35 +0330 Subject: [PATCH 4/4] Bring back noalias for drop glue and std::panic::Location --- compiler/rustc_ty_utils/src/abi.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index 8f98b7ac27309..39a6d64920154 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -275,7 +275,8 @@ fn arg_attrs_for_rust_scalar<'tcx>( offset: Size, is_return: bool, drop_target_pointee: Option>, - involves_raw_ptr: bool, + mut involves_raw_ptr: bool, + _instance: Option>, ) -> ArgAttributes { let mut attrs = ArgAttributes::new(); @@ -306,6 +307,7 @@ fn arg_attrs_for_rust_scalar<'tcx>( Some(kind) } else if let Some(pointee) = drop_target_pointee { // The argument to `drop_in_place` is semantically equivalent to a mutable reference. + involves_raw_ptr = false; Some(PointerKind::MutableRef { unpin: pointee.is_unpin(tcx, cx.typing_env) }) } else { None @@ -356,6 +358,10 @@ fn arg_attrs_for_rust_scalar<'tcx>( attrs.set(ArgAttribute::NoAlias); } + //if no_alias && involves_raw_ptr && !is_return { + // println!("xxxxxxx {instance:?}\nyyyyyyy {:?}", layout.ty); + //} + if matches!(kind, PointerKind::SharedRef { frozen: true }) && !is_return { attrs.set(ArgAttribute::ReadOnly); attrs.set(ArgAttribute::CapturesReadOnly); @@ -528,6 +534,7 @@ fn fn_abi_new_uncached<'tcx>( let span = tracing::debug_span!("arg_of"); let _entered = span.enter(); let is_return = arg_idx.is_none(); + let is_caller_location = arg_idx.is_some_and(|i| i >= inputs.len() + extra_args.len()); let is_drop_target = is_drop_in_place && arg_idx == Some(0); let drop_target_pointee = is_drop_target.then(|| match ty.kind() { ty::RawPtr(ty, _) => *ty, @@ -552,7 +559,8 @@ fn fn_abi_new_uncached<'tcx>( offset, is_return, drop_target_pointee, - involves_raw_ptr, + involves_raw_ptr && !is_caller_location, + instance, ) });