Commit Graph

148 Commits

Author SHA1 Message Date
DianQK
c16c22cc9c
Simplify the canonical clone method to copy
The optimized clone method ends up as the following MIR:

```
_2 = copy ((*_1).0: i32);
_3 = copy ((*_1).1: u64);
_4 = copy ((*_1).2: [i8; 3]);
_0 = Foo { a: move _2, b: move _3, c: move _4 };
```

We can transform this to:

```
_0 = copy (*_1);
```
2024-09-14 13:30:35 +08:00
The 8472
f3bc08adbd ignore/fix layout-sensitive tests 2024-08-31 23:56:45 +02:00
Scott McMurray
99cb0c6bc3 Bless *all* the mir-opt tests 2024-08-18 16:07:33 -07:00
Scott McMurray
249a36ffbd Update mir-opt filechecks 2024-08-18 15:52:23 -07:00
Ben Kimock
d6c0ebef50 Polymorphize RawVec 2024-08-09 20:06:26 -04:00
Camille GILLOT
a0b4d6dfb8 Do not normalize constants eagerly. 2024-07-31 00:59:12 +00:00
DianQK
ae681c940d
Perform instsimplify before inline to eliminate some trivial calls 2024-07-29 18:14:35 +08:00
Jubilee
5d56572f06
Rollup merge of #126502 - cuviper:dump-mir-exclude-alloc-bytes, r=estebank
Ignore allocation bytes in some mir-opt tests

This adds `rustc -Zdump-mir-exclude-alloc-bytes` to skip writing allocation bytes in MIR dumps, and applies it to tests that were failing on s390x due to its big-endian byte order.

Fixes #126261
2024-07-12 13:47:05 -07:00
Scott McMurray
23c8ed14c9 Avoid MIR bloat in inlining
In 126578 we ended up with more binary size increases than expected.

This change attempts to avoid inlining large things into small things, to avoid that kind of increase, in cases when top-down inlining will still be able to do that inlining later.
2024-07-01 05:17:13 -07:00
Josh Stone
7c3673ff6f Bless mir-opt for excluded alloc bytes 2024-06-26 15:30:47 -07:00
Josh Stone
1a05cb2d93 Use -Zdump-mir-exclude-alloc-bytes in some mir-opt tests 2024-06-26 15:05:01 -07:00
bors
d7c59370ce Auto merge of #126844 - scottmcm:more-ptr-cast-gvn, r=saethlin
Remove more `PtrToPtr` casts in GVN

This addresses two things I noticed in MIR:

1. `NonNull::<T>::eq` does `(a as *mut T) == (b as *mut T)`, but it could just compare the `*const T`s, so this removes `PtrToPtr` casts that are on both sides of a pointer comparison, so long as they're not fat-to-thin casts.

2. `NonNull::<T>::addr` does `transmute::<_, usize>(p as *const ())`, but so long as `T: Thin` that cast doesn't do anything, and thus we can directly transmute the `*const T` instead.

r? mir-opt
2024-06-26 14:22:31 +00:00
Scott McMurray
ec9e35618d Also get add nuw from uN::checked_add 2024-06-23 13:29:06 -07:00
Scott McMurray
dd545e148c Make MIR inlining costs in build-std independent of config.toml 2024-06-23 01:48:41 -07:00
Scott McMurray
9088cd95a3 GVN away PtrToPtr-then-Transmute when possible 2024-06-22 20:34:09 -07:00
Scott McMurray
dd1e19e7c2 GVN away PtrToPtr before comparisons
Notably this happens in `NonNull::eq` :/
2024-06-22 20:27:08 -07:00
Scott McMurray
9140c9ad5b Add a mir test for slice::Iter::is_empty 2024-06-22 20:27:07 -07:00
Scott McMurray
b611b6bbb8 Replace NormalizeArrayLen with GVN
GVN is actually on in release, and covers all the same things (or more), with `LowerSliceLen` changed to produce `PtrMetadata`.
2024-06-20 22:16:59 -07:00
Scott McMurray
4a7b6c0e6c More GVN for PtrMetadata
`PtrMetadata` doesn't care about `*const`/`*mut`/`&`/`&mut`, so GVN away those casts in its argument.

