Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[WIP] Don't drop an enum after all its fields have been moved from #68493

Closed

Conversation

ecstatic-morse
Copy link
Contributor

@ecstatic-morse ecstatic-morse commented Jan 23, 2020

Drop elaboration is smart enough to realize when all fields of a struct or tuple are moved out of along a given code path and will remove drop terminators on that code path. However, if all fields of an enum with multiple variants are moved out of, drop terminators are still emitted. After this PR, drop elaboration will eliminate drops of an Option after the value in the Some variant is moved out of. Option::unwrap is an example of code whose MIR should be a bit better optimized as a result.

fn unwrap<T>(opt: Option<T>) -> T {
    match opt {
        Some(inner) => inner,
        None => panic!("unwrap"),
    }
}

When I say all fields of an enum, I mean all fields across all variants, so this optimization does not apply when, e.g., the value in Result::Ok is moved out of. To improve this case, we would need to incorporate information about the variant that is currently live into the {Un,}InitializedPlaces dataflow analyses. This is more involved, but I plan to give it a try.

This is a WIP pending perf results and the addition of a test.

@rust-highfive
Copy link
Collaborator

r? @zackmdavis

(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 Jan 23, 2020
@ecstatic-morse
Copy link
Contributor Author

@bors try
@rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion

@bors
Copy link
Contributor

bors commented Jan 23, 2020

⌛ Trying commit 06dbaec with merge 1352d19...

bors added a commit that referenced this pull request Jan 23, 2020
[WIP] Don't `drop` enum if all fields are moved out of

Previously, drop elaboration is smart enough to realize when all fields of a struct or tuple are moved out of along a given code path and will remove drop terminators on that code path. However, if all fields of an *enum* with multiple variants are moved out of, drop terminators are still emitted. After this PR, drop elaboration will eliminate drops of an `Option` after the value in the `Some` variant is moved out of. `Option::unwrap` is an example of code whose MIR should be a bit better optimized as a result of this.

```rust
fn unwrap<T>(opt: Option<T>) -> T {
    match opt {
        Some(inner) => inner,
        None => panic!("unwrap"),
    }
}
```

When I say all fields of an enum, I mean all fields across all variants, so this optimization does not apply when, e.g., the value in `Result::Ok` is moved out of. To improve this case, we would need to incorporate information about the variant that is currently live into the `{Un,}InitializedPlaces` dataflow analyses. This is more involved, but I plan to give it a try.
@ecstatic-morse ecstatic-morse changed the title [WIP] Don't drop enum if all fields are moved out of [WIP] Don't drop an enum after all fields have been moved from Jan 23, 2020
@ecstatic-morse ecstatic-morse changed the title [WIP] Don't drop an enum after all fields have been moved from [WIP] Don't drop an enum after all its fields have been moved from Jan 23, 2020
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-7 of your PR failed (pretty log, 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.
2020-01-23T20:42:49.3776988Z ========================== Starting Command Output ===========================
2020-01-23T20:42:49.3778934Z [command]/bin/bash --noprofile --norc /home/vsts/work/_temp/73103080-a521-4555-b816-d1d6a1c3ae5f.sh
2020-01-23T20:42:49.3778996Z 
2020-01-23T20:42:49.3781775Z ##[section]Finishing: Disable git automatic line ending conversion
2020-01-23T20:42:49.3787849Z ##[section]Starting: Checkout rust-lang/rust@refs/pull/68493/merge to s
2020-01-23T20:42:49.3789850Z Task         : Get sources
2020-01-23T20:42:49.3789883Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.
2020-01-23T20:42:49.3789916Z Version      : 1.0.0
2020-01-23T20:42:49.3789947Z Author       : Microsoft
---
2020-01-23T20:42:50.4551468Z ##[command]git remote add origin https://github.com/rust-lang/rust
2020-01-23T20:42:50.4562579Z ##[command]git config gc.auto 0
2020-01-23T20:42:50.4564701Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2020-01-23T20:42:50.4567216Z ##[command]git config --get-all http.proxy
2020-01-23T20:42:50.4573556Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/68493/merge:refs/remotes/pull/68493/merge
---
2020-01-23T21:12:06.2060272Z    Compiling semver v0.9.0
2020-01-23T21:12:08.4568859Z error: failed to run custom build command for `libc v0.2.66`
2020-01-23T21:12:08.4569538Z 
2020-01-23T21:12:08.4570068Z Caused by:
2020-01-23T21:12:08.4570761Z   process didn't exit successfully: `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-rustc/release/build/libc-e240690bd4c21baf/build-script-build` (signal: 11, SIGSEGV: invalid memory reference)
2020-01-23T21:12:11.4467048Z error: build failed
2020-01-23T21:12:11.4492698Z command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "build" "-Zconfig-profile" "--target" "x86_64-unknown-linux-gnu" "-Zbinary-dep-depinfo" "-j" "2" "--release" "--locked" "--color" "always" "--features" " llvm" "--manifest-path" "/checkout/src/rustc/Cargo.toml" "--message-format" "json-render-diagnostics"
2020-01-23T21:12:11.4492887Z expected success, got: exit code: 101
2020-01-23T21:12:11.4507562Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
2020-01-23T21:12:11.4507562Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
2020-01-23T21:12:11.4507676Z Build completed unsuccessfully in 0:23:29
2020-01-23T21:12:11.4569964Z == clock drift check ==
2020-01-23T21:12:11.4592650Z   local time: Thu Jan 23 21:12:11 UTC 2020
2020-01-23T21:12:12.0123967Z   network time: Thu, 23 Jan 2020 21:12:12 GMT
2020-01-23T21:12:12.0135482Z == end clock drift check ==
2020-01-23T21:12:12.7750996Z 
2020-01-23T21:12:12.7857581Z ##[error]Bash exited with code '1'.
2020-01-23T21:12:12.7874083Z ##[section]Finishing: Run build
2020-01-23T21:12:12.7893547Z ##[section]Starting: Checkout rust-lang/rust@refs/pull/68493/merge to s
2020-01-23T21:12:12.7895376Z Task         : Get sources
2020-01-23T21:12:12.7895426Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.
2020-01-23T21:12:12.7895475Z Version      : 1.0.0
2020-01-23T21:12:12.7895537Z Author       : Microsoft
2020-01-23T21:12:12.7895537Z Author       : Microsoft
2020-01-23T21:12:12.7895585Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkId=798199)
2020-01-23T21:12:12.7895636Z ==============================================================================
2020-01-23T21:12:13.2544774Z Cleaning any cached credential from repository: rust-lang/rust (GitHub)
2020-01-23T21:12:13.2585430Z ##[section]Finishing: Checkout rust-lang/rust@refs/pull/68493/merge to s
2020-01-23T21:12:13.2706844Z Cleaning up task key
2020-01-23T21:12:13.2707615Z Start cleaning up orphan processes.
2020-01-23T21:12:13.2816137Z Terminate orphan process: pid (3426) (python)
2020-01-23T21:12:13.3027381Z ##[section]Finishing: Finalize Job

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)

