Skip to content

[Flang][OpenMP] Minimize host ops remaining in device compilation #137200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion flang/include/flang/Optimizer/OpenMP/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def FunctionFilteringPass : Pass<"omp-function-filtering"> {
"for the target device.";
let dependentDialects = [
"mlir::func::FuncDialect",
"fir::FIROpsDialect"
"fir::FIROpsDialect",
"mlir::omp::OpenMPDialect"
];
}

Expand Down
448 changes: 448 additions & 0 deletions flang/lib/Optimizer/OpenMP/FunctionFiltering.cpp

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions flang/test/Lower/OpenMP/declare-target-link-tarop-cap.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
!RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
!RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s
!RUN: bbc -emit-hlfir -fopenmp %s -o - | FileCheck %s
!RUN: bbc -emit-hlfir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
!RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s --check-prefixes=BOTH,HOST
!RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefixes=BOTH,DEVICE
!RUN: bbc -emit-hlfir -fopenmp %s -o - | FileCheck %s --check-prefixes=BOTH,HOST
!RUN: bbc -emit-hlfir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s --check-prefixes=BOTH,DEVICE

program test_link

Expand All @@ -20,13 +20,14 @@ program test_link
integer, pointer :: test_ptr2
!$omp declare target link(test_ptr2)

!CHECK-DAG: {{%.*}} = omp.map.info var_ptr({{%.*}} : !fir.ref<i32>, i32) map_clauses(implicit, tofrom) capture(ByRef) -> !fir.ref<i32> {name = "test_int"}
!BOTH-DAG: {{%.*}} = omp.map.info var_ptr({{%.*}} : !fir.ref<i32>, i32) map_clauses(implicit, tofrom) capture(ByRef) -> !fir.ref<i32> {name = "test_int"}
!$omp target
test_int = test_int + 1
!$omp end target


!CHECK-DAG: {{%.*}} = omp.map.info var_ptr({{%.*}} : !fir.ref<!fir.array<3xi32>>, !fir.array<3xi32>) map_clauses(implicit, tofrom) capture(ByRef) bounds({{%.*}}) -> !fir.ref<!fir.array<3xi32>> {name = "test_array_1d"}
!HOST-DAG: {{%.*}} = omp.map.info var_ptr({{%.*}} : !fir.ref<!fir.array<3xi32>>, !fir.array<3xi32>) map_clauses(implicit, tofrom) capture(ByRef) bounds({{%.*}}) -> !fir.ref<!fir.array<3xi32>> {name = "test_array_1d"}
!DEVICE-DAG: {{%.*}} = omp.map.info var_ptr({{%.*}} : !fir.ref<!fir.array<3xi32>>, !fir.array<3xi32>) map_clauses(implicit, tofrom) capture(ByRef) -> !fir.ref<!fir.array<3xi32>> {name = "test_array_1d"}
!$omp target
do i = 1,3
test_array_1d(i) = i * 2
Expand All @@ -35,18 +36,18 @@ program test_link

allocate(test_ptr1)
test_ptr1 = 1
!CHECK-DAG: {{%.*}} = omp.map.info var_ptr({{%.*}} : !fir.ref<!fir.box<!fir.ptr<i32>>>, !fir.box<!fir.ptr<i32>>) map_clauses(implicit, to) capture(ByRef) members({{%.*}} : !fir.llvm_ptr<!fir.ref<i32>>) -> !fir.ref<!fir.box<!fir.ptr<i32>>> {name = "test_ptr1"}
!BOTH-DAG: {{%.*}} = omp.map.info var_ptr({{%.*}} : !fir.ref<!fir.box<!fir.ptr<i32>>>, !fir.box<!fir.ptr<i32>>) map_clauses(implicit, to) capture(ByRef) members({{%.*}} : !fir.llvm_ptr<!fir.ref<i32>>) -> !fir.ref<!fir.box<!fir.ptr<i32>>> {name = "test_ptr1"}
!$omp target
test_ptr1 = test_ptr1 + 1
!$omp end target

!CHECK-DAG: {{%.*}} = omp.map.info var_ptr({{%.*}} : !fir.ref<i32>, i32) map_clauses(implicit, tofrom) capture(ByRef) -> !fir.ref<i32> {name = "test_target"}
!BOTH-DAG: {{%.*}} = omp.map.info var_ptr({{%.*}} : !fir.ref<i32>, i32) map_clauses(implicit, tofrom) capture(ByRef) -> !fir.ref<i32> {name = "test_target"}
!$omp target
test_target = test_target + 1
!$omp end target


