Skip to content

Commit cf72076

Browse files
committed
[OpenMP][FIX] Ensure we allow shared libraries without kernels
This fixes two bugs and adds a test for them: - A shared library with declare target functions but without kernels should not error out due to missing globals. - Enabling LIBOMPTARGET_INFO=32 should not deadlock in the presence of indirect declare targets.
1 parent 66784dc commit cf72076

File tree

6 files changed

+51
-12
lines changed

6 files changed

+51
-12
lines changed

openmp/libomptarget/include/device.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,14 @@ struct DeviceTy {
170170
// Copy data from host to device
171171
int32_t submitData(void *TgtPtrBegin, void *HstPtrBegin, int64_t Size,
172172
AsyncInfoTy &AsyncInfo,
173-
HostDataToTargetTy *Entry = nullptr);
173+
HostDataToTargetTy *Entry = nullptr,
174+
DeviceTy::HDTTMapAccessorTy *HDTTMapPtr = nullptr);
174175
// Copy data from device back to host
175176
int32_t retrieveData(void *HstPtrBegin, void *TgtPtrBegin, int64_t Size,
176177
AsyncInfoTy &AsyncInfo,
177-
HostDataToTargetTy *Entry = nullptr);
178+
HostDataToTargetTy *Entry = nullptr,
179+
DeviceTy::HDTTMapAccessorTy *HDTTMapPtr = nullptr);
180+
178181
// Copy data from current device to destination device directly
179182
int32_t dataExchange(void *SrcPtr, DeviceTy &DstDev, void *DstPtr,
180183
int64_t Size, AsyncInfoTy &AsyncInfo);

openmp/libomptarget/plugins-nextgen/common/src/PluginInterface.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,10 @@ Error GenericDeviceTy::deinit(GenericPluginTy &Plugin) {
789789
sizeof(DeviceMemoryPoolTrackingTy),
790790
&ImageDeviceMemoryPoolTracking);
791791
if (auto Err =
792-
GHandler.readGlobalFromDevice(*this, *Image, TrackerGlobal))
793-
return Err;
792+
GHandler.readGlobalFromDevice(*this, *Image, TrackerGlobal)) {
793+
consumeError(std::move(Err));
794+
continue;
795+
}
794796
DeviceMemoryPoolTracking.combine(ImageDeviceMemoryPoolTracking);
795797
}
796798

@@ -975,6 +977,12 @@ Error GenericDeviceTy::setupDeviceMemoryPool(GenericPluginTy &Plugin,
975977
sizeof(DeviceMemoryPoolTrackingTy),
976978
&DeviceMemoryPoolTracking);
977979
GenericGlobalHandlerTy &GHandler = Plugin.getGlobalHandler();
980+
if (auto Err = GHandler.readGlobalFromImage(*this, Image, TrackerGlobal)) {
981+
[[maybe_unused]] std::string ErrStr = toString(std::move(Err));
982+
DP("Avoid the memory pool: %s.\n", ErrStr.c_str());
983+
return Error::success();
984+
}
985+
978986
if (auto Err = GHandler.writeGlobalToDevice(*this, Image, TrackerGlobal))
979987
return Err;
980988

