Skip to content

Commit 69c28ef

Browse files
committed
merge main into amd-staging
brings in : [OpenMP][NFC] Move mapping related code into OpenMP/Mapping.cpp (llvm#75239) Change-Id: I55c8d32fd1e96d0b769ac276aa9b20eac0cf0f8b
2 parents 545d5ed + fe6f137 commit 69c28ef

File tree

7 files changed

+677
-645
lines changed

7 files changed

+677
-645
lines changed

openmp/libomptarget/include/ExclusiveAccess.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#ifndef OMPTARGET_EXCLUSIVE_ACCESS
1212
#define OMPTARGET_EXCLUSIVE_ACCESS
1313

14+
#include <cassert>
1415
#include <cstddef>
1516
#include <cstdint>
1617
#include <mutex>

openmp/libomptarget/include/OpenMP/Mapping.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef OMPTARGET_OPENMP_MAPPING_H
1414
#define OMPTARGET_OPENMP_MAPPING_H
1515

16+
#include "ExclusiveAccess.h"
1617
#include "Shared/EnvironmentVar.h"
1718
#include "omptarget.h"
1819

@@ -446,4 +447,94 @@ int targetDataUpdate(ident_t *Loc, DeviceTy &Device, int32_t ArgNum,
446447
void **ArgMappers, AsyncInfoTy &AsyncInfo,
447448
bool FromMapper = false);
448449

450+
struct MappingInfoTy {
451+
MappingInfoTy(DeviceTy &Device) : Device(Device) {}
452+
453+
/// Host data to device map type with a wrapper key indirection that allows
454+
/// concurrent modification of the entries without invalidating the underlying
455+
/// entries.
456+
using HostDataToTargetListTy =
457+
std::set<HostDataToTargetMapKeyTy, std::less<>>;
458+
459+
/// The HDTTMap is a protected object that can only be accessed by one thread
460+
/// at a time.
461+
ProtectedObj<HostDataToTargetListTy> HostDataToTargetMap;
462+
463+
/// The type used to access the HDTT map.
464+
using HDTTMapAccessorTy = decltype(HostDataToTargetMap)::AccessorTy;
465+
466+
/// Lookup the mapping of \p HstPtrBegin in \p HDTTMap. The accessor ensures
467+
/// exclusive access to the HDTT map.
468+
LookupResult lookupMapping(HDTTMapAccessorTy &HDTTMap, void *HstPtrBegin,
469+
int64_t Size,
470+
HostDataToTargetTy *OwnedTPR = nullptr);
471+
472+
/// Get the target pointer based on host pointer begin and base. If the
473+
/// mapping already exists, the target pointer will be returned directly. In
474+
/// addition, if required, the memory region pointed by \p HstPtrBegin of size
475+
/// \p Size will also be transferred to the device. If the mapping doesn't
476+
/// exist, and if unified shared memory is not enabled, a new mapping will be
477+
/// created and the data will also be transferred accordingly. nullptr will be
478+
/// returned because of any of following reasons:
479+
/// - Data allocation failed;
480+
/// - The user tried to do an illegal mapping;
481+
/// - Data transfer issue fails.
482+
TargetPointerResultTy getTargetPointer(
483+
HDTTMapAccessorTy &HDTTMap, void *HstPtrBegin, void *HstPtrBase,
484+
int64_t TgtPadding, int64_t Size, map_var_info_t HstPtrName,
485+
bool HasFlagTo, bool HasFlagAlways, bool IsImplicit, bool UpdateRefCount,
486+
bool HasCloseModifier, bool HasPresentModifier, bool HasHoldModifier,
487+
AsyncInfoTy &AsyncInfo, HostDataToTargetTy *OwnedTPR = nullptr,
488+
bool ReleaseHDTTMap = true);
489+
490+
/// Return the target pointer for \p HstPtrBegin in \p HDTTMap. The accessor
491+
/// ensures exclusive access to the HDTT map.
492+
void *getTgtPtrBegin(HDTTMapAccessorTy &HDTTMap, void *HstPtrBegin,
493+
int64_t Size);
494+
495+
/// Return the target pointer begin (where the data will be moved).
496+
/// Used by targetDataBegin, targetDataEnd, targetDataUpdate and target.
497+
/// - \p UpdateRefCount and \p UseHoldRefCount controls which and if the entry
498+
/// reference counters will be decremented.
499+
/// - \p MustContain enforces that the query must not extend beyond an already
500+
/// mapped entry to be valid.
501+
/// - \p ForceDelete deletes the entry regardless of its reference counting
502+
/// (unless it is infinite).
503+
/// - \p FromDataEnd tracks the number of threads referencing the entry at
504+
/// targetDataEnd for delayed deletion purpose.
505+
[[nodiscard]] TargetPointerResultTy
506+
getTgtPtrBegin(void *HstPtrBegin, int64_t Size, bool UpdateRefCount,
507+
bool UseHoldRefCount, bool MustContain = false,
508+
bool ForceDelete = false, bool FromDataEnd = false);
509+
510+
/// Remove the \p Entry from the data map. Expect the entry's total reference
511+
/// count to be zero and the caller thread to be the last one using it. \p
512+
/// HDTTMap ensure the caller holds exclusive access and can modify the map.
513+
/// Return \c OFFLOAD_SUCCESS if the map entry existed, and return \c
514+
/// OFFLOAD_FAIL if not. It is the caller's responsibility to skip calling
515+
/// this function if the map entry is not expected to exist because \p
516+
/// HstPtrBegin uses shared memory.
517+
[[nodiscard]] int eraseMapEntry(HDTTMapAccessorTy &HDTTMap,
518+
HostDataToTargetTy *Entry, int64_t Size);
519+
520+
/// Deallocate the \p Entry from the device memory and delete it. Return \c
521+
/// OFFLOAD_SUCCESS if the deallocation operations executed successfully, and
522+
/// return \c OFFLOAD_FAIL otherwise.
523+
[[nodiscard]] int deallocTgtPtrAndEntry(HostDataToTargetTy *Entry,
524+
int64_t Size);
525+
526+
int associatePtr(void *HstPtrBegin, void *TgtPtrBegin, int64_t Size);
527+
int disassociatePtr(void *HstPtrBegin);
528+
529+
/// Print information about the transfer from \p HstPtr to \p TgtPtr (or vice
530+
/// versa if \p H2D is false). If there is an existing mapping, or if \p Entry
531+
/// is set, the associated metadata will be printed as well.
532+
void printCopyInfo(void *TgtPtr, void *HstPtr, int64_t Size, bool H2D,
533+
HostDataToTargetTy *Entry,
534+
MappingInfoTy::HDTTMapAccessorTy *HDTTMapPtr);
535+
536+
private:
537+
DeviceTy &Device;
538+
};
539+
449540
#endif // OMPTARGET_OPENMP_MAPPING_H

openmp/libomptarget/include/device.h

Lines changed: 15 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,6 @@ struct DeviceTy {
5656

5757
bool HasMappedGlobalData = false;
5858

59-
/// Host data to device map type with a wrapper key indirection that allows
60-
/// concurrent modification of the entries without invalidating the underlying
61-
/// entries.
62-
using HostDataToTargetListTy =
63-
std::set<HostDataToTargetMapKeyTy, std::less<>>;
64-
65-
/// The HDTTMap is a protected object that can only be accessed by one thread
66-
/// at a time.
67-
ProtectedObj<HostDataToTargetListTy> HostDataToTargetMap;
68-
69-
/// The type used to access the HDTT map.
70-
using HDTTMapAccessorTy = decltype(HostDataToTargetMap)::AccessorTy;
71-
7259
PendingCtorsDtorsPerLibrary PendingCtorsDtors;
7360

7461
std::mutex PendingGlobalsMtx;
@@ -87,71 +74,8 @@ struct DeviceTy {
8774
/// Try to initialize the device and return any failure.
8875
llvm::Error init();
8976

90-
// Return true if data can be copied to DstDevice directly
91-
bool isDataExchangable(const DeviceTy &DstDevice);
92-
93-
/// Lookup the mapping of \p HstPtrBegin in \p HDTTMap. The accessor ensures
94-
/// exclusive access to the HDTT map.
95-
LookupResult lookupMapping(HDTTMapAccessorTy &HDTTMap, void *HstPtrBegin,
96-
int64_t Size,
97-
HostDataToTargetTy *OwnedTPR = nullptr);
98-
99-
/// Get the target pointer based on host pointer begin and base. If the
100-
/// mapping already exists, the target pointer will be returned directly. In
101-
/// addition, if required, the memory region pointed by \p HstPtrBegin of size
102-
/// \p Size will also be transferred to the device. If the mapping doesn't
103-
/// exist, and if unified shared memory is not enabled, a new mapping will be
104-
/// created and the data will also be transferred accordingly. nullptr will be
105-
/// returned because of any of following reasons:
106-
/// - Data allocation failed;
107-
/// - The user tried to do an illegal mapping;
108-
/// - Data transfer issue fails.
109-
TargetPointerResultTy getTargetPointer(
110-
HDTTMapAccessorTy &HDTTMap, void *HstPtrBegin, void *HstPtrBase,
111-
int64_t TgtPadding, int64_t Size, map_var_info_t HstPtrName,
112-
bool HasFlagTo, bool HasFlagAlways, bool IsImplicit, bool UpdateRefCount,
113-
bool HasCloseModifier, bool HasPresentModifier, bool HasHoldModifier,
114-
AsyncInfoTy &AsyncInfo, HostDataToTargetTy *OwnedTPR = nullptr,
115-
bool ReleaseHDTTMap = true);
116-
117-
/// Return the target pointer for \p HstPtrBegin in \p HDTTMap. The accessor
118-
/// ensures exclusive access to the HDTT map.
119-
void *getTgtPtrBegin(HDTTMapAccessorTy &HDTTMap, void *HstPtrBegin,
120-
int64_t Size);
121-
122-
/// Return the target pointer begin (where the data will be moved).
123-
/// Used by targetDataBegin, targetDataEnd, targetDataUpdate and target.
124-
/// - \p UpdateRefCount and \p UseHoldRefCount controls which and if the entry
125-
/// reference counters will be decremented.
126-
/// - \p MustContain enforces that the query must not extend beyond an already
127-
/// mapped entry to be valid.
128-
/// - \p ForceDelete deletes the entry regardless of its reference counting
129-
/// (unless it is infinite).
130-
/// - \p FromDataEnd tracks the number of threads referencing the entry at
131-
/// targetDataEnd for delayed deletion purpose.
132-
[[nodiscard]] TargetPointerResultTy
133-
getTgtPtrBegin(void *HstPtrBegin, int64_t Size, bool UpdateRefCount,
134-
bool UseHoldRefCount, bool MustContain = false,
135-
bool ForceDelete = false, bool FromDataEnd = false);
136-
137-
/// Remove the \p Entry from the data map. Expect the entry's total reference
138-
/// count to be zero and the caller thread to be the last one using it. \p
139-
/// HDTTMap ensure the caller holds exclusive access and can modify the map.
140-
/// Return \c OFFLOAD_SUCCESS if the map entry existed, and return \c
141-
/// OFFLOAD_FAIL if not. It is the caller's responsibility to skip calling
142-
/// this function if the map entry is not expected to exist because \p
143-
/// HstPtrBegin uses shared memory.
144-
[[nodiscard]] int eraseMapEntry(HDTTMapAccessorTy &HDTTMap,
145-
HostDataToTargetTy *Entry, int64_t Size);
146-
147-
/// Deallocate the \p Entry from the device memory and delete it. Return \c
148-
/// OFFLOAD_SUCCESS if the deallocation operations executed successfully, and
149-
/// return \c OFFLOAD_FAIL otherwise.
150-
[[nodiscard]] int deallocTgtPtrAndEntry(HostDataToTargetTy *Entry,
151-
int64_t Size);
152-
153-
int associatePtr(void *HstPtrBegin, void *TgtPtrBegin, int64_t Size);
154-
int disassociatePtr(void *HstPtrBegin);
77+
/// Provide access to the mapping handler.
78+
MappingInfoTy &getMappingInfo() { return MappingInfo; }
15579

15680
__tgt_target_table *loadBinary(__tgt_device_image *Img);
15781

@@ -166,6 +90,7 @@ struct DeviceTy {
16690
/// be used (host, shared, device).
16791
void *allocData(int64_t Size, void *HstPtr = nullptr,
16892
int32_t Kind = TARGET_ALLOC_DEFAULT);
93+
16994
/// Deallocates memory which \p TgtPtrBegin points at and returns
17095
/// OFFLOAD_SUCCESS/OFFLOAD_FAIL when succeeds/fails. p Kind dictates what
17196
/// allocator should be used (host, shared, device).
@@ -177,12 +102,16 @@ struct DeviceTy {
177102
int32_t submitData(void *TgtPtrBegin, void *HstPtrBegin, int64_t Size,
178103
AsyncInfoTy &AsyncInfo,
179104
HostDataToTargetTy *Entry = nullptr,
180-
DeviceTy::HDTTMapAccessorTy *HDTTMapPtr = nullptr);
105+
MappingInfoTy::HDTTMapAccessorTy *HDTTMapPtr = nullptr);
106+
181107
// Copy data from device back to host
182108
int32_t retrieveData(void *HstPtrBegin, void *TgtPtrBegin, int64_t Size,
183109
AsyncInfoTy &AsyncInfo,
184110
HostDataToTargetTy *Entry = nullptr,
185-
DeviceTy::HDTTMapAccessorTy *HDTTMapPtr = nullptr);
111+
MappingInfoTy::HDTTMapAccessorTy *HDTTMapPtr = nullptr);
112+
113+
// Return true if data can be copied to DstDevice directly
114+
bool isDataExchangable(const DeviceTy &DstDevice);
186115

187116
// Copy data from current device to destination device directly
188117
int32_t dataExchange(void *SrcPtr, DeviceTy &DstDev, void *DstPtr,
@@ -250,7 +179,12 @@ struct DeviceTy {
250179
void deinit();
251180

252181
/// All offload entries available on this device.
253-
llvm::DenseMap<llvm::StringRef, OffloadEntryTy *> DeviceOffloadEntries;
182+
using DeviceOffloadEntriesMapTy =
183+
llvm::DenseMap<llvm::StringRef, OffloadEntryTy *>;
184+
ProtectedObj<DeviceOffloadEntriesMapTy> DeviceOffloadEntries;
185+
186+
/// Handler to collect and organize host-2-device mapping information.
187+
MappingInfoTy MappingInfo;
254188
};
255189

256190
#endif

openmp/libomptarget/src/OpenMP/API.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ EXTERN int omp_target_is_present(const void *Ptr, int DeviceNum) {
178178
// only check 1 byte. Cannot set size 0 which checks whether the pointer (zero
179179
// lengh array) is mapped instead of the referred storage.
180180
TargetPointerResultTy TPR =
181-
DeviceOrErr->getTgtPtrBegin(const_cast<void *>(Ptr), 1,
182-
/*UpdateRefCount=*/false,
183-
/*UseHoldRefCount=*/false);
181+
DeviceOrErr->getMappingInfo().getTgtPtrBegin(const_cast<void *>(Ptr), 1,
182+
/*UpdateRefCount=*/false,
183+
/*UseHoldRefCount=*/false);
184184
int Rc = TPR.isPresent();
185185
DP("Call to omp_target_is_present returns %d\n", Rc);
186186
return Rc;
@@ -572,8 +572,8 @@ EXTERN int omp_target_associate_ptr(const void *HostPtr, const void *DevicePtr,
572572
FATAL_MESSAGE(DeviceNum, "%s", toString(DeviceOrErr.takeError()).c_str());
573573

574574
void *DeviceAddr = (void *)((uint64_t)DevicePtr + (uint64_t)DeviceOffset);
575-
int Rc = DeviceOrErr->associatePtr(const_cast<void *>(HostPtr),
576-
const_cast<void *>(DeviceAddr), Size);
575+
int Rc = DeviceOrErr->getMappingInfo().associatePtr(
576+
const_cast<void *>(HostPtr), const_cast<void *>(DeviceAddr), Size);
577577
DP("omp_target_associate_ptr returns %d\n", Rc);
578578
return Rc;
579579
}
@@ -599,7 +599,8 @@ EXTERN int omp_target_disassociate_ptr(const void *HostPtr, int DeviceNum) {
599599
if (!DeviceOrErr)
600600
FATAL_MESSAGE(DeviceNum, "%s", toString(DeviceOrErr.takeError()).c_str());
601601

602-
int Rc = DeviceOrErr->disassociatePtr(const_cast<void *>(HostPtr));
602+
int Rc = DeviceOrErr->getMappingInfo().disassociatePtr(
603+
const_cast<void *>(HostPtr));
603604
DP("omp_target_disassociate_ptr returns %d\n", Rc);
604605
return Rc;
605606
}
@@ -638,9 +639,9 @@ EXTERN void *omp_get_mapped_ptr(const void *Ptr, int DeviceNum) {
638639
FATAL_MESSAGE(DeviceNum, "%s", toString(DeviceOrErr.takeError()).c_str());
639640

640641
TargetPointerResultTy TPR =
641-
DeviceOrErr->getTgtPtrBegin(const_cast<void *>(Ptr), 1,
642-
/*UpdateRefCount=*/false,
643-
/*UseHoldRefCount=*/false);
642+
DeviceOrErr->getMappingInfo().getTgtPtrBegin(const_cast<void *>(Ptr), 1,
643+
/*UpdateRefCount=*/false,
644+
/*UseHoldRefCount=*/false);
644645
if (!TPR.isPresent()) {
645646
DP("Ptr " DPxMOD "is not present on device %d, returning nullptr.\n",
646647
DPxPTR(Ptr), DeviceNum);

0 commit comments

Comments
 (0)