Skip to content
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

Change Rc::inc_{weak,strong} to better hint optimization to LLVM #53080

Merged
merged 6 commits into from
Aug 21, 2018

Conversation

dshynkev
Copy link
Contributor

@dshynkev dshynkev commented Aug 5, 2018

As discussed in #13018, Rc::inc_strong and Rc::inc_weak are changed to allow compositions of clone and drop to be better optimized. Almost entirely as in this comment, except that abort on zero is added so that a drop(t.clone()) does not produce a zero check followed by conditional deallocation.

This is different from #21418 in that it doesn't rely on assume, avoiding the prohibitive compilation slowdown.

Before and after IR.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @aidanhs (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 5, 2018
// The reference count will never be zero when this is called;
// nevertheless, we insert an abort here to hint LLVM at
// an otherwise missied optimization.
if self.strong() == 0 || self.strong() == usize::max_value() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, should we instead have unreachable_unchecked for the == 0 case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confusingly, doing this:

-        if self.strong() == 0 || self.strong() == usize::max_value() {
+        if self.strong() == 0 {
+            unsafe { unreachable_unchecked(); }
+        }
+        if self.strong() == usize::max_value() {
             unsafe { abort(); }

produces the same output as before any changes (before in the linked comparison).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, hm, that might be because LLVM for whatever reason decides that subtraction can overflow or some such. Seems fine to leave it then.

// We want to abort on overflow instead of dropping the value.
// The reference count will never be zero when this is called;
// nevertheless, we insert an abort here to hint LLVM at
// an otherwise missied optimization.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: “missied” (here and below).

@TimNN
Copy link
Contributor

TimNN commented Aug 14, 2018

Ping from triage @aidanhs. This PR requires your review.

@eddyb
Copy link
Member

eddyb commented Aug 18, 2018

r? @rkruppe or @alexcrichton

@alexcrichton
Copy link
Member

Awesome, thanks for this @hermord! Out of curiosity, how come there's a check for 0 here? I don't think the code currently checks that, right?

Additionally, it'd be awesome if we could add a codegen test for this! I'm not sure how easy it would be to do so though.

@dshynkev
Copy link
Contributor Author

@alexcrichton The code currently doesn't check for it, but it's needed to hint LLVM that the clone -> drop seqeunce will not bring the reference count to zero. #21418 did this with assume, but that turned out to be prohibitively expensive. I tried hinting this with unreachable_unchecked, as @Mark-Simulacrum suggested in an inline comment upthread, but, for some reason, that hint is discarded. Your comment actually prompted me to investigate this on the LLVM side, but I've been unsuccessful so far.

@dshynkev
Copy link
Contributor Author

The extra check doesn't cause any regression (at least on x86) since both checks get lowered to a single jbe. I imagine other platforms do something similar.

@alexcrichton
Copy link
Member

Ok looks and sounds great to me, thanks!

@bors: r+

@bors
Copy link
Contributor

bors commented Aug 20, 2018

📌 Commit 0b83914 has been approved by alexcrichton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 20, 2018
@kennytm
Copy link
Member

kennytm commented Aug 20, 2018

@bors r-

You have accidentally changed the src/tools/lld submodule.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 20, 2018
@eddyb
Copy link
Member

eddyb commented Aug 20, 2018

You might be able to undo the problem by doing git checkout HEAD~1 src/tools/lld.

@dshynkev
Copy link
Contributor Author

Oops, my apologies. That's what I get for doing git stage -u.

@dshynkev
Copy link
Contributor Author

Fixed now.

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Contributor

bors commented Aug 20, 2018

📌 Commit 0dd10af has been approved by alexcrichton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 20, 2018
@bors
Copy link
Contributor

bors commented Aug 20, 2018

⌛ Testing commit 0dd10af with merge d43445d36457be55114d64ba93025cec9fc07ad8...

@bors
Copy link
Contributor

bors commented Aug 20, 2018

💔 Test failed - status-travis

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 20, 2018
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-nopt of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[01:16:12] failures:
[01:16:12] 
[01:16:12] ---- [codegen] codegen/issue-13018.rs stdout ----
[01:16:12] 
[01:16:12] error: verification with 'FileCheck' failed
[01:16:12] status: exit code: 1
[01:16:12] command: "/checkout/obj/build/x86_64-unknown-linux-gnu/llvm/build/bin/FileCheck" "--input-file" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen/issue-13018/issue-13018.ll" "/checkout/src/test/codegen/issue-13018.rs"
[01:16:12] ------------------------------------------
[01:16:12] 
[01:16:12] ------------------------------------------
[01:16:12] stderr:
[01:16:12] stderr:
[01:16:12] ------------------------------------------
[01:16:12] /checkout/src/test/codegen/issue-13018.rs:17:15: error: CHECK-NOT: excluded string found in input
[01:16:12] // CHECK-NOT: __rust_dealloc
[01:16:12]               ^
[01:16:12] /checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen/issue-13018/issue-13018.ll:1560:13: note: found here
[01:16:12]  call void @__rust_dealloc(i8* %ptr, i64 %4, i64 %5)
[01:16:12] 
[01:16:12] ------------------------------------------
[01:16:12] 
[01:16:12] thread '[codegen] codegen/issue-13018.rs' panicked at 'explicit panic', tools/compiletest/src/runtest.rs:3189:9
---
[01:16:12] test result: FAILED. 92 passed; 1 failed; 13 ignored; 0 measured; 0 filtered out
[01:16:12] 
[01:16:12] 
[01:16:12] 
[01:16:12] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/compiletest" "--compile-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib" "--run-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib" "--rustc-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "--src-base" "/checkout/src/test/codegen" "--build-base" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen" "--stage-id" "stage2-x86_64-unknown-linux-gnu" "--mode" "codegen" "--target" "x86_64-unknown-linux-gnu" "--host" "x86_64-unknown-linux-gnu" "--llvm-filecheck" "/checkout/obj/build/x86_64-unknown-linux-gnu/llvm/build/bin/FileCheck" "--host-rustcflags" "-Crpath -Zunstable-options " "--target-rustcflags" "-Crpath -Zunstable-options  -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--docck-python" "/usr/bin/python2.7" "--lldb-python" "/usr/bin/python2.7" "--gdb" "/usr/bin/gdb" "--llvm-version" "7.0.0\n" "--cc" "" "--cxx" "" "--cflags" "" "--llvm-components" "" "--llvm-cxxflags" "" "--adb-path" "adb" "--adb-test-dir" "/data/tmp/work" "--android-cross-path" "" "--color" "always"
[01:16:12] 
[01:16:12] 
[01:16:12] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
[01:16:12] Build completed unsuccessfully in 0:11:59
[01:16:12] Build completed unsuccessfully in 0:11:59
[01:16:12] Makefile:58: recipe for target 'check' failed
[01:16:12] make: *** [check] Error 1

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:02dc3884
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
---
149120 ./src/llvm-emscripten/test
148688 ./obj/build/bootstrap/debug/incremental
144744 ./obj/build/x86_64-unknown-linux-gnu/test/ui
134256 ./obj/build/bootstrap/debug/incremental/bootstrap-1v3ifugz4t07z
134252 ./obj/build/bootstrap/debug/incremental/bootstrap-1v3ifugz4t07z/s-f41eu73y3c-1v5zkxn-1iplbejydu42a
128740 ./obj/build/x86_64-unknown-linux-gnu/stage0-bootstrap-tools/x86_64-unknown-linux-gnu/release
123188 ./obj/build/x86_64-unknown-linux-gnu/stage1-rustc/x86_64-unknown-linux-gnu
123184 ./obj/build/x86_64-unknown-linux-gnu/stage1-rustc/x86_64-unknown-linux-gnu/release
121648 ./obj/build/x86_64-unknown-linux-gnu/stage0-bootstrap-tools/x86_64-unknown-linux-gnu/release/deps
---
travis_time:end:2afad980:start=1534798874179600624,finish=1534798874192737111,duration=13136487
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:0be319ac
$ ln -s . checkout && for CORE in obj/cores/core.*; do EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|'); if [ -f "$EXE" ]; then printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE"; gdb -q -c "$CORE" "$EXE" -iex 'set auto-load off' -iex 'dir src/' -iex 'set sysroot .' -ex bt -ex q; echo travis_fold":"end:crashlog; fi; done || true
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:0b394fa8
travis_time:start:0b394fa8
$ cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
cat: ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers: No such file or directory
travis_fold:end:after_failure.5
travis_fold:start:after_failure.6
travis_time:start:07ae2920
$ dmesg | grep -i kill

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 @TimNN. (Feature Requests)

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Contributor

bors commented Aug 20, 2018

📌 Commit 79a905e has been approved by alexcrichton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 20, 2018
@bors
Copy link
Contributor

bors commented Aug 21, 2018

⌛ Testing commit 79a905e with merge 7aff6fc...

bors added a commit that referenced this pull request Aug 21, 2018
Change `Rc::inc_{weak,strong}` to better hint optimization to LLVM

As discussed in #13018, `Rc::inc_strong` and `Rc::inc_weak` are changed to allow compositions of `clone` and `drop` to be better optimized. Almost entirely as in [this comment](#13018 (comment)), except that `abort` on zero is added so that a `drop(t.clone())` does not produce a zero check followed by conditional deallocation.

This is different from #21418 in that it doesn't rely on `assume`, avoiding the prohibitive compilation slowdown.

[Before and after IR](https://gist.github.com/hermord/266e55451b7fe0bb8caa6e35d17c86e1).
@bors bors mentioned this pull request Aug 21, 2018
@bors
Copy link
Contributor

bors commented Aug 21, 2018

💔 Test failed - status-travis

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 21, 2018
@rust-highfive
Copy link
Collaborator

The job dist-x86_64-apple of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[00:03:43]       Memory: 8 GB
[00:03:43]       Boot ROM Version: VMW71.00V.0.B64.1704110547
[00:03:43]       Apple ROM Info: [MS_VM_CERT/SHA1/27d66596a61c48dd3dc7216fd715126e33f59ae7]Welcome to the Virtual Machine
[00:03:43]       SMC Version (system): 2.8f0
[00:03:43]       Serial Number (system): VMu3TxZLawDz
[00:03:43] 
[00:03:43] hw.ncpu: 4
[00:03:43] hw.byteorder: 1234
[00:03:43] hw.memsize: 8589934592
---
travis_fold:end:before_deploy.3
travis_fold:start:dpl_0
travis_time:start:069acacc
$ rvm $(travis_internal_ruby) --fuzzy do ruby -S gem install dpl
ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)
    Errno::EHOSTUNREACH: Failed to open TCP connection to api.rubygems.org:443 (No route to host - connect(2) for "api.rubygems.org" port 443) (https://api.rubygems.org/quick/Marshal.4.8/dpl-1.10.0.gemspec.rz)


The command "rvm $(travis_internal_ruby) --fuzzy do ruby -S gem install dpl" failed and exited with 1 during .
Your build has been stopped.

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 @TimNN. (Feature Requests)

@dshynkev
Copy link
Contributor Author

Huh, api.rubygems.org seems up. Random network fluke?

@kennytm
Copy link
Member

kennytm commented Aug 21, 2018

@bors retry #40474

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 21, 2018
@bors
Copy link
Contributor

bors commented Aug 21, 2018

⌛ Testing commit 79a905e with merge 70c33bb...

bors added a commit that referenced this pull request Aug 21, 2018
Change `Rc::inc_{weak,strong}` to better hint optimization to LLVM

As discussed in #13018, `Rc::inc_strong` and `Rc::inc_weak` are changed to allow compositions of `clone` and `drop` to be better optimized. Almost entirely as in [this comment](#13018 (comment)), except that `abort` on zero is added so that a `drop(t.clone())` does not produce a zero check followed by conditional deallocation.

This is different from #21418 in that it doesn't rely on `assume`, avoiding the prohibitive compilation slowdown.

[Before and after IR](https://gist.github.com/hermord/266e55451b7fe0bb8caa6e35d17c86e1).
@bors
Copy link
Contributor

bors commented Aug 21, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing 70c33bb to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants