This is an archive of the discontinued LLVM Phabricator instance.

[Libomptarget] Support mapping indirect host calls to device functions
ClosedPublic

Authored by jhuber6 on Aug 14 2023, 2:24 PM.

Details

Summary

The changes in D157738 allowed for us to emit stub globals on the device
in the offloading entry section. These globals contain the addresses of
device functions and allow us to map host functions to their
corresponding device equivalent. This patch provides the initial support
required to build a table on the device to lookup the associated value.
This is done by finding these entries and creating a global table on the
device that can be searched with a simple binary search.

This requires an allocation, which supposedly should be automatically
freed at plugin shutdown. This includes a basic test which looks up device
pointers via a host pointer using the added function. This will need to be built
upon to provide full support for these calls in the runtime.

To support reverse offloading it would also be useful to provide a reverse table
that allows us to get host functions from device stubs.

Depends on D157738

Diff Detail

Event Timeline

jhuber6 created this revision.Aug 14 2023, 2:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 14 2023, 2:24 PM
Herald added a subscriber: mgrang. · View Herald Transcript
jhuber6 requested review of this revision.Aug 14 2023, 2:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 14 2023, 2:24 PM
openmp/libomptarget/DeviceRTL/src/Misc.cpp
84

Do you want to check if HstPtr is null then return nullptr

openmp/libomptarget/include/omptarget.h
95

Should this be 0x08

jhuber6 added inline comments.Aug 14 2023, 2:59 PM
openmp/libomptarget/DeviceRTL/src/Misc.cpp
84

Yeah that's a good idea.

openmp/libomptarget/include/omptarget.h
95

I didn't design it this way, but the frontend uses different flags for globals and kernels. All the other flags overlap so why should this one be the exception without breaking ABI and redesigning it?

openmp/libomptarget/include/omptarget.h
95

You want the lookup table to be small. Does TARGET_DTOR get added to the indirect lookup table?

openmp/libomptarget/DeviceRTL/src/Misc.cpp
84

Also if the Table does not exist or size zero return the same pointer since it might already be a device pointer.

jhuber6 updated this revision to Diff 550141.Aug 14 2023, 4:53 PM

Return if null host pointer.

jdoerfert added inline comments.Aug 15 2023, 1:16 PM
openmp/libomptarget/DeviceRTL/src/Misc.cpp
86

Check if the HstPtr is outside the table range. This should allows us to avoid the binary search for basically all host pointers in practice.

openmp/libomptarget/include/omptarget.h
95

0x08 plz

jhuber6 added inline comments.Aug 15 2023, 1:20 PM
openmp/libomptarget/DeviceRTL/src/Misc.cpp
86

We'd need to look up the beginning and the end right? That's still two loads to check.

openmp/libomptarget/include/omptarget.h
95

This comes from the previous patch, Clang for whatever reason uses a different set of enums for kernels and globals. All the others overlap so I don't know why this should be the exception. The case where this was important was the registration of the global constructors and destructors, which shouldn't have been looking at globals anyway.

jdoerfert added inline comments.Aug 15 2023, 2:39 PM
openmp/libomptarget/DeviceRTL/src/Misc.cpp
86

Yes, up to two loads/checks.
Below you check log(N) + 1 values.
With Size >= 3 it's a win, assuming device ptrs.

openmp/libomptarget/include/omptarget.h
95

All the others overlap so I don't know why this should be the exception

I don't see this as a valid argument. If we started fresh, we would not make them overlap to help debugging efforts. Why would one "stick to it" for no good reason?

ThorBl added a subscriber: ThorBl.Aug 16 2023, 7:26 AM
jplehr added inline comments.Aug 25 2023, 1:47 PM
openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.cpp
310

Can this error?

jhuber6 updated this revision to Diff 553637.Aug 25 2023, 2:49 PM

Adjust table lookups.

jhuber6 updated this revision to Diff 553642.Aug 25 2023, 2:58 PM

Change flag to 8.

Herald added a project: Restricted Project. · View Herald TranscriptAug 25 2023, 2:58 PM
jdoerfert accepted this revision.Aug 25 2023, 4:21 PM
This revision is now accepted and ready to land.Aug 25 2023, 4:21 PM
This revision was landed with ongoing or failed builds.Aug 25 2023, 4:52 PM
This revision was automatically updated to reflect the committed changes.