-
Notifications
You must be signed in to change notification settings - Fork 12.2k
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
Annotate eligible small immediate arguments with noundef
#127210
base: master
Are you sure you want to change the base?
Conversation
Add both positive and negative test cases for adding `noundef` to immediate arguments that fit within target pointer width.
@rustbot author |
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
…<try> Annotate eligible small immediate arguments with `noundef` Retake of rust-lang#123425. We try to annotate small (fits within target pointer width) aggregate arguments passed as immediates (specifically casted as "appropriately sized integer type") with `noundef`. Example: ```rs #[no_mangle] pub fn short_array_u64x1(v: [u64; 1]) -> [u64; 1] { v } ``` currently produces ```llvm define i64 `@short_array_u64x1(i64` %0) ... ``` This PR changes that to ```llvm define noundef i64 `@short_array_u64x1(i64` noundef %0) ... ``` The `noundef` attribute is added only if the immediate value has no padding. Specifically, the conservative heuristic we use is to: - Peel away layers of `#[repr(Rust)]` or `#[repr(transparent)]` wrappers if present - Check for innermost simple arrays (whose element type is a primitive type) that can fit within target pointer width Union immediates or otherwise anything that contains unions will not have `noundef` attribute applied. Closes rust-lang#123183. cc `@/RalfJung` who pointed out various problems with the previous take, hopefully I addressed most of them in this take. r? `@ghost` (perf)
@@ -822,6 +835,44 @@ fn fn_abi_adjust_for_abi<'tcx>( | |||
Ok(()) | |||
} | |||
|
|||
fn can_annotate_small_immediate_argument_with_noundef<'tcx>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not super confident in the correctness of this eligibility check here. I've added positive and negative test cases for edge cases like unions or array of unions, but I still feel like I'm missing cases. In any case, this is a conservative heuristic for checking "does this immediate have padding".
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (caa50bd): comparison URL. Overall result: no relevant changes - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results (primary -2.9%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 697.337s -> 698.097s (0.11%) |
r? @scottmcm or compiler |
This should now be ready for review. @rustbot ready |
Add `noundef` metadata for arguments that: 1. are passed as immediates 2. fit in target pointer width 3. has no padding
80171db
to
3704226
Compare
What's a repr(Rust) wrapper? The property you are after is "is there any padding or a union inside of this". Also since this is not about stable layout guarantees, I wouldn't bother with the |
if layout.ty.is_union() { | ||
return false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can avoid having to duplicate this check if you put it before the non_1zst_field
call.
Also, what about enums?
// NoOpt: define i32 @repr_c_immediate(i32 %0) | ||
// Opt: define i32 @repr_c_immediate(i32 %0) | ||
#[no_mangle] | ||
pub fn repr_c_immediate(v: ReprCWrapper) -> ReprCWrapper { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any reason why this could not be noundef
I agree with Ralf on this. I think you probably want to look just at the actual computed layout, completely ignoring the original rust code that resulted in it. (That way it's immune to any changes to Also, since this is basically just checking the same thing as https://docs.rs/bytemuck/latest/bytemuck/trait.NoUninit.html and https://docs.rs/zerocopy/latest/zerocopy/trait.AsBytes.html, it might be worth seeing if any of the safe transmute work already has code that you can just steal to do this. @rustbot author |
That seems more correct to me than the current approach yes.
Thanks for the pointers (heh). |
Retake of #123425.
We try to annotate small (fits within target pointer width) aggregate arguments passed as immediates
(specifically casted as "appropriately sized integer type") with
noundef
.Example:
currently produces
This PR changes that to
The
noundef
attribute is added only if the immediate value has no padding. Specifically,the conservative heuristic we use is to:
#[repr(Rust)]
or#[repr(transparent)]
wrappers if presenttarget pointer width
Union immediates or otherwise anything that contains unions will not have
noundef
attributeapplied.
Closes #123183.
cc @RalfJung who pointed out various problems with the previous take, hopefully I addressed most of
them in this take.
r? @ghost (perf)