-
Notifications
You must be signed in to change notification settings - Fork 303
Add initial draft of side effect post #534
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
Merged
Mark-Simulacrum
merged 2 commits into
rust-lang:master
from
Mark-Simulacrum:side-effect
Mar 19, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
layout: post | ||
title: "Resolving Rust's forward progress guarantees" | ||
author: Mark Rousskov | ||
description: "Should side-effect be the fix?" | ||
team: the compiler team <https://www.rust-lang.org/governance/teams/compiler> | ||
--- | ||
|
||
There has been a longstanding miscompilation in Rust: programs that do not make | ||
[forward progress]. Note that the previous link is to the C++ definition; Rust | ||
is not C++, but currently LLVM optimizes all LLVM IR with the assumption that a | ||
lack of forward progress is undefined behavior. | ||
|
||
Note also that Rust does not define a lack of forward progress as [undefined | ||
behavior], while C++ does. It is particularly common to encounter the | ||
miscompilation "intentionally" when writing panic handlers and other such code | ||
with a body of `loop {}`. Some users also report that they've unintentionally | ||
hit this bug in recursive code which accidentally lacks a base case. | ||
|
||
Somewhat recently, LLVM added an intrinsic which tells the optimizer that | ||
forward progress has been made. On nightly Rust, you can enable this with | ||
`-Zinsert-sideeffect`, which will use some heuristics to insert it where it's | ||
possibly needed (currently, massively overshooting the minimal set). | ||
|
||
However, recent attempts to enable this intrinsic by default hit a snag: it's | ||
very expensive on compile times to do so ([3-30% regressions][compile-time | ||
regressions]). There is some runtime effect as well; check builds (which do not | ||
generate LLVM IR or run LLVM passes) regressed by up to 3-7%. | ||
|
||
The current implementation in rustc emits calls to the side effect intrinsic | ||
very aggressively; certainly in way more cases than is strictly necessary. | ||
However, there's not really any good ideas on how to improve the analysis rustc | ||
does without missing edge cases: we'd have to be "as good" as LLVM to emit only | ||
when necessary. | ||
|
||
Upstream, in LLVM, discussion has been ongoing for some time around whether, and | ||
how to, adjust LLVM's model to permit frontends for languages like Rust to | ||
opt-out of the forward progress guarantees. It seems unlikely that a solution | ||
will materialize in upstream LLVM that allows us to opt-out in the short term. | ||
|
||
However, having said that, side effect itself is likely improvable to at least | ||
avoid the excessive consecutive calls, as demonstrated by this [IR][IR-test] | ||
that occurs after LLVM optimizations. It seems plausible that those | ||
improvements may also reduce the compile time hit that we see when enabling | ||
side effect on the rustc side. Having said that, how simple these improvements | ||
are is unclear. | ||
|
||
We would love to hear feedback and suggestions on how to resolve this problem! | ||
Please leave feedback on this internals thread (link to be filled in right | ||
before merging). | ||
|
||
[IR-test]: https://gist.github.com/nikic/7e521def71d106c345a255e464b18d3f | ||
[compile-time regressions]: https://perf.rust-lang.org/compare.html?start=66b0c97070f422cb82baaaafc79ee94cab4396c5&end=548b5e75afd6bad696920dfdb69c9812ce0488f1 | ||
[forward progress]: https://en.cppreference.com/w/cpp/language/memory_model#Forward_progress | ||
[undefined behavior]: https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#undefined-behavior |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.