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

Optimize matches #60730

Merged
merged 7 commits into from
Jun 16, 2019
Merged

Optimize matches #60730

merged 7 commits into from
Jun 16, 2019

Conversation

matthewjasper
Copy link
Contributor

Attempt to fix or improve #60571

This is breaking some diagnostics because the MIR for match arms isn't in source order any more.

cc @Centril

@rust-highfive
Copy link
Collaborator

r? @pnkfelix

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 11, 2019
@matthewjasper
Copy link
Contributor Author

@bors try

@bors
Copy link
Contributor

bors commented May 11, 2019

⌛ Trying commit d7f31a6 with merge df9b1d3...

bors added a commit that referenced this pull request May 11, 2019
Optimize matches

Attempt to fix or improve #60571

This is breaking some diagnostics because the MIR for match arms isn't in source order any more.

cc @Centril
@bors
Copy link
Contributor

bors commented May 11, 2019

☀️ Try build successful - checks-travis
Build commit: df9b1d3

@matthewjasper
Copy link
Contributor Author

@rust-lang/infra Can I have a perf run?

@pietroalbini
Copy link
Member

@rust-timer build df9b1d3

@rust-timer
Copy link
Collaborator

Success: Queued df9b1d3 with parent b8e0d0a, comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit df9b1d3

