-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Hint the maximum length permitted by invariant of slices #77023
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
Could you merge the
I guess the attr doesn't support auto-merging multiple invocations of it. |
☔ The latest upstream changes (presumably #77039) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
0553d9a
to
e203e4d
Compare
@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author |
e203e4d
to
658722e
Compare
That should do all the implementation work. Would you mind to start a perf run to see if this adversly influences inlining somewhere? |
@bors try @rust-timer queue This seems like it's going to complicate the codegen for len() -- adding an assume or similar construct -- which might have a pretty high compile time cost even if not so high a runtime cost. |
Awaiting bors try build completion |
⌛ Trying commit b885434b728adc2bcce88480ed755fb1e4ecb837 with merge 8612477075acc90b2716af836a922f8965189225... |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
💔 Test failed - checks-actions |
b885434
to
482f306
Compare
@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author Looks like the feature |
@bors try |
⌛ Trying commit 482f306df072d2b20507b5577c780b8dfa477edc with merge 9c1568be513095701cd7735f2d9a1b5f1fbac104... |
☀️ Try build successful - checks-actions, checks-azure |
Queued 9c1568be513095701cd7735f2d9a1b5f1fbac104 with parent e599b53, future comparison URL. |
I don't know what to do about CI failure either. I presume this is a limitation of the beta compiler? Can you try putting Please also keep commits squashed as you edit (both this change and further ones if they become necessary); this PR has a small line diff which should not need more than one commit. |
c9a0d01
to
517feb3
Compare
That seems to have done it 🎉 I've squashed the largest part of the history but kept this at two commits, one introducing the test and the other the actual change. I hope that's okay. @rustbot modify labels: +S-waiting-on-review -S-waiting-on-author |
I would prefer to have a single commit, makes it easier to find the code introduced alongside the test. r=me with it squashed. |
Uses assume to check the length against a constant upper bound. The inlined result then informs the optimizer of the sound value range. This was tried with unreachable_unchecked before which introduces a branch. This has the advantage of not being executed in sound code but complicates basic blocks. It resulted in ~2% increased compile time in some worst cases. Add a codegen test for the assumption, testing the issue from rust-lang#67186
517feb3
to
e44784b
Compare
Okay, that's fine for me. @rustbot modify labels: +S-waiting-on-review -S-waiting-on-author |
@bors r+ rollup=never |
📌 Commit e44784b has been approved by |
☀️ Test successful - checks-actions, checks-azure |
Note that this PR caused a slight regression on max-rss; https://perf.rust-lang.org/compare.html?start=4ccf5f731bb71db3470002d6baf5ab4792b821d9&end=beb5ae474d2835962ebdf7416bd1c9ad864fe101&stat=max-rss I guess LLVM hasn't changed much since: #54995 (comment) |
Final perf results are in. This seemed to cause a slowdown across the board with the exception of some synthetic benchmarks. Except for the aforementioned stress test, check builds are not improved or a bit slower. The case in the |
@lzutao We have an automated script for detecting these kinds of regressions and run it weekly as part of perf-triage. That usually happens on a Monday. Also, there's no |
rust-lang#77023 (comment) suggests that the original PR introduced a significant perf regression. This reverts commit e44784b / rust-lang#77023.
Revert "Assume slice len is bounded by allocation size" rust-lang#77023 (comment) suggests that the original PR introduced a significant perf regression. This reverts commit e44784b / rust-lang#77023. cc `@HeroicKatora`
One of the safety invariants of references, and in particular of references to slices, is that they may not cover more than
isize::MAX
bytes. The unsafefrom_raw_parts
constructors of slices explicitly requires the caller to guarantee this fact. Violating it would also be UB with regards to the semantics of generated llvm code.This effectively bounds the length of a (non-ZST) slice from above by a compile time constant. But when the length is loaded from a function argument it appears llvm is not aware of this requirement. The additional value range assertions allow some further elision of code branches, including overflow checks, especially in the presence of artithmetic on the indices.
This may have a performance impact, adding more code to a common method but allowing more optimization. I'm not quite sure, is the Rust side of const-prop strong enough to elide the irrelevant match branches?
Fixes: #67186