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 omits variable with "used" attribute #91262

Open
pogo59 opened this issue May 6, 2024 · 2 comments
Open

Clang omits variable with "used" attribute #91262

pogo59 opened this issue May 6, 2024 · 2 comments

Comments

@pogo59
Copy link
Collaborator

pogo59 commented May 6, 2024

GCC specifies the "used" attribute as:

This attribute, attached to a variable with static storage, means that the variable must be emitted even if it appears that the variable is not referenced.

Inside a constant "if" condition, clang will fail to emit a "used" variable in the not-taken branch. This differs from GCC behavior.

void foo() {
  if (sizeof(double) == sizeof(long double)) {
    __attribute__((used)) static int my_item(__LINE__);
  } else {
    __attribute__((used)) static int my_item(__LINE__);
  }
}

Clang allocates only one variable, GCC allocates both. Given it's a GCC-defined attribute, we should behave the way GCC does. This appears to be a Clang codegen bug, as -emit-llvm -disable-llvm-passes shows only one item.

(Why would I need this? It's part of the instrumentation for my Rotten Green Tests project. My static variables are actually allocated into a custom section, but that's not needed to trigger the bug.)

@llvmbot
Copy link
Collaborator

llvmbot commented May 6, 2024

@llvm/issue-subscribers-clang-codegen

Author: Paul T Robinson (pogo59)

[GCC](https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html) specifies the "used" attribute as: > This attribute, attached to a variable with static storage, means that the variable must be emitted even if it appears that the variable is not referenced.

Inside a constant "if" condition, clang will fail to emit a "used" variable in the not-taken branch. This differs from GCC behavior.

void foo() {
  if (sizeof(double) == sizeof(long double)) {
    __attribute__((used)) static int my_item(__LINE__);
  } else {
    __attribute__((used)) static int my_item(__LINE__);
  }
}

Clang allocates only one variable, GCC allocates both. Given it's a GCC-defined attribute, we should behave the way GCC does. This appears to be a Clang codegen bug, as -emit-llvm -disable-llvm-passes shows only one item.

(Why would I need this? It's part of the instrumentation for my Rotten Green Tests project. My static variables are actually allocated into a custom section, but that's not needed to trigger the bug.)

@efriedma-quic
Copy link
Collaborator

See also #82994 (comment) . (Not the same issue, but it's in the same part of the implementation.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants