Skip to content

Commit 9f72a4a

Browse files
committed
[Flang] [OpenMP] [Semantics] [MLIR] [Lowering] Add lowering support for IS_DEVICE_PTR and
HAS_DEVICE_ADDR clauses on OMP TARGET directive.
1 parent e61d6b7 commit 9f72a4a

File tree

7 files changed

+121
-15
lines changed

7 files changed

+121
-15
lines changed

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,35 @@ bool ClauseProcessor::processUseDevicePtr(
993993
useDeviceLocs, useDeviceSymbols);
994994
});
995995
}
996+
997+
bool ClauseProcessor::processIsDevicePtr(
998+
llvm::SmallVectorImpl<mlir::Value> &operands,
999+
llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes,
1000+
llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs,
1001+
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &isDeviceSymbols)
1002+
const {
1003+
return findRepeatableClause<omp::clause::IsDevicePtr>(
1004+
[&](const omp::clause::IsDevicePtr &devPtrClause,
1005+
const Fortran::parser::CharBlock &) {
1006+
addUseDeviceClause(converter, devPtrClause.v, operands, isDeviceTypes,
1007+
isDeviceLocs, isDeviceSymbols);
1008+
});
1009+
}
1010+
1011+
bool ClauseProcessor::processHasDeviceAddr(
1012+
llvm::SmallVectorImpl<mlir::Value> &operands,
1013+
llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes,
1014+
llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs,
1015+
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &isDeviceSymbols)
1016+
const {
1017+
return findRepeatableClause<omp::clause::HasDeviceAddr>(
1018+
[&](const omp::clause::HasDeviceAddr &devAddrClause,
1019+
const Fortran::parser::CharBlock &) {
1020+
addUseDeviceClause(converter, devAddrClause.v, operands, isDeviceTypes,
1021+
isDeviceLocs, isDeviceSymbols);
1022+
});
1023+
}
1024+
9961025
} // namespace omp
9971026
} // namespace lower
9981027
} // namespace Fortran

flang/lib/Lower/OpenMP/ClauseProcessor.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ class ClauseProcessor {
141141
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
142142
&useDeviceSymbols) const;
143143

