This repository was archived by the owner on May 28, 2025. It is now read-only.
Commit e730bff
committed
Auto merge of rust-lang#136788 - FractalFir:prop_trivial_locals, r=<try>
[perf experiment] A MIR pass removing unneded temporary locals
Experiment with early MIR optimization removing temporary locals
# Motivation
Currently, a lot of MIR contains unneded assigements to temporaries:
### Rust
```rust
fn add_points(lhs:(f32,f32,f32),rhs:(f32,f32,f32))->(f32,f32,f32){
(lhs.0 + rhs.0, lhs.1 + rhs.1, lhs.2 + rhs.2)
}
```
### Orignal MIR
```mir
bb0: {
StorageLive(_3);
StorageLive(_4);
// _4 and _5 are not needed!
_4 = copy (_1.0: f32);
StorageLive(_5);
_5 = copy (_2.0: f32);
_3 = Add(move _4, move _5);
StorageDead(_5);
StorageDead(_4);
StorageLive(_6);
StorageLive(_7);
// _7 and _8 are not needed!
_7 = copy (_1.1: f32);
StorageLive(_8);
_8 = copy (_2.1: f32);
_6 = Add(move _7, move _8);
StorageDead(_8);
StorageDead(_7);
StorageLive(_9);
StorageLive(_10);
// _10 and _9 are not needed!
_10 = copy (_1.2: f32);
StorageLive(_11);
_11 = copy (_2.2: f32);
_9 = Add(move _10, move _11);
StorageDead(_11);
StorageDead(_10);
_0 = (move _3, move _6, move _9);
StorageDead(_9);
StorageDead(_6);
StorageDead(_3);
return;
}
```
This pass tries to remove as such many temporaries as possible. This leads to reduced MIR sizes, which should hopefully speed the next passes up.
```mir
fn add_points(_1: (f32, f32, f32), _2: (f32, f32, f32)) -> (f32, f32, f32) {
debug lhs => _1;
debug rhs => _2;
let mut _0: (f32, f32, f32);
let mut _3: f32;
let mut _4: f32;
let mut _5: f32;
bb0: {
StorageLive(_3);
_3 = Add(copy (_1.0: f32), copy (_2.0: f32));
StorageLive(_4);
_4 = Add(copy (_1.1: f32), copy (_2.1: f32));
StorageLive(_5);
_5 = Add(copy (_1.2: f32), copy (_2.2: f32));
_0 = (move _3, move _4, move _5);
StorageDead(_5);
StorageDead(_4);
StorageDead(_3);
return;
}
}
```
**This PR is not yet meant for merging!**
The current version is still a bit from being done: it does not optimize locals used in calls, and some parts may need tweaking.
Still, I belive it is at least worth timing at this point, which is why I am requesting a perf run.File tree
3 files changed
+171
-2
lines changed- compiler/rustc_mir_transform/src
3 files changed
+171
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | | - | |
| 128 | + | |
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
| |||
654 | 654 | | |
655 | 655 | | |
656 | 656 | | |
| 657 | + | |
657 | 658 | | |
658 | 659 | | |
659 | 660 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
956 | 956 | | |
957 | 957 | | |
958 | 958 | | |
959 | | - | |
| 959 | + | |
960 | 960 | | |
961 | 961 | | |
962 | 962 | | |
| |||
0 commit comments