Commit Graph

155 Commits

Author SHA1 Message Date
许杰友 Jieyou Xu (Joe) b5bc13a367 Mark simplify_aggregate_to_copy mir-opt as unsound
Co-authored-by: DianQK <dianqk@dianqk.net>
(cherry picked from commit 10b8ba4ecb)
2024-11-01 11:15:23 -07:00
Ben Kimock 8d562f6cc5 Disable slice_iter mir-opt test in debug builds 2024-10-07 12:23:27 -04:00
Ben Kimock 128ccc3c26 Bless mir-opt tests 2024-10-07 11:18:37 -04:00
Ben Kimock 6d246e47fb Add precondition checks to ptr::offset, ptr::add, ptr::sub 2024-10-07 11:12:58 -04:00
Stuart Cook dd4f062b07 Rollup merge of #128399 - mammothbane:master, r=Amanieu,tgross35
liballoc: introduce String, Vec const-slicing

This change `const`-qualifies many methods on `Vec` and `String`, notably `as_slice`, `as_str`, `len`. These changes are made behind the unstable feature flag `const_vec_string_slice`.

## Motivation
This is to support simultaneous variance over ownership and constness. I have an enum type that may contain either `String` or `&str`, and I want to produce a `&str` from it in a possibly-`const` context.

```rust
enum StrOrString<'s> {
    Str(&'s str),
    String(String),
}

impl<'s> StrOrString<'s> {
    const fn as_str(&self) -> &str {
        match self {
             // In a const-context, I really only expect to see this variant, but I can't switch the implementation
             // in some mode like #[cfg(const)] -- there has to be a single body
             Self::Str(s) => s,

             // so this is a problem, since it's not `const`
             Self::String(s) => s.as_str(),
        }
    }
}
```

Currently `String` and `Vec` don't support this, but can without functional changes. Similar logic applies for `len`, `capacity`, `is_empty`.

## Changes

The essential thing enabling this change is that `Unique::as_ptr` is `const`. This lets us convert `RawVec::ptr` -> `Vec::as_ptr` -> `Vec::as_slice` -> `String::as_str`.

I had to move the `Deref` implementations into `as_{str,slice}` because `Deref` isn't `#[const_trait]`, but I would expect this change to be invisible up to inlining. I moved the `DerefMut` implementations as well for uniformity.
2024-10-07 15:37:06 +11:00
Nathan Perry d793766a61 liballoc: introduce String, Vec const-slicing
This change `const`-qualifies many methods on Vec and String, notably
`as_slice`, `as_str`, `len`. These changes are made behind the unstable
feature flag `const_vec_string_slice` with the following tracking issue:

https://github.com/rust-lang/rust/issues/129041
2024-10-06 19:58:35 -04:00
Lukas Markeffsky bd31e3ed70 be even more precise about "cast" vs "coercion" 2024-09-24 23:12:02 +02:00
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