Skip to content

[flang][OpenMP] Use function symbol on DECLARE TARGET #134107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions flang/lib/Semantics/resolve-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2514,6 +2514,15 @@ void OmpAttributeVisitor::ResolveOmpObject(
name->ToString());
}
}
if (ompFlag == Symbol::Flag::OmpDeclareTarget) {
if (symbol->IsFuncResult()) {
if (Symbol * func{currScope().symbol()}) {
CHECK(func->IsSubprogram());
func->set(ompFlag);
name->symbol = func;
}
}
}
if (GetContext().directive ==
llvm::omp::Directive::OMPD_target_data) {
checkExclusivelists(symbol, Symbol::Flag::OmpUseDevicePtr,
Expand Down
8 changes: 8 additions & 0 deletions flang/lib/Semantics/unparse-with-symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class SymbolDumpVisitor {
currStmt_ = std::nullopt;
}

bool Pre(const parser::OpenMPDeclareTargetConstruct &x) {
currStmt_ = x.source;
return true;
}
void Post(const parser::OpenMPDeclareTargetConstruct &) {
currStmt_ = std::nullopt;
}

private:
std::optional<SourceName> currStmt_; // current statement we are processing
std::multimap<const char *, const Symbol *> symbols_; // location to symbol
Expand Down
7 changes: 7 additions & 0 deletions flang/test/Lower/OpenMP/declare-target-func-and-subr.f90
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ FUNCTION FUNC_DEFAULT_EXTENDEDLIST() RESULT(I)
I = 1
END FUNCTION FUNC_DEFAULT_EXTENDEDLIST

! ALL-LABEL: func.func @_QPfunc_name_as_result()
! ALL-SAME: {{.*}}attributes {omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (to)>{{.*}}
FUNCTION FUNC_NAME_AS_RESULT()
!$omp declare target(FUNC_NAME_AS_RESULT)
FUNC_NAME_AS_RESULT = 1.0
END FUNCTION FUNC_NAME_AS_RESULT

!! -----

! Check specification valid forms of declare target with subroutines
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
!RUN: %flang_fc1 -fdebug-unparse-with-symbols -fopenmp %s 2>&1 | FileCheck %s

! This used to crash.

module test
contains
function ex(a, b, c)
!$omp declare target(ex)
integer :: a, b, c
ex = a + b + c
end function ex
end module test

!CHECK: !DEF: /test Module
!CHECK: module test
!CHECK: contains
!CHECK: !DEF: /test/ex PUBLIC (Function, OmpDeclareTarget) Subprogram REAL(4)
!CHECK: !DEF: /test/ex/a ObjectEntity INTEGER(4)
!CHECK: !DEF: /test/ex/b ObjectEntity INTEGER(4)
!CHECK: !DEF: /test/ex/c ObjectEntity INTEGER(4)
!CHECK: function ex(a, b, c)
!CHECK: !$omp declare target (ex)
!CHECK: !REF: /test/ex/a
!CHECK: !REF: /test/ex/b
!CHECK: !REF: /test/ex/c
!CHECK: integer a, b, c
!CHECK: !DEF: /test/ex/ex (Implicit, OmpDeclareTarget) ObjectEntity REAL(4)
!CHECK: !REF: /test/ex/a
!CHECK: !REF: /test/ex/b
!CHECK: !REF: /test/ex/c
!CHECK: ex = a+b+c
!CHECK: end function ex
!CHECK: end module test