tianshilei1992 created this revision. Herald added subscribers: cfe-commits, guansong, yaxunl. Herald added a project: clang. tianshilei1992 requested review of this revision. Herald added a reviewer: jdoerfert. Herald added a subscriber: sstefan1.
In current implementation, if it requires an outer task, the mapper array will be privatized no matter whether it has mapper. In fact, when there is no mapper, the mapper array only contains number of nullptr. In the libomptarget, the use of mapper array is `if (mappers_array && mappers_array[i])`, which means we can directly set mapper array to nullptr if there is no mapper. This can avoid unnecessary data copy. In this patch, the data privatization will not be emitted if the mapper array is nullptr. When it comes to the emit of task body, the nullptr will be used directly. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D90101 Files: clang/lib/CodeGen/CGOpenMPRuntime.cpp clang/lib/CodeGen/CGStmtOpenMP.cpp
Index: clang/lib/CodeGen/CGStmtOpenMP.cpp =================================================================== --- clang/lib/CodeGen/CGStmtOpenMP.cpp +++ 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<llvm::ConstantPointerNull>( + 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); Index: clang/lib/CodeGen/CGOpenMPRuntime.cpp =================================================================== --- clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ 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<OMPDependClause>(); - 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<OMPDependClause>(); - 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());
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits