Skip to content

Commit e8b67b7

Browse files
authored
[flang][OpenMP] Further handling of target ... do fixup logic (#115)
Cloning `hlfir.declare` ops during fix up can result in type mismatches. For example, a `fir.ref<fir.array<...>>` variable is mapped a s `fir.box<fir.array<...>>` which results in the fix up logic creating invalid IR. This PR fixes the issue by avoiding cloning `hlfir.declare` ops in the first place since they are specially handled during the fix-up process.
1 parent 1508b2b commit e8b67b7

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,20 @@ class HostClausesInsertionGuard {
179179

180180
mlir::IRMapping mapper;
181181
builder.setInsertionPoint(escapingOperand->getOwner());
182-
mlir::Operation *lastSliceOp;
182+
mlir::Operation *lastSliceOp = nullptr;
183+
184+
for (auto *op : backwardSlice) {
185+
// DeclareOps need special handling by searching for the corresponding ops
186+
// in the host. Therefore, do not clone them since this special handling
187+
// is done later in the fix-up process.
188+
//
189+
// TODO this might need a more elaborate handling in the future but for
190+
// now this seems sufficient for our purposes.
191+
if (llvm::isa<hlfir::DeclareOp>(op))
192+
break;
183193

184-
for (auto *op : backwardSlice)
185194
lastSliceOp = builder.clone(*op, mapper);
195+
}
186196

187197
builder.restoreInsertionPoint(ip);
188198
return lastSliceOp;
@@ -201,6 +211,9 @@ class HostClausesInsertionGuard {
201211
"a block argument)");
202212
mlir::Operation *lastSliceOp = cloneOperandSliceOutsideTargetOp(operand);
203213

214+
if (lastSliceOp == nullptr)
215+
continue;
216+
204217
// Find the index of the operand in the list of results produced by its
205218
// defining op.
206219
unsigned operandResultIdx = 0;

flang/test/Lower/OpenMP/target-do-loop-control-exprs.f90

+29
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,32 @@ subroutine foo(upper_bound)
3333
! CHECK: omp.target
3434

3535
! CHECK: }
36+
37+
subroutine foo_with_dummy_arg(nodes)
38+
implicit none
39+
integer, intent(inout) :: nodes( : )
40+
integer :: i
41+
42+
!$omp target teams distribute parallel do
43+
do i = 1, ubound(nodes, 1)
44+
nodes(i) = i
45+
end do
46+
!$omp end target teams distribute parallel do
47+
end subroutine
48+
49+
! CHECK: func.func @_QPfoo_with_dummy_arg(%[[FUNC_ARG:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "nodes"}) {
50+
51+
! CHECK: %[[ARR_DECL:.*]]:2 = hlfir.declare %[[FUNC_ARG]] dummy_scope
52+
53+
! CHECK: omp.map.info
54+
! CHECK: omp.map.info
55+
! CHECK: omp.map.info
56+
57+
! Verify that we get the box dims of the host array declaration not the target
58+
! one.
59+
60+
! CHECK: fir.box_dims %[[ARR_DECL]]
61+
62+
! CHECK: omp.target
63+
64+
! CHECK: }

0 commit comments

Comments
 (0)