Skip to content
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

[clang] NRVO used in C mode #100902

Open
TedLyngmo opened this issue Jul 27, 2024 · 4 comments · May be fixed by #101038
Open

[clang] NRVO used in C mode #100902

TedLyngmo opened this issue Jul 27, 2024 · 4 comments · May be fixed by #101038
Labels
c clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party

Comments

@TedLyngmo
Copy link

TedLyngmo commented Jul 27, 2024

This C program exits with 1 even though it should exit with 0. To the best of my knowledge, NRVO is not allowed in C:

typedef struct {
    int i, j;
    double a, b;
} S;

int result;

S test(S* q) {
    S s = {0, 0, 0, 0};
    result = &s == q;
    return s;
}

int main(void)
{
  S t = test(&t);
  return result;
}
@github-actions github-actions bot added the clang Clang issues not falling into any other category label Jul 27, 2024
@AaronBallman AaronBallman added the c label Jul 29, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 29, 2024

@llvm/issue-subscribers-c

Author: Ted Lyngmo (TedLyngmo)

This C program exits with `1` even though it should exit with `0`. To the best of my knowledge, NRVO is not allowed in C: ```c typedef struct { int i, j; double a, b; } S;

int result;

S test(S* q) {
S s = {0, 0, 0, 0};
result = &s == q;
return s;
}

int main(void)
{
S t = test(&t);
return result;
}

</details>

@AaronBallman AaronBallman added clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party labels Jul 29, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 29, 2024

@llvm/issue-subscribers-clang-frontend

Author: Ted Lyngmo (TedLyngmo)

This C program exits with `1` even though it should exit with `0`. To the best of my knowledge, NRVO is not allowed in C: ```c typedef struct { int i, j; double a, b; } S;

int result;

S test(S* q) {
S s = {0, 0, 0, 0};
result = &s == q;
return s;
}

int main(void)
{
S t = test(&t);
return result;
}

</details>

@AaronBallman
Copy link
Collaborator

Agreed that this is not a valid optimization in C; Clang is marking the local variable as being available for NRVO: https://godbolt.org/z/PhdKhqPPc

|-FunctionDecl <line:8:1, line:12:1> line:8:3 used test 'S (S *)'
| |-ParmVarDecl <col:8, col:11> col:11 used q 'S *'
| `-CompoundStmt <col:14, line:12:1>
|   |-DeclStmt <line:9:5, col:23>
|   | `-VarDecl <col:5, col:22> col:7 used s 'S' nrvo cinit

AaronBallman added a commit to AaronBallman/llvm-project that referenced this issue Jul 29, 2024
Applying NRVO changes the observable behavior of the abstract machine,
which makes it an invalid optimization to apply in C. This disables the
functionality in C.

This is the only code path where we set the NRVO state for a local
variable in C (the other code paths are in coroutines and template
instantiation, neither of which are available in C).

Fixes llvm#100902
@pinskia
Copy link

pinskia commented Aug 1, 2024

Note GCC has the opposite issue dealing with C++ where it optimizes away the comparison too early even with language mandatory NVRO, see https://cplusplus.github.io/CWG/issues/2868.html and https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84414 . Note the optimizing away the comparison is how GCC handles this case here even though it does NVRO; though the NVRO is done iff the struct return has no address taken on it (after many optmizations).

Here is a replacement function where GCC does not do NVRO and the comparison is still kept around (but still does return slot optimization[RSO]) while clang does the NVRO [incorrectly]:

S test(S* q) __attribute__((noinline));
S test(S* q) {
    S s = {0, 0, 0, 0};
    void *a = q;
    void *a1 = &s;
    __asm__("":"+r"(a));
    __asm__("":"+r"(a1));
    result = a1 == a;
    return s;
}

@EugeneZelenko EugeneZelenko removed the clang Clang issues not falling into any other category label Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants