rust-lang / rust-lang/rustc_codegen_gcc
Extra memcpy when compiling with rustc_codegen_gcc
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 1.2k
- Forks
- 105
- Avg merge
- 8h 20m
- Merged PRs (30d)
- 14
Description
When compiling the following there's an extra memcpy that LLVM is able to optimize away:
use std::mem;
struct SV {
disc: usize,
data: [usize; 40],
capacity: usize,
}
impl SV {
fn new() -> SV {
SV { data: unsafe { mem::uninitialized() },
disc: 0,
capacity: 0 }
}
}
pub struct L {
a: SV,
b: SV
}
pub struct Allocation<T> {
f: *mut T,
}
impl<T> Allocation<T> {
pub fn init(self, value: T) {
use std::ptr;
unsafe {
ptr::write(self.f, value);
}
}
}
#[inline(never)]
pub fn foo(a: Allocation<L>) {
a.init(L {
a: SV::new(),
b: SV::new()
});
}
See: https://rust.godbolt.org/z/1d843PPnx
This test case is derived from https://github.com/rust-lang/rust/issues/58082 which links to https://bugs.llvm.org/show_bug.cgi?id=40574 which suggests that GCC should be able to optimize something like this.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Rust reproducer in the issue and compare rustc_codegen_gcc output with the linked LLVM/Godbolt example. Trace the generated code for Allocation::init and the construction of L, then verify that the unnecessary memcpy is removed without changing behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100