rust-lang / rust-lang/rustc_codegen_gcc

Extra memcpy when compiling with rustc_codegen_gcc

Open
#56 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

perf
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.