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

Evaluate std::fmt::Arguments::new_const() during Compile Time #131663

Closed

Conversation

veera-sivarajan
Copy link
Contributor

@veera-sivarajan veera-sivarajan commented Oct 13, 2024

Fixes #128709

This PR aims to optimize calls to string formating macros without any arguments by evaluating std::fmt::Arguments::new_const() in a const context.

Currently,
println!("hola") compiles to std::io::_print(std::fmt::Arguments::new_const(&["hola\n"])).

With this PR,
println!("hola") compiles to std::io::_print(const { std::fmt::Arguments::new_const(&["hola\n"]) }).

This is accomplished by wrapping calls to std::fmt::Arguments::new_const() in an inline const block when lowering the AST to HIR.

This reduces the generated code to a memcpy instead of multiple getelementptr and store instructions even with -C no-prepopulate-passes -C opt-level=0. Godbolt for code comparison: https://rust.godbolt.org/z/P7Px7de6c

This is a safe and sound transformation because std::fmt::Arguments::new_const() is a trivial constructor function taking a slice containing a 'static string literal as input.

CC #99012

@rustbot
Copy link
Collaborator

rustbot commented Oct 13, 2024

r? @chenyukang

rustbot has assigned @chenyukang.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 13, 2024
@Kobzol
Copy link
Contributor

Kobzol commented Oct 13, 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 Oct 13, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 13, 2024
…<try>

Evaluate `std::fmt::Arguments::new_const()` during Compile Time

Fixes rust-lang#128709

This PR aims to optimize calls to string formating macros without any arguments by evaluating `std::fmt::Arguments::new_const()` in a const context.

Currently,
`println!("hola")` compiles to `std::io::_print(std::fmt::Arguments::new_const(&["hola\n"]))`.

With this PR,
`println!("hola")` compiles to `std::io::_print(const { std::fmt::Arguments::new_const(&["hola\n"]) })`.

This is accomplished in two steps:

1.  Const stabilize `std::fmt::Arguments::new_const()`.
2.  Wrap calls to `std::fmt::Arguments::new_const()` in an inline const block when lowering the AST to HIR.

This reduces the generated code to a `memcpy` instead of multiple `getelementptr` and `store` instructions even with `-C no-prepopulate-passes -C opt-level=0`. Godbolt for code comparison: https://rust.godbolt.org/z/P7Px7de6c

This is a safe and sound transformation because `std::fmt::Arguments::new_const()` is a trivial constructor function taking a slice containing a `'static` string literal as input.

CC rust-lang#99012
@bors
Copy link
Contributor

bors commented Oct 13, 2024

⌛ Trying commit 7638733 with merge ed038de...

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Oct 13, 2024

Some changes occurred in coverage tests.

cc @Zalathar

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Oct 14, 2024

This PR modifies tests/run-make/. If this PR is trying to port a Makefile
run-make test to use rmake.rs, please update the
run-make port tracking issue
so we can track our progress. You can either modify the tracking issue
directly, or you can comment on the tracking issue and link this PR.

cc @jieyouxu

@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Oct 14, 2024
@lqd
Copy link
Member

lqd commented Oct 14, 2024

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Oct 14, 2024

⌛ Trying commit 95b1d81 with merge 814ddfe...

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 14, 2024
…<try>

Evaluate `std::fmt::Arguments::new_const()` during Compile Time

Fixes rust-lang#128709

This PR aims to optimize calls to string formating macros without any arguments by evaluating `std::fmt::Arguments::new_const()` in a const context.

Currently,
`println!("hola")` compiles to `std::io::_print(std::fmt::Arguments::new_const(&["hola\n"]))`.

With this PR,
`println!("hola")` compiles to `std::io::_print(const { std::fmt::Arguments::new_const(&["hola\n"]) })`.

This is accomplished in two steps:

1.  Const stabilize `std::fmt::Arguments::new_const()`.
2.  Wrap calls to `std::fmt::Arguments::new_const()` in an inline const block when lowering the AST to HIR.

This reduces the generated code to a `memcpy` instead of multiple `getelementptr` and `store` instructions even with `-C no-prepopulate-passes -C opt-level=0`. Godbolt for code comparison: https://rust.godbolt.org/z/P7Px7de6c

This is a safe and sound transformation because `std::fmt::Arguments::new_const()` is a trivial constructor function taking a slice containing a `'static` string literal as input.

CC rust-lang#99012
@bors
Copy link
Contributor

bors commented Oct 14, 2024

☀️ Try build successful - checks-actions
Build commit: 814ddfe (814ddfee7d50cf02df6549a9aec46528285bdf12)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (814ddfe): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

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.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

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

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.8% [0.2%, 2.7%] 56
Regressions ❌
(secondary)
1.2% [1.1%, 1.4%] 9
Improvements ✅
(primary)
-0.7% [-2.9%, -0.1%] 14
Improvements ✅
(secondary)
-0.3% [-1.1%, -0.1%] 25
All ❌✅ (primary) 0.5% [-2.9%, 2.7%] 70

Max RSS (memory usage)

