Commit graph

516 commits

Author SHA1 Message Date
Mara Bos
336dc18aa3
Rollup merge of #79027 - tmiasko:inline-always-live-locals, r=oli-obk
Limit storage duration of inlined always live locals

Closes #76375.
2020-11-17 10:06:21 +01:00
bors
b5c37e86ff Auto merge of #78801 - sexxi-goose:min_capture, r=nikomatsakis
RFC-2229: Implement Precise Capture Analysis

### This PR introduces
- Feature gate for RFC-2229 (incomplete) `capture_disjoint_field`
- Rustc Attribute to print out the capture analysis `rustc_capture_analysis`
- Precise capture analysis

### Description of the analysis
1. If the feature gate is not set then all variables that are not local to the closure will be added to the list of captures. (This is for backcompat)
2. The rest of the analysis is based entirely on how the captured `Place`s are used within the closure. Precise information (i.e. projections) about the `Place` is maintained throughout.
3. To reduce the amount of information we need to keep track of, we do a minimization step. In this step, we determine a list such that no Place within this list represents an ancestor path to another entry in the list.  Check rust-lang/project-rfc-2229#9 for more detailed examples.
4. To keep the compiler functional as before we implement a Bridge between the results of this new analysis to existing data structures used for closure captures. Note the new capture analysis results are only part of MaybeTypeckTables that is the information is only available during typeck-ing.

### Known issues
- Statements like `let _ = x` will make the compiler ICE when used within a closure with the feature enabled. More generally speaking the issue is caused by `let` statements that create no bindings and are init'ed using a Place expression.

### Testing
We removed the code that would handle the case where the feature gate is not set, to enable the feature as default and did a bors try and perf run. More information here: #78762

### Thanks
This has been slowly in the works for a while now.
I want to call out `@Azhng` `@ChrisPardy` `@null-sleep` `@jenniferwills` `@logmosier` `@roxelo` for working on this and the previous PRs that led up to this, `@nikomatsakis` for guiding us.

Closes rust-lang/project-rfc-2229#7
Closes rust-lang/project-rfc-2229#9
Closes rust-lang/project-rfc-2229#6
Closes rust-lang/project-rfc-2229#19

r? `@nikomatsakis`
2020-11-17 03:56:03 +00:00
Bastian Kauschke
2bf93bd852 compiler: fold by value 2020-11-16 22:34:57 +01:00
Jonas Schievink
ce775bc4f6
Rollup merge of #79036 - cjgillot:steal, r=oli-obk
Move Steal to rustc_data_structures.
2020-11-15 13:39:59 +01:00
Jonas Schievink
9c6d3c0940
Rollup merge of #79031 - camelid:mir-validate-local-decl, r=jonas-schievink
Validate that locals have a corresponding `LocalDecl`

Fixes #73356.
2020-11-15 13:39:56 +01:00
Jonas Schievink
8825942e86
Rollup merge of #77802 - jyn514:bootstrap-specific, r=nikomatsakis
Allow making `RUSTC_BOOTSTRAP` conditional on the crate name

