Skip to content

Commit 564e04b

Browse files
authored
[flang][OpenMP] Use function symbol on DECLARE TARGET (llvm#134107)
Consider: ``` function foo() !$omp declare target(foo) ! This `foo` was a function-result symbol ... end ``` When resolving symbols, for this case use the symbol corresponding to the function instead of the symbol corresponding to the function result. Currently, this will result in an error: ``` error: A variable that appears in a DECLARE TARGET directive must be declared in the scope of a module or have the SAVE attribute, either explicitly or implicitly ```
1 parent 2026873 commit 564e04b

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,15 @@ void OmpAttributeVisitor::ResolveOmpObject(
25142514
name->ToString());
25152515
}
25162516
}
2517+
if (ompFlag == Symbol::Flag::OmpDeclareTarget) {
2518+
if (symbol->IsFuncResult()) {
2519+
if (Symbol * func{currScope().symbol()}) {
2520+
CHECK(func->IsSubprogram());
2521+
func->set(ompFlag);
2522+
name->symbol = func;
2523+
}
2524+
}
2525+
}
25172526
if (GetContext().directive ==
25182527
llvm::omp::Directive::OMPD_target_data) {
25192528
checkExclusivelists(symbol, Symbol::Flag::OmpUseDevicePtr,

flang/lib/Semantics/unparse-with-symbols.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ class SymbolDumpVisitor {
6161
currStmt_ = std::nullopt;
6262
}
6363

64+
bool Pre(const parser::OpenMPDeclareTargetConstruct &x) {
65+
currStmt_ = x.source;
66+
return true;
67+
}
68+
void Post(const parser::OpenMPDeclareTargetConstruct &) {
69+
currStmt_ = std::nullopt;
70+
}
71+
6472
private:
6573
std::optional<SourceName> currStmt_; // current statement we are processing
6674
std::multimap<const char *, const Symbol *> symbols_; // location to symbol

flang/test/Lower/OpenMP/declare-target-func-and-subr.f90

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ FUNCTION FUNC_DEFAULT_EXTENDEDLIST() RESULT(I)
8585
I = 1
8686
END FUNCTION FUNC_DEFAULT_EXTENDEDLIST
8787

88+
! ALL-LABEL: func.func @_QPfunc_name_as_result()
89+
! ALL-SAME: {{.*}}attributes {omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (to)>{{.*}}
90+
FUNCTION FUNC_NAME_AS_RESULT()
91+
!$omp declare target(FUNC_NAME_AS_RESULT)
92+
FUNC_NAME_AS_RESULT = 1.0
93+
END FUNCTION FUNC_NAME_AS_RESULT
94+
8895
!! -----
8996

9097
! Check specification valid forms of declare target with subroutines
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
!RUN: %flang_fc1 -fdebug-unparse-with-symbols -fopenmp %s 2>&1 | FileCheck %s
2+
3+
! This used to crash.
4+
5+
module test
6+
contains
7+
function ex(a, b, c)
8+
!$omp declare target(ex)
9+
integer :: a, b, c
10+
ex = a + b + c
11+
end function ex
12+
end module test
13+
14+
!CHECK: !DEF: /test Module
15+
!CHECK: module test
16+
!CHECK: contains
17+
!CHECK: !DEF: /test/ex PUBLIC (Function, OmpDeclareTarget) Subprogram REAL(4)
18+
!CHECK: !DEF: /test/ex/a ObjectEntity INTEGER(4)
19+
!CHECK: !DEF: /test/ex/b ObjectEntity INTEGER(4)
20+
!CHECK: !DEF: /test/ex/c ObjectEntity INTEGER(4)
21+
!CHECK: function ex(a, b, c)
22+
!CHECK: !$omp declare target (ex)
23+
!CHECK: !REF: /test/ex/a
24+
!CHECK: !REF: /test/ex/b
25+
!CHECK: !REF: /test/ex/c
26+
!CHECK: integer a, b, c
27+
!CHECK: !DEF: /test/ex/ex (Implicit, OmpDeclareTarget) ObjectEntity REAL(4)
28+
!CHECK: !REF: /test/ex/a
29+
!CHECK: !REF: /test/ex/b
30+
!CHECK: !REF: /test/ex/c
31+
!CHECK: ex = a+b+c
32+
!CHECK: end function ex
33+
!CHECK: end module test
34+

0 commit comments

Comments
 (0)