Results (primary 2.3%, secondary 1.9%)

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)
2.5% [0.8%, 10.4%] 34
Regressions ❌
(secondary)
2.6% [1.6%, 3.3%] 7
Improvements ✅
(primary)
-4.1% [-4.1%, -4.1%] 1
Improvements ✅
(secondary)
-2.7% [-2.7%, -2.7%] 1
All ❌✅ (primary) 2.3% [-4.1%, 10.4%] 35

Cycles

Results (primary 1.8%, secondary -0.6%)

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)
1.8% [0.9%, 4.1%] 15
Regressions ❌
(secondary)
1.0% [1.0%, 1.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.1% [-2.1%, -2.1%] 1
All ❌✅ (primary) 1.8% [0.9%, 4.1%] 15

Binary size

Results (primary 0.1%, secondary 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.6% [0.0%, 1.4%] 47
Regressions ❌
(secondary)
0.4% [0.0%, 2.4%] 47
Improvements ✅
(primary)
-0.3% [-0.6%, -0.0%] 56
Improvements ✅
(secondary)
-0.6% [-1.3%, -0.1%] 14
All ❌✅ (primary) 0.1% [-0.6%, 1.4%] 103

Bootstrap: 782.597s -> 784.903s (0.29%)
Artifact size: 332.70 MiB -> 333.44 MiB (0.22%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Oct 14, 2024
@bors
Copy link
Contributor

bors commented Oct 22, 2024

☔ The latest upstream changes (presumably #132020) made this pull request unmergeable. Please resolve the merge conflicts.

@veera-sivarajan
Copy link
Contributor Author

I don't know what's causing the large number of regressions.

Please let me know if it's better to close this.

@chenyukang
Copy link
Member

@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 Oct 28, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 28, 2024
…<try>

Evaluate `std::fmt::Arguments::new_const()` during Compile Time

Fixes rust-lang#128709

This PR aims to optimize calls to string formating macros without any arguments by evaluating `std::fmt::Arguments::new_const()` in a const context.

Currently,
`println!("hola")` compiles to `std::io::_print(std::fmt::Arguments::new_const(&["hola\n"]))`.

With this PR,
`println!("hola")` compiles to `std::io::_print(const { std::fmt::Arguments::new_const(&["hola\n"]) })`.

This is accomplished by wrapping calls to `std::fmt::Arguments::new_const()` in an inline const block when lowering the AST to HIR.

This reduces the generated code to a `memcpy` instead of multiple `getelementptr` and `store` instructions even with `-C no-prepopulate-passes -C opt-level=0`. Godbolt for code comparison: https://rust.godbolt.org/z/P7Px7de6c

This is a safe and sound transformation because `std::fmt::Arguments::new_const()` is a trivial constructor function taking a slice containing a `'static` string literal as input.

CC rust-lang#99012
@bors
Copy link
Contributor

bors commented Oct 28, 2024

⌛ Trying commit e20cc8e with merge 1e4519d...

@bors
Copy link
Contributor

bors commented Oct 28, 2024

☀️ Try build successful - checks-actions
Build commit: 1e4519d (1e4519d5900fc165d4cffd1a82ce0aeca8bab9fd)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1e4519d): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

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.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

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

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.8% [0.2%, 3.0%] 59
Regressions ❌
(secondary)
1.5% [1.1%, 2.7%] 13
Improvements ✅
(primary)
-0.5% [-2.6%, -0.2%] 16
Improvements ✅
(secondary)
-0.3% [-0.5%, -0.0%] 16
All ❌✅ (primary) 0.5% [-2.6%, 3.0%] 75

Max RSS (memory usage)

Results (primary 1.9%, secondary 2.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)
2.1% [0.7%, 6.5%] 17
Regressions ❌
(secondary)
2.7% [1.7%, 4.3%] 3
Improvements ✅
(primary)
-1.8% [-1.8%, -1.8%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.9% [-1.8%, 6.5%] 18

Cycles

Results (primary 2.6%, secondary -0.9%)

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)
2.6% [1.9%, 3.7%] 6
Regressions ❌
(secondary)
5.1% [5.0%, 5.2%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.1% [-4.4%, -2.3%] 8
All ❌✅ (primary) 2.6% [1.9%, 3.7%] 6

Binary size

Results (primary 0.2%, secondary 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.6% [0.0%, 1.5%] 49
Regressions ❌
(secondary)
0.4% [0.0%, 2.5%] 49
Improvements ✅
(primary)
-0.3% [-0.6%, -0.0%] 57
Improvements ✅
(secondary)
-0.6% [-1.3%, -0.1%] 16
All ❌✅ (primary) 0.2% [-0.6%, 1.5%] 106

Bootstrap: 783.63s -> 779.85s (-0.48%)
Artifact size: 333.67 MiB -> 333.71 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 28, 2024
@chenyukang
Copy link
Member

@veera-sivarajan do you want to continue to resolve the performance issue?
if not we can close this PR now.

@veera-sivarajan
Copy link
Contributor Author

Ah no, I don't have anything to add. Will close this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-run-make Area: port run-make Makefiles to rmake.rs perf-regression Performance regression. 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. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

generated call to core::fmt::Arguments::new_const fails to constant fold
9 participants