Skip to content

Commit 9c418e5

Browse files
author
Lukas Markeffsky
committed
allow mutating function args through &raw const
1 parent 97ed808 commit 9c418e5

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

compiler/rustc_mir_transform/src/deduce_param_attrs.rs

+16-5
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,20 +29,31 @@ impl DeduceReadOnly {
2929
}
3030

3131
impl<'tcx> Visitor<'tcx> for DeduceReadOnly {
32-
fn visit_local(&mut self, local: Local, context: PlaceContext, _location: 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-
match context {
38+
let mark_as_mutable = match context {
3939
PlaceContext::MutatingUse(..) => {
4040
// This is a mutation, so mark it as such.
41-
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()
4248
}
4349
PlaceContext::NonMutatingUse(..) | PlaceContext::NonUse(..) => {
4450
// Not mutating, so it's fine.
51+
false
4552
}
53+
};
54+
55+
if mark_as_mutable {
56+
self.mutable_args.insert(place.local.index() - 1);
4657
}
4758
}
4859

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)