diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp index ca96e2618f506b..43982657471289 100644 --- a/flang/lib/Lower/OpenMP/OpenMP.cpp +++ b/flang/lib/Lower/OpenMP/OpenMP.cpp @@ -179,10 +179,20 @@ class HostClausesInsertionGuard { mlir::IRMapping mapper; builder.setInsertionPoint(escapingOperand->getOwner()); - mlir::Operation *lastSliceOp; + mlir::Operation *lastSliceOp = nullptr; + + for (auto *op : backwardSlice) { + // DeclareOps need special handling by searching for the corresponding ops + // in the host. Therefore, do not clone them since this special handling + // is done later in the fix-up process. + // + // TODO this might need a more elaborate handling in the future but for + // now this seems sufficient for our purposes. + if (llvm::isa(op)) + break; - for (auto *op : backwardSlice) lastSliceOp = builder.clone(*op, mapper); + } builder.restoreInsertionPoint(ip); return lastSliceOp; @@ -201,6 +211,9 @@ class HostClausesInsertionGuard { "a block argument)"); mlir::Operation *lastSliceOp = cloneOperandSliceOutsideTargetOp(operand); + if (lastSliceOp == nullptr) + continue; + // Find the index of the operand in the list of results produced by its // defining op. unsigned operandResultIdx = 0; diff --git a/flang/test/Lower/OpenMP/target-do-loop-control-exprs.f90 b/flang/test/Lower/OpenMP/target-do-loop-control-exprs.f90 index 027251801ff5df..8f4813bb27cd12 100644 --- a/flang/test/Lower/OpenMP/target-do-loop-control-exprs.f90 +++ b/flang/test/Lower/OpenMP/target-do-loop-control-exprs.f90 @@ -33,3 +33,32 @@ subroutine foo(upper_bound) ! CHECK: omp.target ! CHECK: } + +subroutine foo_with_dummy_arg(nodes) + implicit none + integer, intent(inout) :: nodes( : ) + integer :: i + + !$omp target teams distribute parallel do + do i = 1, ubound(nodes, 1) + nodes(i) = i + end do + !$omp end target teams distribute parallel do +end subroutine + +! CHECK: func.func @_QPfoo_with_dummy_arg(%[[FUNC_ARG:.*]]: !fir.box> {fir.bindc_name = "nodes"}) { + +! CHECK: %[[ARR_DECL:.*]]:2 = hlfir.declare %[[FUNC_ARG]] dummy_scope + +! CHECK: omp.map.info +! CHECK: omp.map.info +! CHECK: omp.map.info + +! Verify that we get the box dims of the host array declaration not the target +! one. + +! CHECK: fir.box_dims %[[ARR_DECL]] + +! CHECK: omp.target + +! CHECK: }