`[[gnu::weak]]` disables constant evaluation of constexpr variables in C++14
GCC and Clang disagree on their handling of [[gnu::weak]] before C++17 https://gcc.godbolt.org/z/s7q67vqjz
struct S { static constexpr int var = 0; };
[[gnu::weak]] constexpr int S::var;
static constexpr int usage = S::var;
While it would be unsafe to constant-fold this code, it should be fine to constant-evaluate it, for consistency with GCC
In static bool evaluateVarDeclInit (ExprConstant.cpp), and in static bool CheckWeak (Interp.cpp), we should allow constant evaluation -without diagnostics, but not constant folding) - by looking at Info.EvalMode
@llvm/issue-subscribers-c-14
Author: cor3ntin (cor3ntin)
struct S { static constexpr int var = 0; };
[[gnu::weak]] constexpr int S::var;
static constexpr int usage = S::var;
While it would be unsafe to constant-fold this code, it should be fine to constant-evaluate it, for consistency with GCC
In static bool evaluateVarDeclInit (ExprConstant.cpp), and in static bool CheckWeak (Interp.cpp), we should allow constant evaluation -without diagnostics, but not constant folding) - by looking at Info.EvalMode
@llvm/issue-subscribers-clang-frontend
Author: cor3ntin (cor3ntin)
struct S { static constexpr int var = 0; };
[[gnu::weak]] constexpr int S::var;
static constexpr int usage = S::var;
While it would be unsafe to constant-fold this code, it should be fine to constant-evaluate it, for consistency with GCC
In static bool evaluateVarDeclInit (ExprConstant.cpp), and in static bool CheckWeak (Interp.cpp), we should allow constant evaluation -without diagnostics, but not constant folding) - by looking at Info.EvalMode
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:
- 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.
- 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.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - 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-c-11
Author: cor3ntin (cor3ntin)
struct S { static constexpr int var = 0; };
[[gnu::weak]] constexpr int S::var;
static constexpr int usage = S::var;
While it would be unsafe to constant-fold this code, it should be fine to constant-evaluate it, for consistency with GCC
In static bool evaluateVarDeclInit (ExprConstant.cpp), and in static bool CheckWeak (Interp.cpp), we should allow constant evaluation -without diagnostics, but not constant folding) - by looking at Info.EvalMode
@llvm/issue-subscribers-good-first-issue
Author: cor3ntin (cor3ntin)
struct S { static constexpr int var = 0; };
[[gnu::weak]] constexpr int S::var;
static constexpr int usage = S::var;
While it would be unsafe to constant-fold this code, it should be fine to constant-evaluate it, for consistency with GCC
In static bool evaluateVarDeclInit (ExprConstant.cpp), and in static bool CheckWeak (Interp.cpp), we should allow constant evaluation -without diagnostics, but not constant folding) - by looking at Info.EvalMode
While it would be unsafe to constant-fold this code, it should be fine to constant-evaluate it
What does this mean? We allow it in constant-evaluated contexts, but not non-constant-evaluated contexts? That seems wrong. If C++ ODR requires that the real definition is the same as the weak definition, then we should allow constant-folding. If, somehow, C++ ODR doesn't require that, then any sort of constant evaluation is broken.
A patch like this seems to do the trick. I'm unsure if this is even correct, as the comment above seems to suggest otherwise:
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index b20e2690d0ee..ac0bd38a64d3 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -3646,7 +3646,7 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
// Never use the initializer of a weak variable, not even for constant
// folding. We can't be sure that this is the definition that will be used.
- if (VD->isWeak()) {
+ if (VD->isWeak() && (Info.EvalMode == EvalInfo::EM_ConstantFold) ) {
Info.FFDiag(E, diag::note_constexpr_var_init_weak) << VD;
NoteLValueLocation(Info, Base);
return false;
While it would be unsafe to constant-fold this code, it should be fine to constant-evaluate it
What does this mean? We allow it in constant-evaluated contexts, but not non-constant-evaluated contexts? That seems wrong. If C++ ODR requires that the real definition is the same as the weak definition, then we should allow constant-folding. If, somehow, C++ ODR doesn't require that, then any sort of constant evaluation is broken.
That is the suggestion, and I don't disagree with you perhaps? But not entirely. First, we should very much allow this in constant evaluated contexts, as demanded by the language. Using the initialized value there seems to be consistent with the language requirements here.
I don't think ODR really comes into play here, attribute 'weak' is a GCC modification of the ODR rules, and it isn't exactly clear HOW. To me, Constant EVALUATION is a situation where the user definitely expects that the evaluation will happen with the initializer value.
Constant FOLDING is a little different in this case, it is effectively a use of the 'as-if' rule to evaluate it 'early'. Whoever wrote that diagnostic seems to believe that means that we cannot count on 'weak' symbols to have that value during that evaluation.
I see 2 solutions here:
1- completely remove the diagnostic. We end up folding values under 'as-if' that might have been a different value at runtime, and ce-la-vie. 2- Remove the diagnostic during constexpr evaluation as the patch by @Mr-Anyone does. This is consistent with how 'constexpr' is supposed to work, plus maintains our existing behavior for 'weak'. It is lower risk, but also seems odd.
It's sort of counterintuitive, but the proposed patch changes the behavior of the following:
[[gnu::weak]] extern const int var = 0;
int usage = var;
Just allowing all constant-folding doesn't seem much worse...
My primary concern here is someone actually using weak to allow overriding the variable. Which is probably more common than people using it as a hack to get inline variables without C++17.
Maybe worth asking gcc developers to see what they think.
FWIW IMO the only correct thing would be to reject [[gnu::weak]] constexpr variables. I don't see how they ever make any sense.
My opinion: the current behavior seems reasonable, consistent, and useful, and preferable over the GCC behavior.
The point of the weak attribute is to allow a library to provide a default definition of a symbol and allow it to be overridden by other code by providing a strong definition. So a definition declared weak isn't "the" definition of that symbol, it's just a default / fallback definition. (This matches the corresponding LLVM semantics.) This is a language extension, and as such it overrides the normal language rules, including the ODR. As an example of how this plays out:
[[gnu::weak]] inline void f() {}
void g() { f(); }
... Clang does not inline f into g here or delete the call, because the weak attribute means that that definition of f is just a default and might not be used. However, GCC does inline f here, which may not directly contradict their documentation for the attribute but seems wrong to me, and indeed GCC has a warning "inline function declared weak" presumably because they noticed their behavior here is problematic. Clang doesn't have such a warning and I'd argue doesn't need one, because you do get the semantic effect of both inline and weak if you combine them.
Combining inline and weak for a function with Clang's semantics might not seem especially useful, given that the function can't actually be inlined -- you would mostly get the same result by instead providing a non-inline weak definition -- but it does in theory allow multiple identical weak definition, and results in a weak definition that's discardable if not used in the current translation unit, so it's not completely useless. For example, you can build a header-only library with a weak inline function in it using Clang's semantics.
Similarly, a constexpr variable definition that is declared weak means that it's a default, and should only be used if an overriding strong definition is not provided. Therefore its value should not be used by constant evaluation (because we don't know that that's "the" definition of the symbol). Combining constexpr and weak for a variable is useful, because the other properties of constexpr -- in particular, that the variable is guaranteed to have constant initialization and destruction -- are still enforced for that default definition.
I think we are getting something wrong here, though, per the above understanding of the attribute. We accept the following:
[[gnu::weak]] constexpr int f() { return 0; }
constexpr int k = f();
Given our behavior for weak inline functions and weak constexpr variables, we should not: the definition of f() is just a default, so can be overridden, and thus should not be usable in constant evaluation.
Hi @cor3ntin , I would like to work on this issue
Hi,i would like to work on this issue..
Hi,i would like to work on this issue..
As I noted previously please don't spam issues w/ requests to work on them. Pick one and make progress on that one issue.
Thanks for the clarification. Understood. I’ll focus on one issue and make progress on it rather than commenting on multiple issues. I’ll continue investigating the issue I’m currently working on.