forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#131663 - veera-sivarajan:fix-128709-final, r=…
…<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
- Loading branch information
Showing
11 changed files
with
75 additions
and
19 deletions.
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
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
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
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
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,17 @@ | ||
//@ compile-flags: -C no-prepopulate-passes -C opt-level=0 | ||
|
||
#![crate_type = "lib"] | ||
|
||
// String formating macros without any arguments should compile | ||
// to a `memcpy` followed by a call to a library function. | ||
|
||
#[no_mangle] | ||
pub fn code() { | ||
// CHECK-LABEL: @code | ||
// CHECK-NOT: getelementptr | ||
// CHECK-NOT: store | ||
// CHECK-NOT: ; call core::fmt::Arguments::new_const | ||
// CHECK: call void @llvm.memcpy | ||
// CHECK-NEXT: ; call std::io::stdio::_print | ||
println!("hello world"); | ||
} |
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
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
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,5 @@ | ||
pub fn main() { | ||
const A: std::fmt::Arguments = std::fmt::Arguments::new_const(&[&"hola"]); | ||
//~^ use of unstable library feature | ||
//~| temporary value dropped while borrowed | ||
} |
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,23 @@ | ||
error[E0658]: use of unstable library feature 'fmt_internals' | ||
--> $DIR/const-format-arguments.rs:3:36 | ||
| | ||
LL | const A: std::fmt::Arguments = std::fmt::Arguments::new_const(&[&"hola"]); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add `#![feature(fmt_internals)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/const-format-arguments.rs:3:68 | ||
| | ||
LL | const A: std::fmt::Arguments = std::fmt::Arguments::new_const(&[&"hola"]); | ||
| --------------------------------^^^^^^^^^- | ||
| | | | | ||
| | | temporary value is freed at the end of this statement | ||
| | creates a temporary value which is freed while still in use | ||
| using this value as a constant requires that borrow lasts for `'static` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
Some errors have detailed explanations: E0658, E0716. | ||
For more information about an error, try `rustc --explain E0658`. |
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
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