144+
bool
145+
processIsDevicePtr(llvm::SmallVectorImpl<mlir::Value> &operands,
146+
llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes,
147+
llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs,
148+
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
149+
&isDeviceSymbols) const;
150+
151+
bool
152+
processHasDeviceAddr(llvm::SmallVectorImpl<mlir::Value> &operands,
153+
llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes,
154+
llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs,
155+
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
156+
&isDeviceSymbols) const;
157+
144158
template <typename T>
145159
bool processMotionClauses(Fortran::lower::StatementContext &stmtCtx,
146160
llvm::SmallVectorImpl<mlir::Value> &mapOperands);

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,10 @@ genTargetOp(Fortran::lower::AbstractConverter &converter,
12941294
llvm::SmallVector<mlir::Type> mapSymTypes;
12951295
llvm::SmallVector<mlir::Location> mapSymLocs;
12961296
llvm::SmallVector<const Fortran::semantics::Symbol *> mapSymbols;
1297+
llvm::SmallVector<mlir::Value> devicePtrOperands, deviceAddrOperands;
1298+
llvm::SmallVector<mlir::Type> useDeviceTypes;
1299+
llvm::SmallVector<mlir::Location> useDeviceLocs;
1300+
llvm::SmallVector<const Fortran::semantics::Symbol *> useDeviceSymbols;
12971301

12981302
ClauseProcessor cp(converter, semaCtx, clauseList);
12991303
cp.processIf(llvm::omp::Directive::OMPD_target, ifClauseOperand);
@@ -1303,11 +1307,14 @@ genTargetOp(Fortran::lower::AbstractConverter &converter,
13031307
cp.processNowait(nowaitAttr);
13041308
cp.processMap(currentLocation, directive, stmtCtx, mapOperands, &mapSymTypes,
13051309
&mapSymLocs, &mapSymbols);
1306-
1307-
cp.processTODO<clause::Private, clause::Firstprivate, clause::IsDevicePtr,
1308-
clause::HasDeviceAddr, clause::Reduction, clause::InReduction,
1309-
clause::Allocate, clause::UsesAllocators, clause::Defaultmap>(
1310-
currentLocation, llvm::omp::Directive::OMPD_target);
1310+
cp.processIsDevicePtr(devicePtrOperands, useDeviceTypes, useDeviceLocs,
1311+
useDeviceSymbols);
1312+
cp.processHasDeviceAddr(deviceAddrOperands, useDeviceTypes, useDeviceLocs,
1313+
useDeviceSymbols);
1314+
cp.processTODO<clause::Private, clause::Firstprivate, clause::Reduction,
1315+
clause::InReduction, clause::Allocate, clause::UsesAllocators,
1316+
clause::Defaultmap>(currentLocation,
1317+
llvm::omp::Directive::OMPD_target);
13111318

13121319
// 5.8.1 Implicit Data-Mapping Attribute Rules
13131320
// The following code follows the implicit data-mapping rules to map all the
@@ -1400,7 +1407,8 @@ genTargetOp(Fortran::lower::AbstractConverter &converter,
14001407
? nullptr
14011408
: mlir::ArrayAttr::get(converter.getFirOpBuilder().getContext(),
14021409
dependTypeOperands),
1403-
dependOperands, nowaitAttr, mapOperands);
1410+
dependOperands, nowaitAttr, devicePtrOperands,
1411+
deviceAddrOperands, mapOperands);
14041412

14051413
genBodyOfTargetOp(converter, semaCtx, eval, genNested, targetOp, mapSymTypes,
14061414
mapSymLocs, mapSymbols, currentLocation);
@@ -2059,6 +2067,8 @@ genOMP(Fortran::lower::AbstractConverter &converter,
20592067
!std::get_if<Fortran::parser::OmpClause::Map>(&clause.u) &&
20602068
!std::get_if<Fortran::parser::OmpClause::UseDevicePtr>(&clause.u) &&
20612069
!std::get_if<Fortran::parser::OmpClause::UseDeviceAddr>(&clause.u) &&
2070+
!std::get_if<Fortran::parser::OmpClause::IsDevicePtr>(&clause.u) &&
2071+
!std::get_if<Fortran::parser::OmpClause::HasDeviceAddr>(&clause.u) &&
20622072
!std::get_if<Fortran::parser::OmpClause::ThreadLimit>(&clause.u) &&
20632073
!std::get_if<Fortran::parser::OmpClause::NumTeams>(&clause.u)) {
20642074
TODO(clauseLocation, "OpenMP Block construct clause");

flang/test/Lower/OpenMP/FIR/target.f90

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,45 @@ subroutine omp_target_parallel_do
506506
!CHECK: omp.terminator
507507
!CHECK: }
508508
!$omp end target parallel do
509-
end subroutine omp_target_parallel_do
509+
end subroutine omp_target_parallel_do
510+
511+
!===============================================================================
512+
! Target `is_device_ptr` clause
513+
!===============================================================================
514+
515+
!CHECK-LABEL: func.func @_QPomp_target_is_device_ptr() {
516+
subroutine omp_target_is_device_ptr
517+
use iso_c_binding, only : c_ptr, c_loc
518+
!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"}
519+
type(c_ptr) :: a
520+
!CHECK %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "b", fir.target, uniq_name = "_QFomp_target_is_device_ptrEb"}
521+
integer, target :: b
522+
!CHECK: %[[MAP_0:.*]] = omp.map.info var_ptr(%[[DEV_PTR:.*]] : !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>, !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"}
523+
!CHECK: %[[MAP_1:.*]] = omp.map.info var_ptr(%[[VAL_0:.*]] : !fir.ref<i32>, i32) map_clauses(tofrom) capture(ByRef) -> !fir.ref<i32> {name = "b"}
524+
!CHECK: %[[MAP_2:.*]] = omp.map.info var_ptr(%[[DEV_PTR:.*]] : !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>, !fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByRef) -> !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>> {name = "a"}
525+
!CHECK: omp.target is_device_ptr(%[[DEV_PTR:.*]] : !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>) map_entries(%[[MAP_0:.*]] -> %[[ARG0:.*]], %[[MAP_1:.*]] -> %[[ARG1:.*]], %[[MAP_2:.*]] -> %[[ARG2:.*]] : !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>, !fir.ref<i32>, !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>) {
526+
!CHECK: ^bb0(%[[ARG0]]: !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>, %[[ARG1]]: !fir.ref<i32>, %[[ARG2]]: !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>):
527+
!$omp target map(tofrom: a,b) is_device_ptr(a)
528+
!CHECK: {{.*}} = fir.coordinate_of %[[DEV_PTR:.*]], {{.*}} : (!fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>, !fir.field) -> !fir.ref<i64>
529+
a = c_loc(b)
530+
!CHECK: omp.terminator
531+
!$omp end target
532+
!CHECK: }
533+
end subroutine omp_target_is_device_ptr
534+
535+
!===============================================================================
536+
! Target `has_device_addr` clause
537+
!===============================================================================
538+
539+
!CHECK-LABEL: func.func @_QPomp_target_has_device_addr() {
540+
subroutine omp_target_has_device_addr
541+
integer, pointer :: a
542+
!CHECK: %[[VAL_0:.*]] = fir.alloca !fir.box<!fir.ptr<i32>> {bindc_name = "a", uniq_name = "_QFomp_target_has_device_addrEa"}
543+
!CHECK: omp.target has_device_addr(%[[VAL_0:.*]] : !fir.ref<!fir.box<!fir.ptr<i32>>>) map_entries({{.*}} -> {{.*}}, {{.*}} -> {{.*}} : !fir.llvm_ptr<!fir.ref<i32>>, !fir.ref<!fir.box<!fir.ptr<i32>>>) {
544+
!$omp target has_device_addr(a)
545+
!CHECK: {{.*}} = fir.load %[[VAL_0:.*]] : !fir.ref<!fir.box<!fir.ptr<i32>>>
546+
a = 10
547+
!CHECK: omp.terminator
548+
!$omp end target
549+
!CHECK: }
550+
end subroutine omp_target_has_device_addr

mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,14 +1628,23 @@ def TargetOp : OpenMP_Op<"target", [IsolatedFromAbove, MapClauseOwningOpInterfac
16281628

16291629
The optional $thread_limit specifies the limit on the number of threads
16301630

1631-
The optional $nowait elliminates the implicit barrier so the parent task can make progress
1631+
The optional $nowait eliminates the implicit barrier so the parent task can make progress
16321632
even if the target task is not yet completed.
16331633

16341634
The `depends` and `depend_vars` arguments are variadic lists of values
16351635
that specify the dependencies of this particular target task in relation to
16361636
other tasks.
16371637

1638-
TODO: is_device_ptr, defaultmap, in_reduction
1638+
The optional $is_device_ptr indicates list items are device pointers
1639+
1640+
The optional $has_device_addr indicates that list items already have device
1641+
addresses, so may be directly accessed from target device. May include array
1642+
sections.
1643+
1644+
The optional $map_operands maps data from the task’s environment to the
1645+
device environment.
1646+
1647+
TODO: defaultmap, in_reduction
16391648

16401649
}];
16411650

@@ -1645,15 +1654,18 @@ def TargetOp : OpenMP_Op<"target", [IsolatedFromAbove, MapClauseOwningOpInterfac
16451654
OptionalAttr<TaskDependArrayAttr>:$depends,
16461655
Variadic<OpenMP_PointerLikeType>:$depend_vars,
16471656
UnitAttr:$nowait,
1657+
Variadic<OpenMP_PointerLikeType>:$is_device_ptr,
1658+
Variadic<OpenMP_PointerLikeType>:$has_device_addr,
16481659
Variadic<AnyType>:$map_operands);
1649-
16501660
let regions = (region AnyRegion:$region);
16511661

16521662
let assemblyFormat = [{
16531663
oilist( `if` `(` $if_expr `)`
16541664
| `device` `(` $device `:` type($device) `)`
16551665
| `thread_limit` `(` $thread_limit `:` type($thread_limit) `)`
16561666
| `nowait` $nowait
1667+
| `is_device_ptr` `(` $is_device_ptr `:` type($is_device_ptr) `)`
1668+
| `has_device_addr` `(` $has_device_addr `:` type($has_device_addr) `)`
16571669
| `map_entries` `(` custom<MapEntries>($map_operands, type($map_operands)) `)`
16581670
| `depend` `(` custom<DependVarList>($depend_vars, type($depend_vars), $depends) `)`
16591671
) $region attr-dict

mlir/test/Dialect/OpenMP/invalid.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,7 @@ func.func @omp_target_depend(%data_var: memref<i32>) {
17901790
// expected-error @below {{op expected as many depend values as depend variables}}
17911791
"omp.target"(%data_var) ({
17921792
"omp.terminator"() : () -> ()
1793-
}) {depends = [], operandSegmentSizes = array<i32: 0, 0, 0, 1, 0>} : (memref<i32>) -> ()
1793+
}) {depends = [], operandSegmentSizes = array<i32: 0, 0, 0, 1, 0, 0, 0>} : (memref<i32>) -> ()
17941794
"func.return"() : () -> ()
17951795
}
17961796

mlir/test/Dialect/OpenMP/ops.mlir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,22 +510,22 @@ return
510510

511511

512512
// CHECK-LABEL: omp_target
513-
func.func @omp_target(%if_cond : i1, %device : si32, %num_threads : i32, %map1: memref<?xi32>, %map2: memref<?xi32>) -> () {
513+
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>) -> () {
514514

515515
// Test with optional operands; if_expr, device, thread_limit, private, firstprivate and nowait.
516516
// CHECK: omp.target if({{.*}}) device({{.*}}) thread_limit({{.*}}) nowait
517517
"omp.target"(%if_cond, %device, %num_threads) ({
518518
// CHECK: omp.terminator
519519
omp.terminator
520-
}) {nowait, operandSegmentSizes = array<i32: 1,1,1,0,0>} : ( i1, si32, i32 ) -> ()
520+
}) {nowait, operandSegmentSizes = array<i32: 1,1,1,0,0,0,0>} : ( i1, si32, i32 ) -> ()
521521

522522
// Test with optional map clause.
523523
// CHECK: %[[MAP_A:.*]] = omp.map.info var_ptr(%[[VAL_1:.*]] : memref<?xi32>, tensor<?xi32>) map_clauses(tofrom) capture(ByRef) -> memref<?xi32> {name = ""}
524524
// CHECK: %[[MAP_B:.*]] = omp.map.info var_ptr(%[[VAL_2:.*]] : memref<?xi32>, tensor<?xi32>) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> memref<?xi32> {name = ""}
525-
// CHECK: omp.target map_entries(%[[MAP_A]] -> {{.*}}, %[[MAP_B]] -> {{.*}} : memref<?xi32>, memref<?xi32>) {
525+
// 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>) {
526526
%mapv1 = omp.map.info var_ptr(%map1 : memref<?xi32>, tensor<?xi32>) map_clauses(tofrom) capture(ByRef) -> memref<?xi32> {name = ""}
527527
%mapv2 = omp.map.info var_ptr(%map2 : memref<?xi32>, tensor<?xi32>) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> memref<?xi32> {name = ""}
528-
omp.target map_entries(%mapv1 -> %arg0, %mapv2 -> %arg1 : memref<?xi32>, memref<?xi32>) {
528+
omp.target map_entries(%mapv1 -> %arg0, %mapv2 -> %arg1 : memref<?xi32>, memref<?xi32>) is_device_ptr(%device_ptr : memref<i32>) has_device_addr(%device_addr : memref<?xi32>) {
529529
^bb0(%arg0: memref<?xi32>, %arg1: memref<?xi32>):
530530
omp.terminator
531531
}

0 commit comments

Comments
 (0)