-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Represent Result<usize, Box<T>>
as ScalarPair(i64, ptr)
#121668
Conversation
// CHECK-LABEL: @insert_int | ||
#[no_mangle] | ||
pub fn insert_int(x: usize) -> Result<usize, Box<()>> { | ||
// CHECK: start: | ||
// CHECK-NEXT: inttoptr i{{[0-9]+}} %x to ptr | ||
// CHECK-NEXT: insertvalue | ||
// CHECK-NEXT: ret { i{{[0-9]+}}, ptr } | ||
Ok(x) | ||
} |
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 am surprised that this just works, inserting inttoptr
/ptrtoint
as necessary with only a layout change and no changes to codegen. It feels like something in codegen is a bit too permissive about adding casts whenever it needs to...
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.
This is optimized IR, so it's likely that rustc did something much more boring but LLVM optimized it to this.
Add -C no-prepopulate-passes
in the compile-flags
if you want to look at what cg_llvm is doing directly.
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.
For example, you get an inttoptr
like that after LLVM optimizes a store+load: https://rust.godbolt.org/z/qMPWr7Tzz
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.
(Rust would actually prefer this example to be a GEP off null, like #121242, I think. Not that this PR would need to do that.)
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.
How did I forget that...
Yeah, the IR we generate just spills to an alloca.
// CHECK-LABEL: @result_nop_match_32 | ||
#[no_mangle] | ||
pub fn result_nop_match_32(x: Result<i32, u32>) -> Result<i32, u32> { | ||
// CHECK: start | ||
// CHECK-NEXT: ret i64 %0 | ||
// CHECK: start: | ||
// CHECK-NEXT: insertvalue { i32, i32 } | ||
// CHECK-NEXT: insertvalue { i32, i32 } | ||
// CHECK-NEXT: ret { i32, i32 } |
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.
Although this is no longer just a ret
instruction, this is the fewest possible instructions for scalar pair layout, since you need to pack the two separate arguments into a single return value.
For real, non-noop code, the scalar pair layout is arguably slightly better, since passing the components in separate arguments means that consuming code can just use the second argument directly, instead of having to shift it down from the top 32 bits of an i64 reg.
It was kinda janky in the first place for this test to rely on the fact that integers with differing signedness don't get scalar pair layout; Result<u32, u32>
has always gotten the same scalar pair layout (since 1.27) that Result<i32, u32>
now gets with this PR.
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.
Yup, agreed it's fine to change this test like this. The important part is that it's not branching, not icmp
ing, not select
ing, etc.
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Represent `Result<usize, Box<T>>` as ScalarPair(i64, ptr) This allows types like `Result<usize, std::io::Error>` (and integers of differing sign, e.g. `Result<u64, i64>`) to be passed in a pair of registers instead of through memory, like `Result<u64, u64>` or `Result<Box<T>, Box<U>>` are today. Fixes rust-lang#97540. r? `@ghost`
This comment has been minimized.
This comment has been minimized.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (620bc57): comparison URL. Overall result: ❌ regressions - 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. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis 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 sizeResultsThis 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.
Bootstrap: 649.3s -> 650.378s (0.17%) |
Hmm, nearly all of the changed benchmarks seem to be noisy, so it's hard to tell if it's real or not... |
looks like noise and it being perf neutral to me |
In that case... r? compiler |
This comment has been minimized.
This comment has been minimized.
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.
This seems reasonable to me but I'm not familiar enough with this code to anticipate all the consequences of the change.
r? compiler |
This comment has been minimized.
This comment has been minimized.
💔 Test failed - checks-actions |
It seems that LLVM 17 doesn't fully optimize out unwrap_unchecked. We can just loosen the check lines to account for this, since we don't really care about the exact instructions, we just want to make sure that inttoptr/ptrtoint aren't used for Box.
It seems LLVM 17 doesn't optimize out the unwrap_unchecked. Loosened the check lines since we don't care about the precise instruction sequence. |
Ping @scottmcm, I made some changes to the codegen tests so they also pass on LLVM 17. Let me know if you'd prefer to revert the last commit and use |
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.
Hmm, looks like I accidentally commented on the commit, which has github a bit confused. Let's try this instead...
(And do @rustbot ready
once you're happy with it, please.)
Co-authored-by: Scott McMurray <scottmcm@users.noreply.github.com>
Yeah, something like that works. @rustbot ready |
Oh, right, scalar pair, so clearly more than one parameter 🤦 Test updates look good: |
☀️ Test successful - checks-actions |
Finished benchmarking commit (3cbb932): comparison URL. Overall result: ❌✅ regressions and improvements - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)This benchmark run did not return any relevant results for this metric. CyclesResultsThis 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.
Binary sizeResultsThis 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.
Bootstrap: 675.199s -> 675.071s (-0.02%) |
Quoth nnethercote: Are the binary size increases expected? This change looks basically the same as #121665 (comment), where all of the regressions are due to a uniform ~4k increase in the stdlib "runtime" that's included in every binary. I'm looking into whether we can make some improvements to the stdlib backtrace code as a whole, either by cha—wait a minute. Didn't So it was noise all along? (I'm still gonna look into some backtrace size improvements though, since all the big functions in helloworld are backtrace related. Edit: opened #122462 as a first step) |
This allows types like
Result<usize, std::io::Error>
(and integers of differing sign, e.g.Result<u64, i64>
) to be passed in a pair of registers instead of through memory, likeResult<u64, u64>
orResult<Box<T>, Box<U>>
are today.Fixes #97540.
r? @ghost