Skip to content

Conversation

@jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented Feb 7, 2025

Summary:
If the user tried to initialize a gobal declare target variable with
itself the compiler will hang forever. Add a visited set to make sure
this stops.

Fixes: #69194

Summary:
If the user tried to initialize a gobal declare target variable with
itself the compiler will hang forever. Add a visited set to make sure
this stops.

Fixes: llvm#69194
@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 Feb 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 7, 2025

@llvm/pr-subscribers-clang

Author: Joseph Huber (jhuber6)

Changes

Summary:
If the user tried to initialize a gobal declare target variable with
itself the compiler will hang forever. Add a visited set to make sure
this stops.

Fixes: #69194


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaOpenMP.cpp (+4)
  • (modified) clang/test/OpenMP/declare_target_messages.cpp (+5)
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index b060039d188a1bc..91e459c64030a0b 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -22819,8 +22819,12 @@ class GlobalDeclRefChecker final : public StmtVisitor<GlobalDeclRefChecker> {
   void declareTargetInitializer(Decl *TD) {
     A = TD->getAttr<OMPDeclareTargetDeclAttr>();
     DeclVector.push_back(cast<VarDecl>(TD));
+    llvm::DenseSet<Decl *> Visited;
     while (!DeclVector.empty()) {
       VarDecl *TargetVarDecl = DeclVector.pop_back_val();
+      if (!Visited.insert(TargetVarDecl).second)
+        continue;
+
       if (TargetVarDecl->hasAttr<OMPDeclareTargetDeclAttr>() &&
           TargetVarDecl->hasInit() && TargetVarDecl->hasGlobalStorage()) {
         if (Expr *Ex = TargetVarDecl->getInit())
diff --git a/clang/test/OpenMP/declare_target_messages.cpp b/clang/test/OpenMP/declare_target_messages.cpp
index ce5a833b3866a9b..3c0e766cf72ca8e 100644
--- a/clang/test/OpenMP/declare_target_messages.cpp
+++ b/clang/test/OpenMP/declare_target_messages.cpp
@@ -33,6 +33,11 @@
 // RUN: %clang_cc1 %{common_opts_mac} -verify=expected,omp51,ompvar,omp45-to-51,omp5-and-51,omp5-or-later,omp5-or-later-var,omp45-to-51-var,omp45-to-51-clause,host-5-and-51,no-host5-and-51 -fopenmp %{limit} -o - %s
 // RUN: %clang_cc1 %{common_opts_mac} -verify=expected,omp52,ompvar,omp5-or-later,omp5-or-later-var %{openmp60} %{limit} -o - %s
 
+#pragma omp begin declare target
+static int gg;
+// expected-warning@+1 {{variable 'recursive' is uninitialized when used within its own initialization}}
+int recursive = recursive ^ 3 + gg;
+#pragma omp end declare target
 
 // expected-error@+1 {{unexpected OpenMP directive '#pragma omp end declare target'}}
 #pragma omp end declare target 

@jhuber6 jhuber6 added this to the LLVM 20.X Release milestone Feb 7, 2025
@shiltian
Copy link
Contributor

shiltian commented Feb 7, 2025

The fix looks reasonable to me. @alexey-bataev WDYT?

@jhuber6 jhuber6 changed the title [OpenMP] Fix infinite recursion on global initializers [OpenMP] Fix infinite loop on recursive initializers Feb 7, 2025
Copy link
Member

@alexey-bataev alexey-bataev left a comment

Choose a reason for hiding this comment

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

LG with a nit

@jhuber6 jhuber6 merged commit 605a9e3 into llvm:main Feb 7, 2025
8 checks passed
@jhuber6 jhuber6 deleted the FixRecursion branch February 7, 2025 19:44
@jhuber6
Copy link
Contributor Author

jhuber6 commented Feb 7, 2025

/cherry-pick 605a9e3

@llvmbot
Copy link
Member

llvmbot commented Feb 7, 2025

Failed to cherry-pick: 605a9e3

https://github.com/llvm/llvm-project/actions/runs/13206970400

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
Summary:
If the user tried to initialize a gobal declare target variable with
itself the compiler will hang forever. Add a visited set to make sure
this stops.

Fixes: llvm#69194
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 release:cherry-pick-failed

Projects

Development

Successfully merging this pull request may close these issues.

clang: 18: hangs on a simple syntax error

4 participants