This includes updating MIR to allow calling PtrMetadata on references too, not just raw pointers.  That means that `[T]::len` can be just `_0 = PtrMetadata(_1)`, for example.

# Conflicts:
#	tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-abort.mir
#	tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.panic-unwind.mir
2024-06-20 22:16:59 -07:00
Scott McMurray
4236da52af Give inlining bonuses to things that optimize out 2024-06-19 21:35:37 -07:00
Scott McMurray
4630d1b23b Ban ArrayToPointer and MutToConstPointer from runtime MIR
Apparently MIR borrowck cares about at least one of these for checking variance.

In runtime MIR, though, there's no need for them as `PtrToPtr` does the same thing.

(Banning them simplifies passes like GVN that no longer need to handle multiple cast possibilities.)
2024-06-19 10:44:01 -07:00
Scott McMurray
33c4817d98 Redo SliceIndex implementations 2024-06-15 17:39:25 -07:00
Scott McMurray
17a9d3498b Add ub-checks to slice_index MIR-opt test 2024-06-14 23:18:19 -07:00
Scott McMurray
a4d0fc39ba Add SingleUseConsts mir-opt pass 2024-06-10 00:06:02 -07:00
Scott McMurray
021ccf6c4e Enable GVN for AggregateKind::RawPtr & UnOp::PtrMetadata 2024-06-06 00:25:58 -07:00
Nicholas Nethercote
c9c80d2c5f rustfmt tests/mir-opt.
The only non-obvious changes:
- `building/storage_live_dead_in_statics.rs` has a `#[rustfmt::skip]`
  attribute to avoid reformating a table of data.
- Two `.mir` files have slight changes involving line numbers.
- In `unusual_item_types.rs` an `EMIT_MIR` annotation is moved to
  outside a function, which is the usual spot, because `tidy` complains
  if such a comment is indented.

The commit also tweaks the comments in `rustfmt.toml`.
2024-06-03 14:17:16 +10:00
Camille GILLOT
e110567dcd Revert "Auto merge of #115105 - cjgillot:dest-prop-default, r=oli-obk"
This reverts commit cfb730450f, reversing
changes made to 91c0823ee6.
2024-05-31 00:22:40 +00:00
bors
cfb730450f Auto merge of #115105 - cjgillot:dest-prop-default, r=oli-obk
Enable DestinationPropagation by default.

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

This PR proposes to enable the destination propagation pass by default.
This pass is meant to reduce the amount of copies present in MIR.

At the same time, this PR removes the `RenameReturnPlace` pass, as it is currently unsound.
`DestinationPropagation` is not limited to `_0`, but does not handle borrowed locals.
2024-05-30 14:27:46 +00:00
Camille GILLOT
5fa0ec6ad1 Enable DestinationPropagation by default. 2024-05-29 23:54:57 +00:00
Scott McMurray
0d63e6b608 [ACP 362] genericize ptr::from_raw_parts 2024-05-29 09:34:16 -07:00
Scott McMurray
459ce3f6bb Add an intrinsic for ptr::metadata 2024-05-28 09:28:51 -07:00
ltdk
72b7171031 Add assert_unsafe_precondition to unchecked_{add,sub,neg,mul,shl,shr} methods 2024-05-23 21:02:31 -04:00
Scott McMurray
95c0e5c6a8 Remove Rvalue::CheckedBinaryOp 2024-05-17 20:33:02 -07: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
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
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
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
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
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
Matthew Jasper
a277c901d9 Remove MIR unsafe check
This also remove safety information from MIR.
2024-04-03 08:50:12 +00: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
DianQK
47ed73a7b5
Eliminate UbCheck for non-standard libraries 2024-03-27 21:02:40 +08:00