@ecstatic-morse
Copy link
Contributor Author

@bors try-

@rust-highfive
Copy link
Collaborator

The job dist-x86_64-linux-alt of your PR failed (pretty log, 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.
2020-01-23T21:45:59.2798788Z    Compiling unicode-normalization v0.1.11
2020-01-23T21:45:59.4941383Z error: failed to run custom build command for `byteorder v1.3.2`
2020-01-23T21:45:59.4941579Z 
2020-01-23T21:45:59.4941695Z Caused by:
2020-01-23T21:45:59.4942193Z   process didn't exit successfully: `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-rustc/release/build/byteorder-4f81ecbabe171043/build-script-build` (signal: 11, SIGSEGV: invalid memory reference)
2020-01-23T21:46:05.7722794Z error: build failed
2020-01-23T21:46:05.7750220Z command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "build" "-Zconfig-profile" "--target" "x86_64-unknown-linux-gnu" "-Zbinary-dep-depinfo" "-j" "2" "--release" "--locked" "--color" "always" "--features" "jemalloc llvm" "--manifest-path" "/checkout/src/rustc/Cargo.toml" "--message-format" "json-render-diagnostics"
2020-01-23T21:46:05.7750722Z expected success, got: exit code: 101
2020-01-23T21:46:05.7765254Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap dist --host x86_64-unknown-linux-gnu --target x86_64-unknown-linux-gnu
2020-01-23T21:46:05.7765254Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap dist --host x86_64-unknown-linux-gnu --target x86_64-unknown-linux-gnu
2020-01-23T21:46:05.7765646Z Build completed unsuccessfully in 0:56:24
2020-01-23T21:46:05.7818737Z == clock drift check ==
2020-01-23T21:46:05.7834667Z   local time: Thu Jan 23 21:46:05 UTC 2020
2020-01-23T21:46:06.4993129Z   network time: Thu, 23 Jan 2020 21:46:06 GMT
2020-01-23T21:46:06.4994010Z == end clock drift check ==
2020-01-23T21:46:07.9003668Z 
2020-01-23T21:46:07.9092385Z ##[error]Bash exited with code '1'.
2020-01-23T21:46:07.9129154Z ##[section]Starting: Checkout rust-lang/rust@try to s
2020-01-23T21:46:07.9131215Z ==============================================================================
2020-01-23T21:46:07.9131324Z Task         : Get sources
2020-01-23T21:46:07.9131413Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.

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)

@Mark-Simulacrum
Copy link
Member

@bors retry

@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 Jan 23, 2020
@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Jan 23, 2020

@Mark-Simulacrum This one's not quite right yet unfortunately. So much for my one line change. There's a bit of discussion on Zulip.

Edit: just saw the discord discussion

@ecstatic-morse
Copy link
Contributor Author

This optimization will require additional information about the liveness of enum variants. Closing for now.

# for free to join this conversation on GitHub. Already have an account? # 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.

6 participants