Skip to content

Commit df93f70

Browse files
authored
[flang][OpenMP] Use function symbol on DECLARE TARGET (llvm#134107) (llvm#2584)
2 parents 3a48adc + 8dc9657 commit df93f70

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
@@ -2467,6 +2467,15 @@ void OmpAttributeVisitor::ResolveOmpObject(
24672467
name->ToString());
24682468
}
24692469
}
2470+
if (ompFlag == Symbol::Flag::OmpDeclareTarget) {
2471+
if (symbol->IsFuncResult()) {
2472+
if (Symbol * func{currScope().symbol()}) {
2473+
CHECK(func->IsSubprogram());
2474+
func->set(ompFlag);
2475+
name->symbol = func;
2476+
}
2477+
}
2478+
}
24702479
if (GetContext().directive ==
24712480
llvm::omp::Directive::OMPD_target_data) {
24722481
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)