Skip to content

Commit abb8784

Browse files
authored
[Offload] Allow querying the size of globals (#147698)
The `GlobalTy` helper has been extended to make both the Size and Ptr be optional. Now `getGlobalMetadataFromDevice`/`Image` is able to write the size of the global to the struct, instead of just verifying it.
1 parent 343e3c6 commit abb8784

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

offload/plugins-nextgen/amdgpu/src/rtl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3089,15 +3089,16 @@ struct AMDGPUGlobalHandlerTy final : public GenericGlobalHandlerTy {
30893089
}
30903090

30913091
// Check the size of the symbol.
3092-
if (SymbolSize != DeviceGlobal.getSize())
3092+
if (DeviceGlobal.getSize() && SymbolSize != DeviceGlobal.getSize())
30933093
return Plugin::error(
30943094
ErrorCode::INVALID_BINARY,
30953095
"failed to load global '%s' due to size mismatch (%zu != %zu)",
30963096
DeviceGlobal.getName().data(), SymbolSize,
30973097
(size_t)DeviceGlobal.getSize());
30983098

3099-
// Store the symbol address on the device global metadata.
3099+
// Store the symbol address and size on the device global metadata.
31003100
DeviceGlobal.setPtr(reinterpret_cast<void *>(SymbolAddr));
3101+
DeviceGlobal.setSize(SymbolSize);
31013102

31023103
return Plugin::success();
31033104
}

offload/plugins-nextgen/common/include/GlobalHandler.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ using namespace llvm::object;
3737
/// Common abstraction for globals that live on the host and device.
3838
/// It simply encapsulates the symbol name, symbol size, and symbol address
3939
/// (which might be host or device depending on the context).
40+
/// Both size and address may be absent (signified by 0/nullptr), and can be
41+
/// populated with getGlobalMetadataFromDevice/Image.
4042
class GlobalTy {
4143
// NOTE: Maybe we can have a pointer to the offload entry name instead of
4244
// holding a private copy of the name as a std::string.
@@ -45,7 +47,7 @@ class GlobalTy {
4547
void *Ptr;
4648

4749
public:
48-
GlobalTy(const std::string &Name, uint32_t Size, void *Ptr = nullptr)
50+
GlobalTy(const std::string &Name, uint32_t Size = 0, void *Ptr = nullptr)
4951
: Name(Name), Size(Size), Ptr(Ptr) {}
5052

5153
const std::string &getName() const { return Name; }
@@ -139,8 +141,11 @@ class GenericGlobalHandlerTy {
139141
bool isSymbolInImage(GenericDeviceTy &Device, DeviceImageTy &Image,
140142
StringRef SymName);
141143

142-
/// Get the address and size of a global in the image. Address and size are
143-
/// return in \p ImageGlobal, the global name is passed in \p ImageGlobal.
144+
/// Get the address and size of a global in the image. Address is
145+
/// returned in \p ImageGlobal and the global name is passed in \p
146+
/// ImageGlobal. If no size is present in \p ImageGlobal, then the size of the
147+
/// global will be stored there. If it is present, it will be validated
148+
/// against the real size of the global.
144149
Error getGlobalMetadataFromImage(GenericDeviceTy &Device,
145150
DeviceImageTy &Image, GlobalTy &ImageGlobal);
146151

@@ -149,9 +154,11 @@ class GenericGlobalHandlerTy {
149154
Error readGlobalFromImage(GenericDeviceTy &Device, DeviceImageTy &Image,
150155
const GlobalTy &HostGlobal);
151156

152-
/// Get the address and size of a global from the device. Address is return in
153-
/// \p DeviceGlobal, the global name and expected size are passed in
154-
/// \p DeviceGlobal.
157+
/// Get the address and size of a global from the device. Address is
158+
/// returned in \p ImageGlobal and the global name is passed in \p
159+
/// ImageGlobal. If no size is present in \p ImageGlobal, then the size of the
160+
/// global will be stored there. If it is present, it will be validated
161+
/// against the real size of the global.
155162
virtual Error getGlobalMetadataFromDevice(GenericDeviceTy &Device,
156163
DeviceImageTy &Image,
157164
GlobalTy &DeviceGlobal) = 0;

offload/plugins-nextgen/cuda/src/rtl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,13 +1355,15 @@ class CUDAGlobalHandlerTy final : public GenericGlobalHandlerTy {
13551355
GlobalName))
13561356
return Err;
13571357

1358-
if (CUSize != DeviceGlobal.getSize())
1358+
if (DeviceGlobal.getSize() && CUSize != DeviceGlobal.getSize())
13591359
return Plugin::error(
13601360
ErrorCode::INVALID_BINARY,
13611361
"failed to load global '%s' due to size mismatch (%zu != %zu)",
13621362
GlobalName, CUSize, (size_t)DeviceGlobal.getSize());
13631363

13641364
DeviceGlobal.setPtr(reinterpret_cast<void *>(CUPtr));
1365+
DeviceGlobal.setSize(CUSize);
1366+
13651367
return Plugin::success();
13661368
}
13671369
};

0 commit comments

Comments
 (0)