-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
generated call to core::fmt::Arguments::new_const fails to constant fold #128709
Comments
possible workaround: during the code generation at |
This is not a valid optimization in general, as soon as #![feature(print_internals)]
#![feature(const_fmt_arguments_new)]
pub fn main() {
let n = 42;
// println!("hello {n}"); // Compiles fine
::std::io::_print(const { format_args!("hello {n}\n") }); // Error
} |
yes, but there's already a special case in codegen for when there's no arguments (which happens after the implicit named argument desugaring). this special case is what calls |
@rustbot claim |
…<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
…<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
…<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
normal code:
code that should be equivalent, but is actually more efficient
godbolt link for asm comparison
The text was updated successfully, but these errors were encountered: