Skip to content

Commit f71741b

Browse files
pcwaltonerikdesjardins
authored andcommitted
[rustc_ty_utils] Add the LLVM noalias parameter attribute to drop_in_place in certain cases.
LLVM can make use of the `noalias` parameter attribute on the parameter to `drop_in_place` in areas like argument promotion. Because the Rust compiler fully controls the code for `drop_in_place`, it can soundly deduce parameter attributes on it. In the case of a value that has a programmer-defined Drop implementation, we know that the first thing `drop_in_place` will do is pass a pointer to the object to `Drop::drop`. `Drop::drop` takes `&mut`, so it must be guaranteed that there are no pointers to the object upon entering that function. Therefore, it should be safe to mark `noalias` there. With this patch, we mark `noalias` only when the type is a value with a programmer-defined Drop implementation. This is possibly overly conservative, but I thought that proceeding cautiously was best in this instance.
1 parent e77366b commit f71741b

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

compiler/rustc_ty_utils/src/abi.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ fn adjust_for_rust_scalar<'tcx>(
238238
layout: TyAndLayout<'tcx>,
239239
offset: Size,
240240
is_return: bool,
241+
is_drop_target: bool,
241242
) {
242243
// Booleans are always a noundef i1 that needs to be zero-extended.
243244
if scalar.is_bool() {
@@ -307,6 +308,25 @@ fn adjust_for_rust_scalar<'tcx>(
307308
}
308309
}
309310
}
311+
312+
// If this is the argument to `drop_in_place`, the contents of which we fully control as the
313+
// compiler, then we may be able to mark that argument `noalias`. Currently, we're conservative
314+
// and do so only if `drop_in_place` results in a direct call to the programmer's `drop` method.
315+
// The `drop` method requires `&mut self`, so we're effectively just propagating the `noalias`
316+
// guarantee from `drop` upward to `drop_in_place` in this case.
317+
if is_drop_target {
318+
match *layout.ty.kind() {
319+
ty::RawPtr(inner) => {
320+
if let ty::Adt(adt_def, _) = inner.ty.kind() {
321+
if adt_def.destructor(cx.tcx()).is_some() {
322+
debug!("marking drop_in_place argument as noalias");
323+
attrs.set(ArgAttribute::NoAlias);
324+
}
325+
}
326+
}
327+
_ => bug!("drop target isn't a raw pointer"),
328+
}
329+
}
310330
}
311331

312332
// FIXME(eddyb) perhaps group the signature/type-containing (or all of them?)
@@ -362,10 +382,16 @@ fn fn_abi_new_uncached<'tcx>(
362382
use SpecAbi::*;
363383
let rust_abi = matches!(sig.abi, RustIntrinsic | PlatformIntrinsic | Rust | RustCall);
364384

385+
let is_drop_in_place = match (cx.tcx.lang_items().drop_in_place_fn(), fn_def_id) {
386+
(Some(drop_in_place_fn), Some(fn_def_id)) => drop_in_place_fn == fn_def_id,
387+
_ => false,
388+
};
389+
365390
let arg_of = |ty: Ty<'tcx>, arg_idx: Option<usize>| -> Result<_, FnAbiError<'tcx>> {
366391
let span = tracing::debug_span!("arg_of");
367392
let _entered = span.enter();
368393
let is_return = arg_idx.is_none();
394+
let is_drop_target = is_drop_in_place && arg_idx == Some(0);
369395

370396
let layout = cx.layout_of(ty)?;
371397
let layout = if force_thin_self_ptr && arg_idx == Some(0) {
@@ -379,7 +405,15 @@ fn fn_abi_new_uncached<'tcx>(
379405

380406
let mut arg = ArgAbi::new(cx, layout, |layout, scalar, offset| {
381407
let mut attrs = ArgAttributes::new();
382-
adjust_for_rust_scalar(*cx, &mut attrs, scalar, *layout, offset, is_return);
408+
adjust_for_rust_scalar(
409+
*cx,
410+
&mut attrs,
411+
scalar,
412+
*layout,
413+
offset,
414+
is_return,
415+
is_drop_target,
416+
);
383417
attrs
384418
});
385419

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Tests that the compiler can mark `drop_in_place` as `noalias` when safe to do so.
2+
3+
#![crate_type="lib"]
4+
5+
use std::hint::black_box;
6+
7+
// CHECK: define{{.*}}drop_in_place{{.*}}Foo{{.*}}({{.*}}noalias{{.*}})
8+
9+
#[repr(C)]
10+
pub struct Foo {
11+
a: i32,
12+
b: i32,
13+
c: i32,
14+
}
15+
16+
impl Drop for Foo {
17+
#[inline(never)]
18+
fn drop(&mut self) {
19+
black_box(self.a);
20+
}
21+
}
22+
23+
extern {
24+
fn bar();
25+
fn baz(foo: Foo);
26+
}
27+
28+
pub fn haha() {
29+
let foo = Foo { a: 1, b: 2, c: 3 };
30+
unsafe {
31+
bar();
32+
baz(foo);
33+
}
34+
}

0 commit comments

Comments
 (0)