Skip to content

Commit 3603a84

Browse files
committed
Auto merge of #111517 - lukas-code:addr-of-mutate, r=tmiasko
allow mutating function args through `&raw const` Fixes #111502 by "turning off the sketchy optimization while we figure out if this is ok", like `@JakobDegen` said. The first commit in this PR removes some suspicious looking logic from the same method, but should have no functional changes, since it doesn't modify the `context` outside of the method. Best reviewed commit by commit. r? opsem
2 parents 0a0e045 + 9c418e5 commit 3603a84

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

compiler/rustc_mir_transform/src/deduce_param_attrs.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use rustc_hir::def_id::LocalDefId;
99
use rustc_index::bit_set::BitSet;
1010
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
11-
use rustc_middle::mir::{Body, Local, Location, Operand, Terminator, TerminatorKind, RETURN_PLACE};
11+
use rustc_middle::mir::{Body, Location, Operand, Place, Terminator, TerminatorKind, RETURN_PLACE};
1212
use rustc_middle::ty::{self, DeducedParamAttrs, Ty, TyCtxt};
1313
use rustc_session::config::OptLevel;
1414

@@ -29,31 +29,31 @@ impl DeduceReadOnly {
2929
}
3030

3131
impl<'tcx> Visitor<'tcx> for DeduceReadOnly {
32-
fn visit_local(&mut self, local: Local, mut context: PlaceContext, _: Location) {
32+
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
3333
// We're only interested in arguments.
34-
if local == RETURN_PLACE || local.index() > self.mutable_args.domain_size() {
34+
if place.local == RETURN_PLACE || place.local.index() > self.mutable_args.domain_size() {
3535
return;
3636
}
3737

38-
// Replace place contexts that are moves with copies. This is safe in all cases except
39-
// function argument position, which we already handled in `visit_terminator()` by using the
40-
// ArgumentChecker. See the comment in that method for more details.
41-
//
42-
// In the future, we might want to move this out into a separate pass, but for now let's
43-
// just do it on the fly because that's faster.
44-
if matches!(context, PlaceContext::NonMutatingUse(NonMutatingUseContext::Move)) {
45-
context = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
46-
}
47-
48-
match context {
49-
PlaceContext::MutatingUse(..)
50-
| PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) => {
38+
let mark_as_mutable = match context {
39+
PlaceContext::MutatingUse(..) => {
5140
// This is a mutation, so mark it as such.
52-
self.mutable_args.insert(local.index() - 1);
41+
true
42+
}
43+
PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf) => {
44+
// Whether mutating though a `&raw const` is allowed is still undecided, so we
45+
// disable any sketchy `readonly` optimizations for now.
46+
// But we only need to do this if the pointer would point into the argument.
47+
!place.is_indirect()
5348
}
5449
PlaceContext::NonMutatingUse(..) | PlaceContext::NonUse(..) => {
5550
// Not mutating, so it's fine.
51+
false
5652
}
53+
};
54+
55+
if mark_as_mutable {
56+
self.mutable_args.insert(place.local.index() - 1);
5757
}
5858
}
5959

tests/codegen/addr-of-mutate.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// compile-flags: -C opt-level=3 -C no-prepopulate-passes
2+
// min-llvm-version: 15.0 (for opaque pointers)
3+
4+
#![crate_type = "lib"]
5+
6+
// Test for the absence of `readonly` on the argument when it is mutated via `&raw const`.
7+
// See <https://github.com/rust-lang/rust/issues/111502>.
8+
9+
// CHECK: i8 @foo(ptr noalias nocapture noundef dereferenceable(128) %x)
10+
#[no_mangle]
11+
pub fn foo(x: [u8; 128]) -> u8 {
12+
let ptr = core::ptr::addr_of!(x).cast_mut();
13+
unsafe {
14+
(*ptr)[0] = 1;
15+
}
16+
x[0]
17+
}
18+
19+
// CHECK: i1 @second(ptr noalias nocapture noundef dereferenceable({{[0-9]+}}) %a_ptr_and_b)
20+
#[no_mangle]
21+
pub unsafe fn second(a_ptr_and_b: (*mut (i32, bool), (i64, bool))) -> bool {
22+
let b_bool_ptr = core::ptr::addr_of!(a_ptr_and_b.1.1).cast_mut();
23+
(*b_bool_ptr) = true;
24+
a_ptr_and_b.1.1
25+
}
26+
27+
// If going through a deref (and there are no other mutating accesses), then `readonly` is fine.
28+
// CHECK: i1 @third(ptr noalias nocapture noundef readonly dereferenceable({{[0-9]+}}) %a_ptr_and_b)
29+
#[no_mangle]
30+
pub unsafe fn third(a_ptr_and_b: (*mut (i32, bool), (i64, bool))) -> bool {
31+
let b_bool_ptr = core::ptr::addr_of!((*a_ptr_and_b.0).1).cast_mut();
32+
(*b_bool_ptr) = true;
33+
a_ptr_and_b.1.1
34+
}

0 commit comments

Comments
 (0)