Repository navigation
Description
I tried this code:
pub fn a() -> String {
String::new()
}
pub fn b() -> String {
"".to_string()
}I expected to see this happen: both functions contain identical code.
Instead, this happened: https://rust.godbolt.org/z/P53MMG4W6.
For String::new() it does one more instruction: mov rcx, qword ptr [rip + .L__unnamed_1]. This rcx is then read further below at mov qword ptr [rdi], rcx.
The "".to_string() version however does not do this extra step and immediately reads from a constant: mov qword ptr [rdi], 1.
This means that String::new() does slightly more work than "".to_string() but I expect them to be identical.
Before 1.52.0 the functions generated identical code: https://rust.godbolt.org/z/8647E6YhY.
oli noted the following:
The problem is probably because it copies its value from the constant
instead of immediately creating the appropriate valuerust/library/alloc/src/raw_vec.rs
Line 64 in d620ae1
pub const NEW: Self = Self::new();
Meta
This currently happens on nightly.
Activity
JohnTitor commentedon Jun 7, 2021
Regressed since 1.52.0, between nightly-2021-03-04 and nightly-2021-03-05 so I assume it's caused (revealed?) by #81451.
cynecx commentedon Jun 7, 2021
Reduced llvm-ir:
LLVM fails to figure out that the load can be replaced with a constant (Probably because of the
load bitcastpattern for pointer types).klensy commentedon Jun 7, 2021
It isn't only in
Stringmain thing there in const. The same can be seen in vec:apiraino commentedon Jun 8, 2021
Assigning priority as discussed in the Zulip thread of the Prioritization Working Group.
@rustbot label -I-prioritize +P-high
6 remaining items
nikic commentedon Jan 21, 2022
Fixed upstream by llvm/llvm-project@6a19cb8.
SaadiSave commentedon Jan 21, 2022
I assume that this will make its way into Rust with LLVM 14? Or will it become a patch release?
nikic commentedon Jan 21, 2022
This will be part of the LLVM 14 upgrade.
klensy commentedon Feb 19, 2022
Fixed by #93577 https://rust.godbolt.org/z/vMdfPzfT5, but test should be added, to check regressions in future.
Add regression test for rust-lang#86106
Rollup merge of rust-lang#106036 - JohnTitor:issue-86106, r=nikic
Add regression test for rust-lang#86106
Auto merge of rust-lang#106036 - JohnTitor:issue-86106, r=nikic