Skip to content

Commit 6d664d6

Browse files
committed
Changes to support link clause of declare target with common block
This change is a continuation to changes in llvm#83643. As per the implicit mapping rules, if a variable is specified in link clause of openmp, it needs to be mapped tofrom if the device type is not specified as nohost.
1 parent 4d31fbb commit 6d664d6

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,31 @@ static void genBodyOfTargetOp(
10371037
genNestedEvaluations(converter, eval);
10381038
}
10391039

1040+
// If the symbol is specified in declare target directive, the function returns
1041+
// the corresponding declare target operation.
1042+
static mlir::omp::DeclareTargetInterface
1043+
getDeclareTargetOp(const Fortran::semantics::Symbol &sym,
1044+
Fortran::lower::AbstractConverter &converter) {
1045+
mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
1046+
mlir::Operation *op;
1047+
op = mod.lookupSymbol(converter.mangleName(sym));
1048+
auto declareTargetOp =
1049+
llvm::dyn_cast_if_present<mlir::omp::DeclareTargetInterface>(op);
1050+
// If declare target op is not found Check if common block containing the
1051+
// variable is specified in declare target
1052+
if (!declareTargetOp || !declareTargetOp.isDeclareTarget()) {
1053+
if (auto cB = Fortran::semantics::FindCommonBlockContaining(sym)) {
1054+
op = mod.lookupSymbol(converter.mangleName(*cB));
1055+
declareTargetOp =
1056+
llvm::dyn_cast_if_present<mlir::omp::DeclareTargetInterface>(op);
1057+
}
1058+
}
1059+
if (declareTargetOp && declareTargetOp.isDeclareTarget()) {
1060+
return declareTargetOp;
1061+
}
1062+
return static_cast<mlir::omp::DeclareTargetInterface>(nullptr);
1063+
}
1064+
10401065
static mlir::omp::TargetOp
10411066
genTargetOp(Fortran::lower::AbstractConverter &converter,
10421067
Fortran::semantics::SemanticsContext &semaCtx,
@@ -1122,11 +1147,7 @@ genTargetOp(Fortran::lower::AbstractConverter &converter,
11221147

11231148
// If a variable is specified in declare target link and if device
11241149
// type is not specified as `nohost`, it needs to be mapped tofrom
1125-
mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
1126-
mlir::Operation *op = mod.lookupSymbol(converter.mangleName(sym));
1127-
auto declareTargetOp =
1128-
llvm::dyn_cast_if_present<mlir::omp::DeclareTargetInterface>(op);
1129-
if (declareTargetOp && declareTargetOp.isDeclareTarget()) {
1150+
if (auto declareTargetOp = getDeclareTargetOp(sym, converter)) {
11301151
if (declareTargetOp.getDeclareTargetCaptureClause() ==
11311152
mlir::omp::DeclareTargetCaptureClause::link &&
11321153
declareTargetOp.getDeclareTargetDeviceType() !=

flang/test/Lower/OpenMP/declare-target-link-tarop-cap.f90

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ program test_link
2020
integer, pointer :: test_ptr2
2121
!$omp declare target link(test_ptr2)
2222

23+
integer :: test_int_cb
24+
25+
integer :: test_int_array_cb(3) = (/1,2,3/)
26+
27+
common /test_cb/ test_int_cb, test_int_array_cb
28+
!$omp declare target link(/test_cb/)
29+
2330
!CHECK-DAG: {{%.*}} = omp.map_info var_ptr({{%.*}} : !fir.ref<i32>, i32) map_clauses(implicit, tofrom) capture(ByRef) -> !fir.ref<i32> {name = "test_int"}
2431
!$omp target
2532
test_int = test_int + 1
@@ -52,4 +59,15 @@ program test_link
5259
test_ptr2 = test_ptr2 + 1
5360
!$omp end target
5461

62+
!CHECK-DAG: {{%.*}} = omp.map_info var_ptr({{%.*}} : !fir.ref<i32>, i32) map_clauses(implicit, tofrom) capture(ByRef) -> !fir.ref<i32> {name = "test_int_cb"}
63+
!$omp target
64+
test_int_cb = test_int_cb + 1
65+
!$omp end target
66+
67+
!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_int_array_cb"}
68+
!$omp target
69+
do i = 1,3
70+
test_int_array_cb(i) = i * 2
71+
end do
72+
!$omp end target
5573
end

openmp/libomptarget/test/offloading/fortran/declare-target-vars-in-target-region.f90

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ module test_0
1616
!$omp declare target link(arr1) enter(arr2)
1717
INTEGER :: scalar = 1
1818
!$omp declare target link(scalar)
19+
INTEGER :: scalar_cb = 1
20+
INTEGER :: arr_cb(10) = (/0,0,0,0,0,0,0,0,0,0/)
21+
COMMON /CB/ scalar_cb, arr_cb
22+
!$omp declare target link(/CB/)
1923
end module test_0
2024

2125
subroutine test_with_array_link_and_tofrom()
@@ -73,9 +77,36 @@ subroutine test_with_scalar_link_only()
7377
PRINT *, scalar
7478
end subroutine test_with_scalar_link_only
7579

80+
subroutine test_with_array_cb_link_only()
81+
use test_0
82+
integer :: i = 1
83+
integer :: j = 11
84+
!$omp target map(i, j)
85+
do while (i <= j)
86+
arr_cb(i) = i + 1;
87+
i = i + 1
88+
end do
89+
!$omp end target
90+
91+
! CHECK: 2 3 4 5 6 7 8 9 10 11
92+
PRINT *, arr_cb(:)
93+
end subroutine test_with_array_cb_link_only
94+
95+
subroutine test_with_scalar_cb_link_only()
96+
use test_0
97+
!$omp target
98+
scalar_cb = 10
99+
!$omp end target
100+
101+
! CHECK: 10
102+
PRINT *, scalar_cb
103+
end subroutine test_with_scalar_cb_link_only
104+
76105
program main
77106
call test_with_array_link_and_tofrom()
78107
call test_with_array_link_only()
79108
call test_with_array_enter_only()
80109
call test_with_scalar_link_only()
110+
call test_with_array_cb_link_only()
111+
call test_with_scalar_cb_link_only()
81112
end program

0 commit comments

Comments
 (0)