Skip to content

Commit 6518b12

Browse files
authored
[Offload][NFC] Factor out and rename the __tgt_offload_entry struct (llvm#123785)
Summary: This patch is an NFC renaming to make using the offloading entry type more portable between other targets. Right now this is just moving its definition to LLVM so others can use it. Future work will rework the struct layout.
1 parent e4f03b1 commit 6518b12

File tree

10 files changed

+101
-96
lines changed

10 files changed

+101
-96
lines changed

llvm/include/llvm/Frontend/Offloading/Utility.h

+10-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
namespace llvm {
2222
namespace offloading {
2323

24+
/// This is the record of an object that just be registered with the offloading
25+
/// runtime.
26+
struct EntryTy {
27+
void *Address;
28+
char *SymbolName;
29+
size_t Size;
30+
int32_t Flags;
31+
int32_t Data;
32+
};
33+
2434
/// Offloading entry flags for CUDA / HIP. The first three bits indicate the
2535
/// type of entry while the others are a bit field for additional information.
2636
enum OffloadEntryKindFlag : uint32_t {
@@ -48,15 +58,6 @@ StructType *getEntryTy(Module &M);
4858
/// Create an offloading section struct used to register this global at
4959
/// runtime.
5060
///
51-
/// Type struct __tgt_offload_entry {
52-
/// void *addr; // Pointer to the offload entry info.
53-
/// // (function or global)
54-
/// char *name; // Name of the function or global.
55-
/// size_t size; // Size of the entry info (0 if it a function).
56-
/// int32_t flags;
57-
/// int32_t data;
58-
/// };
59-
///
6061
/// \param M The module to be used
6162
/// \param Addr The pointer to the global being registered.
6263
/// \param Name The symbol name associated with the global.

offload/docs/declare_target_indirect.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The offload entries table that is created for the host and for each of the devic
2525

2626
Compiler will also produce an entry for each procedure listed in **indirect** clause of **declare target** construct:
2727
```C++
28-
struct __tgt_offload_entry {
28+
struct llvm::offloading::EntryTy {
2929
void *addr; // Pointer to the function
3030
char *name; // Name of the function
3131
size_t size; // 0 for function
@@ -82,7 +82,7 @@ struct __omp_offloading_fptr_map_ty {
8282
};
8383
```
8484
85-
Where `host_ptr` is `__tgt_offload_entry::addr` in a **host** offload entry, and `tgt_ptr` is `__tgt_offload_entry::addr` in the corresponding **device** offload entry (which may be found using the populated `Device.HostDataToTargetMap`).
85+
Where `host_ptr` is `llvm::offloading::EntryTy::addr` in a **host** offload entry, and `tgt_ptr` is `llvm::offloading::EntryTy::addr` in the corresponding **device** offload entry (which may be found using the populated `Device.HostDataToTargetMap`).
8686
8787
When all `__omp_offloading_function_ptr_map_ty` entries are collected in a single host array, `libomptarget` sorts the table by `host_ptr` values and passes it to the device plugin for registration, if plugin supports optional `__tgt_rtl_set_function_ptr_map` API.
8888

offload/include/OffloadEntry.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,25 @@ class DeviceImageTy;
2222

2323
class OffloadEntryTy {
2424
DeviceImageTy &DeviceImage;
25-
__tgt_offload_entry &OffloadEntry;
25+
llvm::offloading::EntryTy &OffloadEntry;
2626

2727
public:
28-
OffloadEntryTy(DeviceImageTy &DeviceImage, __tgt_offload_entry &OffloadEntry)
28+
OffloadEntryTy(DeviceImageTy &DeviceImage,
29+
llvm::offloading::EntryTy &OffloadEntry)
2930
: DeviceImage(DeviceImage), OffloadEntry(OffloadEntry) {}
3031

3132
bool isGlobal() const { return getSize() != 0; }
32-
size_t getSize() const { return OffloadEntry.size; }
33+
size_t getSize() const { return OffloadEntry.Size; }
3334

34-
void *getAddress() const { return OffloadEntry.addr; }
35-
llvm::StringRef getName() const { return OffloadEntry.name; }
36-
const char *getNameAsCStr() const { return OffloadEntry.name; }
35+
void *getnAddress() const { return OffloadEntry.Address; }
36+
llvm::StringRef getName() const { return OffloadEntry.SymbolName; }
37+
const char *getNameAsCStr() const { return OffloadEntry.SymbolName; }
3738
__tgt_bin_desc *getBinaryDescription() const;
3839

3940
bool isLink() const { return hasFlags(OMP_DECLARE_TARGET_LINK); }
4041

4142
bool hasFlags(OpenMPOffloadingDeclareTargetFlags Flags) const {
42-
return Flags & OffloadEntry.flags;
43+
return Flags & OffloadEntry.Flags;
4344
}
4445
};
4546

offload/include/PluginManager.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ struct PluginManager {
8181
HostEntriesBeginToTransTableTy HostEntriesBeginToTransTable;
8282
std::mutex TrlTblMtx; ///< For Translation Table
8383
/// Host offload entries in order of image registration
84-
llvm::SmallVector<__tgt_offload_entry *> HostEntriesBeginRegistrationOrder;
84+
llvm::SmallVector<llvm::offloading::EntryTy *>
85+
HostEntriesBeginRegistrationOrder;
8586

8687
/// Map from ptrs on the host to an entry in the Translation Table
8788
HostPtrToTableMapTy HostPtrToTableMap;

offload/include/Shared/APITypes.h

+14-20
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,20 @@
1717
#include "Environment.h"
1818

1919
#include "llvm/ADT/SmallVector.h"
20+
#include "llvm/Frontend/Offloading/Utility.h"
2021

2122
#include <cstddef>
2223
#include <cstdint>
2324

2425
extern "C" {
2526

26-
/// This struct is a record of an entry point or global. For a function
27-
/// entry point the size is expected to be zero
28-
struct __tgt_offload_entry {
29-
void *addr; // Pointer to the offload entry info (function or global)
30-
char *name; // Name of the function or global
31-
size_t size; // Size of the entry info (0 if it is a function)
32-
int32_t flags; // Flags associated with the entry, e.g. 'link'.
33-
int32_t data; // Extra data associated with the entry.
34-
};
35-
3627
/// This struct is a record of the device image information
3728
struct __tgt_device_image {
38-
void *ImageStart; // Pointer to the target code start
39-
void *ImageEnd; // Pointer to the target code end
40-
__tgt_offload_entry *EntriesBegin; // Begin of table with all target entries
41-
__tgt_offload_entry *EntriesEnd; // End of table (non inclusive)
29+
void *ImageStart; // Pointer to the target code start
30+
void *ImageEnd; // Pointer to the target code end
31+
llvm::offloading::EntryTy
32+
*EntriesBegin; // Begin of table with all target entries
33+
llvm::offloading::EntryTy *EntriesEnd; // End of table (non inclusive)
4234
};
4335

4436
struct __tgt_device_info {
@@ -51,14 +43,16 @@ struct __tgt_device_info {
5143
struct __tgt_bin_desc {
5244
int32_t NumDeviceImages; // Number of device types supported
5345
__tgt_device_image *DeviceImages; // Array of device images (1 per dev. type)
54-
__tgt_offload_entry *HostEntriesBegin; // Begin of table with all host entries
55-
__tgt_offload_entry *HostEntriesEnd; // End of table (non inclusive)
46+
llvm::offloading::EntryTy
47+
*HostEntriesBegin; // Begin of table with all host entries
48+
llvm::offloading::EntryTy *HostEntriesEnd; // End of table (non inclusive)
5649
};
5750

5851
/// This struct contains the offload entries identified by the target runtime
5952
struct __tgt_target_table {
60-
__tgt_offload_entry *EntriesBegin; // Begin of the table with all the entries
61-
__tgt_offload_entry
53+
llvm::offloading::EntryTy
54+
*EntriesBegin; // Begin of the table with all the entries
55+
llvm::offloading::EntryTy
6256
*EntriesEnd; // End of the table with all the entries (non inclusive)
6357
};
6458

@@ -107,9 +101,9 @@ struct KernelArgsTy {
107101
} Flags = {0, 0, 0};
108102
// The number of teams (for x,y,z dimension).
109103
uint32_t NumTeams[3] = {0, 0, 0};
110-
// The number of threads (for x,y,z dimension).
104+
// The number of threads (for x,y,z dimension).
111105
uint32_t ThreadLimit[3] = {0, 0, 0};
112-
uint32_t DynCGroupMem = 0; // Amount of dynamic cgroup memory requested.
106+
uint32_t DynCGroupMem = 0; // Amount of dynamic cgroup memory requested.
113107
};
114108
static_assert(sizeof(KernelArgsTy().Flags) == sizeof(uint64_t),
115109
"Invalid struct size");

offload/include/rtl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
/// Map between the host entry begin and the translation table. Each
2424
/// registered library gets one TranslationTable. Use the map from
25-
/// __tgt_offload_entry so that we may quickly determine whether we
25+
/// llvm::offloading::EntryTy so that we may quickly determine whether we
2626
/// are trying to (re)register an existing lib or really have a new one.
2727
struct TranslationTable {
2828
__tgt_target_table HostTable;
@@ -33,14 +33,14 @@ struct TranslationTable {
3333
TargetsImages; // One image per device ID.
3434

3535
// Arrays of entries active on the device.
36-
llvm::SmallVector<llvm::SmallVector<__tgt_offload_entry>>
36+
llvm::SmallVector<llvm::SmallVector<llvm::offloading::EntryTy>>
3737
TargetsEntries; // One table per device ID.
3838

3939
// Table of entry points or NULL if it was not already computed.
4040
llvm::SmallVector<__tgt_target_table *>
4141
TargetsTable; // One table per device ID.
4242
};
43-
typedef std::map<__tgt_offload_entry *, TranslationTable>
43+
typedef std::map<llvm::offloading::EntryTy *, TranslationTable>
4444
HostEntriesBeginToTransTableTy;
4545

4646
/// Map between the host ptr and a table index

offload/plugins-nextgen/common/src/PluginInterface.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -376,24 +376,24 @@ setupIndirectCallTable(GenericPluginTy &Plugin, GenericDeviceTy &Device,
376376
DeviceImageTy &Image) {
377377
GenericGlobalHandlerTy &Handler = Plugin.getGlobalHandler();
378378

379-
llvm::ArrayRef<__tgt_offload_entry> Entries(Image.getTgtImage()->EntriesBegin,
380-
Image.getTgtImage()->EntriesEnd);
379+
llvm::ArrayRef<llvm::offloading::EntryTy> Entries(
380+
Image.getTgtImage()->EntriesBegin, Image.getTgtImage()->EntriesEnd);
381381
llvm::SmallVector<std::pair<void *, void *>> IndirectCallTable;
382382
for (const auto &Entry : Entries) {
383-
if (Entry.size == 0 || !(Entry.flags & OMP_DECLARE_TARGET_INDIRECT))
383+
if (Entry.Size == 0 || !(Entry.Flags & OMP_DECLARE_TARGET_INDIRECT))
384384
continue;
385385

386-
assert(Entry.size == sizeof(void *) && "Global not a function pointer?");
386+
assert(Entry.Size == sizeof(void *) && "Global not a function pointer?");
387387
auto &[HstPtr, DevPtr] = IndirectCallTable.emplace_back();
388388

389-
GlobalTy DeviceGlobal(Entry.name, Entry.size);
389+
GlobalTy DeviceGlobal(Entry.SymbolName, Entry.Size);
390390
if (auto Err =
391391
Handler.getGlobalMetadataFromDevice(Device, Image, DeviceGlobal))
392392
return std::move(Err);
393393

394-
HstPtr = Entry.addr;
394+
HstPtr = Entry.Address;
395395
if (auto Err = Device.dataRetrieve(&DevPtr, DeviceGlobal.getPtr(),
396-
Entry.size, nullptr))
396+
Entry.Size, nullptr))
397397
return std::move(Err);
398398
}
399399

offload/src/PluginManager.cpp

+41-37
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ void PluginManager::registerLib(__tgt_bin_desc *Desc) {
128128
PM->RTLsMtx.lock();
129129

130130
// Add in all the OpenMP requirements associated with this binary.
131-
for (__tgt_offload_entry &Entry :
131+
for (llvm::offloading::EntryTy &Entry :
132132
llvm::make_range(Desc->HostEntriesBegin, Desc->HostEntriesEnd))
133-
if (Entry.flags == OMP_REGISTER_REQUIRES)
134-
PM->addRequirements(Entry.data);
133+
if (Entry.Flags == OMP_REGISTER_REQUIRES)
134+
PM->addRequirements(Entry.Data);
135135

136136
// Extract the exectuable image and extra information if availible.
137137
for (int32_t i = 0; i < Desc->NumDeviceImages; ++i)
@@ -268,9 +268,9 @@ void PluginManager::unregisterLib(__tgt_bin_desc *Desc) {
268268

269269
// Remove entries from PM->HostPtrToTableMap
270270
PM->TblMapMtx.lock();
271-
for (__tgt_offload_entry *Cur = Desc->HostEntriesBegin;
271+
for (llvm::offloading::EntryTy *Cur = Desc->HostEntriesBegin;
272272
Cur < Desc->HostEntriesEnd; ++Cur) {
273-
PM->HostPtrToTableMap.erase(Cur->addr);
273+
PM->HostPtrToTableMap.erase(Cur->Address);
274274
}
275275

276276
// Remove translation table for this descriptor.
@@ -336,35 +336,36 @@ static int loadImagesOntoDevice(DeviceTy &Device) {
336336
}
337337

338338
// 3) Create the translation table.
339-
llvm::SmallVector<__tgt_offload_entry> &DeviceEntries =
339+
llvm::SmallVector<llvm::offloading::EntryTy> &DeviceEntries =
340340
TransTable->TargetsEntries[DeviceId];
341-
for (__tgt_offload_entry &Entry :
341+
for (llvm::offloading::EntryTy &Entry :
342342
llvm::make_range(Img->EntriesBegin, Img->EntriesEnd)) {
343343
__tgt_device_binary &Binary = *BinaryOrErr;
344344

345-
__tgt_offload_entry DeviceEntry = Entry;
346-
if (Entry.size) {
347-
if (Device.RTL->get_global(Binary, Entry.size, Entry.name,
348-
&DeviceEntry.addr) != OFFLOAD_SUCCESS)
349-
REPORT("Failed to load symbol %s\n", Entry.name);
345+
llvm::offloading::EntryTy DeviceEntry = Entry;
346+
if (Entry.Size) {
347+
if (Device.RTL->get_global(Binary, Entry.Size, Entry.SymbolName,
348+
&DeviceEntry.Address) != OFFLOAD_SUCCESS)
349+
REPORT("Failed to load symbol %s\n", Entry.SymbolName);
350350

351351
// If unified memory is active, the corresponding global is a device
352352
// reference to the host global. We need to initialize the pointer on
353353
// the device to point to the memory on the host.
354354
if ((PM->getRequirements() & OMP_REQ_UNIFIED_SHARED_MEMORY) ||
355355
(PM->getRequirements() & OMPX_REQ_AUTO_ZERO_COPY)) {
356-
if (Device.RTL->data_submit(DeviceId, DeviceEntry.addr, Entry.addr,
357-
Entry.size) != OFFLOAD_SUCCESS)
358-
REPORT("Failed to write symbol for USM %s\n", Entry.name);
356+
if (Device.RTL->data_submit(DeviceId, DeviceEntry.Address,
357+
Entry.Address,
358+
Entry.Size) != OFFLOAD_SUCCESS)
359+
REPORT("Failed to write symbol for USM %s\n", Entry.SymbolName);
359360
}
360-
} else if (Entry.addr) {
361-
if (Device.RTL->get_function(Binary, Entry.name, &DeviceEntry.addr) !=
362-
OFFLOAD_SUCCESS)
363-
REPORT("Failed to load kernel %s\n", Entry.name);
361+
} else if (Entry.Address) {
362+
if (Device.RTL->get_function(Binary, Entry.SymbolName,
363+
&DeviceEntry.Address) != OFFLOAD_SUCCESS)
364+
REPORT("Failed to load kernel %s\n", Entry.SymbolName);
364365
}
365366
DP("Entry point " DPxMOD " maps to%s %s (" DPxMOD ")\n",
366-
DPxPTR(Entry.addr), (Entry.size) ? " global" : "", Entry.name,
367-
DPxPTR(DeviceEntry.addr));
367+
DPxPTR(Entry.Address), (Entry.Size) ? " global" : "",
368+
Entry.SymbolName, DPxPTR(DeviceEntry.Address));
368369

369370
DeviceEntries.emplace_back(DeviceEntry);
370371
}
@@ -396,30 +397,31 @@ static int loadImagesOntoDevice(DeviceTy &Device) {
396397
Device.getMappingInfo().HostDataToTargetMap.getExclusiveAccessor();
397398

398399
__tgt_target_table *HostTable = &TransTable->HostTable;
399-
for (__tgt_offload_entry *CurrDeviceEntry = TargetTable->EntriesBegin,
400-
*CurrHostEntry = HostTable->EntriesBegin,
401-
*EntryDeviceEnd = TargetTable->EntriesEnd;
400+
for (llvm::offloading::EntryTy *
401+
CurrDeviceEntry = TargetTable->EntriesBegin,
402+
*CurrHostEntry = HostTable->EntriesBegin,
403+
*EntryDeviceEnd = TargetTable->EntriesEnd;
402404
CurrDeviceEntry != EntryDeviceEnd;
403405
CurrDeviceEntry++, CurrHostEntry++) {
404-
if (CurrDeviceEntry->size == 0)
406+
if (CurrDeviceEntry->Size == 0)
405407
continue;
406408

407-
assert(CurrDeviceEntry->size == CurrHostEntry->size &&
409+
assert(CurrDeviceEntry->Size == CurrHostEntry->Size &&
408410
"data size mismatch");
409411

410412
// Fortran may use multiple weak declarations for the same symbol,
411413
// therefore we must allow for multiple weak symbols to be loaded from
412414
// the fat binary. Treat these mappings as any other "regular"
413415
// mapping. Add entry to map.
414-
if (Device.getMappingInfo().getTgtPtrBegin(HDTTMap, CurrHostEntry->addr,
415-
CurrHostEntry->size))
416+
if (Device.getMappingInfo().getTgtPtrBegin(
417+
HDTTMap, CurrHostEntry->Address, CurrHostEntry->Size))
416418
continue;
417419

418-
void *CurrDeviceEntryAddr = CurrDeviceEntry->addr;
420+
void *CurrDeviceEntryAddr = CurrDeviceEntry->Address;
419421

420422
// For indirect mapping, follow the indirection and map the actual
421423
// target.
422-
if (CurrDeviceEntry->flags & OMP_DECLARE_TARGET_INDIRECT) {
424+
if (CurrDeviceEntry->Flags & OMP_DECLARE_TARGET_INDIRECT) {
423425
AsyncInfoTy AsyncInfo(Device);
424426
void *DevPtr;
425427
Device.retrieveData(&DevPtr, CurrDeviceEntryAddr, sizeof(void *),
@@ -431,19 +433,21 @@ static int loadImagesOntoDevice(DeviceTy &Device) {
431433

432434
DP("Add mapping from host " DPxMOD " to device " DPxMOD " with size %zu"
433435
", name \"%s\"\n",
434-
DPxPTR(CurrHostEntry->addr), DPxPTR(CurrDeviceEntry->addr),
435-
CurrDeviceEntry->size, CurrDeviceEntry->name);
436+
DPxPTR(CurrHostEntry->Address), DPxPTR(CurrDeviceEntry->Address),
437+
CurrDeviceEntry->Size, CurrDeviceEntry->SymbolName);
436438
HDTTMap->emplace(new HostDataToTargetTy(
437-
(uintptr_t)CurrHostEntry->addr /*HstPtrBase*/,
438-
(uintptr_t)CurrHostEntry->addr /*HstPtrBegin*/,
439-
(uintptr_t)CurrHostEntry->addr + CurrHostEntry->size /*HstPtrEnd*/,
439+
(uintptr_t)CurrHostEntry->Address /*HstPtrBase*/,
440+
(uintptr_t)CurrHostEntry->Address /*HstPtrBegin*/,
441+
(uintptr_t)CurrHostEntry->Address +
442+
CurrHostEntry->Size /*HstPtrEnd*/,
440443
(uintptr_t)CurrDeviceEntryAddr /*TgtAllocBegin*/,
441444
(uintptr_t)CurrDeviceEntryAddr /*TgtPtrBegin*/,
442-
false /*UseHoldRefCount*/, CurrHostEntry->name,
445+
false /*UseHoldRefCount*/, CurrHostEntry->SymbolName,
443446
true /*IsRefCountINF*/));
444447

445448
// Notify about the new mapping.
446-
if (Device.notifyDataMapped(CurrHostEntry->addr, CurrHostEntry->size))
449+
if (Device.notifyDataMapped(CurrHostEntry->Address,
450+
CurrHostEntry->Size))
447451
return OFFLOAD_FAIL;
448452
}
449453
}

0 commit comments

Comments
 (0)