This is an archive of the discontinued LLVM Phabricator instance.

[DebugInfo] Check for templated static data member when adding constant to record static fields
Needs ReviewPublic

Authored by akhuang on Oct 12 2020, 6:50 PM.

Details

Reviewers
dblaikie
Summary

Debug info for inline static data members was missing the constant value,
because the initializer for these static data members is not always emitted.

Now also try to get the initializer from the templated static data member if
the variable doesn't have an initializer.

(related to discussion on https://bugs.llvm.org/show_bug.cgi?id=47580)

Diff Detail

Event Timeline

akhuang created this revision.Oct 12 2020, 6:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 12 2020, 6:50 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
akhuang requested review of this revision.Oct 12 2020, 6:50 PM
dblaikie added inline comments.Oct 12 2020, 7:24 PM
clang/lib/CodeGen/CGDebugInfo.cpp
1421–1425

I'd suspect this might not be the most general solution - because it's retrieving the value from the original template static member. Two issues come to mind:

  1. If the initialization is dependent or otherwise not knowable at compile-time, does this code at least not crash (I assume "evaluateValue" returns nullptr if the value can't be evaluated for whatever reason)
  2. If the initialization is dependent, it might still be knowable, eg:
template<typename T>
struct static_decl_templ {
  static constexpr int static_constexpr_decl_templ_var = sizeof(T);
};

I'm guessing this patch as-is doesn't provide a value in this case, but it should be achievable, I would guess/hope/imagine - but don't really know exactly how to do that off-hand. (@zygoloid would know - but maybe a bit of looking around will find it if/before he has a chance to weigh in here)

clang/test/CodeGenCXX/debug-info-static-member.cpp
140–144

I guess the non-11 version of this variable is intended as a stub to make the test across different language levels simpler?

I think it might be better to make that more explicit - rather than having a variable with "constexpr" in the name that isn't actually constexpr. I'd say only add the variable under that macro condition - but change the test to use multiple --check-prefixes, and the check for this variable to use a CPP11 prefix or something like that.

Hmm, I'm slightly confused now that I Think about it - it looks like the macro is about testing if the version is >= 11. But the change also adds a test for C++17? Is C++17 likely to be any different than C++11 for any of this? (or C++20 for that matter) if it's not any different, I'd probably leave the 17 case out & expect the 11 case is sufficient coverage.

saudi added a subscriber: saudi.Oct 13 2020, 8:55 AM
saudi added inline comments.
clang/lib/CodeGen/CGDebugInfo.cpp
1420

I encountered an assert inside evaluateValue() call :
assert(!Init->isValueDependent());

repro code:

template <int VAL> class A {
 static constexpr int dep = VAL;
};
A<10> a;

Note that it crashed with static constexp member but not with static const.

saudi added inline comments.Oct 13 2020, 9:09 AM
clang/lib/CodeGen/CGDebugInfo.cpp
1420

Sorry, I got the wrong line for this comment.
This was actually the case 1. from @dblaikie 's comment for the TemplateDecl evaluation.

akhuang added a subscriber: rsmith.Oct 14 2020, 1:05 PM
akhuang added inline comments.
clang/lib/CodeGen/CGDebugInfo.cpp
1421–1425

@rsmith

Actually, can we just instantiate the initializer for static constexpr data members?

currently it delays creating the initializer for inline static data members; can we not do that if it's a constexpr?

clang/test/CodeGenCXX/debug-info-static-member.cpp
140–144

Yeah, it was intended as a stub, but I can remove that part.

I added c++17 because it appears that in c++17 (and for MS targets) we make constexpr data members implicitly inline, which is why the initializer is not being instantiated.

akhuang updated this revision to Diff 298243.Oct 14 2020, 2:41 PM

update test case and add check for dependent values

akhuang marked an inline comment as not done.Oct 14 2020, 2:44 PM