Motivation: This came up in the [Zulip stream](https://rust-lang.zulipchat.com/#narrow/stream/233931-t-compiler.2Fmajor-changes/topic/Require.20users.20to.20confirm.20they.20know.20RUSTC_.E2.80.A6.20compiler-team.23350/near/208403962) for https://github.com/rust-lang/compiler-team/issues/350.
See also https://github.com/rust-lang/cargo/pull/6608#issuecomment-458546258; this implements https://github.com/rust-lang/cargo/issues/6627.
The goal is for this to eventually allow prohibiting setting `RUSTC_BOOTSTRAP` in build.rs (https://github.com/rust-lang/cargo/issues/7088).

## User-facing changes

- `RUSTC_BOOTSTRAP=1` still works; there is no current plan to remove this.
- Things like `RUSTC_BOOTSTRAP=0` no longer activate nightly features. In practice this shouldn't be a big deal, since `RUSTC_BOOTSTRAP` is the opposite of stable and everyone uses `RUSTC_BOOTSTRAP=1` anyway.
- `RUSTC_BOOTSTRAP=x` will enable nightly features only for crate `x`.
- `RUSTC_BOOTSTRAP=x,y` will enable nightly features only for crates `x` and `y`.

## Implementation changes

The main change is that `UnstableOptions::from_environment` now requires
an (optional) crate name. If the crate name is unknown (`None`), then the new feature is not available and you still have to use `RUSTC_BOOTSTRAP=1`. In practice this means the feature is only available for `--crate-name`, not for `#![crate_name]`; I'm interested in supporting the second but I'm not sure how.

Other major changes:

- Added `Session::is_nightly_build()`, which uses the `crate_name` of
the session
- Added `nightly_options::match_is_nightly_build`, a convenience method
for looking up `--crate-name` from CLI arguments.
`Session::is_nightly_build()`should be preferred where possible, since
it will take into account `#![crate_name]` (I think).
- Added `unstable_features` to `rustdoc::RenderOptions`

I'm not sure whether this counts as T-compiler or T-lang; _technically_ RUSTC_BOOTSTRAP is an implementation detail, but it's been used so much it seems like this counts as a language change too.

r? `@joshtriplett`
cc `@Mark-Simulacrum` `@hsivonen`
2020-11-15 13:39:43 +01:00
Dylan DPC
7e0441dc36
Rollup merge of #78969 - tmiasko:normalize, r=davidtwco
Normalize function type during validation

During inlining, the callee body is normalized and has types revealed,
but some of locals corresponding to the arguments might come from the
caller body which is not. As a result the caller body does not pass
validation without additional normalization.

Closes #78442.
2020-11-15 03:02:51 +01:00
Dylan DPC
6be44ed3b5
Rollup merge of #78966 - tmiasko:inline-never, r=oli-obk
Never inline C variadics, cold functions, functions with incompatible attributes ...

... and fix generator inlining.

Closes #67863.
Closes #78859.
2020-11-15 03:02:48 +01:00
Dylan DPC
335a2554f9
Rollup merge of #78963 - richkadel:llvm-coverage-counters-2.0.4, r=tmandry
Added some unit tests as requested

As discussed in PR #78267, for example:

* https://github.com/rust-lang/rust/pull/78267#discussion_r515404722
* https://github.com/rust-lang/rust/pull/78267#discussion_r515405958

r? ```````@tmandry```````
FYI: ```````@wesleywiser```````

This is pretty much self contained, but depending on feedback and timing, I may have a chance to add a few more unit tests requested against `counters.rs`. I'm looking at those now.
2020-11-15 03:02:46 +01:00
Tomasz Miąsko
f27d56d1ff Limit storage duration of inlined always live locals 2020-11-15 00:00:00 +00:00
bors
361c4ea224 Auto merge of #79049 - tmiasko:lower-intrinsics, r=jonas-schievink
Lower intrinsics calls: forget, size_of, unreachable, wrapping_*

This allows constant propagation to evaluate `size_of` and `wrapping_*`,
and unreachable propagation to propagate a call to `unreachable`.

The lowering is performed as a MIR optimization, rather than during MIR
building to preserve the special status of intrinsics with respect to
unsafety checks and promotion.

Currently enabled by default to determine the performance impact (no
significant impact expected). In practice only useful when combined with
inlining since intrinsics are rarely used directly (with exception of
`unreachable` and `discriminant_value` used by built-in derive macros).

Closes #32716.
2020-11-14 22:05:54 +00:00
Camille GILLOT
41c44b498f Move Steal to rustc_data_structures. 2020-11-14 01:30:56 +01:00
Tomasz Miąsko
6903273339 Lower intrinsics calls: forget, size_of, unreachable, wrapping_*
This allows constant propagation to evaluate `size_of` and `wrapping_*`,
and unreachable propagation to propagate a call to `unreachable`.

The lowering is performed as a MIR optimization, rather than during MIR
building to preserve the special status of intrinsics with respect to
unsafety checks and promotion.
2020-11-14 00:00:00 +00:00
Camelid
7eb1a1afcf Validate that locals have a corresponding LocalDecl 2020-11-13 12:54:42 -08:00
Rich Kadel
b4b0ef3e4b Addressed feedback 2020-11-13 09:07:39 -08:00
Rich Kadel
c131063988 Added a unit test for BcbCounters
Restructured the code a little, to allow getting both the mir::Body and
coverage graph.
2020-11-12 16:22:25 -08:00
Tomasz Miąsko
99be78d135 Always use param_env_reveal_all_normalized in validator 2020-11-12 20:57:43 +01:00
Tomasz Miąsko
d486bfcbff Normalize function type during validation
During inlining, the callee body is normalized and has types revealed,
but some of locals corresponding to the arguments might come from the
caller body which is not. As a result the caller body does not pass
validation without additional normalization.
2020-11-12 20:57:43 +01:00
Tomasz Miąsko
79d853ecce Never inline C variadic functions 2020-11-12 20:09:04 +01:00
Tomasz Miąsko
66cadec176 Fix generator inlining by checking for rust-call abi and spread arg 2020-11-12 20:09:04 +01:00
Tomasz Miąsko
9bb3d6b7d4 Remove check for impossible condition
The callee body is already transformed; the condition is always false.
2020-11-12 19:52:03 +01:00
Tomasz Miąsko
ae4332643d Never inline cold functions
The information about cold attribute is lost during inlining,
Avoid the issue by never inlining cold functions.
2020-11-12 19:52:03 +01:00
Tomasz Miąsko
0b4af1614d Never inline when no_sanitize attributes differ
The inliner looks if a sanitizer is enabled before considering
`no_sanitize` attribute as possible source of incompatibility.

The MIR inlining could happen in a crate with sanitizer disabled, but
code generation in a crate with sanitizer enabled, thus the attribute
would be incorrectly ignored.

To avoid the issue never inline functions with different `no_sanitize`
attributes.
2020-11-12 19:52:03 +01:00
Vishnunarayan K I
8119c4beee review comments 2020-11-12 21:08:18 +05:30
Vishnunarayan K I
5029a19313 check mir exists before validation; fix tests 2020-11-12 21:08:18 +05:30
Vishnunarayan K I
6781907444 fix tests and formatting 2020-11-12 21:08:18 +05:30
Vishnunarayan K I
8bce9af78c add error_occured field to ConstQualifs, fix #76064 2020-11-12 21:08:18 +05:30
Rich Kadel
eb9f2bb3b0 Overcome Sync issues with non-parallel compiler
Per Mark's recommendation at:
https://github.com/rust-lang/rust/pull/78963#issuecomment-725790071
2020-11-11 20:36:41 -08:00
Rich Kadel
bd0eb07af2 Added some unit tests as requested
As discussed in PR #78267, for example:

* https://github.com/rust-lang/rust/pull/78267#discussion_r515404722
* https://github.com/rust-lang/rust/pull/78267#discussion_r515405958
2020-11-11 16:40:17 -08:00
Jonas Schievink
0b521e5c47
Rollup merge of #78899 - tmiasko:inline-diverging, r=oli-obk
Support inlining diverging function calls

The existing heuristic does penalize diverging calls to some degree, but since
it never inlined them previously it might need some further modifications.

Additionally introduce storage markers for all temporaries created by
the inliner. The temporary introduced for destination rebrorrow, didn't
use them previously.
2020-11-11 20:59:05 +01:00
Jonas Schievink
919177f7e4
Rollup merge of #78873 - tmiasko:inline-opts, r=oli-obk
Add flags customizing behaviour of MIR inlining

* `-Zinline-mir-threshold` to change the default threshold.
* `-Zinline-mir-hint-threshold` to change the threshold used by
  functions with inline hint.

Having those as configurable flags makes it possible to experiment with with
different inlining thresholds and substantially increase test coverage of MIR
inlining when used with increased thresholds (for example, necessary to test
#78844).
2020-11-11 20:59:03 +01:00
Nicholas-Baron
261ca04c92 Changed unwrap_or to unwrap_or_else in some places.
The discussion seems to have resolved that this lint is a bit "noisy" in
that applying it in all places would result in a reduction in
readability.

A few of the trivial functions (like `Path::new`) are fine to leave
outside of closures.

The general rule seems to be that anything that is obviously an
allocation (`Box`, `Vec`, `vec![]`) should be in a closure, even if it
is a 0-sized allocation.
2020-11-10 20:07:47 -08:00
Aman Arora
8f0c0d656d Initial work for doing minimum capture analysis for RFC-2229
Co-authored-by: Chris Pardy <chrispardy36@gmail.com>
Co-authored-by: Logan Mosier <logmosier@gmail.com>
2020-11-10 20:58:54 -05:00
Jonas Schievink
a08e7afefd
Rollup merge of #78887 - camelid:dataflow-state-decl, r=jonas-schievink
Add comments to explain memory usage optimization

Add explanatory comments so that people understand that it's just an optimization and doesn't affect behavior.
2020-11-10 14:45:23 +01:00
Jonas Schievink
105f4b8792
Rollup merge of #78875 - petrochenkov:cleantarg, r=Mark-Simulacrum
rustc_target: Further cleanup use of target options

Follow up to https://github.com/rust-lang/rust/pull/77729.

Implements items 2 and 4 from the list in https://github.com/rust-lang/rust/pull/77729#issue-500228243.

The first commit collapses uses of `target.options.foo` into `target.foo`.

The second commit renames some target options to avoid tautology:
`target.target_endian` -> `target.endian`
`target.target_c_int_width` -> `target.c_int_width`
`target.target_os` -> `target.os`
`target.target_env` -> `target.env`
`target.target_vendor` -> `target.vendor`
`target.target_family` -> `target.os_family`
`target.target_mcount` -> `target.mcount`

r? `@Mark-Simulacrum`
2020-11-10 14:45:21 +01:00
Jonas Schievink
9c486882e5
Rollup merge of #78847 - tmiasko:inline-return-place, r=matthewjasper
Assert that a return place is not used for indexing during integration

The inliner integrates call destination place with callee return place
by remapping the local and adding extra projections as necessary.

If a call destination place contains any projections (which is already
possible) and a return place is used in an indexing projection (most
likely doesn't happen yet) the end result would be incorrect.

Add an assertion to ensure that potential issue won't go unnoticed in
the presence of more sophisticated copy propagation scheme.
2020-11-10 14:45:17 +01:00
Tomasz Miąsko
c8943c62f7 Add flags customizing behaviour of MIR inlining
* `-Zinline-mir-threshold` to change the default threshold.
* `-Zinline-mir-hint-threshold` to change the threshold used by
  functions with inline hint.
2020-11-10 00:00:00 +00:00
Camelid
0242f963c6 Add comments to explain memory usage optimization 2020-11-09 13:34:16 -08:00
Dylan DPC
ee1fedf392
Rollup merge of #78580 - tmiasko:inline-loop, r=oli-obk
inliner: Break inlining cycles

Keep track of all instances inlined so far. When examining a new call
sites from an inlined body, skip those where callee had been inlined
already to avoid potential inlining cycles.

Fixes #78573.
2020-11-09 19:06:53 +01:00
Dylan DPC
99f16e637b
Rollup merge of #76468 - SNCPlay42:lifetime-names, r=Mark-Simulacrum
Improve lifetime name annotations for closures & async functions

* Don't refer to async functions as "generators" in error output
* Where possible, emit annotations pointing exactly at the `&` in the return type of closures (when they have explicit return types) and async functions, like we do for arguments.
Addresses #74072, but I wouldn't call that *closed* until annotations are identical for async and non-async functions.
* Emit a better annotation when the lifetime doesn't appear in the full name type, which currently happens for opaque types like `impl Future`. Addresses #74497, but further improves could probably be made (why *doesn't* it appear in the type as `impl Future + '1`?)
This is included in the same PR because the changes to `give_name_if_anonymous_region_appears_in_output` would introduce ICE otherwise (it would return `None` in cases where it didn't previously, which then gets `unwrap`ped)
2020-11-09 19:06:39 +01:00
SNCPlay42
61b52a33b3 use RegionNameHighlight for async fn and closure returns 2020-11-09 16:14:40 +00:00
Dylan DPC
b4589a86cc
Rollup merge of #78674 - tmiasko:inline-substs-for-mir-body, r=oli-obk
inliner: Use substs_for_mir_body

Changes from 68965 extended the kind of instances that are being
inlined. For some of those, the `instance_mir` returns a MIR body that
is already expressed in terms of the types found in substitution array,
and doesn't need further substitution.

Use `substs_for_mir_body` to take that into account.

Resolves #78529.
Resolves #78560.
2020-11-09 01:13:42 +01:00
Tomasz Miąsko
ffa70d75c8 Support inlining diverging function calls
Additionally introduce storage markers for all temporaries created by
the inliner. The temporary introduced for destination rebrorrow, didn't
use them previously.
2020-11-09 00:00:00 +00:00
Tomasz Miąsko
dc4d74d149 inliner: Break inlining cycles
When examining candidates for inlining, reject those that are determined
to be recursive either because of self-recursive calls or calls to any
instances already inlined.
2020-11-09 00:00:00 +00:00
Tomasz Miąsko
b7f16c56d1 inliner: Make inline_call infallible
The inliner does not support inlining of divering calls. Reject them
early on and turn `inline_call` into an infallible operation.
2020-11-09 11:41:10 +01:00
Vadim Petrochenkov
bf66988aa1 Collapse all uses of target.options.foo into target.foo
with an eye on merging `TargetOptions` into `Target`.

`TargetOptions` as a separate structure is mostly an implementation detail of `Target` construction, all its fields logically belong to `Target` and available from `Target` through `Deref` impls.
2020-11-08 17:29:13 +03:00
bors
b1277d04db Auto merge of #78874 - m-ou-se:rollup-3jp1ijj, r=m-ou-se
Rollup of 19 pull requests

Successful merges:

 - #76097 (Stabilize hint::spin_loop)
 - #76227 (Stabilize `Poll::is_ready` and `is_pending` as const)
 - #78065 (make concurrency helper more pleasant to read)
 - #78570 (Remove FIXME comment in print_type_sizes ui test suite)
 - #78572 (Use SOCK_CLOEXEC and accept4() on more platforms.)
 - #78658 (Add a tool to run `x.py` from any subdirectory)
 - #78706 (Fix run-make tests running when LLVM is disabled)
 - #78728 (Constantify `UnsafeCell::into_inner` and related)
 - #78775 (Bump Rustfmt and RLS)
 - #78788 (Correct unsigned equivalent of isize to be usize)
 - #78811 (Make some std::io functions `const`)
 - #78828 (use single char patterns for split() (clippy::single_char_pattern))
 - #78841 (Small cleanup in `TypeFoldable` derive macro)
 - #78842 (Honor the rustfmt setting in config.toml)
 - #78843 (Less verbose debug logging from inlining integrator)
 - #78852 (Convert a bunch of intra-doc links)
 - #78860 (rustc_resolve: Use `#![feature(format_args_capture)]`)
 - #78861 (typo and formatting)
 - #78865 (Don't fire `CONST_ITEM_MUTATION` lint when borrowing a deref)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2020-11-08 13:49:17 +00:00
Mara Bos
91759b2de5
Rollup merge of #78865 - Aaron1011:fix/const-item-mut-reborrow, r=varkor
Don't fire `CONST_ITEM_MUTATION` lint when borrowing a deref

Fixes #78819

This extends the check for dereferences added in PR #77324
to cover mutable borrows, as well as direct writes. If we're operating
on a dereference of a `const` item, we shouldn't be firing the lint.
2020-11-08 13:36:33 +01:00
Mara Bos
e5230fdf96
Rollup merge of #78843 - tmiasko:inline-trace, r=wesleywiser
Less verbose debug logging from inlining integrator

The inlining integrator produces relatively verbose and uninteresting
logs.  Move them from a debug log level to a trace level, so that they
can be easily isolated from others.
2020-11-08 13:36:26 +01:00
bors
87a0997ef9 Auto merge of #78410 - lcnr:revert75443, r=nikomatsakis
revert #75443, update mir validator

This PR reverts rust-lang#75443 to fix rust-lang#75992 and instead uses rust-lang#75419 to fix rust-lang#75313.

Adapts rust-lang#75419 to correctly deal with unevaluated constants as otherwise some `feature(const_evaluatable_checked)` tests would ICE.

Note that rust-lang#72793 was also fixed by rust-lang#75443, but as that issue only concerns `feature(type_alias_impl_trait)` I deleted that test case for now and would reopen that issue.

rust-lang#75443 may have also allowed some other code to now successfully compile which would make this revert a breaking change after 2 stable versions, but I hope that this is a purely theoretical concern.

See https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/generator.20upvars/near/214617274 for more reasoning about this.

r? `@nikomatsakis` `@eddyb` `@RalfJung`
2020-11-08 11:27:06 +00:00