Skip to content

Commit 67ddb33

Browse files
committed
Apply noalias, nonnull, dereferenceable, and align attributes unconditionally.
We've done measurements with Miri and have determined that `noalias` shouldn't break code. The requirements that allow us to add dereferenceable and align have been long documented in the standard library documentation.
1 parent ecfb332 commit 67ddb33

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

compiler/rustc_ty_utils/src/abi.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -276,24 +276,17 @@ fn adjust_for_rust_scalar<'tcx>(
276276
attrs.set(ArgAttribute::NoAliasMutRef);
277277
}
278278
}
279-
}
280279

281-
// If this is the argument to `drop_in_place`, the contents of which we fully control as the
282-
// compiler, then we may be able to mark that argument `noalias`. Currently, we're conservative
283-
// and do so only if `drop_in_place` results in a direct call to the programmer's `drop` method.
284-
// The `drop` method requires `&mut self`, so we're effectively just propagating the `noalias`
285-
// guarantee from `drop` upward to `drop_in_place` in this case.
286-
if is_drop_target {
287-
match *layout.ty.kind() {
288-
ty::RawPtr(inner) => {
289-
if let ty::Adt(adt_def, _) = inner.ty.kind() {
290-
if adt_def.destructor(cx.tcx()).is_some() {
291-
debug!("marking drop_in_place argument as noalias");
292-
attrs.set(ArgAttribute::NoAlias);
293-
}
294-
}
295-
}
296-
_ => bug!("drop target isn't a raw pointer"),
280+
// If this is the argument to `drop_in_place`, the contents of which we fully control as the
281+
// compiler, then we mark this argument as `noalias`, aligned, and dereferenceable. (The
282+
// standard library documents the necessary requirements to uphold these attributes for code
283+
// that calls this method directly.) This can enable better optimizations, such as argument
284+
// promotion.
285+
if is_drop_target {
286+
attrs.set(ArgAttribute::NoAlias);
287+
attrs.set(ArgAttribute::NonNull);
288+
attrs.pointee_size = pointee.size;
289+
attrs.pointee_align = Some(pointee.align);
297290
}
298291
}
299292
}

src/test/codegen/drop-in-place-noalias.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use std::hint::black_box;
66

7-
// CHECK: define{{.*}}drop_in_place{{.*}}Foo{{.*}}({{.*}}noalias{{.*}})
7+
// CHECK: define{{.*}}core{{.*}}ptr{{.*}}drop_in_place{{.*}}Foo{{.*}}({{.*}}noalias {{.*}} align 4 dereferenceable(12){{.*}})
88

99
#[repr(C)]
1010
pub struct Foo {

0 commit comments

Comments
 (0)