diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index a53fbcb43ec43d85d73c862380b8013d8250bffb..039c70710a28a82cadaadf6b1482247b948b335f 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7672,7 +7672,8 @@ extern void walk_specializations		(bool,
 							  void *),
 						 void *);
 extern tree match_mergeable_specialization	(bool is_decl, spec_entry *);
-extern unsigned get_mergeable_specialization_flags (tree tmpl, tree spec);
+extern unsigned get_mergeable_specialization_flags (bool is_decl, tree tmpl,
+						    tree spec);
 extern void add_mergeable_specialization        (bool is_decl, spec_entry *,
 						 tree outer, unsigned);
 extern tree add_to_template_args		(tree, tree);
diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index f4d137b13a170551ff0c3f354cd8f9dc5a7d6be2..c3218bd5caf3a53785e2bd4ceb02f80ac6ad9fb8 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -7981,7 +7981,8 @@ trees_out::decl_value (tree decl, depset *dep)
 	      auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
 
 	      if (streaming_p ())
-		u (get_mergeable_specialization_flags (entry->tmpl, decl));
+		u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
+						       entry->tmpl, decl));
 	      tree_node (entry->tmpl);
 	      tree_node (entry->args);
 	    }
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 684ee0c8a60bbb5443bfa4c1e68708c84e470e8d..32d164f0fd5b75bfcad2007fd3252ed090e95344 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -31561,13 +31561,14 @@ match_mergeable_specialization (bool decl_p, spec_entry *elt)
    specialization lists of TMPL.  */
 
 unsigned
-get_mergeable_specialization_flags (tree tmpl, tree decl)
+get_mergeable_specialization_flags (bool decl_p, tree tmpl, tree decl)
 {
   unsigned flags = 0;
 
+  tree spec = decl_p ? decl : TREE_TYPE (decl);
   for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
        inst; inst = TREE_CHAIN (inst))
-    if (TREE_VALUE (inst) == decl)
+    if (TREE_VALUE (inst) == spec)
       {
 	flags |= 1;
 	break;
@@ -31625,7 +31626,8 @@ add_mergeable_specialization (bool decl_p, spec_entry *elt, tree decl,
 
   if (flags & 1)
     DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
-      = tree_cons (elt->args, decl, DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
+      = tree_cons (elt->args, elt->spec,
+		   DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
 
   if (flags & 2)
     {
diff --git a/gcc/testsuite/g++.dg/modules/tpl-spec-9_a.C b/gcc/testsuite/g++.dg/modules/tpl-spec-9_a.C
new file mode 100644
index 0000000000000000000000000000000000000000..d7c02bb279d4b12afb2f48089690622bbd055b42
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/tpl-spec-9_a.C
@@ -0,0 +1,12 @@
+// PR c++/116364
+// { dg-additional-options "-fmodules-ts -Wno-global-module" }
+// { dg-module-cmi foo:part }
+
+module;
+template <typename> struct S {};
+template <> struct S<int>
+  { static constexpr bool value = true; };
+export module foo:part;
+
+export template <typename T>
+  constexpr bool result = S<T>::value;
diff --git a/gcc/testsuite/g++.dg/modules/tpl-spec-9_b.C b/gcc/testsuite/g++.dg/modules/tpl-spec-9_b.C
new file mode 100644
index 0000000000000000000000000000000000000000..f2ce5c2e90bcd93f1868457eecc3af35850b6f11
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/tpl-spec-9_b.C
@@ -0,0 +1,5 @@
+// PR c++/116364
+// { dg-additional-options "-fmodules-ts" }
+
+export module foo;
+export import :part;
diff --git a/gcc/testsuite/g++.dg/modules/tpl-spec-9_c.C b/gcc/testsuite/g++.dg/modules/tpl-spec-9_c.C
new file mode 100644
index 0000000000000000000000000000000000000000..e79224561ca99479980b76d965c61308502e576f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/tpl-spec-9_c.C
@@ -0,0 +1,5 @@
+// PR c++/116364
+// { dg-additional-options "-fmodules-ts" }
+
+import foo;
+bool x = result<int>;