llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang][OpenMP] Code completion inside an OpenMP var-list clause never terminates (spins in Parser::ParseOpenMPVarList)

Open BayKeremm opened this issue 1 month ago • 7 comments

Summary

With -fopenmp, requesting code completion anywhere inside the variable list of an OpenMP clause such as reduction(...), private(...) or shared(...) makes the parser loop forever. It uses 100% of one core, memory stays flat, and it produces no output. In clangd, this means every completion or signature-help request typed inside such a clause leaves a worker thread spinning permanently. Cancelling the request doesn't stop it, and clangd ignores SIGTERM while the thread runs, so it has to be SIGKILLed.

Reproducer

repro.cpp:

void f() {
  int s = 0;
#pragma omp parallel for reduction(+ : s)
  for (int i = 0; i < 10; ++i) s += i;
}
clang++ -fsyntax-only -fopenmp -Xclang -code-completion-at=repro.cpp:3:40 repro.cpp

Line 3, column 40 is the s inside reduction(+ : s).

Expected: completion results are printed and clang exits. Actual: clang never exits. Nothing is printed to stdout or stderr, CPU usage is 100% and RSS is constant (about 69 MB).

The same hang happens through clangd:

echo -fopenmp > compile_flags.txt
clangd --check=repro.cpp --check-completion --check-lines=3-3

What triggers it

Each case below is a code-completion request with a 5-second timeout:

Case Result
reduction(+ : s), cursor before or after s hangs
reduction(&& : x), reduction(&& : ) (empty list) hangs
private(), shared(x) hangs
same file as C (clang -x c) hangs
-fopenmp-simd instead of -fopenmp hangs
if(), num_threads(), schedule() completes
cursor in the loop body instead of the clause completes
no -fopenmp completes

So it looks specific to clauses parsed through ParseOpenMPVarList; expression clauses are fine.

Backtrace

I sampled it twice, at 3 s and at 7 s. Both samples show the same frames with a stable depth, so it's a loop rather than recursion:

#1  clang::DiagnosticIDs::shouldSuppressAsSystemWarning(...)
#2  clang::DiagnosticIDs::getDiagnosticSeverity(...)
#3  clang::DiagnosticIDs::getDiagnosticLevel(...)
#4  clang::DiagnosticsEngine::ProcessDiag(...)
#5  ??
#6  clang::Parser::ParseAssignmentExpression(clang::TypoCorrectionTypeBehavior)
#7  clang::Parser::ParseOpenMPVarList(llvm::omp::Directive, llvm::omp::Clause, ...)
#8  clang::Parser::ParseOpenMPVarListClause(llvm::omp::Directive, llvm::omp::Clause, bool)
#9  clang::Parser::ParseOpenMPClause(llvm::omp::Directive, llvm::omp::Clause, bool)
#10 clang::Parser::ParseOpenMPExecutableDirective(...)
#11 clang::Parser::ParseOpenMPDeclarativeOrExecutableDirective(...)
...
#23 clang::ParseAST(...)

This build has no debug info. The pattern suggests that ParseOpenMPVarList keeps calling ParseAssignmentExpression without consuming the code-completion token, so its loop condition (not at ) or end of pragma) never becomes false.

Environment

  • clang/clangd 23.1.3 (Arch Linux package clang 23.1.3-3), x86_64-pc-linux-gnu
  • Linux 7.2.8
  • I haven't tested other versions.

Workaround

Hide -fopenmp from clangd in ~/.config/clangd/config.yaml:

CompileFlags:
  Remove: [-fopenmp*]
  Add: [-D_OPENMP=202111]

BayKeremm avatar Oct 19 '26 09:10 BayKeremm

@llvm/issue-subscribers-clang-frontend

Author: Kerem Okyay (BayKeremm)

### Summary

With -fopenmp, requesting code completion anywhere inside the variable list of an OpenMP clause such as reduction(...), private(...) or shared(...) makes the parser loop forever. It uses 100% of one core, memory stays flat, and it produces no output. In clangd, this means every completion or signature-help request typed inside such a clause leaves a worker thread spinning permanently. Cancelling the request doesn't stop it, and clangd ignores SIGTERM while the thread runs, so it has to be SIGKILLed.

Reproducer

repro.cpp:

