-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[rustc_ty_utils] Add the LLVM noalias
parameter attribute to drop_in_place
in certain cases.
#103614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[rustc_ty_utils] Add the LLVM noalias
parameter attribute to drop_in_place
in certain cases.
#103614
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5ce1a00
[rustc_ty_utils] Add the LLVM `noalias` parameter attribute to `drop_…
pcwalton ecfb332
Fix noalias box test
pcwalton 67ddb33
Apply `noalias`, `nonnull`, `dereferenceable`, and `align` attributes…
pcwalton 02cfabe
Update documentation for `drop_in_place()`
pcwalton 53f21aa
Add missing "unsafe" to fix doctest
pcwalton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[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.
- Loading branch information
commit 5ce1a00e812dc6d4512dcaf8d9ff41986904c515
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -199,6 +199,7 @@ fn adjust_for_rust_scalar<'tcx>( | |||||||||||
layout: TyAndLayout<'tcx>, | ||||||||||||
offset: Size, | ||||||||||||
is_return: bool, | ||||||||||||
is_drop_target: bool, | ||||||||||||
) { | ||||||||||||
// Booleans are always a noundef i1 that needs to be zero-extended. | ||||||||||||
if scalar.is_bool() { | ||||||||||||
|
@@ -276,6 +277,25 @@ fn adjust_for_rust_scalar<'tcx>( | |||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// If this is the argument to `drop_in_place`, the contents of which we fully control as the | ||||||||||||
// compiler, then we may be able to mark that argument `noalias`. Currently, we're conservative | ||||||||||||
// and do so only if `drop_in_place` results in a direct call to the programmer's `drop` method. | ||||||||||||
// The `drop` method requires `&mut self`, so we're effectively just propagating the `noalias` | ||||||||||||
// guarantee from `drop` upward to `drop_in_place` in this case. | ||||||||||||
if is_drop_target { | ||||||||||||
match *layout.ty.kind() { | ||||||||||||
ty::RawPtr(inner) => { | ||||||||||||
if let ty::Adt(adt_def, _) = inner.ty.kind() { | ||||||||||||
if adt_def.destructor(cx.tcx()).is_some() { | ||||||||||||
debug!("marking drop_in_place argument as noalias"); | ||||||||||||
attrs.set(ArgAttribute::NoAlias); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
_ => bug!("drop target isn't a raw pointer"), | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// FIXME(eddyb) perhaps group the signature/type-containing (or all of them?) | ||||||||||||
|
@@ -331,10 +351,16 @@ fn fn_abi_new_uncached<'tcx>( | |||||||||||
use SpecAbi::*; | ||||||||||||
let rust_abi = matches!(sig.abi, RustIntrinsic | PlatformIntrinsic | Rust | RustCall); | ||||||||||||
|
||||||||||||
let is_drop_in_place = match (cx.tcx.lang_items().drop_in_place_fn(), fn_def_id) { | ||||||||||||
(Some(drop_in_place_fn), Some(fn_def_id)) => drop_in_place_fn == fn_def_id, | ||||||||||||
_ => false, | ||||||||||||
}; | ||||||||||||
Comment on lines
+347
to
+350
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
let arg_of = |ty: Ty<'tcx>, arg_idx: Option<usize>| -> Result<_, FnAbiError<'tcx>> { | ||||||||||||
let span = tracing::debug_span!("arg_of"); | ||||||||||||
let _entered = span.enter(); | ||||||||||||
let is_return = arg_idx.is_none(); | ||||||||||||
let is_drop_target = is_drop_in_place && arg_idx == Some(0); | ||||||||||||
|
||||||||||||
let layout = cx.layout_of(ty)?; | ||||||||||||
let layout = if force_thin_self_ptr && arg_idx == Some(0) { | ||||||||||||
|
@@ -348,7 +374,15 @@ fn fn_abi_new_uncached<'tcx>( | |||||||||||
|
||||||||||||
let mut arg = ArgAbi::new(cx, layout, |layout, scalar, offset| { | ||||||||||||
let mut attrs = ArgAttributes::new(); | ||||||||||||
adjust_for_rust_scalar(*cx, &mut attrs, scalar, *layout, offset, is_return); | ||||||||||||
adjust_for_rust_scalar( | ||||||||||||
*cx, | ||||||||||||
&mut attrs, | ||||||||||||
scalar, | ||||||||||||
*layout, | ||||||||||||
offset, | ||||||||||||
is_return, | ||||||||||||
is_drop_target, | ||||||||||||
); | ||||||||||||
attrs | ||||||||||||
}); | ||||||||||||
|
||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Tests that the compiler can mark `drop_in_place` as `noalias` when safe to do so. | ||
|
||
#![crate_type="lib"] | ||
|
||
use std::hint::black_box; | ||
|
||
// CHECK: define{{.*}}drop_in_place{{.*}}Foo{{.*}}({{.*}}noalias{{.*}}) | ||
|
||
#[repr(C)] | ||
pub struct Foo { | ||
a: i32, | ||
b: i32, | ||
c: i32, | ||
} | ||
|
||
impl Drop for Foo { | ||
#[inline(never)] | ||
fn drop(&mut self) { | ||
black_box(self.a); | ||
} | ||
} | ||
|
||
extern { | ||
fn bar(); | ||
fn baz(foo: Foo); | ||
} | ||
|
||
pub fn haha() { | ||
let foo = Foo { a: 1, b: 2, c: 3 }; | ||
unsafe { | ||
bar(); | ||
baz(foo); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.