Skip to content

Commit 0c30daf

Browse files
committed
[offload] add support for SPIR-V kernel images
The olProgramCreate entry point currently doesn't define the format of the program binary. The PluginInterface implementation currently only accepts ELF images, which won't work for backends that don't support ELF, like Level Zero. This patch adds support for loading and handling kernels in SPIR-V format.
1 parent c1dc064 commit 0c30daf

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

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

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -403,19 +403,30 @@ Error GenericKernelTy::init(GenericDeviceTy &GenericDevice,
403403
ImagePtr = &Image;
404404

405405
// Retrieve kernel environment object for the kernel.
406-
std::string EnvironmentName = std::string(Name) + "_kernel_environment";
407-
GenericGlobalHandlerTy &GHandler = GenericDevice.Plugin.getGlobalHandler();
408-
if (GHandler.isSymbolInImage(GenericDevice, Image, EnvironmentName)) {
409-
GlobalTy KernelEnv(EnvironmentName, sizeof(KernelEnvironment),
410-
&KernelEnvironment);
411-
if (auto Err =
412-
GHandler.readGlobalFromImage(GenericDevice, *ImagePtr, KernelEnv))
413-
return Err;
414-
} else {
406+
switch (identify_magic(Image.getMemoryBuffer().getBuffer())) {
407+
case file_magic::elf_executable: {
408+
std::string EnvironmentName = std::string(Name) + "_kernel_environment";
409+
GenericGlobalHandlerTy &GHandler = GenericDevice.Plugin.getGlobalHandler();
410+
if (GHandler.isSymbolInImage(GenericDevice, Image, EnvironmentName)) {
411+
GlobalTy KernelEnv(EnvironmentName, sizeof(KernelEnvironment),
412+
&KernelEnvironment);
413+
if (auto Err =
414+
GHandler.readGlobalFromImage(GenericDevice, *ImagePtr, KernelEnv))
415+
return Err;
416+
} else {
417+
KernelEnvironment = KernelEnvironmentTy{};
418+
DP("Failed to read kernel environment for '%s' Using default Bare (0) "
419+
"execution mode\n",
420+
getName());
421+
}
422+
break;
423+
}
424+
case file_magic::spirv_object:
415425
KernelEnvironment = KernelEnvironmentTy{};
416-
DP("Failed to read kernel environment for '%s' Using default Bare (0) "
417-
"execution mode\n",
418-
getName());
426+
break;
427+
default:
428+
return Plugin::error(ErrorCode::INVALID_ARGUMENT,
429+
"Unsupported image format for kernel '%s'", getName());
419430
}
420431

421432
// Max = Config.Max > 0 ? min(Config.Max, Device.Max) : Device.Max;
@@ -808,20 +819,23 @@ Error GenericDeviceTy::unloadBinary(DeviceImageTy *Image) {
808819
DeviceMemoryPoolTracking.combine(ImageDeviceMemoryPoolTracking);
809820
}
810821

811-
GenericGlobalHandlerTy &Handler = Plugin.getGlobalHandler();
812-
auto ProfOrErr = Handler.readProfilingGlobals(*this, *Image);
813-
if (!ProfOrErr)
814-
return ProfOrErr.takeError();
822+
if (utils::elf::isELF(Image->getMemoryBuffer().getBuffer())) {
815823

816-
if (!ProfOrErr->empty()) {
817-
// Dump out profdata
818-
if ((OMPX_DebugKind.get() & uint32_t(DeviceDebugKind::PGODump)) ==
819-
uint32_t(DeviceDebugKind::PGODump))
820-
ProfOrErr->dump();
824+
GenericGlobalHandlerTy &Handler = Plugin.getGlobalHandler();
825+
auto ProfOrErr = Handler.readProfilingGlobals(*this, *Image);
826+
if (!ProfOrErr)
827+
return ProfOrErr.takeError();
821828

822-
// Write data to profiling file
823-
if (auto Err = ProfOrErr->write())
824-
return Err;
829+
if (!ProfOrErr->empty()) {
830+
// Dump out profdata
831+
if ((OMPX_DebugKind.get() & uint32_t(DeviceDebugKind::PGODump)) ==
832+
uint32_t(DeviceDebugKind::PGODump))
833+
ProfOrErr->dump();
834+
835+
// Write data to profiling file
836+
if (auto Err = ProfOrErr->write())
837+
return Err;
838+
}
825839
}
826840

827841
return unloadBinaryImpl(Image);

0 commit comments

Comments
 (0)