From da2bbfbbec6373e9fde803c9f62eaec2cf6df72c Mon Sep 17 00:00:00 2001 From: Dominik Schwaiger Date: Thu, 16 Apr 2026 12:29:39 +0000 Subject: [PATCH] add llvm writable attribute conditionally --- .../src/attributes/rustc_internal.rs | 15 ++++++++++ compiler/rustc_attr_parsing/src/context.rs | 1 + compiler/rustc_codegen_llvm/src/abi.rs | 3 +- compiler/rustc_feature/src/builtin_attrs.rs | 4 +++ .../rustc_hir/src/attrs/data_structures.rs | 3 ++ .../rustc_hir/src/attrs/encode_cross_crate.rs | 1 + compiler/rustc_interface/src/tests.rs | 1 + compiler/rustc_passes/src/check_attr.rs | 1 + compiler/rustc_session/src/options.rs | 2 ++ compiler/rustc_span/src/symbol.rs | 1 + compiler/rustc_target/src/callconv/mod.rs | 5 ++-- compiler/rustc_ty_utils/src/abi.rs | 19 +++++++++++- library/core/src/slice/mod.rs | 1 + library/core/src/str/mod.rs | 1 + .../src/compiler-flags/llvm-writable.md | 11 +++++++ tests/codegen-llvm/llvm-writable.rs | 30 +++++++++++++++++++ 16 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/doc/unstable-book/src/compiler-flags/llvm-writable.md create mode 100644 tests/codegen-llvm/llvm-writable.rs diff --git a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs index 15bcffe529a0..7bf7c6397f13 100644 --- a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs +++ b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs @@ -527,6 +527,21 @@ impl NoArgsAttributeParser for RustcNoMirInlineParser { const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNoMirInline; } +pub(crate) struct RustcNoWritableParser; + +impl NoArgsAttributeParser for RustcNoWritableParser { + const PATH: &[Symbol] = &[sym::rustc_no_writable]; + const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[ + Allow(Target::Fn), + Allow(Target::Closure), + Allow(Target::Method(MethodKind::Inherent)), + Allow(Target::Method(MethodKind::TraitImpl)), + Allow(Target::Method(MethodKind::Trait { body: true })), + ]); + const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNoWritable; +} + pub(crate) struct RustcLintQueryInstabilityParser; impl NoArgsAttributeParser for RustcLintQueryInstabilityParser { diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs index 3f722bef5bf3..08c0b3d70c1a 100644 --- a/compiler/rustc_attr_parsing/src/context.rs +++ b/compiler/rustc_attr_parsing/src/context.rs @@ -314,6 +314,7 @@ mod late { Single>, Single>, Single>, + Single>, Single>, Single>, Single>, diff --git a/compiler/rustc_codegen_llvm/src/abi.rs b/compiler/rustc_codegen_llvm/src/abi.rs index d474ba2d4ec7..dcde960258a5 100644 --- a/compiler/rustc_codegen_llvm/src/abi.rs +++ b/compiler/rustc_codegen_llvm/src/abi.rs @@ -38,11 +38,12 @@ fn apply_attrs_to_callsite( const ABI_AFFECTING_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 1] = [(ArgAttribute::InReg, llvm::AttributeKind::InReg)]; -const OPTIMIZATION_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 4] = [ +const OPTIMIZATION_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 5] = [ (ArgAttribute::NoAlias, llvm::AttributeKind::NoAlias), (ArgAttribute::NonNull, llvm::AttributeKind::NonNull), (ArgAttribute::ReadOnly, llvm::AttributeKind::ReadOnly), (ArgAttribute::NoUndef, llvm::AttributeKind::NoUndef), + (ArgAttribute::Writable, llvm::AttributeKind::Writable), ]; const CAPTURES_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 3] = [ diff --git a/compiler/rustc_feature/src/builtin_attrs.rs b/compiler/rustc_feature/src/builtin_attrs.rs index 1c1bca0cbc3c..6e127d12c721 100644 --- a/compiler/rustc_feature/src/builtin_attrs.rs +++ b/compiler/rustc_feature/src/builtin_attrs.rs @@ -955,6 +955,10 @@ pub struct BuiltinAttribute { rustc_must_match_exhaustively, "enums with `#[rustc_must_match_exhaustively]` must be matched on with a match block that mentions all variants explicitly" ), + rustc_attr!( + rustc_no_writable, + "`#[rustc_no_writable]` stops the compiler from considering mutable reference arguments of this function as implicitly writable" + ), // ========================================================================== // Internal attributes, Testing: diff --git a/compiler/rustc_hir/src/attrs/data_structures.rs b/compiler/rustc_hir/src/attrs/data_structures.rs index f4bb5c0c3819..31b7287d774a 100644 --- a/compiler/rustc_hir/src/attrs/data_structures.rs +++ b/compiler/rustc_hir/src/attrs/data_structures.rs @@ -1508,6 +1508,9 @@ pub enum AttributeKind { /// Represents `#[rustc_no_mir_inline]` RustcNoMirInline, + /// Represents `#[rustc_no_writable]` + RustcNoWritable, + /// Represents `#[rustc_non_const_trait_method]`. RustcNonConstTraitMethod, diff --git a/compiler/rustc_hir/src/attrs/encode_cross_crate.rs b/compiler/rustc_hir/src/attrs/encode_cross_crate.rs index 239c9d0ca530..ad4d0728888b 100644 --- a/compiler/rustc_hir/src/attrs/encode_cross_crate.rs +++ b/compiler/rustc_hir/src/attrs/encode_cross_crate.rs @@ -163,6 +163,7 @@ pub fn encode_cross_crate(&self) -> EncodeCrossCrate { RustcNoImplicitAutorefs => Yes, RustcNoImplicitBounds => No, RustcNoMirInline => Yes, + RustcNoWritable => Yes, RustcNonConstTraitMethod => No, // should be reported via other queries like `constness` RustcNonnullOptimizationGuaranteed => Yes, RustcNounwind => No, diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index 417cde119c21..e54f68b6391e 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -817,6 +817,7 @@ macro_rules! tracked { tracked!(lint_llvm_ir, true); tracked!(llvm_module_flag, vec![("bar".to_string(), 123, "max".to_string())]); tracked!(llvm_plugins, vec![String::from("plugin_name")]); + tracked!(llvm_writable, true); tracked!(location_detail, LocationDetail { file: true, line: false, column: false }); tracked!(maximal_hir_to_mir_coverage, true); tracked!(merge_functions, Some(MergeFunctions::Disabled)); diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 6ee8db1703b8..24e9b71de591 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -350,6 +350,7 @@ fn check_attributes( | AttributeKind::RustcNoImplicitAutorefs | AttributeKind::RustcNoImplicitBounds | AttributeKind::RustcNoMirInline + | AttributeKind::RustcNoWritable | AttributeKind::RustcNonConstTraitMethod | AttributeKind::RustcNonnullOptimizationGuaranteed | AttributeKind::RustcNounwind diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 9fc6036b98b3..0913225f0d94 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -2530,6 +2530,8 @@ pub(crate) fn parse_assert_incr_state( "a list LLVM plugins to enable (space separated)"), llvm_time_trace: bool = (false, parse_bool, [UNTRACKED], "generate JSON tracing data file from LLVM data (default: no)"), + llvm_writable: bool = (false, parse_bool, [TRACKED], + "emit the LLVM writable attribute for mutable reference arguments (default: no)"), location_detail: LocationDetail = (LocationDetail::all(), parse_location_detail, [TRACKED], "what location details should be tracked when using caller_location, either \ `none`, or a comma separated list of location details, for which \ diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 9cd66bc1604d..d2824011a441 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1769,6 +1769,7 @@ rustc_no_implicit_autorefs, rustc_no_implicit_bounds, rustc_no_mir_inline, + rustc_no_writable, rustc_non_const_trait_method, rustc_nonnull_optimization_guaranteed, rustc_nounwind, diff --git a/compiler/rustc_target/src/callconv/mod.rs b/compiler/rustc_target/src/callconv/mod.rs index 7dc270795281..f8c82faaf8bd 100644 --- a/compiler/rustc_target/src/callconv/mod.rs +++ b/compiler/rustc_target/src/callconv/mod.rs @@ -110,9 +110,9 @@ mod attr_impl { // The subset of llvm::Attribute needed for arguments, packed into a bitfield. #[derive(Clone, Copy, Default, Hash, PartialEq, Eq, HashStable_Generic)] - pub struct ArgAttribute(u8); + pub struct ArgAttribute(u16); bitflags::bitflags! { - impl ArgAttribute: u8 { + impl ArgAttribute: u16 { const CapturesNone = 0b111; const CapturesAddress = 0b110; const CapturesReadOnly = 0b100; @@ -121,6 +121,7 @@ impl ArgAttribute: u8 { const ReadOnly = 1 << 5; const InReg = 1 << 6; const NoUndef = 1 << 7; + const Writable = 1 << 8; } } rustc_data_structures::external_bitflags_debug! { ArgAttribute } diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index 5008794bcb19..bc9c5b55cc6d 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -2,8 +2,8 @@ use rustc_abi::Primitive::Pointer; use rustc_abi::{Align, BackendRepr, ExternAbi, PointerKind, Scalar, Size}; -use rustc_hir as hir; use rustc_hir::lang_items::LangItem; +use rustc_hir::{self as hir, find_attr}; use rustc_middle::bug; use rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs; use rustc_middle::query::Providers; @@ -355,6 +355,7 @@ fn arg_attrs_for_rust_scalar<'tcx>( offset: Size, is_return: bool, drop_target_pointee: Option>, + determined_fn_def_id: Option, ) -> ArgAttributes { let mut attrs = ArgAttributes::new(); @@ -432,6 +433,21 @@ fn arg_attrs_for_rust_scalar<'tcx>( attrs.set(ArgAttribute::NoAlias); } + // Set writable if no_alias is set, it's a mutable reference and the feature is enabled. + if tcx.sess.opts.unstable_opts.llvm_writable + && matches!(kind, PointerKind::MutableRef { unpin: true }) + && !is_return + { + let rustc_no_writable = match determined_fn_def_id { + Some(def_id) => find_attr!(tcx, def_id, RustcNoWritable), + None => true, // If no def_id exists, we make the conservative choice and disable the feature. + }; + + if !rustc_no_writable { + attrs.set(ArgAttribute::Writable); + } + } + if matches!(kind, PointerKind::SharedRef { frozen: true }) && !is_return { attrs.set(ArgAttribute::ReadOnly); attrs.set(ArgAttribute::CapturesReadOnly); @@ -624,6 +640,7 @@ fn fn_abi_new_uncached<'tcx>( // Only set `drop_target_pointee` for the data part of a wide pointer. // See `arg_attrs_for_rust_scalar` docs for more information. drop_target_pointee.filter(|_| offset == Size::ZERO), + determined_fn_def_id, ) })) }; diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index d3c37e3caf6a..a838ba009b48 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -754,6 +754,7 @@ pub const fn as_ptr(&self) -> *const T { #[rustc_as_ptr] #[inline(always)] #[must_use] + #[rustc_no_writable] pub const fn as_mut_ptr(&mut self) -> *mut T { self as *mut [T] as *mut T } diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 73fb4c6b2c87..5af399ab1b34 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -589,6 +589,7 @@ pub const fn as_ptr(&self) -> *const u8 { #[rustc_as_ptr] #[must_use] #[inline(always)] + #[rustc_no_writable] pub const fn as_mut_ptr(&mut self) -> *mut u8 { self as *mut str as *mut u8 } diff --git a/src/doc/unstable-book/src/compiler-flags/llvm-writable.md b/src/doc/unstable-book/src/compiler-flags/llvm-writable.md new file mode 100644 index 000000000000..22fd01bbe076 --- /dev/null +++ b/src/doc/unstable-book/src/compiler-flags/llvm-writable.md @@ -0,0 +1,11 @@ +# `llvm-writable` + +--- + +Setting this flag will allow the compiler to insert the [writable](https://llvm.org/docs/LangRef.html#writable) LLVM flag. +This allows for more optimizations but also introduces more Undefined Behaviour. +To be more precise, mutable reference function arguments are now considered to be always writable, which means the compiler may insert writes to those references even if the original code contained no such writes. +The attribute `#[rustc_no_writable]` can be used to disable the optimization on a per-function basis. + +The [Miri](https://github.com/rust-lang/miri) tool can be used to detect some problematic cases. +However, note that when using Tree Borrows, you must set `-Zmiri-tree-borrows-implicit-writes` to ensure that the UB arising from these implicit writes is detected. diff --git a/tests/codegen-llvm/llvm-writable.rs b/tests/codegen-llvm/llvm-writable.rs new file mode 100644 index 000000000000..ea245fc3a6e7 --- /dev/null +++ b/tests/codegen-llvm/llvm-writable.rs @@ -0,0 +1,30 @@ +//! The tests here test that the `-Zllvm-writable` flag and +//! the `#[rustc_no_writable]` attribute have the desired effect. +//@ compile-flags: -Copt-level=3 -C no-prepopulate-passes -Zllvm-writable +#![crate_type = "lib"] +#![feature(rustc_attrs, unsafe_pinned)] + +// CHECK: @mutable_borrow(ptr noalias noundef writable align 4 dereferenceable(4) %_1) +#[no_mangle] +pub fn mutable_borrow(_: &mut i32) {} + +// CHECK: @mutable_unsafe_borrow(ptr noalias noundef writable align 2 dereferenceable(2) %_1) +#[no_mangle] +pub fn mutable_unsafe_borrow(_: &mut std::cell::UnsafeCell) {} + +// CHECK: @option_borrow_mut(ptr noalias noundef writable align 4 dereferenceable_or_null(4) %_1) +#[no_mangle] +pub fn option_borrow_mut(_: Option<&mut i32>) {} + +// CHECK: @box_moved(ptr noalias noundef nonnull align 4 %0) +#[no_mangle] +pub fn box_moved(_: Box) {} + +// CHECK: @unsafe_pinned_borrow_mut(ptr noundef nonnull align 4 %_1) +#[no_mangle] +pub fn unsafe_pinned_borrow_mut(_: &mut std::pin::UnsafePinned) {} + +// CHECK: @mutable_borrow_no_writable(ptr noalias noundef align 4 dereferenceable(4) %_1) +#[no_mangle] +#[rustc_no_writable] +pub fn mutable_borrow_no_writable(_: &mut i32) {}