@@ -1180,9 +1180,9 @@ pub enum TerminatorKind<'tcx> {
FalseEdges {
/// The target normal control flow will take
real_target: BasicBlock,
/// The list of blocks control flow could conceptually take, but won't
/// A block control flow could conceptually take, but won't
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// A block control flow could conceptually take, but won't
/// A block control flow could conceptually jump to, but won't

adt_def.variants.len() + 1
}
TestKind::SwitchInt { switch_ty, ref options, .. } => {
if switch_ty.is_bool() {
Copy link
Member

Choose a reason for hiding this comment

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

I'm trying to figure out the overall picture here.

My current best guess is that, for SwitchInt on a bool, the options ends up holding just some permutation of { 0, 1 } (and we know that to be exhaustive, since the type is bool), and thus options.len() + 1 would give the wrong number of target basic blocks.

Regardless of whether my hypothesis above is correct or incorrect, it might be nice to add some docs to SwitchInt explaining the role of the options field, and what invariants it upholds with respect to the other fields (switch_ty and indices).

// StorageLive(_4);
// _4 = _2;
// ((_0 as A).0: i32) = move _4;
// discriminant(_0) = 0;
Copy link
Member

@pnkfelix pnkfelix May 22, 2019

Choose a reason for hiding this comment

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

(I find this effect on the generated MIR interesting. I don't know whether its bad or good, in terms of code quality i.e. the ability of LLVM to optimize the result)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The diff is a little messed up, all that's happening here is the two blocks are being swapped

@@ -28,16 +28,16 @@ LL | let x;
...
LL | x = 1;
| ^^^^^ cannot assign twice to immutable variable
LL | } else {
LL | x = 2;
| ----- first assignment to `x`
Copy link
Member

Choose a reason for hiding this comment

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

(it always makes me a little sad to see leaking of the reality that our static analysis is happening on a control-flow graph that does not always bias itself to act like syntactic order should be favored when the control-flow is otherwise semi-arbitrary. But there are other instances where things like this leak through and its certainly not your job, or maybe anyone's job, to address it.)

@pnkfelix
Copy link
Member

The perf results look entirely acceptable to me.

@pnkfelix
Copy link
Member

@matthewjasper maybe I missed it, but did you add a test case for the actual problem described in #60571 somewhere in this PR?

@pnkfelix
Copy link
Member

pnkfelix commented May 22, 2019

This all seems fine to me. I left some general feedback, but I don't see a problem with landing this PR as is.

Having said that, we can't merge draft pull requests.

So I'll just say: r=me, and switch this to S-waiting-on-author so that @matthewjasper can decide if its fully-baked, or if it needs more changes and then another round of review.

@pnkfelix pnkfelix 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-review Status: Awaiting review from the assignee but also interested parties. labels May 22, 2019
@Centril
Copy link
Contributor

Centril commented May 22, 2019

The perf results look entirely acceptable to me.

I am surprised that things don't look better tho... I would have expected to regain more of the loss. The mir-opt test for simplify_if looks almost as before (diff in https://github.com/rust-lang/rust/pull/59288/files?file-filters%5B%5D=.stderr#diff-2482362a90df359dd3fd19add1f77160) except for initial -> after-copy-prop. Maybe revert it back to initial to see how it looks then?

@matthewjasper maybe I missed it, but did you add a test case for the actual problem described in #60571 somewhere in this PR?

I don't see a mir-opt test for match cond { true => expr, _ => expr } -- maybe add one?

@matthewjasper
Copy link
Contributor Author

Just to give an update here: it's taken me awhile to get back to this. I've had a look at the encoding perf regression and I think I've identified the problem there. There are some other things that I want to check as well. I'll push an updated PR and mark this ready for review once I'm happy.

@matthewjasper matthewjasper force-pushed the optimize-false-edges branch from d7f31a6 to cafb9d7 Compare May 25, 2019 18:43
@matthewjasper matthewjasper marked this pull request as ready for review May 25, 2019 18:44
@bors
Copy link
Contributor

bors commented May 25, 2019

☔ The latest upstream changes (presumably #59276) made this pull request unmergeable. Please resolve the merge conflicts.

@matthewjasper matthewjasper force-pushed the optimize-false-edges branch 2 times, most recently from 9425a84 to 967d806 Compare May 25, 2019 19:45
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-6.0 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.
travis_time:end:187e1868:start=1558813610429466370,finish=1558813611352910723,duration=923444353
$ git checkout -qf FETCH_HEAD
travis_fold:end:git.checkout

Encrypted environment variables have been removed for security reasons.
See https://docs.travis-ci.com/user/pull-requests/#pull-requests-and-security-restrictions
$ export SCCACHE_BUCKET=rust-lang-ci-sccache2
$ export SCCACHE_REGION=us-west-1
$ export GCP_CACHE_BUCKET=rust-lang-ci-cache
$ export AWS_ACCESS_KEY_ID=AKIA46X5W6CZEJZ6XT55
---

[00:04:45] travis_fold:start:tidy
travis_time:start:tidy
tidy check
[00:04:45] tidy error: /checkout/src/test/mir-opt/simple-match.rs: missing trailing newline
[00:04:50] some tidy checks failed
[00:04:50] 
[00:04:50] 
[00:04:50] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/tidy" "/checkout/src" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "--no-vendor" "--quiet"
[00:04:50] 
[00:04:50] 
[00:04:50] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test src/tools/tidy
[00:04:50] Build completed unsuccessfully in 0:01:14
[00:04:50] Build completed unsuccessfully in 0:01:14
[00:04:50] make: *** [tidy] Error 1
[00:04:50] Makefile:67: recipe for target 'tidy' failed
The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:0175fbed
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
Sat May 25 19:51:52 UTC 2019
---
travis_time:end:024bd5a0:start=1558813913673628966,finish=1558813913678751443,duration=5122477
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:0cfa2659
$ 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 --batch -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:1360d07a
travis_time:start:1360d07a
$ 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:01e3ff4e
$ 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)

@matthewjasper matthewjasper force-pushed the optimize-false-edges branch from 967d806 to 2f57a82 Compare May 26, 2019 07:19
@matthewjasper
Copy link
Contributor Author

@bors try

@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 Jun 15, 2019
@bors
Copy link
Contributor

bors commented Jun 16, 2019

⌛ Testing commit 89ea69a with merge 8e774d33f9956f3e6a69fb85878dc754cb879f63...

@bors
Copy link
Contributor

bors commented Jun 16, 2019

💔 Test failed - checks-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 Jun 16, 2019
@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.
wry
xmoto
zxing-cpp
==> Downloading https://homebrew.bintray.com/bottles/xz-5.2.4.high_sierra.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/e7/e7be50f4ee00e35887f3957263334eb3baba59e8c061919060f9259351be6880?__gda__=exp=1560658978~hmac=d17387110db7f132e30e72a949d877d54e4210a16f2ce2ddb184536cddb08ff1&response-content-disposition=attachment%3Bfilename%3D%22xz-5.2.4.high_sierra.bottle.tar.gz%22&response-content-type=application%2Fgzip&requestInfo=U2FsdGVkX18BAMtcNxgn4RaqmQDPzLPZEyaORdcRx0miRvq5C9n0ub5jqikY5233XwSvvEB43V-cxhyfMb-ByetXSDWJD-RAkm89HLoJfEgovXVg1j8hAes8_kHO38YGrVwP6woF72XM00Oy9rYJgg&response-X-Checksum-Sha1=32dc0b28e61f32b40c20e2993418aa8cb6e746d5&response-X-Checksum-Sha2=e7be50f4ee00e35887f3957263334eb3baba59e8c061919060f9259351be6880
🍺  /usr/local/Cellar/xz/5.2.4: 92 files, 1MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/travis/Library/Caches/Homebrew/boost-1.66.0.high_sierra.bottle.tar.gz... (84.6MB)
Removing: /Users/travis/Library/Caches/Homebrew/carthage-0.28.0.high_sierra.bottle.tar.gz... (8.3MB)
---
Pruned 0 symbolic links and 5 directories from /usr/local
==> Installing dependencies for swig: pcre
==> Installing swig dependency: pcre
==> Downloading https://homebrew.bintray.com/bottles/pcre-8.43.high_sierra.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/03/0389911a93a88efd4a69b52dea8ecb872fdb55bcfff45d2f7313be5f79730861?__gda__=exp=1560658989~hmac=e800ec638b9ca76c9bff1cca98ac14dc8868ca1cb2cc1ae01a7cc99f7a9d1e12&response-content-disposition=attachment%3Bfilename%3D%22pcre-8.43.high_sierra.bottle.tar.gz%22&response-content-type=application%2Fgzip&requestInfo=U2FsdGVkX1_z6srsf8PJuqmBoTlF3dTYZSeyXX-LFxyVnluF46RO616TO-PTQY_n2hbrXQnpuqmos4RH0CPxJUiRRnvYtpMHJUZyQm74RXC9pLogKXBBhgDSbQX1M1kuqpBzZG-ckU6ZJB5RBkvnHA&response-X-Checksum-Sha1=c67d4b99bb245f0ea56b34118dd6325b06a7250c&response-X-Checksum-Sha2=0389911a93a88efd4a69b52dea8ecb872fdb55bcfff45d2f7313be5f79730861
🍺  /usr/local/Cellar/pcre/8.43: 204 files, 5.5MB
==> Installing swig
==> Downloading https://homebrew.bintray.com/bottles/swig-4.0.0.high_sierra.bottle.tar.gz
==> Downloading https://homebrew.bintray.com/bottles/swig-4.0.0.high_sierra.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/ae/aed79cb436b3a0ac5812c4085e3121ffd62866397b8c7eaa06815ed8ec1e22b7?__gda__=exp=1560658992~hmac=b1546b871583b353d631a1af621a6807f1c233fd64996c2f57f029d216c43b65&response-content-disposition=attachment%3Bfilename%3D%22swig-4.0.0.high_sierra.bottle.tar.gz%22&response-content-type=application%2Fgzip&requestInfo=U2FsdGVkX1-Ox7nzqPrnTBs0LoA6EKWvjfs3fvq4y6XrsNq5LP_U0_wxHPGp1tJ4b1mqu-9kP_xtlQDUdt_czxKuTFppob3O7eniZwV-Soo6Ny4cuDz4SoWEa_MB-dmHZr_fxwfanq3Kx_wyEaZmGw&response-X-Checksum-Sha1=a9c428aee4337d91061a69c02d7ae508b627d03f&response-X-Checksum-Sha2=aed79cb436b3a0ac5812c4085e3121ffd62866397b8c7eaa06815ed8ec1e22b7
🍺  /usr/local/Cellar/swig/4.0.0: 722 files, 5.4MB
travis_time:end:0e1c4d77:start=1560657930121139000,finish=1560658309795399000,duration=379674260000
travis_fold:end:install
travis_fold:start:before_script.1
---
[00:01:41]    Compiling serde_derive v1.0.81
[00:02:07]    Compiling serde_json v1.0.33
[00:02:07]    Compiling toml v0.4.10
[00:02:12]    Compiling bootstrap v0.0.0 (/Users/travis/build/rust-lang/rust/src/bootstrap)
No output has been received in the last 30m0s, this potentially indicates a stalled build or something wrong with the build itself.
Check the details on how to adjust your build configuration on: https://docs.travis-ci.com/user/common-build-problems/#Build-times-out-because-no-output-was-received
The build has been terminated

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)

@Centril
Copy link
Contributor

Centril commented Jun 16, 2019

@bors retry spurious 30 min

@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 Jun 16, 2019
@bors
Copy link
Contributor

bors commented Jun 16, 2019

⌛ Testing commit 89ea69a with merge 6865502...

bors added a commit that referenced this pull request Jun 16, 2019
Optimize matches

Attempt to fix or improve #60571

This is breaking some diagnostics because the MIR for match arms isn't in source order any more.

cc @Centril
@bors
Copy link
Contributor

bors commented Jun 16, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: pnkfelix
Pushing 6865502 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 16, 2019
@bors bors merged commit 89ea69a into rust-lang:master Jun 16, 2019
@rust-highfive
Copy link
Collaborator

📣 Toolstate changed by #60730!

Tested on commit 6865502.
Direct link to PR: #60730

💔 rls on windows: test-pass → test-fail (cc @Xanewok, @rust-lang/infra).

rust-highfive added a commit to rust-lang-nursery/rust-toolstate that referenced this pull request Jun 16, 2019
Tested on commit rust-lang/rust@6865502.
Direct link to PR: <rust-lang/rust#60730>

💔 rls on windows: test-pass → test-fail (cc @Xanewok, @rust-lang/infra).
@matthewjasper matthewjasper deleted the optimize-false-edges branch July 29, 2019 19:53
@Nadrieril
Copy link
Member

@matthewjasper what benchmark did you use to measure the impact of a1d0266 (#60730)? You mention "encoding" but that's a compile-time benchmark today. Context is that I just tried reverting that commit to see if it was still useful and nothing on perf.rlo picked up a difference (see here).

@matthewjasper
Copy link
Contributor Author

I can't really remember using anything other than the perf.rlo benchmarks. Maybe I had a test that consisted only of a very large match against a lot of string literals.

@Nadrieril
Copy link
Member

Oh wait, your commit says "spend less time optimizing", so it was a compile time problem! The encoding benchmark makes sense then. I remember encoding existed already at the time, must be that. That's awesome it means we can remove the hack!

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 19, 2023
… r=<try>

match lowering: Remove the `make_target_blocks` hack

This hack was introduced 4 years ago in [`a1d0266` (rust-lang#60730)](rust-lang@a1d0266) to improve LLVM optimization time, specifically noticed in the `encoding` benchmark. Measurements today indicate it is no longer needed.

r? `@matthewjasper`
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 19, 2023
… r=matthewjasper

match lowering: Remove the `make_target_blocks` hack

This hack was introduced 4 years ago in [`a1d0266` (rust-lang#60730)](rust-lang@a1d0266) to improve LLVM optimization time, specifically noticed in the `encoding` benchmark. Measurements today indicate it is no longer needed.

r? `@matthewjasper`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.

9 participants