llvm / llvm/llvm-project

[flang][OpenMP] declare target extended-list does not apply EXTERNAL attribute to implicit procedure symbols

Open
#194,805 0 comments 0 reactions 0 assignees View on GitHub
flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

`!$omp declare target(name)` does not implicitly apply the EXTERNAL attribute to the named procedure. When an external procedure appears only in a declare target extended-list and nowhere else in the program unit, Flang fails with Cannot call function `name`like a subroutine

**Reproducer**
```
program test
!$omp declare target(sub)
!$omp target
call sub(42)
!$omp end target
end program

subroutine sub(n)
integer :: n
!$omp declare target
print *, "n =", n
end subroutine
```
Compile with :
```
flang -fopenmp -fopenmp-version=52
```

**Expected Output** : Compile successfully

**Actual Output** :
```
error: Cannot call function 'sub' like a subroutine
call sub(42)
^^^
/app/example.f90:2:24: Implicit declaration of 'sub'
!$omp declare target(sub)
```

Name resolution in resolve-names.cpp runs before the OMP directive visitor. By the time resolve-directives.cpp processes !$omp declare target(sub), the symbol sub has already been created as an implicitly-typed variable. The directive resolver marks it with OmpDeclareTarget but never converts it to a procedure. So call sub(42) fails because sub is still a variable in the symbol table.

**Standard Reference**

OpenMP 5.2 §3.2.1 defines a Fortran extended-list item as either a variable list item or a procedure name. OpenMP 5.2 §7.8 states that `declare target` directives apply to procedures and/or variables. OpenMP 5.2 §7.8.1 states that when a `declare target` directive has an extended-list argument, its effect is as if an `enter` clause was specified with that extended list.

Therefore, a name that appears in `!$omp declare target(name)` may denote a procedure name. Flang should not permanently resolve such a name as an implicitly typed data object before later semantic analysis sees it used as a subroutine in a `CALL` statement.

OpenMP 5.2 does not explicitly state that `declare target(name)` gives the named entity the Fortran `EXTERNAL` attribute. This issue is therefore described as a Fortran/OpenMP name-resolution problem: the compiler should preserve the possibility that a `DECLARE TARGET` extended-list item is a procedure name.

Contributor guide

Open the contributing guide

Research direction

Start with the supplied reproducer using flang -fopenmp -fopenmp-version=52. Read resolve-names.cpp and resolve-directives.cpp to trace how the DECLARE TARGET extended-list name is resolved and then processed. Done means the reproducer compiles successfully without the implicit-declaration or call-form errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.