diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp --- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -8870,10 +8870,8 @@ /// Additional arguments for emitOffloadingArraysArgument function. struct ArgumentsOptions { bool ForEndCall = false; - bool IsTask = false; ArgumentsOptions() = default; - ArgumentsOptions(bool ForEndCall, bool IsTask) - : ForEndCall(ForEndCall), IsTask(IsTask) {} + ArgumentsOptions(bool ForEndCall) : ForEndCall(ForEndCall) {} }; } // namespace @@ -8909,9 +8907,9 @@ : Info.MapTypesArray, /*Idx0=*/0, /*Idx1=*/0); - // Always emit the mapper array address in case of a target task for - // privatization. - if (!Options.IsTask && !Info.HasMapper) + // If there is no user-defined mapper, set the mapper array to nullptr to + // avoid an unnecessary data privatization + if (!Info.HasMapper) MappersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); else MappersArrayArg = @@ -9664,11 +9662,9 @@ TargetDataInfo Info; // Fill up the arrays and create the arguments. emitOffloadingArrays(CGF, CombinedInfo, Info); - bool HasDependClauses = D.hasClausesOfKind(); - emitOffloadingArraysArgument(CGF, Info.BasePointersArray, - Info.PointersArray, Info.SizesArray, - Info.MapTypesArray, Info.MappersArray, Info, - {/*ForEndTask=*/false, HasDependClauses}); + emitOffloadingArraysArgument( + CGF, Info.BasePointersArray, Info.PointersArray, Info.SizesArray, + Info.MapTypesArray, Info.MappersArray, Info, {/*ForEndTask=*/false}); InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; InputInfo.BasePointersArray = Address(Info.BasePointersArray, CGM.getPointerAlign()); @@ -10319,8 +10315,7 @@ llvm::Value *MappersArrayArg = nullptr; emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, SizesArrayArg, MapTypesArrayArg, - MappersArrayArg, Info, - {/*ForEndCall=*/true, /*IsTask=*/false}); + MappersArrayArg, Info, {/*ForEndCall=*/true}); // Emit device ID if any. llvm::Value *DeviceID = nullptr; @@ -10519,10 +10514,9 @@ // Fill up the arrays and create the arguments. emitOffloadingArrays(CGF, CombinedInfo, Info); bool HasDependClauses = D.hasClausesOfKind(); - emitOffloadingArraysArgument(CGF, Info.BasePointersArray, - Info.PointersArray, Info.SizesArray, - Info.MapTypesArray, Info.MappersArray, Info, - {/*ForEndTask=*/false, HasDependClauses}); + emitOffloadingArraysArgument( + CGF, Info.BasePointersArray, Info.PointersArray, Info.SizesArray, + Info.MapTypesArray, Info.MappersArray, Info, {/*ForEndTask=*/false}); InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; InputInfo.BasePointersArray = Address(Info.BasePointersArray, CGM.getPointerAlign()); diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp --- a/clang/lib/CodeGen/CGStmtOpenMP.cpp +++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp @@ -4210,16 +4210,21 @@ /*IndexTypeQuals=*/0); SVD = createImplicitFirstprivateForType(getContext(), Data, SizesType, CD, S.getBeginLoc()); - MVD = createImplicitFirstprivateForType( - getContext(), Data, BaseAndPointerAndMapperType, CD, S.getBeginLoc()); TargetScope.addPrivate( BPVD, [&InputInfo]() { return InputInfo.BasePointersArray; }); TargetScope.addPrivate(PVD, [&InputInfo]() { return InputInfo.PointersArray; }); TargetScope.addPrivate(SVD, [&InputInfo]() { return InputInfo.SizesArray; }); - TargetScope.addPrivate(MVD, - [&InputInfo]() { return InputInfo.MappersArray; }); + // If there is no user-defined mapper, the mapper array will be nullptr. In + // this case, we don't need to privatize it. + if (!dyn_cast_or_null( + InputInfo.MappersArray.getPointer())) { + MVD = createImplicitFirstprivateForType( + getContext(), Data, BaseAndPointerAndMapperType, CD, S.getBeginLoc()); + TargetScope.addPrivate(MVD, + [&InputInfo]() { return InputInfo.MappersArray; }); + } } (void)TargetScope.Privatize(); // Build list of dependences. @@ -4269,8 +4274,10 @@ CGF.GetAddrOfLocalVar(PVD), /*Index=*/0); InputInfo.SizesArray = CGF.Builder.CreateConstArrayGEP( CGF.GetAddrOfLocalVar(SVD), /*Index=*/0); - InputInfo.MappersArray = CGF.Builder.CreateConstArrayGEP( - CGF.GetAddrOfLocalVar(MVD), /*Index=*/0); + // If MVD is nullptr, the mapper array is not privatized + if (!MVD) + InputInfo.MappersArray = CGF.Builder.CreateConstArrayGEP( + CGF.GetAddrOfLocalVar(MVD), /*Index=*/0); } Action.Enter(CGF);