[PATCH] D127259: [CodeGen] guarantee templated static variables are initialized in the reverse instantiation order

Yuanfang Chen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 7 15:35:50 PDT 2022


ychen created this revision.
ychen added reviewers: rsmith, rnk.
Herald added a project: All.
ychen requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Based on the Richard's suggestion in D126341 <https://reviews.llvm.org/D126341>:
`If we can actually describe a rule that we provide for initialization
order of instantiated variables, and we can easily implement that rule
and be confident we won't want to substantially weaken it later, and we
can thereby assure our users that we will satisfy that rule, then I
think that could be interesting, but anything less than that doesn't
seem worthwhile to me.`

I'm giving it try here. IMHO the implementation is pretty simple and
does not change behavior for unrelated constructs like the timing when
instantiated variable are passed to CodeGen.

This is based on the same ordering guarantee needed for inline variables D127233 <https://reviews.llvm.org/D127233>.
To provide this guarantee, we also need to
emit DeferredDeclsToEmit in DFS order. e5df59ff78faebd897e81907606ce6074aac0df
originally supported this but it is not exactly DFS order for cases like
the one in cwg362. For the example of Fib<5>, it triggers instantiation
of Fib<4> and Fib<3>. However, due to the way GlobalEagerInstantiationScope
is implemented, Fib<4> does not actually trigger Fib<3> instantiation
since it is already triggered by Fib<5>. This breaks the guarantee.

This patch makes sure DeferredDeclsToEmit is emit in DFS order by moving
DeferredDeclsToEmit storage from the call stack to a explicit stack-like
data structure. Then the DFS order could be enforced.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127259

Files:
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaExpr.cpp
  clang/test/CodeGenCXX/static-member-variable-explicit-specialization.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127259.434975.patch
Type: text/x-patch
Size: 9306 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220607/2704536c/attachment.bin>


More information about the cfe-commits mailing list