Skip to content

Commit

Permalink
[Flang][OpenMP] Added parser support for device_type clause
Browse files Browse the repository at this point in the history
This patch adds parser suppert for the device_type clause used by the Declare Target directive.

Differential Revision: https://reviews.llvm.org/D143671
  • Loading branch information
TIFitis committed Feb 16, 2023
1 parent 5c988cb commit 849c440
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 1 deletion.
4 changes: 4 additions & 0 deletions flang/examples/FlangOmpReport/FlangOmpReportVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ void OpenMPCounterVisitor::Post(const OmpDefaultClause::Type &c) {
clauseDetails +=
"type=" + std::string{OmpDefaultClause::EnumToString(c)} + ";";
}
void OpenMPCounterVisitor::Post(const OmpDeviceTypeClause::Type &c) {
clauseDetails +=
"type=" + std::string{OmpDeviceTypeClause::EnumToString(c)} + ";";
}
void OpenMPCounterVisitor::Post(
const OmpDefaultmapClause::ImplicitBehavior &c) {
clauseDetails +=
Expand Down
1 change: 1 addition & 0 deletions flang/examples/FlangOmpReport/FlangOmpReportVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct OpenMPCounterVisitor {
void Post(const OmpDefaultClause::Type &c);
void Post(const OmpDefaultmapClause::ImplicitBehavior &c);
void Post(const OmpDefaultmapClause::VariableCategory &c);
void Post(const OmpDeviceTypeClause::Type &c);
void Post(const OmpScheduleModifierType::ModType &c);
void Post(const OmpLinearModifier::Type &c);
void Post(const OmpDependenceType::Type &c);
Expand Down
2 changes: 2 additions & 0 deletions flang/include/flang/Parser/dump-parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,8 @@ class ParseTreeDumper {
NODE_ENUM(OmpScheduleClause, ScheduleType)
NODE(parser, OmpDeviceClause)
NODE_ENUM(OmpDeviceClause, DeviceModifier)
NODE(parser, OmpDeviceTypeClause)
NODE_ENUM(OmpDeviceTypeClause, Type)
NODE(parser, OmpScheduleModifier)
NODE(OmpScheduleModifier, Modifier1)
NODE(OmpScheduleModifier, Modifier2)
Expand Down
6 changes: 6 additions & 0 deletions flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3396,6 +3396,12 @@ struct OmpDeviceClause {
std::tuple<std::optional<DeviceModifier>, ScalarIntExpr> t;
};

// device_type(any | host | nohost)
struct OmpDeviceTypeClause {
ENUM_CLASS(Type, Any, Host, Nohost)
WRAPPER_CLASS_BOILERPLATE(OmpDeviceTypeClause, Type);
};

// 2.12 if-clause -> IF ([ directive-name-modifier :] scalar-logical-expr)
struct OmpIfClause {
TUPLE_CLASS_BOILERPLATE(OmpIfClause);
Expand Down
8 changes: 8 additions & 0 deletions flang/lib/Parser/openmp-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ TYPE_PARSER(construct<OmpDeviceClause>(
":"),
scalarIntExpr))

// device_type(any | host | nohost)
TYPE_PARSER(construct<OmpDeviceTypeClause>(
"ANY" >> pure(OmpDeviceTypeClause::Type::Any) ||
"HOST" >> pure(OmpDeviceTypeClause::Type::Host) ||
"NOHOST" >> pure(OmpDeviceTypeClause::Type::Nohost)))

// 2.12 IF (directive-name-modifier: scalar-logical-expr)
TYPE_PARSER(construct<OmpIfClause>(
maybe(
Expand Down Expand Up @@ -208,6 +214,8 @@ TYPE_PARSER(
parenthesized(Parser<OmpDependClause>{}))) ||
"DEVICE" >> construct<OmpClause>(construct<OmpClause::Device>(
parenthesized(Parser<OmpDeviceClause>{}))) ||
"DEVICE_TYPE" >> construct<OmpClause>(construct<OmpClause::DeviceType>(
parenthesized(Parser<OmpDeviceTypeClause>{}))) ||
"DIST_SCHEDULE" >>
construct<OmpClause>(construct<OmpClause::DistSchedule>(
parenthesized("STATIC" >> maybe("," >> scalarIntExpr)))) ||
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Parser/unparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2616,6 +2616,7 @@ class UnparseVisitor {
WALK_NESTED_ENUM(OmpMapType, Type) // OMP map-type
WALK_NESTED_ENUM(OmpScheduleClause, ScheduleType) // OMP schedule-type
WALK_NESTED_ENUM(OmpDeviceClause, DeviceModifier) // OMP device modifier
WALK_NESTED_ENUM(OmpDeviceTypeClause, Type) // OMP DEVICE_TYPE
WALK_NESTED_ENUM(OmpIfClause, DirectiveNameModifier) // OMP directive-modifier
WALK_NESTED_ENUM(OmpCancelType, Type) // OMP cancel-type
#undef WALK_NESTED_ENUM
Expand Down
22 changes: 22 additions & 0 deletions flang/test/Parser/omp-declare_target-device_type.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
! RUN: %flang_fc1 -fdebug-unparse -fopenmp %s | FileCheck --ignore-case %s
! RUN: %flang_fc1 -fdebug-dump-parse-tree -fopenmp %s | FileCheck --check-prefix="PARSE-TREE" %s

subroutine openmp_declare_target
!CHECK: !$omp declare target device_type(host)
!$omp declare target device_type(host)
!CHECK: !$omp declare target device_type(nohost)
!$omp declare target device_type(nohost)
!CHECK: !$omp declare target device_type(any)
!$omp declare target device_type(any)
integer :: a(1024), i
!CHECK: do
do i = 1, 1024
a(i) = i
!CHECK: end do
end do

!PARSE-TREE: OpenMPDeclarativeConstruct -> OpenMPDeclareTargetConstruct
!PARSE-TREE: OmpDeclareTargetSpecifier -> OmpDeclareTargetWithClause -> OmpClauseList -> OmpClause -> DeviceType -> OmpDeviceTypeClause -> Type = Host
!PARSE-TREE: OmpDeclareTargetSpecifier -> OmpDeclareTargetWithClause -> OmpClauseList -> OmpClause -> DeviceType -> OmpDeviceTypeClause -> Type = Nohost
!PARSE-TREE: OmpDeclareTargetSpecifier -> OmpDeclareTargetWithClause -> OmpClauseList -> OmpClause -> DeviceType -> OmpDeviceTypeClause -> Type = Any
END subroutine openmp_declare_target
6 changes: 6 additions & 0 deletions flang/test/Semantics/OpenMP/omp-declare-target01.f90
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ module declare_target01

!$omp declare target to (my_var)

!$omp declare target to (my_var) device_type(host)

!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear on the DECLARE TARGET directive
!$omp declare target to (my_var%t_i)

Expand All @@ -65,6 +67,8 @@ module declare_target01

!$omp declare target to (arr)

!$omp declare target to (arr) device_type(nohost)

!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear on the DECLARE TARGET directive
!$omp declare target to (arr(1))

Expand All @@ -82,6 +86,8 @@ module declare_target01

!$omp declare target link (my_var2)

!$omp declare target link (my_var2) device_type(any)

!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear on the DECLARE TARGET directive
!$omp declare target link (my_var2%t_i)

Expand Down
7 changes: 6 additions & 1 deletion llvm/include/llvm/Frontend/OpenMP/OMP.td
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ def OMPC_Device : Clause<"device"> {
let clangClass = "OMPDeviceClause";
let flangClass = "OmpDeviceClause";
}
def OMPC_DeviceType : Clause<"device_type"> {
let flangClass = "OmpDeviceTypeClause";
}
def OMPC_Threads : Clause<"threads"> { let clangClass = "OMPThreadsClause"; }
def OMPC_Simd : Clause<"simd"> { let clangClass = "OMPSIMDClause"; }
def OMPC_Map : Clause<"map"> {
Expand Down Expand Up @@ -397,7 +400,6 @@ def OMPC_Uniform : Clause<"uniform"> {
let flangClass = "Name";
let isValueList = true;
}
def OMPC_DeviceType : Clause<"device_type"> {}
def OMPC_Match : Clause<"match"> {}
def OMPC_AdjustArgs : Clause<"adjust_args"> { }
def OMPC_AppendArgs : Clause<"append_args"> { }
Expand Down Expand Up @@ -1095,6 +1097,9 @@ def OMP_DeclareTarget : Directive<"declare target"> {
VersionedClause<OMPC_Link>,
VersionedClause<OMPC_Indirect>
];
let allowedOnceClauses = [
VersionedClause<OMPC_DeviceType, 50>
];
}
def OMP_EndDeclareTarget : Directive<"end declare target"> {}
def OMP_DistributeParallelFor : Directive<"distribute parallel for"> {
Expand Down

0 comments on commit 849c440

Please sign in to comment.