diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp index 0a57a1496289f..fb24c8d1fe3eb 100644 --- a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp +++ b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp @@ -751,6 +751,20 @@ bool ClauseProcessor::processDepend( }); } +bool ClauseProcessor::processHasDeviceAddr( + llvm::SmallVectorImpl &operands, + llvm::SmallVectorImpl &isDeviceTypes, + llvm::SmallVectorImpl &isDeviceLocs, + llvm::SmallVectorImpl &isDeviceSymbols) + const { + return findRepeatableClause( + [&](const omp::clause::HasDeviceAddr &devAddrClause, + const Fortran::parser::CharBlock &) { + addUseDeviceClause(converter, devAddrClause.v, operands, isDeviceTypes, + isDeviceLocs, isDeviceSymbols); + }); +} + bool ClauseProcessor::processIf( omp::clause::If::DirectiveNameModifier directiveName, mlir::Value &result) const { @@ -771,6 +785,20 @@ bool ClauseProcessor::processIf( return found; } +bool ClauseProcessor::processIsDevicePtr( + llvm::SmallVectorImpl &operands, + llvm::SmallVectorImpl &isDeviceTypes, + llvm::SmallVectorImpl &isDeviceLocs, + llvm::SmallVectorImpl &isDeviceSymbols) + const { + return findRepeatableClause( + [&](const omp::clause::IsDevicePtr &devPtrClause, + const Fortran::parser::CharBlock &) { + addUseDeviceClause(converter, devPtrClause.v, operands, isDeviceTypes, + isDeviceLocs, isDeviceSymbols); + }); +} + bool ClauseProcessor::processLink( llvm::SmallVectorImpl &result) const { return findRepeatableClause( @@ -993,6 +1021,7 @@ bool ClauseProcessor::processUseDevicePtr( useDeviceLocs, useDeviceSymbols); }); } + } // namespace omp } // namespace lower } // namespace Fortran diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.h b/flang/lib/Lower/OpenMP/ClauseProcessor.h index d31d6a5c20623..df8f4f5310fcb 100644 --- a/flang/lib/Lower/OpenMP/ClauseProcessor.h +++ b/flang/lib/Lower/OpenMP/ClauseProcessor.h @@ -66,6 +66,12 @@ class ClauseProcessor { bool processDeviceType(mlir::omp::DeclareTargetDeviceType &result) const; bool processFinal(Fortran::lower::StatementContext &stmtCtx, mlir::Value &result) const; + bool + processHasDeviceAddr(llvm::SmallVectorImpl &operands, + llvm::SmallVectorImpl &isDeviceTypes, + llvm::SmallVectorImpl &isDeviceLocs, + llvm::SmallVectorImpl + &isDeviceSymbols) const; bool processHint(mlir::IntegerAttr &result) const; bool processMergeable(mlir::UnitAttr &result) const; bool processNowait(mlir::UnitAttr &result) const; @@ -104,6 +110,12 @@ class ClauseProcessor { bool processIf(omp::clause::If::DirectiveNameModifier directiveName, mlir::Value &result) const; bool + processIsDevicePtr(llvm::SmallVectorImpl &operands, + llvm::SmallVectorImpl &isDeviceTypes, + llvm::SmallVectorImpl &isDeviceLocs, + llvm::SmallVectorImpl + &isDeviceSymbols) const; + bool processLink(llvm::SmallVectorImpl &result) const; // This method is used to process a map clause. diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp index 340921c867246..50ad889052ab0 100644 --- a/flang/lib/Lower/OpenMP/OpenMP.cpp +++ b/flang/lib/Lower/OpenMP/OpenMP.cpp @@ -1294,6 +1294,11 @@ genTargetOp(Fortran::lower::AbstractConverter &converter, llvm::SmallVector mapSymTypes; llvm::SmallVector mapSymLocs; llvm::SmallVector mapSymbols; + llvm::SmallVector devicePtrOperands, deviceAddrOperands; + llvm::SmallVector devicePtrTypes, deviceAddrTypes; + llvm::SmallVector devicePtrLocs, deviceAddrLocs; + llvm::SmallVector devicePtrSymbols, + deviceAddrSymbols; ClauseProcessor cp(converter, semaCtx, clauseList); cp.processIf(llvm::omp::Directive::OMPD_target, ifClauseOperand); @@ -1303,11 +1308,15 @@ genTargetOp(Fortran::lower::AbstractConverter &converter, cp.processNowait(nowaitAttr); cp.processMap(currentLocation, directive, stmtCtx, mapOperands, &mapSymTypes, &mapSymLocs, &mapSymbols); + cp.processIsDevicePtr(devicePtrOperands, devicePtrTypes, devicePtrLocs, + devicePtrSymbols); + cp.processHasDeviceAddr(deviceAddrOperands, deviceAddrTypes, deviceAddrLocs, + deviceAddrSymbols); - cp.processTODO( - currentLocation, llvm::omp::Directive::OMPD_target); + cp.processTODO(currentLocation, + llvm::omp::Directive::OMPD_target); // 5.8.1 Implicit Data-Mapping Attribute Rules // The following code follows the implicit data-mapping rules to map all the @@ -1400,7 +1409,8 @@ genTargetOp(Fortran::lower::AbstractConverter &converter, ? nullptr : mlir::ArrayAttr::get(converter.getFirOpBuilder().getContext(), dependTypeOperands), - dependOperands, nowaitAttr, mapOperands); + dependOperands, nowaitAttr, devicePtrOperands, deviceAddrOperands, + mapOperands); genBodyOfTargetOp(converter, semaCtx, eval, genNested, targetOp, mapSymTypes, mapSymLocs, mapSymbols, currentLocation); @@ -2059,6 +2069,8 @@ genOMP(Fortran::lower::AbstractConverter &converter, !std::get_if(&clause.u) && !std::get_if(&clause.u) && !std::get_if(&clause.u) && + !std::get_if(&clause.u) && + !std::get_if(&clause.u) && !std::get_if(&clause.u) && !std::get_if(&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 821196b83c3b9..022327f9c25da 100644 --- a/flang/test/Lower/OpenMP/FIR/target.f90 +++ b/flang/test/Lower/OpenMP/FIR/target.f90 @@ -506,4 +506,45 @@ subroutine omp_target_parallel_do !CHECK: omp.terminator !CHECK: } !$omp end target parallel do - end subroutine omp_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: %[[VAL_0:.*]] = 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 + !CHECK: %[[VAL_1:.*]] = fir.alloca i32 {bindc_name = "b", fir.target, uniq_name = "_QFomp_target_is_device_ptrEb"} + integer, target :: b + !CHECK: %[[MAP_0:.*]] = omp.map.info var_ptr(%[[DEV_PTR:.*]] : !fir.ref>, !fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>) map_clauses(tofrom) capture(ByRef) -> !fir.ref> {name = "a"} + !CHECK: %[[MAP_1:.*]] = omp.map.info var_ptr(%[[VAL_0:.*]] : !fir.ref, i32) map_clauses(tofrom) capture(ByRef) -> !fir.ref {name = "b"} + !CHECK: %[[MAP_2:.*]] = omp.map.info var_ptr(%[[DEV_PTR:.*]] : !fir.ref>, !fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByRef) -> !fir.ref> {name = "a"} + !CHECK: omp.target is_device_ptr(%[[DEV_PTR:.*]] : !fir.ref>) map_entries(%[[MAP_0:.*]] -> %[[ARG0:.*]], %[[MAP_1:.*]] -> %[[ARG1:.*]], %[[MAP_2:.*]] -> %[[ARG2:.*]] : !fir.ref>, !fir.ref, !fir.ref>) { + !CHECK: ^bb0(%[[ARG0]]: !fir.ref>, %[[ARG1]]: !fir.ref, %[[ARG2]]: !fir.ref>): + !$omp target map(tofrom: a,b) is_device_ptr(a) + !CHECK: {{.*}} = fir.coordinate_of %[[VAL_0:.*]], {{.*}} : (!fir.ref>, !fir.field) -> !fir.ref + 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 + !CHECK: %[[VAL_0:.*]] = fir.alloca !fir.box> {bindc_name = "a", uniq_name = "_QFomp_target_has_device_addrEa"} + integer, pointer :: a + !CHECK: omp.target has_device_addr(%[[VAL_0:.*]] : !fir.ref>>) map_entries({{.*}} -> {{.*}}, {{.*}} -> {{.*}} : !fir.llvm_ptr>, !fir.ref>>) { + !$omp target has_device_addr(a) + !CHECK: {{.*}} = fir.load %[[VAL_0:.*]] : !fir.ref>> + a = 10 + !CHECK: omp.terminator + !$omp end target + !CHECK: } +end subroutine omp_target_has_device_addr diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPClauseOperands.h b/mlir/include/mlir/Dialect/OpenMP/OpenMPClauseOperands.h index 6454076f7593b..4ce7e47da046b 100644 --- a/mlir/include/mlir/Dialect/OpenMP/OpenMPClauseOperands.h +++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPClauseOperands.h @@ -81,6 +81,9 @@ struct GrainsizeClauseOps { Value grainsizeVar; }; +struct HasDeviceAddrOps { + llvm::SmallVector hasDeviceAddrVars; +}; struct HintClauseOps { IntegerAttr hintAttr; }; @@ -94,6 +97,10 @@ struct InReductionClauseOps { llvm::SmallVector inReductionDeclSymbols; }; +struct IsDevicePtrOps { + llvm::SmallVector isDevicePtrVars; +}; + struct LinearClauseOps { llvm::SmallVector linearVars, linearStepVars; }; @@ -251,13 +258,12 @@ using SimdLoopClauseOps = using SingleClauseOps = detail::Clauses; -// TODO `defaultmap`, `has_device_addr`, `is_device_ptr`, `uses_allocators` -// clauses. +// TODO `defaultmap`, `uses_allocators` clauses. using TargetClauseOps = detail::Clauses; + HasDeviceAddrOps, IfClauseOps, InReductionClauseOps, + IsDevicePtrOps, MapClauseOps, NowaitClauseOps, + PrivateClauseOps, ReductionClauseOps, ThreadLimitClauseOps>; using TargetDataClauseOps = detail::Clauses; diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td index a38a82f9cc607..2a8582be38331 100644 --- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td +++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td @@ -1678,14 +1678,23 @@ def TargetOp : OpenMP_Op<"target", [IsolatedFromAbove, MapClauseOwningOpInterfac 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. The `depends` and `depend_vars` arguments are variadic lists of values that specify the dependencies of this particular target task in relation to other tasks. - TODO: is_device_ptr, defaultmap, in_reduction + The optional $is_device_ptr indicates list items are device pointers. + + The optional $has_device_addr indicates that list items already have device + addresses, so they may be directly accessed from the target device. This + includes array sections. + + The optional $map_operands maps data from the task’s environment to the + device environment. + + TODO: defaultmap, in_reduction }]; @@ -1695,8 +1704,9 @@ def TargetOp : OpenMP_Op<"target", [IsolatedFromAbove, MapClauseOwningOpInterfac OptionalAttr:$depends, Variadic:$depend_vars, UnitAttr:$nowait, + Variadic:$is_device_ptr, + Variadic:$has_device_addr, Variadic:$map_operands); - let regions = (region AnyRegion:$region); let builders = [ @@ -1708,6 +1718,8 @@ def TargetOp : OpenMP_Op<"target", [IsolatedFromAbove, MapClauseOwningOpInterfac | `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` `(` custom($map_operands, type($map_operands)) `)` | `depend` `(` custom($depend_vars, type($depend_vars), $depends) `)` ) $region attr-dict diff --git a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp index 543655338db8c..2d5b2231d2dd5 100644 --- a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp +++ b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp @@ -1258,10 +1258,11 @@ void TargetOp::build(OpBuilder &builder, OperationState &state, // TODO Store clauses in op: allocateVars, allocatorVars, inReductionVars, // inReductionDeclSymbols, privateVars, privatizers, reductionVars, // reductionByRefAttr, reductionDeclSymbols. - TargetOp::build(builder, state, clauses.ifVar, clauses.deviceVar, - clauses.threadLimitVar, - makeArrayAttr(ctx, clauses.dependTypeAttrs), - clauses.dependVars, clauses.nowaitAttr, clauses.mapVars); + TargetOp::build( + builder, state, clauses.ifVar, clauses.deviceVar, clauses.threadLimitVar, + makeArrayAttr(ctx, clauses.dependTypeAttrs), clauses.dependVars, + clauses.nowaitAttr, clauses.isDevicePtrVars, clauses.hasDeviceAddrVars, + clauses.mapVars); } LogicalResult TargetOp::verify() { diff --git a/mlir/test/Dialect/OpenMP/invalid.mlir b/mlir/test/Dialect/OpenMP/invalid.mlir index 1134db77d5baa..27a440b3f97ca 100644 --- a/mlir/test/Dialect/OpenMP/invalid.mlir +++ b/mlir/test/Dialect/OpenMP/invalid.mlir @@ -1809,7 +1809,7 @@ func.func @omp_target_depend(%data_var: memref) { // expected-error @below {{op expected as many depend values as depend variables}} "omp.target"(%data_var) ({ "omp.terminator"() : () -> () - }) {depends = [], operandSegmentSizes = array} : (memref) -> () + }) {depends = [], operandSegmentSizes = array} : (memref) -> () "func.return"() : () -> () } diff --git a/mlir/test/Dialect/OpenMP/ops.mlir b/mlir/test/Dialect/OpenMP/ops.mlir index e2c255c7a3ccc..ad5b74b84ac70 100644 --- a/mlir/test/Dialect/OpenMP/ops.mlir +++ b/mlir/test/Dialect/OpenMP/ops.mlir @@ -510,22 +510,22 @@ return // CHECK-LABEL: omp_target -func.func @omp_target(%if_cond : i1, %device : si32, %num_threads : i32, %map1: memref, %map2: memref) -> () { +func.func @omp_target(%if_cond : i1, %device : si32, %num_threads : i32, %device_ptr: memref, %device_addr: memref, %map1: memref, %map2: memref) -> () { // 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} : ( i1, si32, i32 ) -> () + }) {nowait, operandSegmentSizes = array} : ( i1, si32, i32 ) -> () // Test with optional map clause. // CHECK: %[[MAP_A:.*]] = omp.map.info var_ptr(%[[VAL_1:.*]] : memref, tensor) map_clauses(tofrom) capture(ByRef) -> memref {name = ""} // CHECK: %[[MAP_B:.*]] = omp.map.info var_ptr(%[[VAL_2:.*]] : memref, tensor) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> memref {name = ""} - // CHECK: omp.target map_entries(%[[MAP_A]] -> {{.*}}, %[[MAP_B]] -> {{.*}} : memref, memref) { + // CHECK: omp.target is_device_ptr(%[[VAL_4:.*]] : memref) has_device_addr(%[[VAL_5:.*]] : memref) map_entries(%[[MAP_A]] -> {{.*}}, %[[MAP_B]] -> {{.*}} : memref, memref) { %mapv1 = omp.map.info var_ptr(%map1 : memref, tensor) map_clauses(tofrom) capture(ByRef) -> memref {name = ""} %mapv2 = omp.map.info var_ptr(%map2 : memref, tensor) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> memref {name = ""} - omp.target map_entries(%mapv1 -> %arg0, %mapv2 -> %arg1 : memref, memref) { + omp.target map_entries(%mapv1 -> %arg0, %mapv2 -> %arg1 : memref, memref) is_device_ptr(%device_ptr : memref) has_device_addr(%device_addr : memref) { ^bb0(%arg0: memref, %arg1: memref): omp.terminator }