!CHECK-DAG: {{%.*}} = omp.map.info var_ptr({{%.*}} : !fir.ref<!fir.box<!fir.ptr<i32>>>, !fir.box<!fir.ptr<i32>>) map_clauses(implicit, to) capture(ByRef) members({{%.*}} : !fir.llvm_ptr<!fir.ref<i32>>) -> !fir.ref<!fir.box<!fir.ptr<i32>>> {name = "test_ptr2"}
!BOTH-DAG: {{%.*}} = omp.map.info var_ptr({{%.*}} : !fir.ref<!fir.box<!fir.ptr<i32>>>, !fir.box<!fir.ptr<i32>>) map_clauses(implicit, to) capture(ByRef) members({{%.*}} : !fir.llvm_ptr<!fir.ref<i32>>) -> !fir.ref<!fir.box<!fir.ptr<i32>>> {name = "test_ptr2"}
test_ptr2 => test_target
!$omp target
test_ptr2 = test_ptr2 + 1
Expand Down
55 changes: 37 additions & 18 deletions flang/test/Lower/OpenMP/host-eval.f90
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ subroutine teams()

!$omp end target

! BOTH: omp.teams
! BOTH-SAME: num_teams({{.*}}) thread_limit({{.*}}) {
! HOST: omp.teams
! HOST-SAME: num_teams({{.*}}) thread_limit({{.*}}) {

! DEVICE-NOT: omp.teams
!$omp teams num_teams(1) thread_limit(2)
call foo()
!$omp end teams
Expand Down Expand Up @@ -76,13 +78,18 @@ subroutine distribute_parallel_do()
!$omp end distribute parallel do
!$omp end target teams

! BOTH: omp.teams
! HOST: omp.teams
! DEVICE-NOT: omp.teams
!$omp teams

! BOTH: omp.parallel
! BOTH-SAME: num_threads({{.*}})
! BOTH: omp.distribute
! BOTH-NEXT: omp.wsloop
! HOST: omp.parallel
! HOST-SAME: num_threads({{.*}})
! HOST: omp.distribute
! HOST-NEXT: omp.wsloop

! DEVICE-NOT: omp.parallel
! DEVICE-NOT: omp.distribute
! DEVICE-NOT: omp.wsloop
!$omp distribute parallel do num_threads(1)
do i=1,10
call foo()
Expand Down Expand Up @@ -140,14 +147,20 @@ subroutine distribute_parallel_do_simd()
!$omp end distribute parallel do simd
!$omp end target teams

! BOTH: omp.teams
! HOST: omp.teams
! DEVICE-NOT: omp.teams
!$omp teams

! BOTH: omp.parallel
! BOTH-SAME: num_threads({{.*}})
! BOTH: omp.distribute
! BOTH-NEXT: omp.wsloop
! BOTH-NEXT: omp.simd
! HOST: omp.parallel
! HOST-SAME: num_threads({{.*}})
! HOST: omp.distribute
! HOST-NEXT: omp.wsloop
! HOST-NEXT: omp.simd

! DEVICE-NOT: omp.parallel
! DEVICE-NOT: omp.distribute
! DEVICE-NOT: omp.wsloop
! DEVICE-NOT: omp.simd
!$omp distribute parallel do simd num_threads(1)
do i=1,10
call foo()
Expand Down Expand Up @@ -194,10 +207,12 @@ subroutine distribute()
!$omp end distribute
!$omp end target teams

! BOTH: omp.teams
! HOST: omp.teams
! DEVICE-NOT: omp.teams
!$omp teams

! BOTH: omp.distribute
! HOST: omp.distribute
! DEVICE-NOT: omp.distribute
!$omp distribute
do i=1,10
call foo()
Expand Down Expand Up @@ -246,11 +261,15 @@ subroutine distribute_simd()
!$omp end distribute simd
!$omp end target teams

! BOTH: omp.teams
! HOST: omp.teams
! DEVICE-NOT: omp.teams
!$omp teams

! BOTH: omp.distribute
! BOTH-NEXT: omp.simd
! HOST: omp.distribute
! HOST-NEXT: omp.simd

! DEVICE-NOT: omp.distribute
! DEVICE-NOT: omp.simd
!$omp distribute simd
do i=1,10
call foo()
Expand Down
5 changes: 1 addition & 4 deletions flang/test/Lower/OpenMP/real10.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
!CHECK: hlfir.declare %{{.*}} {uniq_name = "_QFEx"} : (!fir.ref<f80>) -> (!fir.ref<f80>, !fir.ref<f80>)

program p
!$omp declare target
real(10) :: x
!$omp target
continue
!$omp end target
end

Loading