[all-commits] [llvm/llvm-project] e42388: [CodeGen] guarantee templated static variables are...

Yuanfang Chen via All-commits all-commits at lists.llvm.org
Fri Mar 3 00:13:08 PST 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: e423885e272c0e57c6d240d07bc934e0a8649417
      https://github.com/llvm/llvm-project/commit/e423885e272c0e57c6d240d07bc934e0a8649417
  Author: Yuanfang Chen <yuanfang.chen at sony.com>
  Date:   2023-03-03 (Fri, 03 Mar 2023)

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

  Log Message:
  -----------
  [CodeGen] guarantee templated static variables are initialized in the reverse instantiation order

Based on Richard's suggestion in 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 variables are passed to CodeGen.

This is based on the same ordering guarantee needed for inline variables D127233.
To provide this guarantee, we also need to
emit DeferredDeclsToEmit in the DFS order. https://github.com/llvm/llvm-project/commit/e5df59ff78faebd897e81907606ce6074aac0df6
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 the 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 emitted in DFS order by moving
DeferredDeclsToEmit storage from the call stack to an explicit stack-like
data structure. Then the DFS order could be enforced.

Differential Revision: https://reviews.llvm.org/D127259




More information about the All-commits mailing list