void f() {
  int s = 0;
#pragma omp parallel for reduction(+ : s)
  for (int i = 0; i &lt; 10; ++i) s += i;
}
clang++ -fsyntax-only -fopenmp -Xclang -code-completion-at=repro.cpp:3:40 repro.cpp

Line 3, column 40 is the s inside reduction(+ : s).

Expected: completion results are printed and clang exits. Actual: clang never exits. Nothing is printed to stdout or stderr, CPU usage is 100% and RSS is constant (about 69 MB).

The same hang happens through clangd:

echo -fopenmp &gt; compile_flags.txt
clangd --check=repro.cpp --check-completion --check-lines=3-3

What triggers it

Each case below is a code-completion request with a 5-second timeout:

Case Result
reduction(+ : s), cursor before or after s hangs
reduction(&amp;&amp; : x), reduction(&amp;&amp; : ) (empty list) hangs
private(), shared(x) hangs
same file as C (clang -x c) hangs
-fopenmp-simd instead of -fopenmp hangs
if(), num_threads(), schedule() completes
cursor in the loop body instead of the clause completes
no -fopenmp completes

So it looks specific to clauses parsed through ParseOpenMPVarList; expression clauses are fine.

Backtrace

I sampled it twice, at 3 s and at 7 s. Both samples show the same frames with a stable depth, so it's a loop rather than recursion:

#<!-- -->1  clang::DiagnosticIDs::shouldSuppressAsSystemWarning(...)
#<!-- -->2  clang::DiagnosticIDs::getDiagnosticSeverity(...)
#<!-- -->3  clang::DiagnosticIDs::getDiagnosticLevel(...)
#<!-- -->4  clang::DiagnosticsEngine::ProcessDiag(...)
#<!-- -->5  ??
#<!-- -->6  clang::Parser::ParseAssignmentExpression(clang::TypoCorrectionTypeBehavior)
#<!-- -->7  clang::Parser::ParseOpenMPVarList(llvm::omp::Directive, llvm::omp::Clause, ...)
#<!-- -->8  clang::Parser::ParseOpenMPVarListClause(llvm::omp::Directive, llvm::omp::Clause, bool)
#<!-- -->9  clang::Parser::ParseOpenMPClause(llvm::omp::Directive, llvm::omp::Clause, bool)
#<!-- -->10 clang::Parser::ParseOpenMPExecutableDirective(...)
#<!-- -->11 clang::Parser::ParseOpenMPDeclarativeOrExecutableDirective(...)
...
#<!-- -->23 clang::ParseAST(...)

This build has no debug info. The pattern suggests that ParseOpenMPVarList keeps calling ParseAssignmentExpression without consuming the code-completion token, so its loop condition (not at ) or end of pragma) never becomes false.

Environment

  • clang/clangd 23.1.3 (Arch Linux package clang 23.1.3-3), x86_64-pc-linux-gnu
  • Linux 7.2.8
  • I haven't tested other versions.

Workaround

Hide -fopenmp from clangd in ~/.config/clangd/config.yaml:

CompileFlags:
  Remove: [-fopenmp*]
  Add: [-D_OPENMP=202111]

Hi! I'd like to work on it

Anastasia-Artemova avatar Oct 19 '26 17:10 Anastasia-Artemova

Hi! We are a 2-student team participating in the CS3423 Compilers II hackathon at IIT Hyderabad, and We would like to work on this issue.

If the issue is still available, we'd like to reproduce the hang, investigate the parser's handling of code completion in ParseOpenMPVarList, and work on a fix with regression tests.

Could you please let us know, We request assignment

Thank you!

ShivaGanesh265 avatar Oct 19 '26 18:10 ShivaGanesh265

Reproducer: https://godbolt.org/z/e64jnaPPx

TPPPP72 avatar Oct 20 '26 02:10 TPPPP72

@Anastasia-Artemova @ShivaGanesh265 We don't assign issue unless we've already find the solution. You can just open a pull request.

TPPPP72 avatar Oct 20 '26 02:10 TPPPP72

Hey, @TPPPP72 Can you check the PR once? And If any suggestions or things that need fixing?

ShivaGanesh265 avatar Oct 27 '26 13:10 ShivaGanesh265

I had the same issue with clang 18.1.3 and 20.1.2 on Linux Mint 22.3

GiovanniKarra avatar Nov 13 '26 12:11 GiovanniKarra