Skip to content

Conversation

@mizvekov
Copy link
Contributor

This ammends the fix commited in https://reviews.llvm.org/D109770 / 6cf6fa6

Comparing the number of template parameter lists with the number of template parameters is obviously wrong.

Even then, the number of parameters being the same doesn't mean the templates are compatible.

This change compares if the template parameters are actually equivalent.

This fixes the crash, but I am not sure what is the design and intention here, this openmp template support looks too fragile.

The added test case still doesn't work, but at least we don't crash now.

This ammends the fix commited in https://reviews.llvm.org/D109770 /
6cf6fa6

Comparing the number of template parameter lists with the number of
template parameters is obviously wrong.

Even then, the number of parameters being the same doesn't mean
the templates are compatible.

This change compares if the template parameters are actually equivalent.

This fixes the crash, but I am not sure what is the design and intention
here, this openmp template support looks too fragile.

The added test case still doesn't work, but at least we don't crash now.
@mizvekov mizvekov self-assigned this Oct 21, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:openmp OpenMP related changes to Clang labels Oct 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2025

@llvm/pr-subscribers-clang

Author: Matheus Izvekov (mizvekov)

Changes

This ammends the fix commited in https://reviews.llvm.org/D109770 / 6cf6fa6

Comparing the number of template parameter lists with the number of template parameters is obviously wrong.

Even then, the number of parameters being the same doesn't mean the templates are compatible.

This change compares if the template parameters are actually equivalent.

This fixes the crash, but I am not sure what is the design and intention here, this openmp template support looks too fragile.

The added test case still doesn't work, but at least we don't crash now.


Full diff: https://github.com/llvm/llvm-project/pull/164511.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaOpenMP.cpp (+3-1)
  • (added) clang/test/SemaOpenMP/openmp-begin-declare-variant_template.cpp (+12)
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 5b5b1b685e153..6d5cb0fcaea24 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -7246,7 +7246,9 @@ void SemaOpenMP::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope(
     FunctionDecl *UDecl = nullptr;
     if (IsTemplated && isa<FunctionTemplateDecl>(CandidateDecl)) {
       auto *FTD = cast<FunctionTemplateDecl>(CandidateDecl);
-      if (FTD->getTemplateParameters()->size() == TemplateParamLists.size())
+      // FIXME: Should this compare the template parameter lists on all levels?
+      if (SemaRef.Context.isSameTemplateParameterList(
+              FTD->getTemplateParameters(), TemplateParamLists.back()))
         UDecl = FTD->getTemplatedDecl();
     } else if (!IsTemplated)
       UDecl = dyn_cast<FunctionDecl>(CandidateDecl);
diff --git a/clang/test/SemaOpenMP/openmp-begin-declare-variant_template.cpp b/clang/test/SemaOpenMP/openmp-begin-declare-variant_template.cpp
new file mode 100644
index 0000000000000..ded8f58253540
--- /dev/null
+++ b/clang/test/SemaOpenMP/openmp-begin-declare-variant_template.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple x86_64 -fopenmp -verify %s
+
+// FIXME: Is this supposed to work?
+
+#pragma omp begin declare variant match(implementation={extension(allow_templates)})
+template <class T> void f(T) {}
+// expected-note@-1 {{explicit instantiation refers here}}
+#pragma end
+template <int> struct A {};
+template <bool B> A<B> f() = delete;
+template void f<float>(float);
+// expected-error@-1 {{explicit instantiation of undefined function template 'f'}}

@alexey-bataev
Copy link
Member

LGTM at least as a temporary workaround

Copy link
Collaborator

@JonChesterfield JonChesterfield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, better than what came before, step forward it is. Thanks!

@mizvekov mizvekov merged commit 7731156 into main Oct 23, 2025
14 checks passed
@mizvekov mizvekov deleted the users/mizvekov/fix-openmp-template-crash branch October 23, 2025 16:38
dvbuka pushed a commit to dvbuka/llvm-project that referenced this pull request Oct 27, 2025
This ammends the fix commited in https://reviews.llvm.org/D109770 /
6cf6fa6

Comparing the number of template parameter lists with the number of
template parameters is obviously wrong.

Even then, the number of parameters being the same doesn't mean the
templates are compatible.

This change compares if the template parameters are actually equivalent.

This fixes the crash, but I am not sure what is the design and intention
here, this openmp template support looks too fragile.

The added test case still doesn't work, but at least we don't crash now.
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
This ammends the fix commited in https://reviews.llvm.org/D109770 /
6cf6fa6

Comparing the number of template parameter lists with the number of
template parameters is obviously wrong.

Even then, the number of parameters being the same doesn't mean the
templates are compatible.

This change compares if the template parameters are actually equivalent.

This fixes the crash, but I am not sure what is the design and intention
here, this openmp template support looks too fragile.

The added test case still doesn't work, but at least we don't crash now.
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
This ammends the fix commited in https://reviews.llvm.org/D109770 /
6cf6fa6

Comparing the number of template parameter lists with the number of
template parameters is obviously wrong.

Even then, the number of parameters being the same doesn't mean the
templates are compatible.

This change compares if the template parameters are actually equivalent.

This fixes the crash, but I am not sure what is the design and intention
here, this openmp template support looks too fragile.

The added test case still doesn't work, but at least we don't crash now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:openmp OpenMP related changes to Clang clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants