Commit Graph

587 Commits

Author SHA1 Message Date
Guillaume Gomez 6e046fef29
Rollup merge of #120424 - RalfJung:raw-ptr-meta, r=Nilstrieb
raw pointer metadata API: data address -> data pointer

A pointer consists of [more than just an address](https://github.com/rust-lang/rfcs/pull/3559), so let's not equate "pointer" and "address" in these docs.
2024-01-30 11:19:16 +01:00
Ralf Jung b4e1c569fe raw pointer metadata API: data address -> data pointer 2024-01-29 07:56:38 +01:00
Markus Reiter 021739c840
Update tests. 2024-01-27 16:38:57 +01:00
Matthias Krüger 346397d081
Rollup merge of #119562 - LegionMammal978:rename-pin-pointer, r=Amanieu,dtolnay
Rename `pointer` field on `Pin`

A few days ago, I was helping another user create a self-referential type using `PhantomPinned`. However, I noticed an odd behavior when I tried to access one of the type's fields via `Pin`'s `Deref` impl:

```rust
use std::{marker::PhantomPinned, ptr};

struct Pinned {
    data: i32,
    pointer: *const i32,
    _pin: PhantomPinned,
}

fn main() {
    let mut b = Box::pin(Pinned {
        data: 42,
        pointer: ptr::null(),
        _pin: PhantomPinned,
    });
    {
        let pinned = unsafe { b.as_mut().get_unchecked_mut() };
        pinned.pointer = &pinned.data;
    }
    println!("{}", unsafe { *b.pointer });
}
```

```rust
error[E0658]: use of unstable library feature 'unsafe_pin_internals'
  --> <source>:19:30
   |
19 |     println!("{}", unsafe { *b.pointer });
   |                              ^^^^^^^^^

error[E0277]: `Pinned` doesn't implement `std::fmt::Display`
  --> <source>:19:20
   |
19 |     println!("{}", unsafe { *b.pointer });
   |                    ^^^^^^^^^^^^^^^^^^^^^ `Pinned` cannot be formatted with the default formatter
   |
   = help: the trait `std::fmt::Display` is not implemented for `Pinned`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
```

Since the user named their field `pointer`, it conflicts with the `pointer` field on `Pin`, which is public but unstable since Rust 1.60.0 with #93176. On versions from 1.33.0 to 1.59.0, where the field on `Pin` is private, this program compiles and prints `42` as expected.

To avoid this confusing behavior, this PR renames `pointer` to `__pointer`, so that it's less likely to conflict with a `pointer` field on the underlying type, as accessed through the `Deref` impl. This is technically a breaking change for anyone who names their field `__pointer` on the inner type; if this is undesirable, it could be renamed to something more longwinded. It's also a nightly breaking change for any external users of `unsafe_pin_internals`.
2024-01-26 23:15:49 +01:00
Ralf Jung 64cd13ff3b add test for GVN issue; cleanup in dataflow_const_prop 2024-01-26 10:40:29 +01:00
Michael Goulet 3004e8c44b Remove coroutine info when building coroutine drop body 2024-01-25 03:26:29 +00:00
Camille GILLOT d7a7be4049 Add test for jump-threading assume. 2024-01-23 00:00:22 +00:00
Camille GILLOT 161c674ef0 Add Assume custom MIR. 2024-01-22 23:55:10 +00:00
Nadrieril 203cc6930e
Rollup merge of #119461 - cjgillot:jump-threading-interp, r=tmiasko
Use an interpreter in MIR jump threading

This allows to understand assignments of aggregate constants. This case appears more frequently with GVN promoting aggregates to constants.
2024-01-21 06:38:36 +01:00
George Bateman 803b810eac
Remove feature(offset_of) from tests 2024-01-19 20:38:51 +00:00
Camille GILLOT be9668d398 Use an interpreter in jump threading. 2024-01-18 22:53:07 +00:00
Camille GILLOT 22ed51e136 Do not read a scalar on a non-scalar layout. 2024-01-16 22:32:48 +00:00
Camille GILLOT 3c48243b6f Simplify Len. 2024-01-16 22:20:54 +00:00
Camille GILLOT 5fc23ad8e6 Simplify unary operations. 2024-01-16 22:20:54 +00:00
Camille GILLOT 666030c51b Simplify binary ops. 2024-01-16 22:20:53 +00:00
LegionMammal978 bc3fb5245a Rename `pointer` field on `Pin`
The internal, unstable field of `Pin` can conflict with fields from the
inner type accessed via the `Deref` impl. Rename it from `pointer` to
`__pointer`, to make it less likely to conflict with anything else.
2024-01-16 14:58:42 -05:00
bors bf2637f4e8 Auto merge of #119954 - scottmcm:option-unwrap-failed, r=WaffleLapkin
Split out `option::unwrap_failed` like we have `result::unwrap_failed`

...and like `option::expect_failed`
2024-01-16 15:32:39 +00:00
bors fa0dc208d0 Auto merge of #119672 - cjgillot:dse-sandwich, r=oli-obk
Sandwich MIR optimizations between DSE.

This PR reorders MIR optimization passes in an attempt to increase their efficiency.

- Stop running CopyProp before GVN, it's useless as GVN will do the same thing anyway. Instead, we perform CopyProp at the end of the pipeline, to ensure we do not emit copy/move chains.
- Run DSE before GVN, as it increases the probability to have single-assignment locals.
- Run DSE after the final CopyProp to turn copies into moves.

r? `@ghost`
2024-01-16 11:34:16 +00:00
bors f9c2421a2a Auto merge of #119439 - cjgillot:gvn-faster, r=oli-obk
Avoid some redundant work in GVN

The first 2 commits are about reducing the perf effect.

Third commit avoids doing redundant work: is a local is SSA, it already has been simplified, and the resulting value is in `self.locals`. No need to call any code on it.

The last commit avoids removing some storage statements.

r? wg-mir-opt
2024-01-16 05:17:49 +00:00
Scott McMurray 23483664a2 Split out `option::unwrap_failed` like we have `result::unwrap_failed`
...and like `option::expect_failed`
2024-01-14 12:45:01 -08:00
Matthias Krüger 8294356a5d
Rollup merge of #119842 - Zalathar:kind, r=oli-obk
coverage: Add enums to accommodate other kinds of coverage mappings

Extracted from  #118305.

LLVM supports several different kinds of coverage mapping regions, but currently we only ever emit ordinary “code” regions.  This PR performs the plumbing required to add other kinds of regions as enum variants, but does not add any specific variants other than `Code`.

The main motivation for this change is branch coverage, but it will also allow separate experimentation with gap regions and skipped regions, which might help in producing more accurate and useful coverage reports.

---

``@rustbot`` label +A-code-coverage
2024-01-11 19:42:51 +01:00
Camille GILLOT bc35ee41fa Do not run simplify_locals inside DSE.
The full pass is run short after.
2024-01-11 09:58:22 +00:00
Camille GILLOT 0aedd6e86f Sandwich MIR optimizations between DSE. 2024-01-11 09:58:19 +00:00
Zalathar 124fff0777 coverage: Add enums to accommodate other kinds of coverage mappings 2024-01-11 16:43:12 +11:00
bors 3a6bf351a3 Auto merge of #119677 - cjgillot:early-cfg-opt, r=oli-obk
Reorder early post-inlining passes.

`RemoveZsts`, `RemoveUnneededDrops` and `UninhabitedEnumBranching` only depend on types, so they should be executed together early after MIR inlining introduces those types.

This does not change the end-result, but this makes the pipeline a bit more consistent.
2024-01-11 04:09:07 +00:00
Guillaume Gomez 9b905417f5
Rollup merge of #119699 - cjgillot:simplify-unreachable, r=oli-obk
Merge dead bb pruning and unreachable bb deduplication.

Both routines share the same basic structure: iterate on all bbs to identify work, and then renumber bbs.

We can do both at once.
2024-01-09 13:23:18 +01:00
bors 75c68cfd2b Auto merge of #119675 - cjgillot:set-no-discriminant, r=tmiasko
Skip threading over no-op SetDiscriminant.

Fixes https://github.com/rust-lang/rust/issues/119674
2024-01-07 15:34:05 +00:00
Camille GILLOT 4071572cb4 Merge dead bb pruning and unreachable bb deduplication. 2024-01-07 15:12:10 +00:00
Camille GILLOT 7e39100586 Avoid recording no-op replacements. 2024-01-07 13:54:05 +00:00
Camille GILLOT 4ee01faaf0 Do not re-simplify SSA locals. 2024-01-07 13:54:05 +00:00
Camille GILLOT a8c4d43cb1 Reorder early post-inlining passes. 2024-01-07 01:42:57 +00:00
bors 78c988fe3e Auto merge of #119035 - saethlin:remove-linker-requirement, r=onur-ozkan
Run Miri and mir-opt tests without a target linker

Normally, we need a linker for the target to build the standard library. That's only because `std` declares crate-type lib and dylib; building the dylib is what creates a need for the linker.

But for mir-opt tests (and for Miri) we do not need to build a `libstd.so`. So with this PR, when we build the standard library for mir-opt tests, instead of `cargo build` we run `cargo rustc --crate-type=lib` which overrides the configured crate types in `std`'s manifest.

I've also swapped in what seems to me a better hack than `BOOTSTRAP_SKIP_TARGET_SANITY` to prevent cross-interpreting with Miri from checking for a target linker and expanded it to mir-opt tests too. Whether it's actually better is up to a reviewer.
2024-01-07 00:32:24 +00:00
Camille GILLOT 41eb9a49af Skip threading over no-op SetDiscriminant. 2024-01-07 00:28:20 +00:00
Ben Kimock 735a6a4212 Run Miri and mir-opt tests without a target linker 2024-01-06 14:17:33 -05:00
Martin Nordholts 95eb5bcb67 rustc_mir_transform: Make DestinationPropagation stable for queries
By using FxIndexMap instead of FxHashMap, so that the order of visiting
of locals is deterministic.

We also need to bless
copy_propagation_arg.foo.DestinationPropagation.panic*.diff. Do not
review the diff of the diff. Instead look at the diff file before and
after this commit. Both before and after this commit, 3 statements are
replaced with nop. It's just that due to change in ordering, different
statements are replaced. But the net result is the same.
2024-01-05 20:55:32 +01:00
Michael Goulet 3a983ad3b0
Rollup merge of #119577 - tmiasko:lint, r=oli-obk
Migrate memory overlap check from validator to lint

The check attempts to identify potential undefined behaviour, rather
than whether MIR is well-formed. It belongs in the lint not validator.

Follow up to changes from #119077.
2024-01-05 10:57:22 -05:00
Michael Goulet e74a0cdfed
Rollup merge of #119566 - Zalathar:remove-spanview, r=Swatinem,Nilstrieb
Remove `-Zdump-mir-spanview`

The `-Zdump-mir-spanview` flag was added back in #76074, as a development/debugging aid for the initial work on what would eventually become `-Cinstrument-coverage`. It causes the compiler to emit an HTML file containing a function's source code, with various spans highlighted based on the contents of MIR.

When the suggestion was made to [triage and remove unnecessary `-Z` flags (Zulip)](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/.60-Z.60.20option.20triage), I noted that this flag could potentially be worth removing, but I wanted to keep it around to see whether I found it useful for my own coverage work.

But when I actually tried to use it, I ran into various issues (e.g. it crashes on `tests/coverage/closure.rs`). If I can't trust it to work properly without a full overhaul, then instead of diving down a rabbit hole of trying to fix arcane span-handling bugs, it seems better to just remove this obscure old code entirely.

---

````@rustbot```` label +A-code-coverage
2024-01-05 10:57:21 -05:00
Tomasz Miąsko df116ec246 Migrate memory overlap check from validator to lint
The check attempts to identify potential undefined behaviour, rather
than whether MIR is well-formed. It belongs in the lint not validator.
2024-01-04 23:32:22 +01:00
Tomasz Miąsko a084e063e6 Fix validation and linting of injected MIR
Reevaluate `body.should_skip()` after updating the MIR phase to ensure
that injected MIR is processed correctly.

Update a few custom MIR tests that were ill-formed for the injected
phase.
2024-01-04 23:06:42 +01:00
Matthias Krüger a919d97aaa
Rollup merge of #119325 - RalfJung:custom-mir, r=compiler-errors
custom mir: make it clear what the return block is

Custom MIR recently got support for specifying the "unwind action", so now there's two things coming after the actual call part of `Call` terminators. That's not very self-explaining so I propose we change the syntax to imitate keyword arguments:
```
Call(popped = Vec::pop(v), ReturnTo(drop), UnwindContinue())
```

Also fix some outdated docs and add some docs to `Call` and `Drop`.
2024-01-04 15:33:58 +01:00
Zalathar af3205421f Remove `-Zdump-mir-spanview` 2024-01-04 13:43:27 +11:00
Nilstrieb ffafcd8819 Update to bitflags 2 in the compiler
This involves lots of breaking changes. There are two big changes that
force changes. The first is that the bitflag types now don't
automatically implement normal derive traits, so we need to derive them
manually.

Additionally, bitflags now have a hidden inner type by default, which
breaks our custom derives. The bitflags docs recommend using the impl
form in these cases, which I did.
2023-12-30 18:17:28 +01:00
bors 8d76d07666 Auto merge of #116012 - cjgillot:gvn-const, r=oli-obk
Implement constant propagation on top of MIR SSA analysis

This implements the idea I proposed in https://github.com/rust-lang/rust/pull/110719#issuecomment-1718324700

Based on https://github.com/rust-lang/rust/pull/109597

The value numbering "GVN" pass formulates each rvalue that appears in MIR with an abstract form (the `Value` enum), and assigns an integer `VnIndex` to each. This abstract form can be used to deduplicate values, reusing an earlier local that holds the same value instead of recomputing. This part is proposed in #109597.

From this abstract representation, we can perform more involved simplifications, for example in https://github.com/rust-lang/rust/pull/111344.

With the abstract representation `Value`, we can also attempt to evaluate each to a constant using the interpreter. This builds a `VnIndex -> OpTy` map. From this map, we can opportunistically replace an operand or a rvalue with a constant if their value has an associated `OpTy`.

The most relevant commit is [Evaluated computed values to constants.](2767c4912e)"

r? `@oli-obk`
2023-12-30 03:45:58 +00:00
Michael Goulet fcb42b42d6 Remove movability from TyKind::Coroutine 2023-12-28 16:35:01 +00:00
Ralf Jung 0f9baa8a31 custom mir: make it clear what the return block is 2023-12-26 20:15:26 +01:00
bors 2fe50cd72c Auto merge of #119129 - jyn514:verbose, r=compiler-errors,estebank
rework `-Zverbose`

implements the changes described in https://github.com/rust-lang/compiler-team/issues/706

the first commit is only a name change from `-Zverbose` to `-Zverbose-internals` and does not change behavior. the second commit changes diagnostics.

possible follow up work:
- `ty::pretty` could print more info with `--verbose` than it does currently. `-Z verbose-internals` shows too much info in a way that's not helpful to users. michael had ideas about this i didn't fully understand: https://rust-lang.zulipchat.com/#narrow/stream/233931-t-compiler.2Fmajor-changes/topic/uplift.20some.20-Zverbose.20calls.20and.20rename.20to.E2.80.A6.20compiler-team.23706/near/408984200
- `--verbose` should imply `-Z write-long-types-to-disk=no`. the code in `ty_string_with_limit` should take `--verbose` into account (apparently this affects `Ty::sort_string`, i'm not familiar with this code). writing a file to disk should suggest passing `--verbose`.

r? `@compiler-errors` cc `@estebank`
2023-12-26 12:27:29 +00:00
jyn cb6d033316 don't elide shared parts of types in diagnostics when `--verbose` is passed
this also changes some parts of lifetime printing, which previously were not gated behind `-Z verbose`
2023-12-24 16:47:18 -05:00
Camille GILLOT 2837727471 Replace legacy ConstProp by GVN. 2023-12-24 20:08:57 +00:00
Camille GILLOT a03c972816 Enable GVN by default. 2023-12-24 20:08:57 +00:00
Tomasz Miąsko ba430a36c0 Enable -Zlint-mir by default for mir-opt tests 2023-12-21 00:00:00 +00:00