Commit Graph

38 Commits

Author SHA1 Message Date
bors
79611d90b6 Auto merge of #122551 - RayMuir:copy_fmt, r=saethlin
Added "copy" to Debug fmt for copy operands

In MIR's debug mode (--emit mir) the printing for Operands is slightly inconsistent.

The RValues - values on the right side of an Assign - are usually printed with their Operand when they are Places.

Example:
_2 = move _3

But for arguments, the operand is omitted.

_2 = _1

I propose a change be made, to display the place with the operand.

_2 = copy _1

Move and copy have different semantics, meaning this difference is important and helpful to the user. It also adds consistency to the pretty printing.

-- EDIT --

 Consider this example Rust program and its MIR output with the **updated pretty printer.**

This was generated with the arguments --emit mir --crate-type lib -Zmir-opt-level=0 (Otherwise, it's optimised away since it's a junk program).

```rust
fn main(foo: i32) {
    let v = 10;

    if v == 20 {
        foo;
    }
    else {
        v;
    }
}
```

```MIR
// WARNING: This output format is intended for human consumers only
// and is subject to change without notice. Knock yourself out.
fn main(_1: i32) -> () {
    debug foo => _1;
    let mut _0: ();
    let _2: i32;
    let mut _3: bool;
    let mut _4: i32;
    let _5: i32;
    let _6: i32;
    scope 1 {
        debug v => _2;
    }

    bb0: {
        StorageLive(_2);
        _2 = const 10_i32;
        StorageLive(_3);
        StorageLive(_4);
        _4 = copy _2;
        _3 = Eq(move _4, const 20_i32);
        switchInt(move _3) -> [0: bb2, otherwise: bb1];
    }

    bb1: {
        StorageDead(_4);
        StorageLive(_5);
        _5 = copy _1;
        StorageDead(_5);
        _0 = const ();
        goto -> bb3;
    }

    bb2: {
        StorageDead(_4);
        StorageLive(_6);
        _6 = copy _2;
        StorageDead(_6);
        _0 = const ();
        goto -> bb3;
    }

    bb3: {
        StorageDead(_3);
        StorageDead(_2);
        return;
    }
}
```

In this example program, we can see that when we move a place, it is preceded by "move". e.g. ``` _3 = Eq(move _4, const 20_i32);```. However, when we copy a place such as ```_5 = _1;```, it is not preceded by the operand in the original printout. I propose to change the print to include the copy ```_5 = copy _1``` as in this example.

Regarding the arguments part. When I originally submitted this PR, I was under the impression this only affected the print for arguments to a function, but actually, it affects anything that uses a copy. This is preferable anyway with regard to consistency. The PR is about making ```copy``` explicit.
2024-08-19 23:10:46 +00:00
Scott McMurray
249a36ffbd Update mir-opt filechecks 2024-08-18 15:52:23 -07:00
Ralf Jung
79503dd742 stabilize raw_ref_op 2024-08-18 19:46:53 +02: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
f6942112eb Add a GVN test for 127089 that doesn't optimize to a constant 2024-06-30 11:30:54 -07:00
Josh Stone
1a05cb2d93 Use -Zdump-mir-exclude-alloc-bytes in some mir-opt tests 2024-06-26 15:05:01 -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
55d13379ac [GVN] Add tests for generic pointees with 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
021ccf6c4e Enable GVN for AggregateKind::RawPtr & UnOp::PtrMetadata 2024-06-06 00:25:58 -07:00
Nicholas Nethercote
ac24299636 Reformat mir! macro invocations to use braces.
The `mir!` macro has multiple parts:
- An optional return type annotation.
- A sequence of zero or more local declarations.
- A mandatory starting anonymous basic block, which is brace-delimited.
- A sequence of zero of more additional named basic blocks.

Some `mir!` invocations use braces with a "block" style, like so:
```
mir! {
    let _unit: ();
    {
	let non_copy = S(42);
	let ptr = std::ptr::addr_of_mut!(non_copy);
	// Inside `callee`, the first argument and `*ptr` are basically
	// aliasing places!
	Call(_unit = callee(Move(*ptr), ptr), ReturnTo(after_call), UnwindContinue())
    }
    after_call = {
	Return()
    }
}
```
Some invocations use parens with a "block" style, like so:
```
mir!(
    let x: [i32; 2];
    let one: i32;
    {
	x = [42, 43];
	one = 1;
	x = [one, 2];
	RET = Move(x);
	Return()
    }
)
```
And some invocations uses parens with a "tighter" style, like so:
```
mir!({
    SetDiscriminant(*b, 0);
    Return()
})
```
This last style is generally used for cases where just the mandatory
starting basic block is present. Its braces are placed next to the
parens.

