Skip to content

Commit

Permalink
[CodeGen] guarantee variable templates are initialized in the reverse…
Browse files Browse the repository at this point in the history
… instantiation order

Following up D127259.

Fixes #61028.
  • Loading branch information
Yuanfang Chen committed Mar 5, 2023
1 parent bd1f7c4 commit 7f8d844
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
19 changes: 12 additions & 7 deletions clang/lib/Sema/SemaExpr.cpp
Expand Up @@ -19888,16 +19888,11 @@ static void DoMarkVarDeclReferenced(
DRE->setDecl(DRE->getDecl());
else if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
ME->setMemberDecl(ME->getMemberDecl());
} else if (FirstInstantiation ||
isa<VarTemplateSpecializationDecl>(Var)) {
// FIXME: For a specialization of a variable template, we don't
// distinguish between "declaration and type implicitly instantiated"
// and "implicit instantiation of definition requested", so we have
// no direct way to avoid enqueueing the pending instantiation
// multiple times.
} else if (FirstInstantiation) {
SemaRef.PendingInstantiations
.push_back(std::make_pair(Var, PointOfInstantiation));
} else {
bool Inserted = false;
for (auto &I : SemaRef.SavedPendingInstantiations) {
auto Iter = llvm::find_if(
I, [Var](const Sema::PendingImplicitInstantiation &P) {
Expand All @@ -19906,9 +19901,19 @@ static void DoMarkVarDeclReferenced(
if (Iter != I.end()) {
SemaRef.PendingInstantiations.push_back(*Iter);
I.erase(Iter);
Inserted = true;
break;
}
}

// FIXME: For a specialization of a variable template, we don't
// distinguish between "declaration and type implicitly instantiated"
// and "implicit instantiation of definition requested", so we have
// no direct way to avoid enqueueing the pending instantiation
// multiple times.
if (isa<VarTemplateSpecializationDecl>(Var) && !Inserted)
SemaRef.PendingInstantiations
.push_back(std::make_pair(Var, PointOfInstantiation));
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions clang/test/CodeGenCXX/static-init-variable-template.cpp
@@ -0,0 +1,22 @@
// RUN: %clang_cc1 -std=c++14 -S -emit-llvm -disable-llvm-passes -o - %s -triple x86_64-linux-gnu | FileCheck %s

template<int N> int Fib = Fib<N-2> + Fib<N-1>;
template<> int Fib<0> = 0;
template<> int Fib<1> = 1;
int f = Fib<5>;

template<int N> int Fib2 = Fib2<N-1> + Fib2<N-2>;
template<> int Fib2<0> = 0;
template<> int Fib2<1> = 1;
int f2 = Fib2<5>;

// CHECK: @llvm.global_ctors = appending global [9 x { i32, ptr, ptr }] [
// CHECK-SAME: { i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init.4, ptr @_Z3FibILi2EE },
// CHECK-SAME: { i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init.3, ptr @_Z3FibILi3EE },
// CHECK-SAME: { i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init.5, ptr @_Z3FibILi4EE },
// CHECK-SAME: { i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init.2, ptr @_Z3FibILi5EE },
// CHECK-SAME: { i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init.8, ptr @_Z4Fib2ILi2EE },
// CHECK-SAME: { i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init.9, ptr @_Z4Fib2ILi3EE },
// CHECK-SAME: { i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init.7, ptr @_Z4Fib2ILi4EE },
// CHECK-SAME: { i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init.6, ptr @_Z4Fib2ILi5EE },
// CHECK-SAME: { i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_static_init_variable_template.cpp, ptr null }

0 comments on commit 7f8d844

Please sign in to comment.