Skip to content
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
15 changes: 15 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,21 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcNoMirInlineParser {
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNoMirInline;
}

pub(crate) struct RustcNoWritableParser;

impl<S: Stage> NoArgsAttributeParser<S> for RustcNoWritableParser {
const PATH: &[Symbol] = &[sym::rustc_no_writable];
const ON_DUPLICATE: OnDuplicate<S> = 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<S: Stage> NoArgsAttributeParser<S> for RustcLintQueryInstabilityParser {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ attribute_parsers!(
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,
Single<WithoutArgs<RustcNoImplicitBoundsParser>>,
Single<WithoutArgs<RustcNoMirInlineParser>>,
Single<WithoutArgs<RustcNoWritableParser>>,
Single<WithoutArgs<RustcNonConstTraitMethodParser>>,
Single<WithoutArgs<RustcNonnullOptimizationGuaranteedParser>>,
Single<WithoutArgs<RustcNounwindParser>>,
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_llvm/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ trait ArgAttributesExt {
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] = [
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,10 @@ pub static BUILTIN_ATTRIBUTES: &[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:
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_hir/src/attrs/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/attrs/encode_cross_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl AttributeKind {
RustcNoImplicitAutorefs => Yes,
RustcNoImplicitBounds => No,
RustcNoMirInline => Yes,
RustcNoWritable => Yes,
RustcNonConstTraitMethod => No, // should be reported via other queries like `constness`
RustcNonnullOptimizationGuaranteed => Yes,
RustcNounwind => No,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ fn test_unstable_options_tracking_hash() {
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));
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| AttributeKind::RustcNoImplicitAutorefs
| AttributeKind::RustcNoImplicitBounds
| AttributeKind::RustcNoMirInline
| AttributeKind::RustcNoWritable
| AttributeKind::RustcNonConstTraitMethod
| AttributeKind::RustcNonnullOptimizationGuaranteed
| AttributeKind::RustcNounwind
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2530,6 +2530,8 @@ options! {
"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 \
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,7 @@ symbols! {
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,
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_target/src/callconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is unfortunate. We should benchmark this to ensure the larger bitfield isn't a problem.

bitflags::bitflags! {
impl ArgAttribute: u8 {
impl ArgAttribute: u16 {
const CapturesNone = 0b111;
const CapturesAddress = 0b110;
const CapturesReadOnly = 0b100;
Expand All @@ -121,6 +121,7 @@ mod attr_impl {
const ReadOnly = 1 << 5;
const InReg = 1 << 6;
const NoUndef = 1 << 7;
const Writable = 1 << 8;
}
}
rustc_data_structures::external_bitflags_debug! { ArgAttribute }
Expand Down
19 changes: 18 additions & 1 deletion compiler/rustc_ty_utils/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::{assert_matches, iter};

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;
Expand Down Expand Up @@ -355,6 +355,7 @@ fn arg_attrs_for_rust_scalar<'tcx>(
offset: Size,
is_return: bool,
drop_target_pointee: Option<Ty<'tcx>>,
determined_fn_def_id: Option<DefId>,
) -> ArgAttributes {
let mut attrs = ArgAttributes::new();

Expand Down Expand Up @@ -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),
Comment thread
quiode marked this conversation as resolved.
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);
Expand Down Expand Up @@ -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,
)
}))
};
Expand Down
1 change: 1 addition & 0 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ impl<T> [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
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ impl str {
#[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
}
Expand Down
11 changes: 11 additions & 0 deletions src/doc/unstable-book/src/compiler-flags/llvm-writable.md
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 30 additions & 0 deletions tests/codegen-llvm/llvm-writable.rs
Comment thread
quiode marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -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<i16>) {}

// 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<i32>) {}

// CHECK: @unsafe_pinned_borrow_mut(ptr noundef nonnull align 4 %_1)
#[no_mangle]
pub fn unsafe_pinned_borrow_mut(_: &mut std::pin::UnsafePinned<i32>) {}

// 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) {}
Loading