Skip to content

Commit 4112cbc

Browse files
[SYCL] Exclude exported symbols from kernel bundles (#4660)
Because exported device functions are interspersed with kernels in the offload entries they are thought to be kernels in device images. These changes use the list of exported symbols generated by sycl-post-link to filter out the exported device functions when creating device images.
1 parent c408e03 commit 4112cbc

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

sycl/include/CL/sycl/detail/pi.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,13 @@ class DeviceBinaryImage {
369369
return AssertUsed;
370370
}
371371
const PropertyRange &getProgramMetadata() const { return ProgramMetadata; }
372+
const PropertyRange getExportedSymbols() const {
373+
// We can't have this variable as a class member, since it would break
374+
// the ABI backwards compatibility.
375+
DeviceBinaryImage::PropertyRange ExportedSymbols;
376+
ExportedSymbols.init(Bin, __SYCL_PI_PROPERTY_SET_SYCL_EXPORTED_SYMBOLS);
377+
return ExportedSymbols;
378+
}
372379
virtual ~DeviceBinaryImage() {}
373380

374381
protected:

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,12 @@ void ProgramManager::addImages(pi_device_binaries DeviceBinary) {
10591059
KernelSetId KSId = getNextKernelSetId();
10601060
{
10611061
std::lock_guard<std::mutex> KernelIDsGuard(m_KernelIDsMutex);
1062+
1063+
// Register all exported symbols
1064+
auto ExportedSymbols = Img->getExportedSymbols();
1065+
for (const pi_device_binary_property &ExportedSymbol : ExportedSymbols)
1066+
m_ExportedSymbols.insert(ExportedSymbol->Name);
1067+
10621068
for (_pi_offload_entry EntriesIt = EntriesB; EntriesIt != EntriesE;
10631069
++EntriesIt) {
10641070
auto Result = KSIdMap.insert(std::make_pair(EntriesIt->name, KSId));
@@ -1074,6 +1080,13 @@ void ProgramManager::addImages(pi_device_binaries DeviceBinary) {
10741080
continue;
10751081
}
10761082

1083+
// Skip creating unique kernel ID if it is an exported device
1084+
// function. Exported device functions appear in the offload entries
1085+
// among kernels, but are identifiable by being listed in properties.
1086+
if (m_ExportedSymbols.find(EntriesIt->name) !=
1087+
m_ExportedSymbols.end())
1088+
continue;
1089+
10771090
// ... and create a unique kernel ID for the entry
10781091
std::shared_ptr<detail::kernel_id_impl> KernelIDImpl =
10791092
std::make_shared<detail::kernel_id_impl>(EntriesIt->name);
@@ -1373,9 +1386,11 @@ ProgramManager::getSYCLDeviceImagesWithCompatibleState(
13731386
auto KernelID = m_KernelIDs.find(EntriesIt->name);
13741387

13751388
if (KernelID == m_KernelIDs.end()) {
1376-
// Service kernels do not have kernel IDs
1377-
assert(m_ServiceKernels.find(EntriesIt->name) !=
1378-
m_ServiceKernels.end() &&
1389+
// Service kernels and exported symbols do not have kernel IDs
1390+
assert((m_ServiceKernels.find(EntriesIt->name) !=
1391+
m_ServiceKernels.end() ||
1392+
m_ExportedSymbols.find(EntriesIt->name) !=
1393+
m_ExportedSymbols.end()) &&
13791394
"Kernel ID in device binary missing from cache");
13801395
continue;
13811396
}

sycl/source/detail/program_manager/program_manager.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ class ProgramManager {
299299
/// Access must be guarded by the m_KernelIDsMutex mutex.
300300
std::unordered_set<std::string> m_ServiceKernels;
301301

302+
/// Caches all exported symbols to allow faster lookup when excluding these
303+
// from kernel bundles.
304+
/// Access must be guarded by the m_KernelIDsMutex mutex.
305+
std::unordered_set<std::string> m_ExportedSymbols;
306+
302307
// Keeps track of pi_program to image correspondence. Needed for:
303308
// - knowing which specialization constants are used in the program and
304309
// injecting their current values before compiling the SPIR-V; the binary

0 commit comments

Comments
 (0)