Commit Graph

887 Commits

Author SHA1 Message Date
Scott McMurray 0d63e6b608 [ACP 362] genericize `ptr::from_raw_parts` 2024-05-29 09:34:16 -07:00
bors f2e1a3a80a Auto merge of #125360 - RalfJung:packed-field-reorder, r=fmease
don't inhibit random field reordering on repr(packed(1))

`inhibit_struct_field_reordering_opt` being false means we exclude this type from random field shuffling. However, `packed(1)` types can still be shuffled! The logic was added in https://github.com/rust-lang/rust/pull/48528 since it's pointless to reorder fields in packed(1) types (there's no padding that could be saved) -- but that shouldn't inhibit `-Zrandomize-layout` (which did not exist at the time).

We could add an optimization elsewhere to not bother sorting the fields for `repr(packed)` types, but I don't think that's worth the effort.

This *does* change the behavior in that we may now reorder fields of `packed(1)` structs (e.g. if there are niches, we'll try to move them to the start/end, according to `NicheBias`).  We were always allowed to do that but so far we didn't. Quoting the [reference](https://doc.rust-lang.org/reference/type-layout.html):

> On their own, align and packed do not provide guarantees about the order of fields in the layout of a struct or the layout of an enum variant, although they may be combined with representations (such as C) which do provide such guarantees.
2024-05-29 11:57:13 +00:00
Scott McMurray 7150839552 Add custom mir support for `PtrMetadata` 2024-05-28 09:28:51 -07:00
Scott McMurray 459ce3f6bb Add an intrinsic for `ptr::metadata` 2024-05-28 09:28:51 -07:00
Oli Scherer ddc5f9b6c1 Create const block DefIds in typeck instead of ast lowering 2024-05-28 13:38:43 +00:00
Jubilee 25b079a1cf
Rollup merge of #125559 - scottmcm:simplify-shift-ubcheck, r=workingjubilee
Simplify the `unchecked_sh[lr]` ub-checks a bit

It can use the constant in the check, rather than passing it as a parameter.
2024-05-26 15:28:28 -07:00
bors 75e2c5dcd0 Auto merge of #125518 - saethlin:check-arguments-new-in-const, r=joboet
Move the checks for Arguments constructors to inline const

Thanks `@Skgland` for pointing out this opportunity: https://github.com/rust-lang/rust/pull/117804#discussion_r1612964362
2024-05-26 01:10:39 +00:00
Scott McMurray 0c84361342 Simplify the `unchecked_sh[lr]` ub-checks a bit 2024-05-25 15:58:26 -07:00
Ben Kimock 9763222f59 Move the checks for Arguments constructors to inline const 2024-05-24 21:09:15 -04:00
ltdk 72b7171031 Add assert_unsafe_precondition to unchecked_{add,sub,neg,mul,shl,shr} methods 2024-05-23 21:02:31 -04:00
Ralf Jung 37aeb75eb6 don't inhibit random field reordering on repr(packed(1)) 2024-05-21 19:22:04 +02:00
Matthias Krüger 9987e900c0
Rollup merge of #125173 - scottmcm:never-checked, r=davidtwco
Remove `Rvalue::CheckedBinaryOp`

Zulip conversation: <https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/intrinsics.20vs.20binop.2Funop/near/438729996>
cc `@RalfJung`

While it's a draft,
r? ghost
2024-05-20 18:13:48 +02:00
Matthias Krüger e0d922842d
Rollup merge of #125106 - Zalathar:expressions, r=davidtwco
coverage: Memoize and simplify counter expressions

When creating coverage counter expressions as part of coverage instrumentation, we often end up creating obviously-redundant expressions like `c1 + (c0 - c1)`, which is equivalent to just `c0`.

To avoid doing so, this PR checks when we would create an expression matching one of 5 patterns, and uses the simplified form instead:
- `(a - b) + b` → `a`.
- `(a + b) - b` → `a`.
- `(a + b) - a` → `b`.
- `a + (b - a)` → `b`.
- `a - (a - b)` → `b`.

Of all the different ways to combine 3 operands and 2 operators, these are the patterns that allow simplification.

(Some of those patterns currently don't occur in practice, but are included anyway for completeness, to avoid having to add them later as branch coverage and MC/DC coverage support expands.)

---

This PR also adds memoization for newly-created (or newly-simplified) counter expressions, to avoid creating duplicates.

This currently makes no difference to the final mappings, but is expected to be useful for MC/DC coverage of match expressions, as proposed by https://github.com/rust-lang/rust/pull/124278#issuecomment-2106754753.
2024-05-20 18:13:47 +02:00
Scott McMurray 95c0e5c6a8 Remove `Rvalue::CheckedBinaryOp` 2024-05-17 20:33:02 -07:00
Santiago Pastorino 6b46a919e1
Rename Unsafe to Safety 2024-05-17 18:33:37 -03:00
Zalathar d01df6f9aa coverage: Simplify counter expressions using simple algebra
Some of these cases currently don't occur in practice, but are included for
completeness, and to avoid having to add them later as branch coverage and
MC/DC coverage start building more complex expressions.
2024-05-14 13:58:40 +10:00
bors ec1b69852f Auto merge of #124795 - scottmcm:simplify-slice-from-raw-parts, r=joboet
Avoid a cast in `ptr::slice_from_raw_parts(_mut)`

Casting to `*const ()` or `*mut ()` is no longer needed after https://github.com/rust-lang/rust/pull/123840 so let's make the MIR smaller (and more inline-able, as seen in the tests).

If [ACP#362](https://github.com/rust-lang/libs-team/issues/362) goes through we can keep calling `ptr::from_raw_parts(_mut)` in these also without the cast, but that hasn't had any libs-api attention yet, so I'm not waiting on it.
2024-05-08 19:37:57 +00:00
bors 0f40f14b61 Auto merge of #123332 - Nadrieril:testkind-never, r=matthewjasper
never patterns: lower never patterns to `Unreachable` in MIR

This lowers a `!` pattern to "goto Unreachable". Ideally I'd like to read from the place to make it clear that the UB is coming from an invalid value, but that's tricky so I'm leaving it for later.

r? `@compiler-errors` how do you feel about a lil bit of MIR lowering
2024-05-07 15:14:20 +00:00
Scott McMurray 61517dbbe6 Avoid a cast in `ptr::slice_from_raw_parts(_mut)`
Casting to `*const ()` or `*mut ()` just bloats the MIR, so let's not.

If ACP#362 goes through we can keep calling `ptr::from_raw_parts(_mut)` in these also without the cast, but that hasn't had any libs-api attention yet, so I'm not waiting on it.
2024-05-06 01:53:54 -07:00
Guillaume Gomez d3e042dc4e
Rollup merge of #124749 - RossSmyth:stable_range, r=davidtwco
Stabilize exclusive_range_pattern (v2)

This PR is identical to #124459, which was approved and merged but then removed from master by a force-push due to a [CI bug](https://rust-lang.zulipchat.com/#narrow/stream/242791-t-infra/topic/ci.20broken.3F).

r? ghost

Original PR description:

---

Stabilization report: https://github.com/rust-lang/rust/issues/37854#issuecomment-1842398130
FCP: https://github.com/rust-lang/rust/issues/37854#issuecomment-1872520294

Stabilization was blocked by a lint that was merged here: #118879

Documentation PR is here: rust-lang/reference#1484

`@rustbot` label +F-exclusive_range_pattern +T-lang
2024-05-05 16:42:48 +02:00
Nadrieril 57e8aebb6c Lower never patterns to Unreachable in mir 2024-05-04 16:30:01 +02:00
Nadrieril 92d65a92e2 Add tests 2024-05-04 16:20:47 +02:00
bors d2d24e395a Auto merge of #123602 - cjgillot:gvn-borrowed, r=oli-obk
Account for immutably borrowed locals in MIR copy-prop and GVN

For the most part, we consider that immutably borrowed `Freeze` locals still fulfill SSA conditions. As the borrow is immutable, any use of the local will have the value given by the single assignment, and there can be no surprise.

This allows copy-prop to merge a non-borrowed local with a borrowed local. We chose to keep copy-classes heads unborrowed, as those may be easier to optimize in later passes.

This also allows to GVN the value behind an immutable borrow. If a SSA local is borrowed, dereferencing that borrow is equivalent to copying the local's value: re-executing the assignment between the borrow and the dereference would be UB.

r? `@ghost` for perf
2024-05-03 21:50:13 +00:00
Ross Smyth 6967d1c0fc Stabilize exclusive_range 2024-05-02 19:42:31 -04:00
George Bateman 2348eb271c
Update mir-opt tests, add proper regression test 2024-04-28 21:10:09 +01:00
Gary Guo cfee72aa24 Fix tests and bless 2024-04-24 13:12:33 +01:00
Oli Scherer aef0f4024a Error on using `yield` without also using `#[coroutine]` on the closure
And suggest adding the `#[coroutine]` to the closure
2024-04-24 08:05:29 +00:00
León Orell Valerian Liehr 332cac2c6d
Rollup merge of #122598 - Nadrieril:full-derefpats, r=matthewjasper
deref patterns: lower deref patterns to MIR

This lowers deref patterns to MIR. This is a bit tricky because this is the first kind of pattern that requires storing a value in a temporary. Thanks to https://github.com/rust-lang/rust/pull/123324 false edges are no longer a problem.

The thing I'm not confident about is the handling of fake borrows. This PR ignores any fake borrows inside a deref pattern. We are guaranteed to at least fake borrow the place of the first pointer value, which could be enough, but I'm not certain.
2024-04-23 17:25:15 +02:00
Guillaume Gomez 6a326d889a
Rollup merge of #124230 - reitermarkus:generic-nonzero-stable, r=dtolnay
Stabilize generic `NonZero`.

Tracking issue: https://github.com/rust-lang/rust/issues/120257

r? `@dtolnay`
2024-04-22 20:26:00 +02:00
Markus Reiter 33e68aadc9
Stabilize generic `NonZero`. 2024-04-22 18:48:47 +02:00
Zalathar a892c2387e coverage: Add a mir-opt test for branch coverage of match arms 2024-04-22 21:55:33 +10:00
Zalathar da37b14121 coverage: Move mir-opt coverage tests into a subdirectory 2024-04-22 21:55:33 +10:00
Scott McMurray 5e785b1420 Update tests after 123949 2024-04-21 11:24:54 -07:00
Scott McMurray 9520cebfc5 InstSimplify `from_raw_parts(p, ())` → `p as _` 2024-04-21 11:08:37 -07:00
Scott McMurray de64ff76f8 Use it in the library, and `InstSimplify` it away in the easy places 2024-04-21 11:08:37 -07:00
Scott McMurray 4f4442655e Add an intrinsic that lowers to AggregateKind::RawPtr 2024-04-21 11:08:37 -07:00
Scott McMurray 70df9d9a13 Add a mir-opt test for `byte_add` on pointers 2024-04-21 11:08:36 -07:00
Scott McMurray b76faff1b2 Add a MIR pre-codegen test for Vec::deref 2024-04-21 11:08:36 -07:00
Scott McMurray 5800dc1faa New slice indexing pre-codegen MIR test 2024-04-21 11:08:36 -07:00
Camille GILLOT 73db28757b Use newly exposed Freeze trait. 2024-04-20 19:25:20 +00:00
Camille GILLOT 9b846d4637 Dereference immutable borrows in GVN. 2024-04-20 19:22:13 +00:00
Camille GILLOT 73db12a223 Merge borrowed locals too. 2024-04-20 19:20:38 +00:00
Camille GILLOT 308cc76510 GVN borrowed locals too. 2024-04-20 19:19:06 +00:00
Camille GILLOT 5d144e301c Add tests. 2024-04-20 19:10:23 +00:00
Nadrieril 50531806ee Add a non-shallow fake borrow 2024-04-20 16:01:35 +02:00
Nadrieril 511bd78863 Rework fake borrow calculation 2024-04-20 16:01:35 +02:00
Ralf Jung 75d0fdd967 mir-opt tests: rename unit-test -> test-mir-pass 2024-04-20 13:19:34 +02:00
bors a61b14d15e Auto merge of #124156 - DianQK:disable-match_branches, r=RalfJung
Disable SimplifyToExp in MatchBranchSimplification

Due to the miscompilation mentioned in #124150, We need to disable MatchBranchSimplification temporarily.

To fully resolve this issue, my plan is:
1. Disable SimplifyToExp in MatchBranchSimplification (this PR).
2. Remove all potentially unclear transforms in #124122.
3. Gradually add back the removed transforms (possibly multiple PRs).

r? `@Nilstrieb` or `@oli-obk`
2024-04-20 08:47:07 +00:00
bors 9c7b1f4848 Auto merge of #124114 - scottmcm:better-checked, r=workingjubilee
Make `checked` ops emit *unchecked* LLVM operations where feasible

For things with easily pre-checked overflow conditions -- shifts and unsigned subtraction -- write the checked methods in such a way that we stop emitting wrapping versions of them.

For example, today <https://rust.godbolt.org/z/qM9YK8Txb> neither
```rust
a.checked_sub(b).unwrap()
```
nor
```rust
a.checked_sub(b).unwrap_unchecked()
```
actually optimizes to `sub nuw`.  After this PR they do.

cc #103299
2024-04-20 04:11:16 +00:00
DianQK b52be28fda
Disable SimplifyToExp in MatchBranchSimplification 2024-04-20 05:37:53 +08:00
Maybe Waffle 0bbe362901 Correctly change type when adding adjustments on top of `NeverToAny` 2024-04-19 11:05:02 +00:00
Maybe Waffle 4d749cad25 Add a test for `a == b` where `a: !, b: !`
(this currently produces malformed mir: we call `eq` with first argument not
being a reference)
2024-04-19 11:05:02 +00:00
Scott McMurray 986d9f104b Make `checked` ops emit *unchecked* LLVM operations where feasible
For things with easily pre-checked overflow conditions -- shifts and unsigned subtraction -- write then checked methods in such a way that we stop emitting wrapping versions of them.

For example, today <https://rust.godbolt.org/z/qM9YK8Txb> neither
```rust
a.checked_sub(b).unwrap()
```
nor
```rust
a.checked_sub(b).unwrap_unchecked()
```
actually optimizes to `sub nuw`.  After this PR they do.
2024-04-18 18:11:21 -07:00
Scott McMurray 90b4c86335 Ensure `[rust] debuginfo-level-std` doesn't change core's MIR 2024-04-18 09:35:36 -07:00
Scott McMurray 6094063c35 Update `checked_ops` so 32- and 64-bit gets the same checks 2024-04-18 09:35:36 -07:00
Scott McMurray d05545c05d At debuginfo=0, don't inline debuginfo when inlining 2024-04-18 09:35:35 -07:00
CastilloDel f0f867e2d3 Run filecheck on dest-prop/simple.rs 2024-04-14 12:24:18 +02:00
CastilloDel 069209034d Run filecheck on dest-prop/dead_stores_79191.rs and dead_stores_better.rs 2024-04-14 11:35:55 +02:00
CastilloDel 853311c358 Run filecheck on dest-prop/cycle.rs 2024-04-14 11:35:37 +02:00
CastilloDel f238eba621 Run filecheck on dest-prop/copy_propagation.rs 2024-04-14 11:35:23 +02:00
CastilloDel 6bd68fcceb Run filecheck on dest-prop/branch.rs 2024-04-14 11:35:01 +02:00
bors 59c808fcd9 Auto merge of #122387 - DianQK:re-enable-early-otherwise-branch, r=cjgillot
Re-enable the early otherwise branch optimization

Closes #95162. Fixes #119014.

This is the first part of #121397.

An invalid enum discriminant can come from anywhere. We have to check to see if all successors contain the discriminant statement. This should have a pass to hoist instructions.

r? cjgillot
2024-04-09 01:02:29 +00:00
bors b234e44944 Auto merge of #122077 - oli-obk:eager_opaque_checks4, r=lcnr
Pass list of defineable opaque types into canonical queries

This eliminates `DefiningAnchor::Bubble` for good and brings the old solver closer to the new one wrt cycles and nested obligations. At that point the difference between `DefiningAnchor::Bind([])` and `DefiningAnchor::Error` was academic. We only used the difference for some sanity checks, which actually had to be worked around in places, so I just removed `DefiningAnchor` entirely and just stored the list of opaques that may be defined.

fixes #108498
fixes https://github.com/rust-lang/rust/issues/116877

* [x] run crater
  - https://github.com/rust-lang/rust/pull/122077#issuecomment-2013293931
2024-04-08 23:01:50 +00:00
Oli Scherer c8dfb59406 bless mir-opt tests 2024-04-08 15:08:06 +00:00
DianQK 166bb1bd46
Don't change the otherwise of the switch 2024-04-08 19:20:07 +08:00
DianQK e752af765e
Transforms a match containing negative numbers into an assignment statement as well 2024-04-08 19:00:57 +08:00
DianQK 1f061f47e2
Transforms match into an assignment statement 2024-04-08 19:00:53 +08:00
DianQK badb73b921
Update matches_reduce_branches.rs 2024-04-08 18:54:33 +08:00
DianQK f5c256fa0f
Add FileCheck for `early_otherwise_branch*.rs` 2024-04-07 21:14:29 +08:00
DianQK 31e74771f0
Resolve unsound hoisting of discriminant in `EarlyOtherwiseBranch` 2024-04-07 21:14:26 +08:00
Matthias Krüger 504a78e2f2
Rollup merge of #123324 - Nadrieril:false-edges2, r=matthewjasper
match lowering: make false edges more precise

When lowering match expressions, we add false edges to hide details of the lowering from borrowck. Morally we pretend we're testing the patterns (and guards) one after the other in order. See the tests for examples. Problem is, the way we implement this today is too coarse for deref patterns.

In deref patterns, a pattern like `deref [1, x]` matches on a `Vec` by creating a temporary to store the output of the call to `deref()` and then uses that to continue matching. Here the pattern has a binding, which we set up after the pre-binding block. Problem is, currently the false edges tell borrowck that the pre-binding block can be reached from a previous arm as well, so the `deref()` temporary may not be initialized. This triggers an error when we try to use the binding `x`.

We could call `deref()` a second time, but this opens the door to soundness issues if the deref impl is weird. Instead in this PR I rework false edges a little bit.

What we need from false edges is a (fake) path from each candidate to the next, specifically from candidate C's pre-binding block to next candidate D's pre-binding block. Today, we link the pre-binding blocks directly. In this PR, I link them indirectly by choosing an earlier node on D's success path. Specifically, I choose the earliest block on D's success path that doesn't make a loop (if I chose e.g. the start block of the whole match (which is on the success path of all candidates), that would make a loop). This turns out to be rather straightforward to implement.

r? `@matthewjasper` if you have the bandwidth, otherwise let me know
2024-04-04 14:51:16 +02:00
Nadrieril 8021192d34 More precise false edges 2024-04-03 21:02:47 +02:00
joboet 989660c3e6
rename `expose_addr` to `expose_provenance` 2024-04-03 16:00:38 +02:00
Matthew Jasper a277c901d9 Remove MIR unsafe check
This also remove safety information from MIR.
2024-04-03 08:50:12 +00:00
bors 76cf07d5df Auto merge of #122225 - DianQK:nits-120268, r=cjgillot
Rename `UninhabitedEnumBranching` to `UnreachableEnumBranching`

Per [#120268](https://github.com/rust-lang/rust/pull/120268#discussion_r1517492060), I rename `UninhabitedEnumBranching` to `UnreachableEnumBranching` .

I solved some nits to add some comments.

I adjusted the workaround restrictions. This should be useful for `a <= b` and `if let Some/Ok(v)`. For enum with few variants, `early-tailduplication` should not cause compile time overhead.

r? RalfJung
2024-04-03 06:22:23 +00:00
Jacob Pratt e9ef8e1efa
Rollup merge of #122935 - RalfJung:with-exposed-provenance, r=Amanieu
rename ptr::from_exposed_addr -> ptr::with_exposed_provenance

As discussed on [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/136281-t-opsem/topic/To.20expose.20or.20not.20to.20expose/near/427757066).

The old name, `from_exposed_addr`, makes little sense as it's not the address that is exposed, it's the provenance. (`ptr.expose_addr()` stays unchanged as we haven't found a better option yet. The intended interpretation is "expose the provenance and return the address".)

The new name nicely matches `ptr::without_provenance`.
2024-04-02 20:37:39 -04:00
bors 88c2f4f5f5 Auto merge of #123385 - matthiaskrgr:rollup-v69vjbn, r=matthiaskrgr
Rollup of 8 pull requests

Successful merges:

 - #123198 (Add fn const BuildHasherDefault::new)
 - #123226 (De-LLVM the unchecked shifts [MCP#693])
 - #123302 (Make sure to insert `Sized` bound first into clauses list)
 - #123348 (rustdoc: add a couple of regression tests)
 - #123362 (Check that nested statics in thread locals are duplicated per thread.)
 - #123368 (CFI: Support non-general coroutines)
 - #123375 (rustdoc: synthetic auto trait impls: accept unresolved region vars for now)
 - #123378 (Update sysinfo to 0.30.8)

Failed merges:

 - #123349 (Fix capture analysis for by-move closure bodies)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-04-02 21:23:53 +00:00
Matthias Krüger 1b0e46f8a0
Rollup merge of #123226 - scottmcm:u32-shifts, r=WaffleLapkin
De-LLVM the unchecked shifts [MCP#693]

This is just one part of the MCP (https://github.com/rust-lang/compiler-team/issues/693), but it's the one that IMHO removes the most noise from the standard library code.

Seems net simpler this way, since MIR already supported heterogeneous shifts anyway, and thus it's not more work for backends than before.

r? WaffleLapkin
2024-04-02 21:22:01 +02:00
bors a77322c16f Auto merge of #118310 - scottmcm:three-way-compare, r=davidtwco
Add `Ord::cmp` for primitives as a `BinOp` in MIR

Update: most of this OP was written months ago.  See https://github.com/rust-lang/rust/pull/118310#issuecomment-2016940014 below for where we got to recently that made it ready for review.

---

There are dozens of reasonable ways to implement `Ord::cmp` for integers using comparison, bit-ops, and branches.  Those differences are irrelevant at the rust level, however, so we can make things better by adding `BinOp::Cmp` at the MIR level:

1. Exactly how to implement it is left up to the backends, so LLVM can use whatever pattern its optimizer best recognizes and cranelift can use whichever pattern codegens the fastest.
2. By not inlining those details for every use of `cmp`, we drastically reduce the amount of MIR generated for `derive`d `PartialOrd`, while also making it more amenable to MIR-level optimizations.

Having extremely careful `if` ordering to μoptimize resource usage on broadwell (#63767) is great, but it really feels to me like libcore is the wrong place to put that logic.  Similarly, using subtraction [tricks](https://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign) (#105840) is arguably even nicer, but depends on the optimizer understanding it (https://github.com/llvm/llvm-project/issues/73417) to be practical.  Or maybe [bitor is better than add](https://discourse.llvm.org/t/representing-in-ir/67369/2?u=scottmcm)?  But maybe only on a future version that [has `or disjoint` support](https://discourse.llvm.org/t/rfc-add-or-disjoint-flag/75036?u=scottmcm)?  And just because one of those forms happens to be good for LLVM, there's no guarantee that it'd be the same form that GCC or Cranelift would rather see -- especially given their very different optimizers.  Not to mention that if LLVM gets a spaceship intrinsic -- [which it should](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Suboptimal.20inlining.20in.20std.20function.20.60binary_search.60/near/404250586) -- we'll need at least a rustc intrinsic to be able to call it.

As for simplifying it in Rust, we now regularly inline `{integer}::partial_cmp`, but it's quite a large amount of IR.  The best way to see that is with 8811efa88b (diff-d134c32d028fbe2bf835fef2df9aca9d13332dd82284ff21ee7ebf717bfa4765R113) -- I added a new pre-codegen MIR test for a simple 3-tuple struct, and this PR change it from 36 locals and 26 basic blocks down to 24 locals and 8 basic blocks.  Even better, as soon as the construct-`Some`-then-match-it-in-same-BB noise is cleaned up, this'll expose the `Cmp == 0` branches clearly in MIR, so that an InstCombine (#105808) can simplify that to just a `BinOp::Eq` and thus fix some of our generated code perf issues.  (Tracking that through today's `if a < b { Less } else if a == b { Equal } else { Greater }` would be *much* harder.)

---

r? `@ghost`
But first I should check that perf is ok with this
~~...and my true nemesis, tidy.~~
2024-04-02 19:21:44 +00:00
scottmcm 4626521831
Update tests/mir-opt/inline/unchecked_shifts.rs
Co-authored-by: Waffle Maybe <waffle.lapkin@gmail.com>
2024-04-02 17:21:20 +00:00
Nadrieril 65efa5b3b9 Add FileCheck directives to the new tests. 2024-03-30 18:11:41 +01:00
Nadrieril 75d2e67ed2 Sort `Eq` candidates in the failure case too 2024-03-30 17:37:15 +01:00
Nadrieril 5ef9ad37ab Add test 2024-03-30 17:37:15 +01:00
Nadrieril e67f5294df Regroup mir-opt tests of match building 2024-03-30 17:37:15 +01:00
Scott McMurray 0601f0c66d De-LLVM the unchecked shifts [MCP#693]
This is just one part of the MCP, but it's the one that IMHO removes the most noise from the standard library code.

Seems net simpler this way, since MIR already supported heterogeneous shifts anyway, and thus it's not more work for backends than before.
2024-03-30 03:32:11 -07:00
DianQK 47ed73a7b5
Eliminate `UbCheck` for non-standard libraries 2024-03-27 21:02:40 +08:00
Matthias Krüger b63dca138e
Rollup merge of #123049 - compiler-errors:coroutine-closure-rcvr, r=oli-obk
In `ConstructCoroutineInClosureShim`, pass receiver by mut ref, not mut pointer

The receivers were compatible at codegen time, but did not necessarily have the same layouts due to niches, which was caught by miri.

Fixes rust-lang/miri#3400

r? oli-obk
2024-03-26 21:23:49 +01:00
Michael Goulet 22bc5c538d In ConstructCoroutineInClosureShim, pass receiver by ref, not pointer 2024-03-26 12:10:51 -04:00
Matthias Krüger 94e8c6c334
Rollup merge of #123067 - Nadrieril:always-simplify-or, r=oli-obk
match lowering: consistently merge simple or-patterns

There are two places where we expand or-patterns in match lowering: the main one is `test_candidates_with_or`, and there's one in `match_candidates` that's an optimization for the simple case where the whole pattern is just one or-pattern.

To reduce duplication, we merge or-pattern alternatives into a single block when possible, but we only to that in `test_candidates_with_or`. This PR fixes this oversight and merges them in `match_candidates` too.

This is a part of splitting up https://github.com/rust-lang/rust/pull/122046 into smaller bits.
2024-03-26 17:06:41 +01:00
Nadrieril d1d9aa3108 Consistently merge simplifiable or-patterns 2024-03-25 23:52:17 +01:00
Kevin Reid 3010fa9afb In `pretty_print_type()`, print `async fn` futures' paths instead of spans.
This makes `-Zprint-type-sizes`'s output easier to read, because the
name of an `async fn` is more immediately recognizable than its span.

I also deleted the comment "FIXME(eddyb) should use `def_span`." because
it appears to have already been fixed by commit 67727aa7c3.
2024-03-25 08:01:15 -07:00
Scott McMurray 8d5977d6af Slightly simplify the `iN::partial_cmp` MIR
This saves some debug and scope metadata in every single function that calls it.

Normally wouldn't be worth it, but with the derives there's *so* many of these.
2024-03-24 10:01:37 -07:00
Zalathar b5ee20f714 Clean up unnecessary headers/flags in coverage mir-opt tests
These headers and flags were historically needed, but are now unnecessary due
to various changes in how coverage information is stored in MIR.
2024-03-24 22:17:47 +11:00
Scott McMurray 3da115a93b Add+Use `mir::BinOp::Cmp` 2024-03-23 23:23:41 -07:00
Scott McMurray 744c664ba2 Add a MIR pre-codegen test for derived PartialOrd 2024-03-23 23:06:44 -07:00
Matthias Krüger 3d9ee88ea2
Rollup merge of #122168 - compiler-errors:inline-coroutine-body-validation, r=cjgillot
Fix validation on substituted callee bodies in MIR inliner

When inlining a coroutine, we will substitute the MIR body with the args of the call. There is code in the MIR validator that attempts to prevent query cycles, and will use the coroutine body directly when it detects that's the body that's being validated. That means that when inlining a coroutine body that has been substituted, it may no longer be parameterized over the original args of the coroutine, which will lead to substitution ICEs.

Fixes #119064
2024-03-24 01:05:51 +01:00
bors 2f090c30dd Auto merge of #122629 - RalfJung:assert-unsafe-precondition, r=saethlin
refactor check_{lang,library}_ub: use a single intrinsic

This enacts the plan I laid out [here](https://github.com/rust-lang/rust/pull/122282#issuecomment-1996917998): use a single intrinsic, called `ub_checks` (in aniticpation of https://github.com/rust-lang/compiler-team/issues/725), that just exposes the value of `debug_assertions` (consistently implemented in both codegen and the interpreter). Put the language vs library UB logic into the library.

This makes it easier to do something like https://github.com/rust-lang/rust/pull/122282 in the future: that just slightly alters the semantics of `ub_checks` (making it more approximating when crates built with different flags are mixed), but it no longer affects whether these checks can happen in Miri or compile-time.

The first commit just moves things around; I don't think these macros and functions belong into `intrinsics.rs` as they are not intrinsics.

r? `@saethlin`
2024-03-23 21:11:00 +00:00
Ralf Jung 6177530420 refactor check_{lang,library}_ub: use a single intrinsic, put policy into library 2024-03-23 18:45:05 +01:00
Ralf Jung 038e7c6c38 rename MIR int2ptr casts to match library name 2024-03-23 13:18:33 +01:00
Ben Kimock 07994c9310 Enable more mir-opt tests in debug builds 2024-03-22 20:14:39 -04:00
Michael Goulet 78ebb939c1 Fix validation on substituted callee bodies in MIR inliner 2024-03-22 17:17:03 -04:00
Michael Goulet da8a39a9de Failing test 2024-03-22 17:15:22 -04:00
Zalathar 91aae58568 coverage: Clean up marker statements that aren't needed later
Some of the marker statements used by coverage are added during MIR building
for use by the InstrumentCoverage pass (during analysis), and are not needed
afterwards.
2024-03-22 20:20:41 +11:00
Michael Goulet f1fef64e19 Fix ABI for FnMut/Fn impls for async closures 2024-03-19 16:59:24 -04:00
Michael Goulet 05116c5c30 Only split by-ref/by-move futures for async closures 2024-03-19 16:59:23 -04:00
bors a385e5667c Auto merge of #122392 - BoxyUwU:misc_cleanup, r=lcnr
misc cleanups from debugging something

rename `instantiate_canonical_with_fresh_inference_vars` to `instantiate_canonical`  the substs for the canonical are not solely infer vars as that would be wildly wrong and it is rather confusing to see this method called and think that the entire canonicalization setup is completely broken when it is not 👍

also update region debug printing to be more like the custom impls for Ty/Const, right now regions in debug output are horribly verbose and make it incredibly hard to read but with this atleast boundvars and placeholders when debugging the new solver do not take up excessive amounts of space.

r? `@lcnr`
2024-03-19 15:38:41 +00:00
bors 91b87c4f45 Auto merge of #122645 - saethlin:portable-mir-opt-tests, r=Nadrieril
Remove some only- clauses from mir-opt tests

Derived from https://github.com/rust-lang/rust/pull/122295

Many of these tests were originally codegen tests, and MIR is more trivially portable than LLVM IR. We simply don't need to restrict the platform in most cases.

r? Nadrieril
2024-03-19 06:56:49 +00:00
Boxy 8124b26122 update region debug formatting 2024-03-18 16:44:12 +00:00
Ben Kimock 68f284f337 Remove some only- clauses from mir-opt tests 2024-03-18 10:07:43 -04:00
Ralf Jung c96fa5e143 add_retag: ensure box-to-raw-ptr casts are preserved for Miri 2024-03-18 10:32:25 +01:00
Ralf Jung 23a4ad12ce simplify_cfg: rename some passes so that they make more sense 2024-03-17 19:59:15 +01:00
DianQK ec313d1edb
Update the test case for `SimplifyCfg-after-unreachable-enum-branching` 2024-03-13 22:35:11 +08:00
Matthias Krüger e6ba504029
Rollup merge of #121908 - Nadrieril:dynamic-variant-collection, r=matthewjasper
match lowering: don't collect test alternatives ahead of time

I'm very happy with this one. Before this, when sorting candidates into the possible test branches, we manually computed `usize` indices to determine in which branch each candidate goes. To make this work we had a first pass that collected the possible alternatives we'd have to deal with, and a second pass that actually sorts the candidates.

In this PR, I replace `usize` indices with a dedicated enum. This makes `sort_candidates` easier to follow, and we don't need the first pass anymore.

r? ``@matthewjasper``
2024-03-13 06:41:21 +01:00
Matthias Krüger b4cbc882eb
Rollup merge of #121865 - Kirandevraj:unnamed-fields-filecheck, r=oli-obk
Add FileCheck annotations to MIR-opt unnamed-fields tests

Part of #116971
Adds filecheck annotations to unnamed-fields mir-opt tests in `tests/mir-opt/unnamed-fields`
2024-03-12 06:29:02 +01:00
kirandevraj ba70528fd8 updating variable names in CHECK 2024-03-12 01:51:52 +05:30
Ralf Jung 7d99e80c55 MIR printing: print the path of uneval'd const; refer to promoteds in a consistent way 2024-03-10 14:59:41 +01:00
kirandevraj 6f1156a9ff fixing mir pass name to text comment 2024-03-10 01:24:35 +05:30
DianQK 8ddd966223
Rename `UninhabitedEnumBranching` to `UnreachableEnumBranching` 2024-03-09 14:32:27 +08:00
Ben Kimock 5a93a59fd5 Distinguish between library and lang UB in assert_unsafe_precondition 2024-03-08 18:53:58 -05:00
DianQK b5bd98d540
Update MIR with `MirPatch` in `UninhabitedEnumBranching` 2024-03-08 08:15:14 +08:00
DianQK 3d7f8b4e5b
Get all variants to eliminate the default branching if we cannot get the layout of type 2024-03-07 22:58:51 +08:00
DianQK 08ae8380ce
Replace the default branch with an unreachable branch If it is the last variant 2024-03-07 22:58:51 +08:00
DianQK d8b7b5be7d
Regenerate `uninhabited_enum_branching.rs` 2024-03-07 22:58:51 +08:00
Guillaume Boisseau 30976fbe2b
Rollup merge of #121716 - Nadrieril:simple-binding-order, r=matthewjasper
match lowering: Lower bindings in a predictable order

After the recent refactorings, we can now lower bindings in a truly predictable order. The order in https://github.com/rust-lang/rust/pull/120214 was an improvement but not very clear. With this PR, we lower bindings from left to right, with the special case that `x @ pat` is traversed as `pat @ x` (i.e. `x` is lowered after any bindings in `pat`).

This description only applies in the absence of or-patterns. Or-patterns make everything complicated, because the binding place depends on the subpattern. Until I have a better idea I leave them to be handled in whatever weird order arises from today's code.

r? `@matthewjasper`
2024-03-02 20:13:23 +01:00
Nadrieril 8c3688cbb5 Allocate candidate vectors as we sort them 2024-03-02 18:33:19 +01:00
Nadrieril 3d3b321c60 Use an enum instead of manually tracking indices for `target_blocks` 2024-03-02 18:33:17 +01:00
Matthias Krüger 0d2205f9a6
Rollup merge of #121750 - Nadrieril:switchkind-if, r=matthewjasper
match lowering: Separate the `bool` case from other integers in `TestKind`

`TestKind::SwitchInt` had a special case for `bool` essentially everywhere it's used, so I made `TestKind::If` to handle the bool case on its own.

r? `@matthewjasper`
2024-03-01 22:38:49 +01:00
kirandevraj 3ab693689a mir-opt unnamed-fields filecheck annotations 2024-03-01 23:33:24 +05:30
Matthias Krüger 1a4c93e3ed
Rollup merge of #121784 - Zalathar:if-or-converge, r=Nadrieril
Make the success arms of `if lhs || rhs` meet up in a separate block

Extracted from #118305, where this is necessary to avoid introducing a bug when injecting marker statements into the then/else arms.

---

In the previous code (#111752), the success block of `lhs` would jump directly to the success block of `rhs`. However, `rhs_success_block` could already contain statements that are specific to the RHS, and the direct goto causes them to be executed in the LHS success path as well.

This patch therefore creates a fresh block that the LHS and RHS success blocks can both jump to.

---

I think the reason we currently get away with this is that `rhs_success_block` usually doesn't contain anything other than StorageDead statements for locals used by the RHS, and those statements don't seem to cause problems in the LHS success path (which never makes those locals live).

But if we start adding meaningful statements for branch coverage (or MC/DC coverage), it's important to keep the LHS and RHS blocks separate.
2024-03-01 17:51:30 +01:00
Zalathar a7832b14b1 Make the success arms of `if lhs || rhs` meet up in a separate block
In the previous code, the success block of `lhs` would jump directly to the
success block of `rhs`. However, `rhs_success_block` could already contain
statements that are specific to the RHS, and the direct goto causes them to be
executed in the LHS success path as well.

This patch therefore creates a fresh block that the LHS and RHS success blocks
can both jump to.
2024-02-29 20:24:43 +11:00
r0cky 88b9e7a602 Bless test 2024-02-29 10:55:05 +08:00
Nadrieril d6332ae79c Separate the `bool` case from other integers in `TestKind` 2024-02-28 18:23:26 +01:00
Nadrieril 00497ad24b Lower bindings in a predictable order 2024-02-28 01:41:25 +01:00
Markus Reiter b2fbb8a053
Use generic `NonZero` in tests. 2024-02-25 12:03:48 +01:00
bors e9f9594913 Auto merge of #121114 - Nilstrieb:no-inline!, r=saethlin
Add `#[rustc_no_mir_inline]` for standard library UB checks

should help with #121110 and also with #120848

Because the MIR inliner cannot know whether the checks are enabled or not, so inlining is an unnecessary compile time pessimization when debug assertions are disabled. LLVM knows whether they are enabled or not, so it can optimize accordingly without wasting time.

r? `@saethlin`
2024-02-25 03:47:31 +00:00
Nilstrieb 81d7069e34 Add `#[rustc_no_mir_inline]` for standard library UB checks
Co-authored-by: Ben Kimock <kimockb@gmail.com>
2024-02-24 21:19:41 +01:00
Ben Kimock 2f3c0b9859 Ignore less tests in debug builds 2024-02-23 18:04:01 -05:00
许杰友 Jieyou Xu (Joe) 6e48b96692
[AUTO_GENERATED] Migrate compiletest to use `ui_test`-style `//@` directives 2024-02-22 16:04:04 +00:00
Ralf Jung b58f647d54 rename ptr::invalid -> ptr::without_provenance
also introduce ptr::dangling matching NonNull::dangling
2024-02-21 20:15:52 +01:00
bors 096598dc79 Auto merge of #121172 - Nadrieril:simplify-empty-selection, r=matthewjasper
match lowering: simplify empty candidate selection

In match lowering, `match_simplified_candidates` is tasked with removing candidates that are fully matched and linking them up properly. The code that does that was needlessly complicated; this PR simplifies it.

The overall change isn't big but I split it up into tiny commits to convince myself that I was correctly preserving behavior. The test changes are all due to the first commit. Let me know if you'd prefer me to split up the PR to make reviewing easier.

r? `@matthewjasper`
2024-02-21 03:11:24 +00:00
bors 4e65074933 Auto merge of #120904 - Nadrieril:match-lowering-intermediate-repr, r=matthewjasper
match lowering: eagerly simplify match pairs

This removes one important complication from match lowering. Before this, match pair simplification (which includes collecting bindings and type ascriptions) was intertwined with the whole match lowering algorithm.

I'm avoiding this by storing in each `MatchPair` the sub-`MatchPair`s that correspond to its subfields. This makes it possible to simplify everything (except or-patterns) in `Candidate::new()`.

This should open up further simplifications. It will also give us proper control over the order of bindings.

r? `@matthewjasper`
2024-02-21 01:11:34 +00:00
bors 2b43e75c98 Auto merge of #120863 - saethlin:slice-get-checked, r=the8472
Use intrinsics::debug_assertions in debug_assert_nounwind

This is the first item in https://github.com/rust-lang/rust/issues/120848.

Based on the benchmarking in this PR, it looks like, for the programs in our benchmark suite, enabling all these additional checks does not introduce significant compile-time overhead, with the single exception of `Alignment::new_unchecked`. Therefore, I've added `#[cfg(debug_assertions)]` to that one call site, so that it remains compiled out in the distributed standard library.

The trailing commas in the previous calls to `debug_assert_nounwind!` were causing the macro to expand to `panic_nouwnind_fmt`, which requires more work to set up its arguments, and that overhead alone is measured between this perf run and the next: https://github.com/rust-lang/rust/pull/120863#issuecomment-1937423502
2024-02-20 14:04:57 +00:00
Ben Kimock 581e171773 Convert debug_assert_nounwind to intrinsics::debug_assertions 2024-02-19 20:38:09 -05:00
Oli Scherer 9062697917 Always evaluate free constants and statics, even if previous errors occurred 2024-02-19 22:11:13 +00:00
Nadrieril 308b4824aa Don't repeatedly simplify already-simplified match pairs 2024-02-19 21:28:26 +01:00
Nadrieril c00b38e9fb Don't bother to save a block 2024-02-17 20:21:57 +01:00
Oli Scherer 93bc34073e
Rollup merge of #121039 - cjgillot:gvn-adjust, r=compiler-errors
Correctly compute adjustment casts in GVN

Fixes https://github.com/rust-lang/rust/issues/120925

r? `@oli-obk`
2024-02-14 11:53:41 +01:00
Camille GILLOT 7ec9601a0b Add test. 2024-02-13 17:21:53 +00:00
Matthias Krüger b785fdb80b
Rollup merge of #120978 - Nadrieril:sane-blocks, r=matthewjasper
match lowering: simplify block creation

Match lowering was doing complicated things with block creation. As far as I can tell it was trying to avoid creating unneeded blocks, but of the three places that start out with `otherwise = &mut None`, two of them called `otherwise.unwrap_or_else(|| self.cfg.start_new_block())` anyway. As far as I can tell the only place where this PR makes a difference is in `lower_match_tree`, which did indeed sometimes avoid creating the unreachable final block + FakeRead. Unless this is important I propose we do the naive thing instead.

I have not checked all the graph isomorphisms by hand, but at a glance the test diff looks sensible.

r? `@matthewjasper`
2024-02-13 17:38:11 +01:00
Nadrieril faaf81bbbc Start blocks eagerly 2024-02-12 17:37:05 +01:00