Sergio Afonso 8fe11a26ae
[Flang][OpenMP] Lowering of host-evaluated clauses (#116219)
This patch adds support for lowering OpenMP clauses and expressions
attached to constructs nested inside of a target region that need to be
evaluated in the host device. This is done through the use of the
`OpenMP_HostEvalClause` `omp.target` set of operands and entry block
arguments.

When lowering clauses for a target construct, a more involved
`processHostEvalClauses()` function is called, which looks at the
current and potentially other nested constructs in order to find and
lower clauses that need to be processed outside of the `omp.target`
operation under construction. This populates an instance of a global
structure with the resulting MLIR values.

The resulting list of host-evaluated values is used to initialize the
`host_eval` operands when constructing the `omp.target` operation, and
then replaced with the corresponding block arguments after creating that
operation's region.

Afterwards, while lowering nested operations, those that might
potentially be evaluated on the host (i.e. `num_teams`, `thread_limit`,
`num_threads` and `collapse`) check first whether there is an active
global host-evaluated information structure and whether it holds values
referring to these clauses. If that is the case, the stored values
(`omp.target` entry block arguments at that stage) are used instead of
lowering these clauses again.
2025-01-14 13:55:17 +00:00

158 lines
4.0 KiB
Fortran

! The "thread_limit" clause was added to the "target" construct in OpenMP 5.1.
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=51 %s -o - | FileCheck %s --check-prefixes=BOTH,HOST
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=51 -fopenmp-is-target-device %s -o - | FileCheck %s --check-prefixes=BOTH,DEVICE
! BOTH-LABEL: func.func @_QPteams
subroutine teams()
! BOTH: omp.target
! HOST-SAME: host_eval(%{{.*}} -> %[[NUM_TEAMS:.*]], %{{.*}} -> %[[THREAD_LIMIT:.*]] : i32, i32)
! DEVICE-NOT: host_eval({{.*}})
! DEVICE-SAME: {
!$omp target
! BOTH: omp.teams
! HOST-SAME: num_teams( to %[[NUM_TEAMS]] : i32) thread_limit(%[[THREAD_LIMIT]] : i32)
! DEVICE-SAME: num_teams({{.*}}) thread_limit({{.*}})
!$omp teams num_teams(1) thread_limit(2)
call foo()
!$omp end teams
!$omp end target
! BOTH: omp.teams
! BOTH-SAME: num_teams({{.*}}) thread_limit({{.*}}) {
!$omp teams num_teams(1) thread_limit(2)
call foo()
!$omp end teams
end subroutine teams
! BOTH-LABEL: func.func @_QPdistribute_parallel_do
subroutine distribute_parallel_do()
! BOTH: omp.target
! HOST-SAME: host_eval(%{{.*}} -> %[[LB:.*]], %{{.*}} -> %[[UB:.*]], %{{.*}} -> %[[STEP:.*]], %{{.*}} -> %[[NUM_THREADS:.*]] : i32, i32, i32, i32)
! DEVICE-NOT: host_eval({{.*}})
! DEVICE-SAME: {
! BOTH: omp.teams
!$omp target teams
! BOTH: omp.parallel
! HOST-SAME: num_threads(%[[NUM_THREADS]] : i32)
! DEVICE-SAME: num_threads({{.*}})
! BOTH: omp.distribute
! BOTH-NEXT: omp.wsloop
! BOTH-NEXT: omp.loop_nest
! HOST-SAME: (%{{.*}}) : i32 = (%[[LB]]) to (%[[UB]]) inclusive step (%[[STEP]])
!$omp distribute parallel do num_threads(1)
do i=1,10
call foo()
end do
!$omp end distribute parallel do
!$omp end target teams
! BOTH: omp.target
! BOTH-NOT: host_eval({{.*}})
! BOTH-SAME: {
! BOTH: omp.teams
!$omp target teams
call foo() !< Prevents this from being SPMD.
! BOTH: omp.parallel
! BOTH-SAME: num_threads({{.*}})
! BOTH: omp.distribute
! BOTH-NEXT: omp.wsloop
!$omp distribute parallel do num_threads(1)
do i=1,10
call foo()
end do
!$omp end distribute parallel do
!$omp end target teams
! BOTH: omp.teams
!$omp teams
! BOTH: omp.parallel
! BOTH-SAME: num_threads({{.*}})
! BOTH: omp.distribute
! BOTH-NEXT: omp.wsloop
!$omp distribute parallel do num_threads(1)
do i=1,10
call foo()
end do
!$omp end distribute parallel do
!$omp end teams
end subroutine distribute_parallel_do
! BOTH-LABEL: func.func @_QPdistribute_parallel_do_simd
subroutine distribute_parallel_do_simd()
! BOTH: omp.target
! HOST-SAME: host_eval(%{{.*}} -> %[[LB:.*]], %{{.*}} -> %[[UB:.*]], %{{.*}} -> %[[STEP:.*]], %{{.*}} -> %[[NUM_THREADS:.*]] : i32, i32, i32, i32)
! DEVICE-NOT: host_eval({{.*}})
! DEVICE-SAME: {
! BOTH: omp.teams
!$omp target teams
! BOTH: omp.parallel
! HOST-SAME: num_threads(%[[NUM_THREADS]] : i32)
! DEVICE-SAME: num_threads({{.*}})
! BOTH: omp.distribute
! BOTH-NEXT: omp.wsloop
! BOTH-NEXT: omp.simd
! BOTH-NEXT: omp.loop_nest
! HOST-SAME: (%{{.*}}) : i32 = (%[[LB]]) to (%[[UB]]) inclusive step (%[[STEP]])
!$omp distribute parallel do simd num_threads(1)
do i=1,10
call foo()
end do
!$omp end distribute parallel do simd
!$omp end target teams
! BOTH: omp.target
! BOTH-NOT: host_eval({{.*}})
! BOTH-SAME: {
! BOTH: omp.teams
!$omp target teams
call foo() !< Prevents this from being SPMD.
! BOTH: omp.parallel
! BOTH-SAME: num_threads({{.*}})
! BOTH: omp.distribute
! BOTH-NEXT: omp.wsloop
! BOTH-NEXT: omp.simd
!$omp distribute parallel do simd num_threads(1)
do i=1,10
call foo()
end do
!$omp end distribute parallel do simd
!$omp end target teams
! BOTH: omp.teams
!$omp teams
! BOTH: omp.parallel
! BOTH-SAME: num_threads({{.*}})
! BOTH: omp.distribute
! BOTH-NEXT: omp.wsloop
! BOTH-NEXT: omp.simd
!$omp distribute parallel do simd num_threads(1)
do i=1,10
call foo()
end do
!$omp end distribute parallel do simd
!$omp end teams
end subroutine distribute_parallel_do_simd