Skip to content
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

gvn: Promote/propagate const local array #126444

Open
wants to merge 31 commits into
base: master
Choose a base branch
from

Conversation

tesuji
Copy link
Contributor

@tesuji tesuji commented Jun 13, 2024

Rewriting of #125916 which used PromoteTemps pass.

This allows promoting constant local arrays as anonymous constants. So that's in codegen for
a local array, rustc outputs llvm.memcpy (which is easy for LLVM to optimize) instead of a series
of store on stack (a.k.a in-place initialization). This makes rustc on par with clang on this specific case.
See more in #73825 or zulip for more info.

Here is a simple micro benchmark that shows the performance differences between promoting arrays or not.

Prior discussions on zulip.

This patch saves about 600 KB (~0.5%) of librustc_driver.so.
image

Fix #73825

r? cjgillot

Unresolved questions

  • Should we ignore nested arrays?
    I think that promoting nested arrays is bloating codegen.
  • Should stack_threshold be at least 32 bytes? Like the benchmark showed.
    If yes, the test should be updated to make arrays larger than 32 bytes.
  • Is this concerning that call(move _1) is now call(const [array])?
    It reverted back to call(move _1)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 13, 2024
@rustbot
Copy link
Collaborator

rustbot commented Jun 13, 2024

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

@tesuji tesuji changed the title WIP: Trying to promote const local array using GVN pass WIP: Promote/propagate const local array using GVN pass Jun 13, 2024
@tesuji tesuji changed the title WIP: Promote/propagate const local array using GVN pass [WIP] gvn: Promote/propagate const local array Jun 13, 2024
@rust-log-analyzer

This comment has been minimized.

@jieyouxu
Copy link
Contributor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 13, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 13, 2024
[WIP] gvn: Promote/propagate const local array

Rewriting of rust-lang#125916 which used PromoteTemps pass.

Fix rust-lang#73825

### Current status

