Skip to content

[Clang] [CodeGen] Wrong code generation for extern reference #127475

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

Closed
16bit-ykiko opened this issue Feb 17, 2025 · 5 comments · Fixed by #128409
Closed

[Clang] [CodeGen] Wrong code generation for extern reference #127475

16bit-ykiko opened this issue Feb 17, 2025 · 5 comments · Fixed by #128409
Assignees
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" miscompilation regression

Comments

@16bit-ykiko
Copy link
Contributor

Minimal Reproducible Example:

/// src.cpp
#include <vector>

struct S {
    std::vector<int> c;
};

static S ls = {{1, 2, 3}};

S& s = ls;

/// main.cpp
#include <cstdio>
#include <vector>

struct S {
    std::vector<int> c;
};

extern S& s;

int main() {
    for (size_t i = 0; i < s.c.size(); i++) {
        printf("aaa\n");
    }

    for (auto& i : s.c) {
        printf("bbb\n");
    }
    return 0;
}

https://godbolt.org/z/W1YoYsMYh

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Feb 17, 2025
@dtcxzyw
Copy link
Member

dtcxzyw commented Feb 17, 2025

Reduced: https://godbolt.org/z/TMMhcEo7s

@dtcxzyw dtcxzyw added clang:codegen IR generation bugs: mangling, exceptions, etc. and removed clang Clang issues not falling into any other category labels Feb 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 17, 2025

@llvm/issue-subscribers-clang-codegen

Author: ykiko (16bit-ykiko)

Minimal Reproducible Example:
/// src.cpp
#include &lt;vector&gt;

struct S {
    std::vector&lt;int&gt; c;
};

static S ls = {{1, 2, 3}};

S&amp; s = ls;

/// main.cpp
#include &lt;cstdio&gt;
#include &lt;vector&gt;

struct S {
    std::vector&lt;int&gt; c;
};

extern S&amp; s;

int main() {
    for (size_t i = 0; i &lt; s.c.size(); i++) {
        printf("aaa\n");
    }

    for (auto&amp; i : s.c) {
        printf("bbb\n");
    }
    return 0;
}

https://godbolt.org/z/W1YoYsMYh

@dtcxzyw dtcxzyw self-assigned this Feb 17, 2025
@sakria9
Copy link
Contributor

sakria9 commented Feb 17, 2025

I use git bisect and find commit 0a9c08c , which emits

  store ptr getelementptr (i8, ptr @s, i64 1), ptr %__range1, align 8
  %3 = load ptr, ptr %__range1, align 8
  %call1 = call ptr @_ZNSt6vectorIiSaIiEE5beginEv(ptr noundef nonnull align 8 dereferenceable(24) %3) #1

then commit is c9637af (which try to fix 0a9c08c #95474 ) , which emits:

  store ptr @s, ptr %__range1, align 8
  %3 = load ptr, ptr %__range1, align 8
  %call1 = call ptr @_ZNSt6vectorIiSaIiEE5beginEv(ptr noundef nonnull align 8 dereferenceable(24) %3) #1

@dtcxzyw
Copy link
Member

dtcxzyw commented Feb 17, 2025

Related code:

llvm::Constant *constant = nullptr;
if (emission.IsConstantAggregate ||
D.mightBeUsableInConstantExpressions(getContext())) {
assert(!capturedByInit && "constant init contains a capturing block?");
constant = ConstantEmitter(*this).tryEmitAbstractForInitializer(D);

@llvmbot
Copy link
Member

llvmbot commented Mar 5, 2025

@llvm/issue-subscribers-clang-frontend

Author: ykiko (16bit-ykiko)

Minimal Reproducible Example:
/// src.cpp
#include &lt;vector&gt;

struct S {
    std::vector&lt;int&gt; c;
};

static S ls = {{1, 2, 3}};

S&amp; s = ls;

/// main.cpp
#include &lt;cstdio&gt;
#include &lt;vector&gt;

struct S {
    std::vector&lt;int&gt; c;
};

extern S&amp; s;

int main() {
    for (size_t i = 0; i &lt; s.c.size(); i++) {
        printf("aaa\n");
    }

    for (auto&amp; i : s.c) {
        printf("bbb\n");
    }
    return 0;
}

https://godbolt.org/z/W1YoYsMYh

llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this issue Mar 5, 2025
efriedma-quic pushed a commit to efriedma-quic/llvm-project that referenced this issue Mar 10, 2025
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this issue Mar 11, 2025
jph-13 pushed a commit to jph-13/llvm-project that referenced this issue Mar 21, 2025
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this issue May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" miscompilation regression
Projects
None yet
6 participants