-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an InstSimplify for repetitive array expressions #135274
Merged
+176
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/mir-opt/instsimplify/aggregate_array.const_items.InstSimplify-after-simplifycfg.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
- // MIR for `const_items` before InstSimplify-after-simplifycfg | ||
+ // MIR for `const_items` after InstSimplify-after-simplifycfg | ||
|
||
fn const_items() -> [u8; 5] { | ||
let mut _0: [u8; 5]; | ||
|
||
bb0: { | ||
- _0 = [const const_items::A, const const_items::B, const const_items::C, const const_items::D, const const_items::E]; | ||
+ _0 = [const const_items::A; 5]; | ||
return; | ||
} | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
.../mir-opt/instsimplify/aggregate_array.equal_referents.InstSimplify-after-simplifycfg.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
- // MIR for `equal_referents` before InstSimplify-after-simplifycfg | ||
+ // MIR for `equal_referents` after InstSimplify-after-simplifycfg | ||
|
||
fn equal_referents() -> [&u8; 5] { | ||
let mut _0: [&u8; 5]; | ||
|
||
bb0: { | ||
_0 = [const equal_referents::A, const equal_referents::B, const equal_referents::C, const equal_referents::D, const equal_referents::E]; | ||
return; | ||
} | ||
} | ||
|
13 changes: 13 additions & 0 deletions
13
tests/mir-opt/instsimplify/aggregate_array.literals.InstSimplify-after-simplifycfg.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
- // MIR for `literals` before InstSimplify-after-simplifycfg | ||
+ // MIR for `literals` after InstSimplify-after-simplifycfg | ||
|
||
fn literals() -> [u8; 5] { | ||
let mut _0: [u8; 5]; | ||
|
||
bb0: { | ||
- _0 = [const 0_u8, const 0_u8, const 0_u8, const 0_u8, const 0_u8]; | ||
+ _0 = [const 0_u8; 5]; | ||
return; | ||
} | ||
} | ||
|
39 changes: 39 additions & 0 deletions
39
tests/mir-opt/instsimplify/aggregate_array.local.InstSimplify-after-simplifycfg.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
- // MIR for `local` before InstSimplify-after-simplifycfg | ||
+ // MIR for `local` after InstSimplify-after-simplifycfg | ||
|
||
fn local() -> [u8; 5] { | ||
let mut _0: [u8; 5]; | ||
let _1: u8; | ||
let mut _2: u8; | ||
let mut _3: u8; | ||
let mut _4: u8; | ||
let mut _5: u8; | ||
let mut _6: u8; | ||
scope 1 { | ||
debug val => _1; | ||
} | ||
|
||
bb0: { | ||
StorageLive(_1); | ||
_1 = const 0_u8; | ||
StorageLive(_2); | ||
_2 = copy _1; | ||
StorageLive(_3); | ||
_3 = copy _1; | ||
StorageLive(_4); | ||
_4 = copy _1; | ||
StorageLive(_5); | ||
_5 = copy _1; | ||
StorageLive(_6); | ||
_6 = copy _1; | ||
_0 = [move _2, move _3, move _4, move _5, move _6]; | ||
StorageDead(_6); | ||
StorageDead(_5); | ||
StorageDead(_4); | ||
StorageDead(_3); | ||
StorageDead(_2); | ||
StorageDead(_1); | ||
return; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//@ test-mir-pass: InstSimplify-after-simplifycfg | ||
#![crate_type = "lib"] | ||
|
||
// This is the easy case, and the most plausible to run into in real code. | ||
// EMIT_MIR aggregate_array.literals.InstSimplify-after-simplifycfg.diff | ||
pub fn literals() -> [u8; 5] { | ||
// CHECK-LABEL: fn literals( | ||
// CHECK: _0 = [const 0_u8; 5]; | ||
[0, 0, 0, 0, 0] | ||
} | ||
|
||
// Check that hiding the const value behind a const item doesn't prevent the optimization | ||
// EMIT_MIR aggregate_array.const_items.InstSimplify-after-simplifycfg.diff | ||
pub fn const_items() -> [u8; 5] { | ||
const A: u8 = 0; | ||
const B: u8 = 0; | ||
const C: u8 = 0; | ||
const D: u8 = 0; | ||
const E: u8 = 0; | ||
|
||
// CHECK-LABEL: fn const_items( | ||
// CHECK: _0 = [const const_items::A; 5]; | ||
[A, B, C, D, E] | ||
} | ||
|
||
// EMIT_MIR aggregate_array.strs.InstSimplify-after-simplifycfg.diff | ||
pub fn strs() -> [&'static str; 5] { | ||
// CHECK-LABEL: fn strs( | ||
// CHECK: _0 = [const "a"; 5]; | ||
["a", "a", "a", "a", "a"] | ||
} | ||
|
||
// InstSimplify isn't able to see through the move operands, but GVN can. | ||
// EMIT_MIR aggregate_array.local.InstSimplify-after-simplifycfg.diff | ||
pub fn local() -> [u8; 5] { | ||
// CHECK-LABEL: fn local( | ||
// CHECK: _0 = [move _2, move _3, move _4, move _5, move _6]; | ||
let val = 0; | ||
[val, val, val, val, val] | ||
} | ||
|
||
// All of these consts refer to the same value, but the addresses are all different. | ||
// It would be wrong to apply the optimization here. | ||
// EMIT_MIR aggregate_array.equal_referents.InstSimplify-after-simplifycfg.diff | ||
pub fn equal_referents() -> [&'static u8; 5] { | ||
const DATA: &[u8] = &[0, 0, 0, 0, 0]; | ||
const A: &u8 = &DATA[0]; | ||
const B: &u8 = &DATA[1]; | ||
const C: &u8 = &DATA[2]; | ||
const D: &u8 = &DATA[3]; | ||
const E: &u8 = &DATA[4]; | ||
|
||
// CHECK-LABEL: fn equal_referents( | ||
// CHECK: _0 = [const equal_referents::A, const equal_referents::B, const equal_referents::C, const equal_referents::D, const equal_referents::E]; | ||
[A, B, C, D, E] | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/mir-opt/instsimplify/aggregate_array.strs.InstSimplify-after-simplifycfg.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
- // MIR for `strs` before InstSimplify-after-simplifycfg | ||
+ // MIR for `strs` after InstSimplify-after-simplifycfg | ||
|
||
fn strs() -> [&str; 5] { | ||
let mut _0: [&str; 5]; | ||
|
||
bb0: { | ||
- _0 = [const "a", const "a", const "a", const "a", const "a"]; | ||
+ _0 = [const "a"; 5]; | ||
return; | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be cool to see more tests exercising this eval call. Specifically, maybe something like
[CONST, CONST, ...]
for some global const, something with ZSTs, something gauging whether or not it works with things like nested slices/string lits (probably doesn't? const eval doesn't deduplicate allocations like that right?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The optimization doesn't work on ZSTs. Ultimately that doesn't matter because RemoveZsts will run before InstSimplify and completely eliminate the statement we would optimize.
But it makes me a bit worried that this doesn't work with ZSTs because we generate a local per array element for ZSTs, but we don't for a numeric literal, and this optimization relies on generating an Operand::Const, not a local per array element that we then Move from.