- [ ] Waiting for [consensus](https://rust-lang.zulipchat.com/#narrow/stream/136281-t-opsem/topic/Could.20const.20read-only.20arrays.20be.20const.20promoted.3F).

r? ghost
@bors
Copy link
Contributor

bors commented Jun 13, 2024

⌛ Trying commit 550fb81 with merge e26c0b3...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Jun 14, 2024

☀️ Try build successful - checks-actions
Build commit: e26c0b3 (e26c0b3f8c9af007281a11df56a0bf825d8b4cb0)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (e26c0b3): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (primary 3.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
9.2% [4.2%, 18.7%] 4
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-9.1% [-9.9%, -8.3%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.1% [-9.9%, 18.7%] 6

Cycles

Results (primary -4.0%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-4.0% [-5.3%, -1.7%] 5
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -4.0% [-5.3%, -1.7%] 5

Binary size

Results (primary -0.1%, secondary 0.0%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.0% [0.0%, 0.0%] 1
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
-0.1% [-0.2%, -0.0%] 5
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.2%, 0.0%] 6

Bootstrap: 671.518s -> 674.73s (0.48%)
Artifact size: 320.38 MiB -> 320.39 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 14, 2024
@rust-log-analyzer

This comment has been minimized.

@Kobzol
Copy link
Contributor

Kobzol commented Jun 14, 2024

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 14, 2024
@bors
Copy link
Contributor

bors commented Jun 14, 2024

⌛ Trying commit 6c6de58 with merge 7e160d4...

bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 14, 2024
[WIP] gvn: Promote/propagate const local array

Rewriting of rust-lang#125916 which used PromoteTemps pass.

Fix rust-lang#73825

### Current status

- [ ] Waiting for [consensus](https://rust-lang.zulipchat.com/#narrow/stream/136281-t-opsem/topic/Could.20const.20read-only.20arrays.20be.20const.20promoted.3F).

r? ghost
@bors
Copy link
Contributor

bors commented Jun 14, 2024

☀️ Try build successful - checks-actions
Build commit: 7e160d4 (7e160d4b55bb5a27be0696f45db247ccc2e166d9)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (7e160d4): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (primary 1.7%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
11.0% [3.9%, 21.3%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-7.6% [-10.4%, -4.8%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.7% [-10.4%, 21.3%] 6

Cycles

Results (secondary 9.0%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
9.0% [9.0%, 9.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

Results (primary -0.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.0% [0.0%, 0.0%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.2%, -0.0%] 5
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.2%, 0.0%] 6

Bootstrap: 670.938s -> 673.147s (0.33%)
Artifact size: 320.39 MiB -> 319.79 MiB (-0.19%)

@rustbot rustbot added the A-mir-opt Area: MIR optimizations label Jun 14, 2024
@rust-log-analyzer

This comment has been minimized.

big arrays make the diff hard to read
@tesuji
Copy link
Contributor Author

tesuji commented Jun 15, 2024

How does it affect debug build ? Isn't gvn run only if mir_opt_level >= 2
image

@Kobzol
Copy link
Contributor

Kobzol commented Jun 15, 2024

Don't mind max RSS, it's quite noisy. Unless there are regressions across the board, you can mostly ignore it.

compiler/rustc_mir_transform/src/gvn.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_transform/src/gvn.rs Outdated Show resolved Hide resolved
@@ -382,7 +396,16 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
let ty = match kind {
AggregateTy::Array => {
assert!(fields.len() > 0);
Ty::new_array(self.tcx, fields[0].layout.ty, fields.len() as u64)
let field_ty = fields[0].layout.ty;
// Ignore nested array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you write why?

Copy link
Contributor Author

@tesuji tesuji Jun 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just feel that nested arrays are rarer than 1-demensional arrays.
And it is kinda redundant that in MIR output that each Local is const promoted as a const array.

If nested array are allowed, we would have.

let a = [[b], [c]];

promoted as

_b = const [.. ];
_c = const [.. ];
-_a = const [move _b, moved _c];
+_a = const [[..], [..]];

We could const promote only _a, but it is more complex to ignore _b and _c.

Comment on lines +433 to +435
if ty.layout.size().bytes() <= STACK_THRESHOLD {
return None;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this case, should we attempt to fallback to the next branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the fallback is return None at line L471 ? Maybe I read the code wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean the matches!(ty.abi, Abi::Scalar(..) | Abi::ScalarPair(..)) branch.

Copy link
Contributor Author

@tesuji tesuji Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But ty.abi of array is not Abi::Scalar* though. And with STACK_THRESHOLD, I fallback to default behavior on master branch. Also I committed 4fb498e.
But I don't get what the differences between ecx.intern_with_temp_alloc and ecx.alloc, in the case of big array (sizeof(array) > stack_size). Or should I use rustc_const_eval::const_eval::machine::MemoryKind?

compiler/rustc_mir_transform/src/gvn.rs Outdated Show resolved Hide resolved
Comment on lines 1439 to 1449
// Ignore arrays in operands.
// Prevent code bloat that makes
// `_2 = _1` now resolved to `_2 = <evaluated array>`.
let disallow_dup_array = if rvalue.ty(self.local_decls, self.tcx).is_array()
&& let Some(locals) = self.rev_locals.get(value).as_deref()
&& let [first, ..] = locals[..]
{
first != lhs.local
} else {
false
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this is meant to do. Why is this only checking the first inside rev_local?

Copy link
Contributor Author

@tesuji tesuji Jun 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it, duplicate arrays in code will be duplicated in mir output.
Consider this

// code
a = [1,2,3];
b = [1,2,3];
// mir with and without this condition.
_1 = const [1,2,3];
-_2 = _1;
+_2 = const [1,2,3];

In short, we want to run try_as_local for duplicate arrays. But I don't know how often it is to worry about it.

tests/codegen/issue-73825-gvn-const-local-array.rs Outdated Show resolved Hide resolved
tests/mir-opt/const_array_locals.rs Outdated Show resolved Hide resolved
tests/mir-opt/const_array_locals.rs Show resolved Hide resolved
StorageDead(_12);
StorageLive(_13);
StorageLive(_14);
_14 = [const 1_i32, const 0_i32, const 0_i32];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this one made a constant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sizeof(_14) is 12 bytes ( < STACK_THRESHOULD). After codegen, it is just a mov xmm instruction, which is very cheap.

Co-authored-by: Camille Gillot <gillot.camille@gmail.com>
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot assigned oli-obk and unassigned cjgillot Jul 4, 2024
@oli-obk oli-obk assigned cjgillot and unassigned oli-obk Jul 5, 2024
@rustbot
Copy link
Collaborator

rustbot commented Jul 7, 2024

Could not assign reviewer from: cjgillot.
User(s) cjgillot are either the PR author, already assigned, or on vacation, and there are no other candidates.
Use r? to specify someone else to assign.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir-opt Area: MIR optimizations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New optimization: Move non-mutable array of Copy type to .rodata
10 participants