[clang] 7f8d844 - [CodeGen] guarantee variable templates are initialized in the reverse instantiation order
Yuanfang Chen via cfe-commits
cfe-commits at lists.llvm.org
Sun Mar 5 15:43:57 PST 2023
Author: Yuanfang Chen
Date: 2023-03-05T15:31:23-08:00
New Revision: 7f8d844df5e91f8f689d0e9658e811d21bc4a605
URL: https://github.com/llvm/llvm-project/commit/7f8d844df5e91f8f689d0e9658e811d21bc4a605
DIFF: https://github.com/llvm/llvm-project/commit/7f8d844df5e91f8f689d0e9658e811d21bc4a605.diff
LOG: [CodeGen] guarantee variable templates are initialized in the reverse instantiation order
Following up D127259.
Fixes https://github.com/llvm/llvm-project/issues/61028.
Added:
clang/test/CodeGenCXX/static-init-variable-template.cpp
Modified:
clang/lib/Sema/SemaExpr.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index c3e2b6fdeaf1f..56abde04eaf0d 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -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) {
@@ -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));
}
}
}
diff --git a/clang/test/CodeGenCXX/static-init-variable-template.cpp b/clang/test/CodeGenCXX/static-init-variable-template.cpp
new file mode 100644
index 0000000000000..672ab11059a30
--- /dev/null
+++ b/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 }
More information about the cfe-commits
mailing list