openmp/libomptarget/src/device.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -609,13 +609,14 @@ static void printCopyInfo(int DeviceId, bool H2D, void *SrcPtrBegin,
609609

610610
// Submit data to device
611611
int32_t DeviceTy::submitData(void *TgtPtrBegin, void *HstPtrBegin, int64_t Size,
612-
AsyncInfoTy &AsyncInfo,
613-
HostDataToTargetTy *Entry) {
612+
AsyncInfoTy &AsyncInfo, HostDataToTargetTy *Entry,
613+
DeviceTy::HDTTMapAccessorTy *HDTTMapPtr) {
614614
if (getInfoLevel() & OMP_INFOTYPE_DATA_TRANSFER) {
615-
HDTTMapAccessorTy HDTTMap = HostDataToTargetMap.getExclusiveAccessor(Entry);
615+
HDTTMapAccessorTy HDTTMap =
616+
HostDataToTargetMap.getExclusiveAccessor(!!Entry || !!HDTTMapPtr);
616617
LookupResult LR;
617618
if (!Entry) {
618-
LR = lookupMapping(HDTTMap, HstPtrBegin, Size);
619+
LR = lookupMapping(HDTTMapPtr ? *HDTTMapPtr : HDTTMap, HstPtrBegin, Size);
619620
Entry = LR.TPR.getEntry();
620621
}
621622
printCopyInfo(DeviceID, /* H2D */ true, HstPtrBegin, TgtPtrBegin, Size,
@@ -638,12 +639,14 @@ int32_t DeviceTy::submitData(void *TgtPtrBegin, void *HstPtrBegin, int64_t Size,
638639
// Retrieve data from device
639640
int32_t DeviceTy::retrieveData(void *HstPtrBegin, void *TgtPtrBegin,
640641
int64_t Size, AsyncInfoTy &AsyncInfo,
641-
HostDataToTargetTy *Entry) {
642+
HostDataToTargetTy *Entry,
643+
DeviceTy::HDTTMapAccessorTy *HDTTMapPtr) {
642644
if (getInfoLevel() & OMP_INFOTYPE_DATA_TRANSFER) {
643-
HDTTMapAccessorTy HDTTMap = HostDataToTargetMap.getExclusiveAccessor(Entry);
645+
HDTTMapAccessorTy HDTTMap =
646+
HostDataToTargetMap.getExclusiveAccessor(!!Entry || !!HDTTMapPtr);
644647
LookupResult LR;
645648
if (!Entry) {
646-
LR = lookupMapping(HDTTMap, HstPtrBegin, Size);
649+
LR = lookupMapping(HDTTMapPtr ? *HDTTMapPtr : HDTTMap, HstPtrBegin, Size);
647650
Entry = LR.TPR.getEntry();
648651
}
649652
printCopyInfo(DeviceID, /* H2D */ false, TgtPtrBegin, HstPtrBegin, Size,

openmp/libomptarget/src/omptarget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static int initLibrary(DeviceTy &Device) {
225225
AsyncInfoTy AsyncInfo(Device);
226226
void *DevPtr;
227227
Device.retrieveData(&DevPtr, CurrDeviceEntryAddr, sizeof(void *),
228-
AsyncInfo);
228+
AsyncInfo, /* Entry */ nullptr, &HDTTMap);
229229
if (AsyncInfo.synchronize() != OFFLOAD_SUCCESS)
230230
return OFFLOAD_FAIL;
231231
CurrDeviceEntryAddr = DevPtr;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
int func() { return 42; }
3+
#pragma omp declare target indirect to(func)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// clang-format off
2+
// RUN: %clang-generic -fPIC -shared %S/../Inputs/declare_indirect_func.c -o %T/liba.so -fopenmp-version=51
3+
// RUN: %libomptarget-compile-generic -L %T -l a -o %t -fopenmp-version=51
4+
// RUN: env LIBOMPTARGET_INFO=32 LD_LIBRARY_PATH=%T:$LD_LIBRARY_PATH %t | %fcheck-generic
5+
// clang-format on
6+
7+
#include <stdio.h>
8+
9+
extern int func(); // Provided in liba.so, returns 42
10+
typedef int (*fp_t)();
11+
12+
int main() {
13+
int x = 0;
14+
fp_t fp = &func;
15+
printf("TARGET\n");
16+
#pragma omp target map(from : x)
17+
x = fp();
18+
// CHECK: Copying data from device to host, {{.*}} Size=8
19+
// CHECK: Copying data from device to host, {{.*}} Size=4
20+
// CHECK: 42
21+
printf("%i\n", x);
22+
}

0 commit comments

Comments
 (0)