llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Copy elision when initializing a base class subobject

Open Fedr opened this issue 3 years ago • 9 comments

According to https://github.com/llvm/llvm-project/issues/33864

Copy elision cannot be guaranteed when initializing a base class subobject, because base classes can have different layout than the corresponding complete object type.

But if we slightly modify the code example as follows:

struct Base {
    Base(Base&&) = delete;
    Base() {}
};

Base make_base() { return {}; }

template<typename F>
struct call_wrapper {
    F&& f;
    constexpr operator decltype(auto)() && {
        return static_cast<F&&>(f)();
    }
};
template<typename F>
call_wrapper(F&&) -> call_wrapper<F>;

struct B : public Base {
    B() : Base(call_wrapper{make_base}) {} // <<< error in GCC, ok in Clang
};

Then Clang starts accepting it and performing copy elision for base class initialization. It looks like a bug because of the same reason. Please note that GCC keeps rejecting the modified example. Online demo: https://gcc.godbolt.org/z/rMbhdf61v

Fedr avatar Jan 11 '23 07:01 Fedr

@llvm/issue-subscribers-clang-frontend

llvmbot avatar Jan 11 '23 15:01 llvmbot

@zygoloid was speaking specifically about guaranteed copy elision but I am not sure if there is room for the implementation to still allow this or not.

shafik avatar Jan 11 '23 19:01 shafik

If copy elision is allowed for base class initialization, then it leads to known problems. Below is a modified example from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86849

#include <cstring>

struct A {
    A() {}
    int n;
    char k;
};

A f() {
    A result;
    std::memset(&result, 0, sizeof(A));
    return result; // (nrvo)
}

struct B {
    char x, y, z;
};

template<typename F>
struct call_wrapper {
    F&& f;
    constexpr operator decltype(auto)() && {
        return static_cast<F&&>(f)();
    }
};
template<typename F>
call_wrapper(F&&) -> call_wrapper<F>;

struct X : A, virtual B { // B is in A's tail padding
    X() : B{1, 2, 3}, A(call_wrapper{f}) {}
};

int main() {
    X x;
    return x.x; // should return 1, but returns 0 in Clang
}

Online demo: https://gcc.godbolt.org/z/j4jan41KW

Fedr avatar Jan 12 '23 07:01 Fedr

Even simpler example without memset and templates:

struct A {
    A() {}
    int n;
    char k = 0;
};

struct B {
    char x, y, z;
};

struct F {
    operator A() { return {}; }
};

struct X : A, virtual B { // B is in A's tail padding
    X() : B{1, 2, 3}, A(F{}) {}
};

int main() {
    return X{}.x; // should return 1, but returns 127 or 0 in Clang depending on optimization settings
}

Online demo: https://gcc.godbolt.org/z/so6dd9eG9

Fedr avatar Jan 14 '23 09:01 Fedr

seems start to accept since bbb8a0df7367 due to bisection

hongtaihu avatar Oct 15 '26 15:10 hongtaihu

8. seems start to accept since bbb8a0d due to bisection

This case is probably different enough it would help to add a new test along w/ the tests added in the PR.

shafik avatar Oct 15 '26 17:10 shafik

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

@llvm/issue-subscribers-good-first-issue

Author: Fedor Chelnokov (Fedr)

According to https://github.com/llvm/llvm-project/issues/33864 > Copy elision cannot be guaranteed when initializing a base class subobject, because base classes can have different layout than the corresponding complete object type.

But if we slightly modify the code example as follows:

struct Base {
    Base(Base&amp;&amp;) = delete;
    Base() {}
};

Base make_base() { return {}; }

template&lt;typename F&gt;
struct call_wrapper {
    F&amp;&amp; f;
    constexpr operator decltype(auto)() &amp;&amp; {
        return static_cast&lt;F&amp;&amp;&gt;(f)();
    }
};
template&lt;typename F&gt;
call_wrapper(F&amp;&amp;) -&gt; call_wrapper&lt;F&gt;;

struct B : public Base {
    B() : Base(call_wrapper{make_base}) {} // &lt;&lt;&lt; error in GCC, ok in Clang
};

Then Clang starts accepting it and performing copy elision for base class initialization. It looks like a bug because of the same reason. Please note that GCC keeps rejecting the modified example. Online demo: https://gcc.godbolt.org/z/rMbhdf61v

Opened #232902 with the base class case added next to the GH39319 tests in cxx1z-copy-omission.cpp.

Glitch-On avatar Oct 16 '26 19:10 Glitch-On