Skip to content

Commit 17ce48e

Browse files
committed
Add lowering changes for declMapperOp's region.
1 parent 171538a commit 17ce48e

File tree

4 files changed

+123
-82
lines changed

4 files changed

+123
-82
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,6 +2701,7 @@ static void
27012701
genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
27022702
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
27032703
const parser::OpenMPDeclareMapperConstruct &declareMapperConstruct) {
2704+
mlir::Location loc = converter.genLocation(declareMapperConstruct.source);
27042705
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
27052706
lower::StatementContext stmtCtx;
27062707
const auto &spec =
@@ -2718,24 +2719,50 @@ genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
27182719
else
27192720
mapperNameStr =
27202721
"default_" + varType.declTypeSpec->derivedTypeSpec().name().ToString();
2722+
auto mlirType = converter.genType(varType.declTypeSpec->derivedTypeSpec());
2723+
auto declMapperOp = firOpBuilder.create<mlir::omp::DeclareMapperOp>(
2724+
loc, mapperNameStr, mlirType);
2725+
auto &region = declMapperOp.getRegion();
27212726

2727+
// Save insert point just after the DeclMapperOp.
27222728
mlir::OpBuilder::InsertPoint insPt = firOpBuilder.saveInsertionPoint();
2723-
firOpBuilder.setInsertionPointToStart(converter.getModuleOp().getBody());
2724-
auto mlirType = converter.genType(varType.declTypeSpec->derivedTypeSpec());
2725-
auto varVal = firOpBuilder.createTemporaryAlloc(
2726-
converter.getCurrentLocation(), mlirType, varName.ToString());
2727-
symTable.addSymbol(*varName.symbol, varVal);
2729+
firOpBuilder.createBlock(&region);
2730+
auto varVal =
2731+
firOpBuilder.createTemporaryAlloc(loc, mlirType, varName.ToString());
2732+
converter.bindSymbol(*varName.symbol, varVal);
27282733

2729-
mlir::omp::DeclareMapperOperands clauseOps;
2734+
// Insert dummy instruction to remember the insertion position. The
2735+
// marker will be deleted by clean up passes since there are no uses.
2736+
// Remembering the position for further insertion is important since
2737+
// there are hlfir.declares inserted above while setting block arguments
2738+
// and new code from the body should be inserted after that.
2739+
mlir::Value undefMarker =
2740+
firOpBuilder.create<fir::UndefOp>(loc, firOpBuilder.getIndexType());
2741+
2742+
// Create blocks for unstructured regions. This has to be done since
2743+
// blocks are initially allocated with the function as the parent region.
2744+
if (eval.lowerAsUnstructured()) {
2745+
lower::createEmptyRegionBlocks<mlir::omp::TerminatorOp, mlir::omp::YieldOp>(
2746+
firOpBuilder, eval.getNestedEvaluations());
2747+
}
2748+
2749+
firOpBuilder.create<mlir::omp::TerminatorOp>(loc);
2750+
2751+
// Set the insertion point after the marker.
2752+
firOpBuilder.setInsertionPointAfter(undefMarker.getDefiningOp());
2753+
genNestedEvaluations(converter, eval);
2754+
2755+
// Populate the declareMapper region with the map information.
2756+
mlir::omp::DeclareMapperInfoOperands clauseOps;
27302757
const auto *clauseList{
27312758
parser::Unwrap<parser::OmpClauseList>(declareMapperConstruct.t)};
27322759
List<Clause> clauses = makeClauses(*clauseList, semaCtx);
27332760
ClauseProcessor cp(converter, semaCtx, clauses);
2734-
cp.processMap(converter.getCurrentLocation(), stmtCtx, clauseOps);
2735-
auto declMapperOp = firOpBuilder.create<mlir::omp::DeclareMapperOp>(
2736-
converter.getCurrentLocation(), mapperNameStr, varVal, mlirType,
2737-
clauseOps.mapVars);
2738-
converter.getMLIRSymbolTable()->insert(declMapperOp.getOperation());
2761+
cp.processMap(loc, stmtCtx, clauseOps);
2762+
2763+
firOpBuilder.create<mlir::omp::DeclareMapperInfoOp>(loc, clauseOps.mapVars);
2764+
2765+
// Restore the insert point to just after the DeclareMapperOp.
27392766
firOpBuilder.restoreInsertionPoint(insPt);
27402767
}
27412768

flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ class MapInfoFinalizationPass
447447
for (auto *user : mapOp->getUsers()) {
448448
if (llvm::isa<mlir::omp::TargetOp, mlir::omp::TargetDataOp,
449449
mlir::omp::TargetUpdateOp, mlir::omp::TargetExitDataOp,
450-
mlir::omp::TargetEnterDataOp, mlir::omp::DeclareMapperOp>(
451-
user))
450+
mlir::omp::TargetEnterDataOp,
451+
mlir::omp::DeclareMapperInfoOp>(user))
452452
return user;
453453

454454
if (auto mapUser = llvm::dyn_cast<mlir::omp::MapInfoOp>(user))

flang/test/Lower/OpenMP/Todo/omp-declare-mapper.f90

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,92 @@
11
! This test checks lowering of OpenMP declare mapper Directive.
22

33
! RUN: split-file %s %t
4-
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/declare-mapper-1.f90 -o - | FileCheck %t/declare-mapper-1.f90
5-
! RUN %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/declare-mapper-2.f90 -o - | FileCheck %t/declare-mapper-2.f90
4+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-1.f90 -o - | FileCheck %t/omp-declare-mapper-1.f90
5+
! RUN %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-2.f90 -o - | FileCheck %t/omp-declare-mapper-2.f90
66

