@@ -923,32 +923,70 @@ static void genBodyOfTargetOp(
923923 while (!valuesDefinedAbove.empty ()) {
924924 for (mlir::Value val : valuesDefinedAbove) {
925925 mlir::Operation *valOp = val.getDefiningOp ();
926- if (mlir::isMemoryEffectFree (valOp)) {
926+ assert (valOp != nullptr );
927+
928+ // NOTE: We skip BoxDimsOp's as the lesser of two evils is to map the
929+ // indices separately, as the alternative is to eventually map the Box,
930+ // which comes with a fairly large overhead comparatively. We could be
931+ // more robust about this and check using a BackwardsSlice to see if we
932+ // run the risk of mapping a box.
933+ if (mlir::isMemoryEffectFree (valOp) &&
934+ !mlir::isa<fir::BoxDimsOp>(valOp)) {
927935 mlir::Operation *clonedOp = valOp->clone ();
928936 entryBlock->push_front (clonedOp);
929- val.replaceUsesWithIf (clonedOp->getResult (0 ),
930- [entryBlock](mlir::OpOperand &use) {
931- return use.getOwner ()->getBlock () == entryBlock;
932- });
937+
938+ auto replace = [entryBlock](mlir::OpOperand &use) {
939+ return use.getOwner ()->getBlock () == entryBlock;
940+ };
941+
942+ valOp->getResults ().replaceUsesWithIf (clonedOp->getResults (), replace);
943+ valOp->replaceUsesWithIf (clonedOp, replace);
933944 } else {
934945 auto savedIP = firOpBuilder.getInsertionPoint ();
935946 firOpBuilder.setInsertionPointAfter (valOp);
936947 auto copyVal =
937948 firOpBuilder.createTemporary (val.getLoc (), val.getType ());
938949 firOpBuilder.createStoreWithConvert (copyVal.getLoc (), val, copyVal);
939950
940- llvm::SmallVector<mlir::Value> bounds;
951+ lower::AddrAndBoundsInfo info = lower::getDataOperandBaseAddr (
952+ firOpBuilder, val, /* isOptional=*/ false , val.getLoc ());
953+ llvm::SmallVector<mlir::Value> bounds =
954+ Fortran::lower::genImplicitBoundsOps<mlir::omp::MapBoundsOp,
955+ mlir::omp::MapBoundsType>(
956+ firOpBuilder, info,
957+ hlfir::translateToExtendedValue (val.getLoc (), firOpBuilder,
958+ hlfir::Entity{val})
959+ .first ,
960+ /* dataExvIsAssumedSize=*/ false , val.getLoc ());
961+
941962 std::stringstream name;
942963 firOpBuilder.setInsertionPoint (targetOp);
964+
965+ llvm::omp::OpenMPOffloadMappingFlags mapFlag =
966+ llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_IMPLICIT;
967+ mlir::omp::VariableCaptureKind captureKind =
968+ mlir::omp::VariableCaptureKind::ByRef;
969+
970+ mlir::Type eleType = copyVal.getType ();
971+ if (auto refType =
972+ mlir::dyn_cast<fir::ReferenceType>(copyVal.getType ()))
973+ eleType = refType.getElementType ();
974+
975+ if (fir::isa_trivial (eleType) || fir::isa_char (eleType)) {
976+ captureKind = mlir::omp::VariableCaptureKind::ByCopy;
977+ } else if (!fir::isa_builtin_cptr_type (eleType)) {
978+ mapFlag |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO;
979+ }
980+
943981 mlir::Value mapOp = createMapInfoOp (
944982 firOpBuilder, copyVal.getLoc (), copyVal,
945983 /* varPtrPtr=*/ mlir::Value{}, name.str (), bounds,
946984 /* members=*/ llvm::SmallVector<mlir::Value>{},
947985 /* membersIndex=*/ mlir::ArrayAttr{},
948986 static_cast <
949987 std::underlying_type_t <llvm::omp::OpenMPOffloadMappingFlags>>(
950- llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_IMPLICIT ),
951- mlir::omp::VariableCaptureKind::ByCopy , copyVal.getType ());
988+ mapFlag ),
989+ captureKind , copyVal.getType ());
952990
953991 // Get the index of the first non-map argument before modifying mapVars,
954992 // then append an element to mapVars and an associated entry block
@@ -2586,6 +2624,10 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
25862624// ===----------------------------------------------------------------------===//
25872625// OpenMPDeclarativeConstruct visitors
25882626// ===----------------------------------------------------------------------===//
2627+ static void genOMP (lower::AbstractConverter &converter, lower::SymMap &symTable,
2628+ semantics::SemanticsContext &semaCtx,
2629+ lower::pft::Evaluation &eval,
2630+ const parser::OpenMPUtilityConstruct &);
25892631
25902632static void
25912633genOMP (lower::AbstractConverter &converter, lower::SymMap &symTable,
@@ -2907,8 +2949,8 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
29072949static void genOMP (lower::AbstractConverter &converter, lower::SymMap &symTable,
29082950 semantics::SemanticsContext &semaCtx,
29092951 lower::pft::Evaluation &eval,
2910- const parser::OpenMPErrorConstruct &) {
2911- TODO (converter.getCurrentLocation (), " OpenMPErrorConstruct " );
2952+ const parser::OpenMPUtilityConstruct &) {
2953+ TODO (converter.getCurrentLocation (), " OpenMPUtilityConstruct " );
29122954}
29132955
29142956static void genOMP (lower::AbstractConverter &converter, lower::SymMap &symTable,
0 commit comments