Skip to content

Commit 605a9e3

Browse files
authored
[OpenMP] Fix infinite loop on recursive initializers (#126269)
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
1 parent 8a03658 commit 605a9e3

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22819,8 +22819,12 @@ class GlobalDeclRefChecker final : public StmtVisitor<GlobalDeclRefChecker> {
2281922819
void declareTargetInitializer(Decl *TD) {
2282022820
A = TD->getAttr<OMPDeclareTargetDeclAttr>();
2282122821
DeclVector.push_back(cast<VarDecl>(TD));
22822+
llvm::SmallDenseSet<Decl *> Visited;
2282222823
while (!DeclVector.empty()) {
2282322824
VarDecl *TargetVarDecl = DeclVector.pop_back_val();
22825+
if (!Visited.insert(TargetVarDecl).second)
22826+
continue;
22827+
2282422828
if (TargetVarDecl->hasAttr<OMPDeclareTargetDeclAttr>() &&
2282522829
TargetVarDecl->hasInit() && TargetVarDecl->hasGlobalStorage()) {
2282622830
if (Expr *Ex = TargetVarDecl->getInit())

clang/test/OpenMP/declare_target_messages.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
// 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
3434
// RUN: %clang_cc1 %{common_opts_mac} -verify=expected,omp52,ompvar,omp5-or-later,omp5-or-later-var %{openmp60} %{limit} -o - %s
3535

36+
#pragma omp begin declare target
37+
static int gg;
38+
// expected-warning@+1 {{variable 'recursive' is uninitialized when used within its own initialization}}
39+
int recursive = recursive ^ 3 + gg;
40+
#pragma omp end declare target
3641

3742
// expected-error@+1 {{unexpected OpenMP directive '#pragma omp end declare target'}}
3843
#pragma omp end declare target

0 commit comments

Comments
 (0)