7-
!--- declare-mapper-1.f90
8-
subroutine mapper
9-
implicit none
7+
!--- omp-declare-mapper-1.f90
8+
subroutine declare_mapper_1
9+
integer, parameter :: nvals = 250
1010
type my_type
11-
integer, pointer :: my_buffer
12-
integer :: my_buffer_size
11+
integer :: num_vals
12+
integer, allocatable :: values(:)
1313
end type
14-
!CHECK: %[[MY_VAR:.*]] = fir.alloca ![[VAR_TYPE:.*]] {bindc_name = "my_var"}
15-
!CHECK: %[[MAP_INFO:.*]] = omp.map.info var_ptr(%[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) -> !fir.ref<![[VAR_TYPE]]> {name = "my_var"}
16-
!CHECK: omp.declare_mapper @my_mapper : %[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]> : ![[VAR_TYPE]] map_entries(%[[MAP_INFO]] : !fir.ref<![[VAR_TYPE]]>)
17-
!$omp DECLARE MAPPER(my_mapper : my_type :: my_var) map(tofrom : my_var)
18-
end subroutine
1914

20-
!--- declare-mapper-2.f90
21-
subroutine mapper_default
22-
implicit none
15+
type my_type2
16+
type(my_type) :: my_type_var
17+
type(my_type) :: temp
18+
real, dimension(nvals) :: unmapped
19+
real, dimension(nvals) :: arr
20+
end type
21+
type(my_type2) :: t
22+
real :: x, y(nvals)
23+
!CHECK: omp.declare_mapper @default_my_type : ![[VAR_TYPE:.*]] {
24+
!CHECK: %[[VAL_5:.*]] = fir.alloca ![[VAR_TYPE]] {bindc_name = "var"}
25+
!CHECK: %[[VAL_6:.*]]:2 = hlfir.declare %[[VAL_5]] {uniq_name = "_QFdeclare_mapper_1Evar"} : (!fir.ref<![[VAR_TYPE]]>) -> (!fir.ref<![[VAR_TYPE]]>, !fir.ref<![[VAR_TYPE]]>)
26+
!CHECK: %[[VAL_7:.*]] = hlfir.designate %[[VAL_6]]#0{{.*}} : (!fir.ref<![[VAR_TYPE]]>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
27+
!CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_7]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
28+
!CHECK: %[[VAL_9:.*]] = arith.constant 0 : index
29+
!CHECK: %[[VAL_10:.*]]:3 = fir.box_dims %[[VAL_8]], %[[VAL_9]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>, index) -> (index, index, index)
30+
!CHECK: %[[VAL_11:.*]] = arith.constant 1 : index
31+
!CHECK: %[[VAL_12:.*]] = arith.constant 1 : index
32+
!CHECK: %[[VAL_13:.*]] = arith.subi %[[VAL_12]], %[[VAL_10]]#0 : index
33+
!CHECK: %[[VAL_14:.*]] = hlfir.designate %[[VAL_6]]#0{"num_vals"} : (!fir.ref<![[VAR_TYPE]]>) -> !fir.ref<i32>
34+
!CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_14]] : !fir.ref<i32>
35+
!CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_15]] : (i32) -> i64
36+
!CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_16]] : (i64) -> index
37+
!CHECK: %[[VAL_18:.*]] = arith.subi %[[VAL_17]], %[[VAL_10]]#0 : index
38+
!CHECK: %[[VAL_19:.*]] = omp.map.bounds lower_bound(%[[VAL_13]] : index) upper_bound(%[[VAL_18]] : index) extent(%[[VAL_10]]#1 : index) stride(%[[VAL_11]] : index) start_idx(%[[VAL_10]]#0 : index)
39+
!CHECK: %[[VAL_20:.*]] = arith.constant 1 : index
40+
!CHECK: %[[VAL_21:.*]] = fir.coordinate_of %[[VAL_6]]#0, %[[VAL_20]] : (!fir.ref<![[VAR_TYPE]]>, index) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
41+
!CHECK: %[[VAL_22:.*]] = fir.box_offset %[[VAL_21]] base_addr : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>
42+
!CHECK: %[[VAL_23:.*]] = omp.map.info var_ptr(%[[VAL_21]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.array<?xi32>) var_ptr_ptr(%[[VAL_22]] : !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_19]]) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>> {name = ""}
43+
!CHECK: %[[VAL_24:.*]] = omp.map.info var_ptr(%[[VAL_21]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.box<!fir.heap<!fir.array<?xi32>>>) map_clauses(to) capture(ByRef) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>> {name = "var%[[VAL_25:.*]](1:var%[[VAL_26:.*]])"}
44+
!CHECK: %[[VAL_27:.*]] = omp.map.info var_ptr(%[[VAL_6]]#1 : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) members(%[[VAL_24]], %[[VAL_23]] : [1], [1, 0] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) -> !fir.ref<![[VAR_TYPE]]> {name = "var"}
45+
!CHECK: omp.declare_mapper_info map_entries(%[[VAL_27]], %[[VAL_24]], %[[VAL_23]] : !fir.ref<![[VAR_TYPE]]>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>)
46+
!CHECK: omp.terminator
47+
!CHECK: }
48+
!$omp declare mapper (my_type :: var) map (var, var%values (1:var%num_vals))
49+
end subroutine declare_mapper_1
50+
51+
!--- omp-declare-mapper-2.f90
52+
subroutine declare_mapper_2
53+
integer, parameter :: nvals = 250
2354
type my_type
24-
integer, pointer :: my_buffer
25-
integer :: my_buffer_size
55+
integer :: num_vals
56+
integer, allocatable :: values(:)
57+
end type
58+
59+
type my_type2
60+
type(my_type) :: my_type_var
61+
type(my_type) :: temp
62+
real, dimension(nvals) :: unmapped
63+
real, dimension(nvals) :: arr
2664
end type
27-
!CHECK: %[[MY_VAR:.*]] = fir.alloca ![[VAR_TYPE:.*]] {bindc_name = "my_var"}
28-
!CHECK: %[[MAP_INFO:.*]] = omp.map.info var_ptr(%[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) -> !fir.ref<![[VAR_TYPE]]> {name = "my_var"}
29-
!CHECK: omp.declare_mapper @default_my_type : %[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]> : ![[VAR_TYPE]] map_entries(%[[MAP_INFO]] : !fir.ref<![[VAR_TYPE]]>)
30-
!$omp DECLARE MAPPER(my_type :: my_var) map(tofrom : my_var)
31-
end subroutine
65+
type(my_type2) :: t
66+
real :: x, y(nvals)
67+
!CHECK: omp.declare_mapper @my_mapper : ![[VAR_TYPE:.*]] {
68+
!CHECK: %[[VAL_0:.*]] = fir.alloca ![[VAR_TYPE]] {bindc_name = "v"}
69+
!CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_2Ev"} : (!fir.ref<![[VAR_TYPE]]>) -> (!fir.ref<![[VAR_TYPE]]>, !fir.ref<![[VAR_TYPE]]>)
70+
!CHECK: %[[VAL_2:.*]] = arith.constant 250 : index
71+
!CHECK: %[[VAL_3:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1>
72+
!CHECK: %[[VAL_4:.*]] = hlfir.designate %[[VAL_1]]#0{"arr"} shape %[[VAL_3]] : (!fir.ref<![[VAR_TYPE]]>, !fir.shape<1>) -> !fir.ref<!fir.array<250xf32>>
73+
!CHECK: %[[VAL_5:.*]] = arith.constant 1 : index
74+
!CHECK: %[[VAL_6:.*]] = arith.constant 0 : index
75+
!CHECK: %[[VAL_7:.*]] = arith.subi %[[VAL_2]], %[[VAL_5]] : index
76+
!CHECK: %[[VAL_8:.*]] = omp.map.bounds lower_bound(%[[VAL_6]] : index) upper_bound(%[[VAL_7]] : index) extent(%[[VAL_2]] : index) stride(%[[VAL_5]] : index) start_idx(%[[VAL_5]] : index)
77+
!CHECK: %[[VAL_9:.*]] = omp.map.info var_ptr(%[[VAL_4]] : !fir.ref<!fir.array<250xf32>>, !fir.array<250xf32>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_8]]) -> !fir.ref<!fir.array<250xf32>> {name = "v%[[VAL_10:.*]]"}
78+
!CHECK: %[[VAL_11:.*]] = omp.map.info var_ptr(%[[VAL_12:.*]]#1 : !fir.ref<f32>, f32) map_clauses(tofrom) capture(ByRef) -> !fir.ref<f32> {name = "x"}
79+
!CHECK: %[[VAL_13:.*]] = arith.constant 0 : index
80+
!CHECK: %[[VAL_14:.*]] = arith.constant 1 : index
81+
!CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_16:.*]], %[[VAL_14]] : index
82+
!CHECK: %[[VAL_17:.*]] = omp.map.bounds lower_bound(%[[VAL_13]] : index) upper_bound(%[[VAL_15]] : index) extent(%[[VAL_16]] : index) stride(%[[VAL_14]] : index) start_idx(%[[VAL_14]] : index)
83+
!CHECK: %[[VAL_18:.*]] = omp.map.info var_ptr(%[[VAL_19:.*]]#1 : !fir.ref<!fir.array<250xf32>>, !fir.array<250xf32>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_17]]) -> !fir.ref<!fir.array<250xf32>> {name = "y(:)"}
84+
!CHECK: %[[VAL_20:.*]] = hlfir.designate %[[VAL_1]]#0{"temp"} : (!fir.ref<![[VAR_TYPE]]>) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
85+
!CHECK: %[[VAL_21:.*]] = omp.map.info var_ptr(%[[VAL_20]] : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {name = "v%[[VAL_22:.*]]"}
86+
!CHECK: %[[VAL_23:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) members(%[[VAL_9]], %[[VAL_21]] : [3], [1] : !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<![[VAR_TYPE]]> {name = "v", partial_map = true}
87+
!CHECK: omp.declare_mapper_info map_entries(%[[VAL_11]], %[[VAL_18]], %[[VAL_23]], %[[VAL_9]], %[[VAL_21]] : !fir.ref<f32>, !fir.ref<!fir.array<250xf32>>, !fir.ref<![[VAR_TYPE]]>, !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
88+
!CHECK: omp.terminator
89+
!CHECK: }
90+
!$omp declare mapper (my_mapper : my_type2 :: v) map (v%arr, x, y(:)) &
91+
!$omp& map (alloc : v%temp)
92+
end subroutine declare_mapper_2

0 commit comments

Comments
 (0)