[flang-commits] [flang] [Flang] [OpenMP] [Lowering] Add lowering support for IS_DEVICE_PTR and HAS_DEVICE_ADDR clauses on OMP TARGET directive. (PR #67752)
Raghu Maddhipatla via flang-commits
flang-commits at lists.llvm.org
Wed Oct 25 07:58:17 PDT 2023
https://github.com/raghavendhra updated https://github.com/llvm/llvm-project/pull/67752
>From 76fb22a2df1059ff334f4934eeabf64aeb0af5f7 Mon Sep 17 00:00:00 2001
From: Raghu Maddhipatla <Raghu.Maddhipatla at amd.com>
Date: Thu, 28 Sep 2023 18:20:32 -0500
Subject: [PATCH] [Flang] [OpenMP] [MLIR] [Lowering] Add lowering support for
IS_DEVICE_PTR and HAS_DEVICE_ADDR clauses on OMP TARGET directive.
---
flang/lib/Lower/OpenMP.cpp | 54 +++++++++++++++++--
flang/test/Lower/OpenMP/FIR/target.f90 | 39 ++++++++++++++
mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td | 17 +++++-
mlir/test/Dialect/OpenMP/ops.mlir | 8 +--
4 files changed, 109 insertions(+), 9 deletions(-)
diff --git a/flang/lib/Lower/OpenMP.cpp b/flang/lib/Lower/OpenMP.cpp
index 5f5e968eaaa6414..e5914b4395d5beb 100644
--- a/flang/lib/Lower/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP.cpp
@@ -557,6 +557,18 @@ class ClauseProcessor {
llvm::SmallVectorImpl<mlir::Location> &useDeviceLocs,
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
&useDeviceSymbols) const;
+ bool
+ processIsDevicePtr(llvm::SmallVectorImpl<mlir::Value> &operands,
+ llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes,
+ llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs,
+ llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
+ &isDeviceSymbols) const;
+ bool
+ processHasDeviceAddr(llvm::SmallVectorImpl<mlir::Value> &operands,
+ llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes,
+ llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs,
+ llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
+ &isDeviceSymbols) const;
// Call this method for these clauses that should be supported but are not
// implemented yet. It triggers a compilation error if any of the given
@@ -1824,6 +1836,34 @@ bool ClauseProcessor::processUseDevicePtr(
});
}
+bool ClauseProcessor::processIsDevicePtr(
+ llvm::SmallVectorImpl<mlir::Value> &operands,
+ llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes,
+ llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs,
+ llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &isDeviceSymbols)
+ const {
+ return findRepeatableClause<ClauseTy::IsDevicePtr>(
+ [&](const ClauseTy::IsDevicePtr *devPtrClause,
+ const Fortran::parser::CharBlock &) {
+ addUseDeviceClause(converter, devPtrClause->v, operands, isDeviceTypes,
+ isDeviceLocs, isDeviceSymbols);
+ });
+}
+
+bool ClauseProcessor::processHasDeviceAddr(
+ llvm::SmallVectorImpl<mlir::Value> &operands,
+ llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes,
+ llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs,
+ llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &isDeviceSymbols)
+ const {
+ return findRepeatableClause<ClauseTy::HasDeviceAddr>(
+ [&](const ClauseTy::HasDeviceAddr *devAddrClause,
+ const Fortran::parser::CharBlock &) {
+ addUseDeviceClause(converter, devAddrClause->v, operands, isDeviceTypes,
+ isDeviceLocs, isDeviceSymbols);
+ });
+}
+
template <typename... Ts>
void ClauseProcessor::processTODO(mlir::Location currentLocation,
llvm::omp::Directive directive) const {
@@ -2411,6 +2451,10 @@ genTargetOp(Fortran::lower::AbstractConverter &converter,
mlir::Value ifClauseOperand, deviceOperand, threadLimitOperand;
mlir::UnitAttr nowaitAttr;
llvm::SmallVector<mlir::Value> mapOperands;
+ llvm::SmallVector<mlir::Value> devicePtrOperands, deviceAddrOperands;
+ llvm::SmallVector<mlir::Type> useDeviceTypes;
+ llvm::SmallVector<mlir::Location> useDeviceLocs;
+ llvm::SmallVector<const Fortran::semantics::Symbol *> useDeviceSymbols;
ClauseProcessor cp(converter, clauseList);
cp.processIf(stmtCtx,
@@ -2421,11 +2465,13 @@ genTargetOp(Fortran::lower::AbstractConverter &converter,
cp.processNowait(nowaitAttr);
cp.processMap(currentLocation, directive, semanticsContext, stmtCtx,
mapOperands);
+ cp.processIsDevicePtr(devicePtrOperands, useDeviceTypes, useDeviceLocs,
+ useDeviceSymbols);
+ cp.processHasDeviceAddr(deviceAddrOperands, useDeviceTypes, useDeviceLocs,
+ useDeviceSymbols);
cp.processTODO<Fortran::parser::OmpClause::Private,
Fortran::parser::OmpClause::Depend,
Fortran::parser::OmpClause::Firstprivate,
- Fortran::parser::OmpClause::IsDevicePtr,
- Fortran::parser::OmpClause::HasDeviceAddr,
Fortran::parser::OmpClause::Reduction,
Fortran::parser::OmpClause::InReduction,
Fortran::parser::OmpClause::Allocate,
@@ -2436,7 +2482,7 @@ genTargetOp(Fortran::lower::AbstractConverter &converter,
return genOpWithBody<mlir::omp::TargetOp>(
converter, eval, currentLocation, outerCombined, &clauseList,
ifClauseOperand, deviceOperand, threadLimitOperand, nowaitAttr,
- mapOperands);
+ devicePtrOperands, deviceAddrOperands, mapOperands);
}
static mlir::omp::TeamsOp
@@ -2830,6 +2876,8 @@ genOMP(Fortran::lower::AbstractConverter &converter,
!std::get_if<Fortran::parser::OmpClause::Map>(&clause.u) &&
!std::get_if<Fortran::parser::OmpClause::UseDevicePtr>(&clause.u) &&
!std::get_if<Fortran::parser::OmpClause::UseDeviceAddr>(&clause.u) &&
+ !std::get_if<Fortran::parser::OmpClause::IsDevicePtr>(&clause.u) &&
+ !std::get_if<Fortran::parser::OmpClause::HasDeviceAddr>(&clause.u) &&
!std::get_if<Fortran::parser::OmpClause::ThreadLimit>(&clause.u) &&
!std::get_if<Fortran::parser::OmpClause::NumTeams>(&clause.u)) {
TODO(clauseLocation, "OpenMP Block construct clause");
diff --git a/flang/test/Lower/OpenMP/FIR/target.f90 b/flang/test/Lower/OpenMP/FIR/target.f90
index 9b1fb5c15ac1d2d..3b67a7d3201c7fb 100644
--- a/flang/test/Lower/OpenMP/FIR/target.f90
+++ b/flang/test/Lower/OpenMP/FIR/target.f90
@@ -302,3 +302,42 @@ subroutine omp_target_parallel_do
!CHECK: }
!$omp end target parallel do
end subroutine omp_target_parallel_do
+
+!===============================================================================
+! Target `is_device_ptr` clause
+!===============================================================================
+
+!CHECK-LABEL: func.func @_QPomp_target_is_device_ptr() {
+subroutine omp_target_is_device_ptr
+ use iso_c_binding, only : c_ptr, c_loc
+ !CHECK: %[[DEV_PTR:.*]] = fir.alloca !fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}> {bindc_name = "a", uniq_name = "_QFomp_target_is_device_ptrEa"}
+ type(c_ptr) :: a
+ integer, target :: b
+ !CHECK: %[[MAP_0:.*]] = omp.map_info var_ptr(%[[VAL_0]] : !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>) map_clauses(tofrom) capture(ByRef) -> !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>> {name = "a"}
+ !CHECK: %[[MAP_1:.*]] = omp.map_info var_ptr(%[[VAL_1]] : !fir.ref<i32>) map_clauses(tofrom) capture(ByRef) -> !fir.ref<i32> {name = "b"}
+ !CHECK: omp.target is_device_ptr(%0 : !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>) map_entries(%[[MAP_0:.*]], %[[MAP_1:.*]] : !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>, !fir.ref<i32>) {
+ !$omp target map(tofrom: a,b) is_device_ptr(a)
+ !!!CHECK: {{.*}} = fir.coordinate_of %[[DEV_PTR:.*]], {{.*}} : (!fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>, !fir.field) -> !fir.ref<i64>
+ a = c_loc(b)
+ !CHECK: omp.terminator
+ !$omp end target
+ !CHECK: }
+end subroutine omp_target_is_device_ptr
+
+ !===============================================================================
+ ! Target `has_device_addr` clause
+ !===============================================================================
+
+ !CHECK-LABEL: func.func @_QPomp_target_has_device_addr() {
+ subroutine omp_target_has_device_addr
+ integer, pointer :: a
+ !CHECK: %[[VAL_0:.*]] = fir.alloca !fir.box<!fir.ptr<i32>> {bindc_name = "a", uniq_name = "_QFomp_target_has_device_addrEa"}
+ !CHECK: %[[MAP:.*]] = omp.map_info var_ptr({{.*}}) map_clauses(tofrom) capture(ByRef) -> {{.*}} {name = "a"}
+ !CHECK: omp.target has_device_addr(%[[VAL_0]] : !fir.ref<!fir.box<!fir.ptr<i32>>>) map_entries(%[[MAP]] : {{.*}}) {
+ !$omp target map(tofrom: a) has_device_addr(a)
+ !CHECK: {{.*}} = fir.load %[[VAL_0]] : !fir.ref<!fir.box<!fir.ptr<i32>>>
+ a = 10
+ !CHECK: omp.terminator
+ !$omp end target
+ !CHECK: }
+end subroutine omp_target_has_device_addr
diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
index 5da05476a683725..37e8cc3567317c2 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -1390,10 +1390,19 @@ def TargetOp : OpenMP_Op<"target",[OutlineableOpenMPOpInterface, AttrSizedOperan
The optional $thread_limit specifies the limit on the number of threads
- The optional $nowait elliminates the implicit barrier so the parent task can make progress
+ The optional $nowait eliminates the implicit barrier so the parent task can make progress
even if the target task is not yet completed.
- TODO: is_device_ptr, depend, defaultmap, in_reduction
+ The optional $is_device_ptr indicates list items are device pointers
+
+ The optional $use_device_addr indicates that list items already have device
+ addresses, so may be directly accessed from target device. May include array
+ sections.
+
+ The optional $map_operands maps data from the task’s environment to the
+ device environment.
+
+ TODO: depend, defaultmap, in_reduction
}];
@@ -1401,6 +1410,8 @@ def TargetOp : OpenMP_Op<"target",[OutlineableOpenMPOpInterface, AttrSizedOperan
Optional<AnyInteger>:$device,
Optional<AnyInteger>:$thread_limit,
UnitAttr:$nowait,
+ Variadic<OpenMP_PointerLikeType>:$is_device_ptr,
+ Variadic<OpenMP_PointerLikeType>:$has_device_addr,
Variadic<OpenMP_PointerLikeType>:$map_operands);
let regions = (region AnyRegion:$region);
@@ -1410,6 +1421,8 @@ def TargetOp : OpenMP_Op<"target",[OutlineableOpenMPOpInterface, AttrSizedOperan
| `device` `(` $device `:` type($device) `)`
| `thread_limit` `(` $thread_limit `:` type($thread_limit) `)`
| `nowait` $nowait
+ | `is_device_ptr` `(` $is_device_ptr `:` type($is_device_ptr) `)`
+ | `has_device_addr` `(` $has_device_addr `:` type($has_device_addr) `)`
| `map_entries` `(` $map_operands `:` type($map_operands) `)`
) $region attr-dict
}];
diff --git a/mlir/test/Dialect/OpenMP/ops.mlir b/mlir/test/Dialect/OpenMP/ops.mlir
index 13cbea6c9923c22..ed0cc7d02a7c4d2 100644
--- a/mlir/test/Dialect/OpenMP/ops.mlir
+++ b/mlir/test/Dialect/OpenMP/ops.mlir
@@ -480,22 +480,22 @@ func.func @omp_simdloop_pretty_multiple(%lb1 : index, %ub1 : index, %step1 : ind
}
// CHECK-LABEL: omp_target
-func.func @omp_target(%if_cond : i1, %device : si32, %num_threads : i32, %map1: memref<?xi32>, %map2: memref<?xi32>) -> () {
+func.func @omp_target(%if_cond : i1, %device : si32, %num_threads : i32, %device_ptr: memref<i32>, %device_addr: memref<?xi32>, %map1: memref<?xi32>, %map2: memref<?xi32>) -> () {
// Test with optional operands; if_expr, device, thread_limit, private, firstprivate and nowait.
// CHECK: omp.target if({{.*}}) device({{.*}}) thread_limit({{.*}}) nowait
"omp.target"(%if_cond, %device, %num_threads) ({
// CHECK: omp.terminator
omp.terminator
- }) {nowait, operandSegmentSizes = array<i32: 1,1,1,0>} : ( i1, si32, i32 ) -> ()
+ }) {nowait, operandSegmentSizes = array<i32: 1,1,1,0,0,0>} : ( i1, si32, i32 ) -> ()
// Test with optional map clause.
// CHECK: %[[MAP_A:.*]] = omp.map_info var_ptr(%[[VAL_1:.*]] : memref<?xi32>) map_clauses(tofrom) capture(ByRef) -> memref<?xi32> {name = ""}
// CHECK: %[[MAP_B:.*]] = omp.map_info var_ptr(%[[VAL_2:.*]] : memref<?xi32>) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> memref<?xi32> {name = ""}
- // CHECK: omp.target map_entries(%[[MAP_A]], %[[MAP_B]] : memref<?xi32>, memref<?xi32>) {
+ // CHECK: omp.target is_device_ptr(%[[VAL_4:.*]] : memref<i32>) has_device_addr(%[[VAL_5:.*]] : memref<?xi32>) map_entries(%[[MAP_A]], %[[MAP_B]] : memref<?xi32>, memref<?xi32>) {
%mapv1 = omp.map_info var_ptr(%map1 : memref<?xi32>) map_clauses(tofrom) capture(ByRef) -> memref<?xi32> {name = ""}
%mapv2 = omp.map_info var_ptr(%map2 : memref<?xi32>) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> memref<?xi32> {name = ""}
- omp.target map_entries(%mapv1, %mapv2 : memref<?xi32>, memref<?xi32>){}
+ omp.target map_entries(%mapv1, %mapv2 : memref<?xi32>, memref<?xi32>) is_device_ptr(%device_ptr : memref<i32>) has_device_addr(%device_addr : memref<?xi32>) {}
// CHECK: %[[MAP_C:.*]] = omp.map_info var_ptr(%[[VAL_1:.*]] : memref<?xi32>) map_clauses(to) capture(ByRef) -> memref<?xi32> {name = ""}
// CHECK: %[[MAP_D:.*]] = omp.map_info var_ptr(%[[VAL_2:.*]] : memref<?xi32>) map_clauses(always, from) capture(ByRef) -> memref<?xi32> {name = ""}
// CHECK: omp.target map_entries(%[[MAP_C]], %[[MAP_D]] : memref<?xi32>, memref<?xi32>) {
More information about the flang-commits
mailing list