This commit changes all `mir!` invocations to use braces with a "block"
style. Why?

- Consistency is good.

- The contents of the invocation is a block of code, so it's odd to use
  parens. They are more normally used for function-like macros.

- Most importantly, the next commit will enable rustfmt for
  `tests/mir-opt/`. rustfmt is more aggressive about formatting macros
  that use parens than macros that use braces. Without this commit's
  changes, rustfmt would break a couple of `mir!` macro invocations that
  use braces within `tests/mir-opt` by inserting an extraneous comma.
  E.g.:
  ```
  mir!(type RET = (i32, bool);, { // extraneous comma after ';'
      RET.0 = 1;
      RET.1 = true;
      Return()
  })
  ```
  Switching those `mir!` invocations to use braces avoids that problem,
  resulting in this, which is nicer to read as well as being valid
  syntax:
  ```
  mir! {
      type RET = (i32, bool);
      {
	  RET.0 = 1;
	  RET.1 = true;
	  Return()
      }
  }
  ```
2024-06-03 13:24:44 +10: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
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
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
Ralf Jung
75d0fdd967 mir-opt tests: rename unit-test -> test-mir-pass 2024-04-20 13:19:34 +02:00
许杰友 Jieyou Xu (Joe)
6e48b96692
[AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
Camille GILLOT
a2a9125d6a Add test for ptr ops with same provenance. 2024-02-09 21:06:24 +00:00
Camille GILLOT
5a6f14c4f4 Split gvn wide ptr tests. 2024-02-09 21:01:57 +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
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
Ralf Jung
0f9baa8a31 custom mir: make it clear what the return block is 2023-12-26 20:15:26 +01:00
Camille GILLOT
8022057ebb Avoid overflow in GVN constant indexing. 2023-12-17 19:50:38 +00:00
Tomasz Miąsko
78da577650 Custom MIR: Support cleanup blocks
Cleanup blocks are declared with `bb (cleanup) = { ... }`.

`Call` and `Drop` terminators take an additional argument describing the
unwind action, which is one of the following:

* `UnwindContinue()`
* `UnwindUnreachable()`
* `UnwindTerminate(reason)`, where reason is `ReasonAbi` or `ReasonInCleanup`
* `UnwindCleanup(block)`

Also support unwind resume and unwind terminate terminators:

* `UnwindResume()`
* `UnwindTerminate(reason)`
2023-11-14 08:23:58 +01:00
Camille GILLOT
d80eb3a498 Verify that the alloc_id is Memory. 2023-10-25 17:59:30 +00:00
Camille GILLOT
c4cc9ca060 Do not merge fn pointer casts. 2023-10-25 06:47:04 +00:00
Camille GILLOT
ac0228da59 FileCheck gvn. 2023-10-25 06:46:48 +00:00
Camille GILLOT
5e78b9cdb3 Disambiguate non-deterministic constants. 2023-10-25 06:46:48 +00:00
Camille GILLOT
dbf9ea30dd Transform large arrays into Repeat expressions when possible. 2023-10-25 06:46:48 +00:00
Camille GILLOT
80a5e8522d Extract simplify_aggregate. 2023-10-25 06:46:47 +00:00
Camille GILLOT
692e528647 Simplify projections in GVN. 2023-10-25 06:46:47 +00:00
Camille GILLOT
4ff03cd1a4 Allow to run filecheck in mir-opt tests. 2023-10-19 15:51:52 +00:00
Camille GILLOT
b2ff77cb78 Do not clone valtree and slice constants. 2023-09-24 09:09:05 +00:00
Camille GILLOT
1ea9399803 Do not check copiability. 2023-09-24 09:09:04 +00:00
Camille GILLOT
8b848af325 Add global value numbering pass. 2023-09-24 09:09:04 +00:00