Skip to content

Commit

Permalink
[OpenMP][Flang][Semantics] Add semantics support for USE_DEVICE_PTR c…
Browse files Browse the repository at this point in the history
…lause on OMP TARGET DATA directive.

Initial support for USE_DEVICE_PTR clause on OMP TARGET DATA directive.

Reviewed By: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D148254
  • Loading branch information
raghavendhra committed Apr 27, 2023
1 parent b1465cd commit d6ef90f
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 10 deletions.
17 changes: 17 additions & 0 deletions flang/docs/OpenMP-semantics.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ w/
For the following OpenMP regions:

* `target` regions
* `target data` regions
* `teams` regions
* `parallel` regions
* `simd` regions
Expand Down Expand Up @@ -407,6 +408,16 @@ More details are listed in the following table:
<td>OmpLastPrivate
</td>
</tr>
<tr>
<td>use_device_ptr
</td>
<td>Yes
</td>
<td>New Symbol
</td>
<td>OmpUseDevicePtr
</td>
</tr>
</table>

To determine the right data-sharing attribute,
Expand Down Expand Up @@ -519,6 +530,12 @@ will be determined and represented as `Flag` in the `Symbol` object
of the variable.
No `Symbol` or `Scope` will be created.

However, there are some exceptions for this, Pointers that appear in a
use_device_ptr clause are privatized and the device pointers to the
corresponding list items in the device data environment are assigned into the
private versions so it is best to follow the representation for privatised
variables i.e represent them with a new Symbol and `OmpUseDevicePtr` flag.

The basic steps to determine the data-mapping attribute are:

1. If _map_ clause is present,
Expand Down
1 change: 1 addition & 0 deletions flang/include/flang/Semantics/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ class Symbol {
OmpShared, OmpPrivate, OmpLinear, OmpFirstPrivate, OmpLastPrivate,
// OpenMP data-mapping attribute
OmpMapTo, OmpMapFrom, OmpMapAlloc, OmpMapRelease, OmpMapDelete,
OmpUseDevicePtr,
// OpenMP data-copying attribute
OmpCopyIn, OmpCopyPrivate,
// OpenMP miscellaneous flags
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Parser/openmp-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ TYPE_PARSER(
"TO" >> construct<OmpClause>(construct<OmpClause::To>(
parenthesized(Parser<OmpObjectList>{}))) ||
"USE_DEVICE_PTR" >> construct<OmpClause>(construct<OmpClause::UseDevicePtr>(
parenthesized(nonemptyList(name)))) ||
parenthesized(Parser<OmpObjectList>{}))) ||
"UNIFIED_ADDRESS" >>
construct<OmpClause>(construct<OmpClause::UnifiedAddress>()) ||
"UNIFIED_SHARED_MEMORY" >>
Expand Down
11 changes: 6 additions & 5 deletions flang/lib/Semantics/check-omp-structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2766,11 +2766,12 @@ const parser::OmpObjectList *OmpStructureChecker::GetOmpObjectList(
const parser::OmpClause &clause) {

// Clauses with OmpObjectList as its data member
using MemberObjectListClauses = std::tuple<parser::OmpClause::Copyprivate,
parser::OmpClause::Copyin, parser::OmpClause::Firstprivate,
parser::OmpClause::From, parser::OmpClause::Lastprivate,
parser::OmpClause::Link, parser::OmpClause::Private,
parser::OmpClause::Shared, parser::OmpClause::To>;
using MemberObjectListClauses =
std::tuple<parser::OmpClause::Copyprivate, parser::OmpClause::Copyin,
parser::OmpClause::Firstprivate, parser::OmpClause::From,
parser::OmpClause::Lastprivate, parser::OmpClause::Link,
parser::OmpClause::Private, parser::OmpClause::Shared,
parser::OmpClause::To, parser::OmpClause::UseDevicePtr>;

// Clauses with OmpObjectList in the tuple
using TupleObjectListClauses = std::tuple<parser::OmpClause::Allocate,
Expand Down
7 changes: 6 additions & 1 deletion flang/lib/Semantics/resolve-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
return false;
}

bool Pre(const parser::OmpClause::UseDevicePtr &x) {
ResolveOmpObjectList(x.v, Symbol::Flag::OmpUseDevicePtr);
return false;
}

void Post(const parser::Name &);

// Keep track of labels in the statements that causes jumps to target labels
Expand Down Expand Up @@ -506,7 +511,7 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
Symbol::Flag::OmpPrivate, Symbol::Flag::OmpLinear,
Symbol::Flag::OmpFirstPrivate, Symbol::Flag::OmpLastPrivate,
Symbol::Flag::OmpReduction, Symbol::Flag::OmpCriticalLock,
Symbol::Flag::OmpCopyIn};
Symbol::Flag::OmpCopyIn, Symbol::Flag::OmpUseDevicePtr};

static constexpr Symbol::Flags ompFlagsRequireMark{
Symbol::Flag::OmpThreadprivate};
Expand Down
1 change: 0 additions & 1 deletion flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,6 @@ bool OmpVisitor::NeedsScope(const parser::OpenMPBlockConstruct &x) {
const auto &beginBlockDir{std::get<parser::OmpBeginBlockDirective>(x.t)};
const auto &beginDir{std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
switch (beginDir.v) {
case llvm::omp::Directive::OMPD_target_data:
case llvm::omp::Directive::OMPD_master:
case llvm::omp::Directive::OMPD_ordered:
case llvm::omp::Directive::OMPD_taskgroup:
Expand Down
21 changes: 21 additions & 0 deletions flang/test/Semantics/OpenMP/use_device_ptr.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
! RUN: %flang -fc1 -fopenmp -fdebug-dump-symbols %s | FileCheck %s
! OpenMP Version 5.0
! 2.10.1 use_device_ptr clause
! List items that appear in a use_device_ptr clause are converted into device
! pointers to the corresponding list item in the device data environment.

subroutine omp_target_data
use iso_c_binding
integer :: a(1024)
!CHECK: b size=8 offset=4096: ObjectEntity type: TYPE(c_ptr)
type(C_PTR) :: b
integer, pointer :: arrayB
a = 1
!$omp target data map(tofrom: a, arrayB) use_device_ptr(b)
!CHECK: b (OmpUseDevicePtr)
allocate(arrayB)
call c_f_pointer(b, arrayB)
a = arrayB
!$omp end target data
end subroutine omp_target_data

3 changes: 1 addition & 2 deletions llvm/include/llvm/Frontend/OpenMP/OMP.td
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ def OMPC_From : Clause<"from"> {
}
def OMPC_UseDevicePtr : Clause<"use_device_ptr"> {
let clangClass = "OMPUseDevicePtrClause";
let flangClass = "Name";
let isValueList = true;
let flangClass = "OmpObjectList";
}
def OMPC_IsDevicePtr : Clause<"is_device_ptr"> {
let clangClass = "OMPIsDevicePtrClause";
Expand Down

0 comments on commit d6ef90f

Please sign in to comment.