@@ -7090,11 +7090,19 @@ class MappableExprsHandler {
70907090 Address LB = Address::invalid();
70917091 bool IsArraySection = false;
70927092 bool HasCompleteRecord = false;
7093- // ATTACH information for delayed processing
7093+ };
7094+
7095+ /// A struct to store the attach pointer and pointee information, to be used
7096+ /// when emitting an attach entry.
7097+ struct AttachInfoTy {
70947098 Address AttachPtrAddr = Address::invalid();
70957099 Address AttachPteeAddr = Address::invalid();
70967100 const ValueDecl *AttachPtrDecl = nullptr;
70977101 const Expr *AttachMapExpr = nullptr;
7102+
7103+ bool isValid() const {
7104+ return AttachPtrAddr.isValid() && AttachPteeAddr.isValid();
7105+ }
70987106 };
70997107
71007108 /// Check if there's any component list where the attach pointer expression
@@ -7381,12 +7389,14 @@ class MappableExprsHandler {
73817389 return ConstLength.getSExtValue() != 1;
73827390 }
73837391
7384- /// Utility function to add an ATTACH entry to the CombinedInfo structure.
7385- /// Generates an ATTACH entry: &pointer, &pointer[idx], sizeof(pointer),
7386- /// ATTACH
7387- void addAttachEntry(CodeGenFunction &CGF, MapCombinedInfoTy &CombinedInfo,
7388- Address AttachBaseAddr, Address AttachFirstElemAddr,
7389- const ValueDecl *BaseDecl, const Expr *MapExpr) const {
7392+ /// Emit an attach entry into \p CombinedInfo, using the information from \p
7393+ /// AttachInfo. For example, for a map of form `int *p; ... map(p[1:10])`,
7394+ /// an attach entry has the following form:
7395+ /// &p, &p[1], sizeof(void*), ATTACH
7396+ void emitAttachEntry(CodeGenFunction &CGF, MapCombinedInfoTy &CombinedInfo,
7397+ const AttachInfoTy &AttachInfo) const {
7398+ assert(AttachInfo.isValid() &&
7399+ "Expected valid attach pointer/pointee information!");
73907400
73917401 // Size is the size of the pointer itself - use pointer size, not BaseDecl
73927402 // size
@@ -7398,11 +7408,14 @@ class MappableExprsHandler {
73987408 8),
73997409 CGF.Int64Ty, /*isSigned=*/true);
74007410
7401- CombinedInfo.Exprs.emplace_back(BaseDecl, MapExpr);
7402- CombinedInfo.BasePointers.push_back(AttachBaseAddr.emitRawPointer(CGF));
7411+ CombinedInfo.Exprs.emplace_back(AttachInfo.AttachPtrDecl,
7412+ AttachInfo.AttachMapExpr);
7413+ CombinedInfo.BasePointers.push_back(
7414+ AttachInfo.AttachPtrAddr.emitRawPointer(CGF));
74037415 CombinedInfo.DevicePtrDecls.push_back(nullptr);
74047416 CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
7405- CombinedInfo.Pointers.push_back(AttachFirstElemAddr.emitRawPointer(CGF));
7417+ CombinedInfo.Pointers.push_back(
7418+ AttachInfo.AttachPteeAddr.emitRawPointer(CGF));
74067419 CombinedInfo.Sizes.push_back(PointerSize);
74077420 CombinedInfo.Types.push_back(OpenMPOffloadMappingFlags::OMP_MAP_ATTACH);
74087421 CombinedInfo.Mappers.push_back(nullptr);
@@ -7524,7 +7537,8 @@ class MappableExprsHandler {
75247537 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
75257538 MapCombinedInfoTy &CombinedInfo,
75267539 MapCombinedInfoTy &StructBaseCombinedInfo,
7527- StructRangeInfoTy &PartialStruct, bool IsFirstComponentList,
7540+ StructRangeInfoTy &PartialStruct, AttachInfoTy &AttachInfo,
7541+ bool IsFirstComponentList,
75287542 bool IsImplicit, bool GenerateAllInfoForClauses,
75297543 const ValueDecl *Mapper = nullptr, bool ForDeviceAddr = false,
75307544 const ValueDecl *BaseDecl = nullptr, const Expr *MapExpr = nullptr,
@@ -8255,25 +8269,12 @@ class MappableExprsHandler {
82558269 if (!EncounteredME)
82568270 PartialStruct.HasCompleteRecord = true;
82578271
8258- // Add ATTACH entries for pointer-attachment: delay if PartialStruct is
8259- // being populated, otherwise add immediately.
8272+ // Populate ATTACH information for later processing by emitAttachEntry.
82608273 if (shouldEmitAttachEntry(AttachPtrExpr, BaseDecl, CGF, CurDir)) {
8261- Address AttachPteeBeginAddr = FinalLowestElem;
8262-
8263- if (PartialStruct.Base.isValid()) {
8264- // We're populating PartialStruct, delay ATTACH entry addition until
8265- // after emitCombinedEntry.
8266- PartialStruct.AttachPtrAddr = AttachPtrAddr;
8267- PartialStruct.AttachPteeAddr = AttachPteeBeginAddr;
8268- PartialStruct.AttachPtrDecl = BaseDecl;
8269- PartialStruct.AttachMapExpr = MapExpr;
8270- } else if (IsMappingWholeStruct) {
8271- addAttachEntry(CGF, StructBaseCombinedInfo, AttachPtrAddr,
8272- AttachPteeBeginAddr, BaseDecl, MapExpr);
8273- } else {
8274- addAttachEntry(CGF, CombinedInfo, AttachPtrAddr, AttachPteeBeginAddr,
8275- BaseDecl, MapExpr);
8276- }
8274+ AttachInfo.AttachPtrAddr = AttachPtrAddr;
8275+ AttachInfo.AttachPteeAddr = FinalLowestElem;
8276+ AttachInfo.AttachPtrDecl = BaseDecl;
8277+ AttachInfo.AttachMapExpr = MapExpr;
82778278 }
82788279
82798280 if (!IsNonContiguous)
@@ -8965,6 +8966,7 @@ class MappableExprsHandler {
89658966 continue;
89668967
89678968 StructRangeInfoTy PartialStruct;
8969+ AttachInfoTy AttachInfo;
89688970 MapCombinedInfoTy GroupCurInfo;
89698971 // Current group's struct base information:
89708972 MapCombinedInfoTy GroupStructBaseCurInfo;
@@ -8978,7 +8980,7 @@ class MappableExprsHandler {
89788980 L.Components.back().isNonContiguous();
89798981 generateInfoForComponentList(
89808982 L.MapType, L.MapModifiers, L.MotionModifiers, L.Components,
8981- GroupCurInfo, GroupStructBaseCurInfo, PartialStruct,
8983+ GroupCurInfo, GroupStructBaseCurInfo, PartialStruct, AttachInfo,
89828984 /*IsFirstComponentList=*/false, L.IsImplicit,
89838985 /*GenerateAllInfoForClauses*/ true, L.Mapper, L.ForDeviceAddr, VD,
89848986 L.VarRef, /*OverlappedElements*/ {});
@@ -9033,12 +9035,10 @@ class MappableExprsHandler {
90339035
90349036 // If there is an entry in PartialStruct it means we have a struct with
90359037 // individual members mapped. Emit an extra combined entry.
9036- MapCombinedInfoTy AttachCombinedInfo;
90379038 if (PartialStruct.Base.isValid()) {
90389039 GroupUnionCurInfo.NonContigInfo.Dims.push_back(0);
90399040 std::optional<size_t> CombinedEntryIndex = emitCombinedEntry(
9040- CurInfo, AttachCombinedInfo, GroupUnionCurInfo.Types,
9041- PartialStruct,
9041+ CurInfo, GroupUnionCurInfo.Types, PartialStruct, AttachInfo,
90429042 /*IsMapThis*/ !VD, OMPBuilder, VD,
90439043 /*OffsetForMemberOfFlag=*/CombinedInfo.BasePointers.size(),
90449044 /*NotTargetParam=*/true);
@@ -9052,7 +9052,8 @@ class MappableExprsHandler {
90529052 // Append this group's results to the overall CurInfo in the correct
90539053 // order: combined-entry -> original-field-entries -> attach-entry
90549054 CurInfo.append(GroupUnionCurInfo);
9055- CurInfo.append(AttachCombinedInfo);
9055+ if (AttachInfo.isValid())
9056+ emitAttachEntry(CGF, CurInfo, AttachInfo);
90569057
90579058 IsFirstGroup = false;
90589059 }
@@ -9208,29 +9209,22 @@ class MappableExprsHandler {
92089209 /// Generate code for the combined entry if we have a partially mapped struct
92099210 /// and take care of the mapping flags of the arguments corresponding to
92109211 /// individual struct members.
9211- /// AttachCombinedInfo will be populated with ATTACH entries if
9212+ /// If a valid \p AttachInfo exists, its pointee addr will be updated to point
9213+ /// to the combined-entry's begin address, if emitted.
92129214 /// \p PartialStruct contains attach base-pointer information.
92139215 /// \returns The index of the combined entry if one was added, std::nullopt
92149216 /// otherwise.
92159217 std::optional<size_t> emitCombinedEntry(
9216- MapCombinedInfoTy &CombinedInfo, MapCombinedInfoTy &AttachCombinedInfo ,
9217- MapFlagsArrayTy &CurTypes, const StructRangeInfoTy &PartialStruct,
9218+ MapCombinedInfoTy &CombinedInfo, MapFlagsArrayTy &CurTypes ,
9219+ const StructRangeInfoTy &PartialStruct, AttachInfoTy &AttachInfo ,
92189220 bool IsMapThis, llvm::OpenMPIRBuilder &OMPBuilder, const ValueDecl *VD,
92199221 unsigned OffsetForMemberOfFlag, bool NotTargetParams) const {
92209222 if (CurTypes.size() == 1 &&
92219223 ((CurTypes.back() & OpenMPOffloadMappingFlags::OMP_MAP_MEMBER_OF) !=
92229224 OpenMPOffloadMappingFlags::OMP_MAP_MEMBER_OF) &&
9223- !PartialStruct.IsArraySection) {
9224- // Even if we are not creating a combined-entry, we need to process any
9225- // previously delayed ATTACH entries.
9226- if (PartialStruct.AttachPtrAddr.isValid() &&
9227- PartialStruct.AttachPteeAddr.isValid())
9228- addAttachEntry(CGF, AttachCombinedInfo, PartialStruct.AttachPtrAddr,
9229- PartialStruct.AttachPteeAddr,
9230- PartialStruct.AttachPtrDecl,
9231- PartialStruct.AttachMapExpr);
9225+ !PartialStruct.IsArraySection)
92329226 return std::nullopt;
9233- }
9227+
92349228 Address LBAddr = PartialStruct.LowestElem.second;
92359229 Address HBAddr = PartialStruct.HighestElem.second;
92369230 if (PartialStruct.HasCompleteRecord) {
@@ -9326,12 +9320,17 @@ class MappableExprsHandler {
93269320 // be done. So, for instance, if we have:
93279321 // S *ps;
93289322 // ... map(ps->a, ps->b)
9329- // We won't emit separate ATTACH entries for the two list items, just one.
9330- if (PartialStruct.AttachPtrAddr.isValid() &&
9331- PartialStruct.AttachPteeAddr.isValid())
9332- addAttachEntry(CGF, AttachCombinedInfo, PartialStruct.AttachPtrAddr,
9333- LBAddr, PartialStruct.AttachPtrDecl,
9334- PartialStruct.AttachMapExpr);
9323+ // When we are emitting a combined entry. If AttachInfo is valid,
9324+ // update the pointee address to point to the begin address of the combined
9325+ // entry. This ensures that if we have multiple maps like:
9326+ // `map(ps->a, ps->b)`, we still get a single ATTACH entry, like:
9327+ //
9328+ // &ps[0], &ps->a, sizeof(ps->a to ps->b), ALLOC // combined-entry
9329+ // &ps[0], &ps->a, sizeof(ps->a), TO | FROM
9330+ // &ps[0], &ps->b, sizeof(ps->b), TO | FROM
9331+ // &ps, &ps->a, sizeof(void*), ATTACH // Use combined-entry's LB
9332+ if (AttachInfo.isValid())
9333+ AttachInfo.AttachPteeAddr = LBAddr;
93359334
93369335 return CombinedEntryIndex;
93379336 }
@@ -9738,31 +9737,32 @@ class MappableExprsHandler {
97389737 bool IsEligibleForTargetParamFlag) {
97399738 MapCombinedInfoTy CurInfoForComponentLists;
97409739 StructRangeInfoTy PartialStruct;
9740+ AttachInfoTy AttachInfo;
97419741
97429742 if (DeclComponentListsFromClauses.empty())
97439743 return;
97449744
97459745 generateInfoForCaptureFromComponentLists(
97469746 VD, DeclComponentListsFromClauses, CurInfoForComponentLists,
9747- PartialStruct, IsEligibleForTargetParamFlag);
9747+ PartialStruct, AttachInfo, IsEligibleForTargetParamFlag);
97489748
97499749 // If there is an entry in PartialStruct it means we have a
97509750 // struct with individual members mapped. Emit an extra combined
97519751 // entry.
9752- MapCombinedInfoTy AttachCombinedInfo;
97539752 if (PartialStruct.Base.isValid()) {
97549753 CurCaptureVarInfo.append(PartialStruct.PreliminaryMapData);
97559754 (void)emitCombinedEntry(
9756- CurCaptureVarInfo, AttachCombinedInfo ,
9757- CurInfoForComponentLists.Types, PartialStruct ,
9758- Cap->capturesThis(), OMPBuilder, nullptr, OffsetForMemberOfFlag,
9755+ CurCaptureVarInfo, CurInfoForComponentLists.Types ,
9756+ PartialStruct, AttachInfo, Cap->capturesThis(), OMPBuilder ,
9757+ nullptr, OffsetForMemberOfFlag,
97599758 /*NotTargetParams*/ !IsEligibleForTargetParamFlag);
97609759 }
97619760
97629761 // We do the appends to get the entries in the following order:
9763- // combined-entry -> individual-field-entries -> attach-entry
9762+ // combined-entry -> individual-field-entries -> attach-entry,
97649763 CurCaptureVarInfo.append(CurInfoForComponentLists);
9765- CurCaptureVarInfo.append(AttachCombinedInfo);
9764+ if (AttachInfo.isValid())
9765+ emitAttachEntry(CGF, CurCaptureVarInfo, AttachInfo);
97669766 };
97679767
97689768 // Group component lists by their AttachPtrExpr and process them in order
@@ -9825,7 +9825,7 @@ class MappableExprsHandler {
98259825 void generateInfoForCaptureFromComponentLists(
98269826 const ValueDecl *VD, ArrayRef<MapData> DeclComponentLists,
98279827 MapCombinedInfoTy &CurComponentListInfo, StructRangeInfoTy &PartialStruct,
9828- bool IsListEligibleForTargetParamFlag) const {
9828+ AttachInfoTy &AttachInfo, bool IsListEligibleForTargetParamFlag) const {
98299829 // Find overlapping elements (including the offset from the base element).
98309830 llvm::SmallDenseMap<
98319831 const MapData *,
@@ -9965,8 +9965,8 @@ class MappableExprsHandler {
99659965 OverlappedComponents = Pair.getSecond();
99669966 generateInfoForComponentList(
99679967 MapType, MapModifiers, {}, Components, CurComponentListInfo,
9968- StructBaseCombinedInfo, PartialStruct, AddTargetParamFlag, IsImplicit ,
9969- /*GenerateAllInfoForClauses*/ false, Mapper,
9968+ StructBaseCombinedInfo, PartialStruct, AttachInfo, AddTargetParamFlag ,
9969+ IsImplicit, /*GenerateAllInfoForClauses*/ false, Mapper,
99709970 /*ForDeviceAddr=*/false, VD, VarRef, OverlappedComponents);
99719971 AddTargetParamFlag = false;
99729972 }
@@ -9984,9 +9984,9 @@ class MappableExprsHandler {
99849984 if (It == OverlappedData.end())
99859985 generateInfoForComponentList(
99869986 MapType, MapModifiers, {}, Components, CurComponentListInfo,
9987- StructBaseCombinedInfo, PartialStruct, AddTargetParamFlag ,
9988- IsImplicit, /*GenerateAllInfoForClauses*/ false, Mapper ,
9989- /*ForDeviceAddr=*/false, VD, VarRef,
9987+ StructBaseCombinedInfo, PartialStruct, AttachInfo ,
9988+ AddTargetParamFlag, IsImplicit, /*GenerateAllInfoForClauses*/ false,
9989+ Mapper, /*ForDeviceAddr=*/false, VD, VarRef,
99909990 /*OverlappedElements*/ {});
99919991 AddTargetParamFlag = false;
99929992 }
0 commit comments