Skip to content

Commit

Permalink
Change urProgramCreateWithBinary signature in adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
againull committed Oct 1, 2024
1 parent c838a67 commit 584869e
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 92 deletions.
20 changes: 10 additions & 10 deletions source/adapters/cuda/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,23 +483,23 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetNativeHandle(
}

UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary(
ur_context_handle_t hContext, ur_device_handle_t hDevice, size_t size,
const uint8_t *pBinary, const ur_program_properties_t *pProperties,
ur_context_handle_t hContext, uint32_t numDevices,
ur_device_handle_t *phDevices, size_t *pLengths, const uint8_t **ppBinaries,
const ur_program_properties_t *pProperties,
ur_program_handle_t *phProgram) {
if (numDevices > 1)
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;

UR_CHECK_ERROR(
createProgram(hContext, hDevice, size, pBinary, pProperties, phProgram));
if (numDevices == 0)
return UR_RESULT_ERROR_INVALID_DEVICE;

UR_CHECK_ERROR(createProgram(hContext, phDevices[0], pLengths[0],
ppBinaries[0], pProperties, phProgram));
(*phProgram)->BinaryType = UR_PROGRAM_BINARY_TYPE_COMPILED_OBJECT;

return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinaryExp(
ur_context_handle_t, uint32_t, ur_device_handle_t *, size_t *,
const uint8_t **, const ur_program_properties_t *, ur_program_handle_t *) {
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

// This entry point is only used for native specialization constants (SPIR-V),
// and the CUDA plugin is AOT only so this entry point is not supported.
UR_APIEXPORT ur_result_t UR_APICALL urProgramSetSpecializationConstants(
Expand Down
1 change: 0 additions & 1 deletion source/adapters/cuda/ur_interface_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,6 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetProgramExpProcAddrTable(
pDdiTable->pfnBuildExp = urProgramBuildExp;
pDdiTable->pfnCompileExp = urProgramCompileExp;
pDdiTable->pfnLinkExp = urProgramLinkExp;
pDdiTable->pfnCreateWithBinaryExp = urProgramCreateWithBinaryExp;

return UR_RESULT_SUCCESS;
}
Expand Down
20 changes: 12 additions & 8 deletions source/adapters/hip/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,19 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetNativeHandle(
///
/// Note: Only supports one device
UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary(
ur_context_handle_t hContext, ur_device_handle_t hDevice, size_t size,
const uint8_t *pBinary, const ur_program_properties_t *pProperties,
ur_context_handle_t hContext, uint32_t numDevices,
ur_device_handle_t *phDevices, size_t *pLengths, const uint8_t **ppBinaries,
const ur_program_properties_t *pProperties,
ur_program_handle_t *phProgram) {
if (numDevices > 1)
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;

if (numDevices == 0)
return UR_RESULT_ERROR_INVALID_DEVICE;

auto hDevice = phDevices[0];
auto pBinary = ppBinaries[0];
auto size = pLengths[0];
UR_ASSERT(std::find(hContext->getDevices().begin(),
hContext->getDevices().end(),
hDevice) != hContext->getDevices().end(),
Expand Down Expand Up @@ -522,12 +532,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary(
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinaryExp(
ur_context_handle_t, uint32_t, ur_device_handle_t *, size_t *,
const uint8_t **, const ur_program_properties_t *, ur_program_handle_t *) {
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

// This entry point is only used for native specialization constants (SPIR-V),
// and the HIP plugin is AOT only so this entry point is not supported.
UR_APIEXPORT ur_result_t UR_APICALL urProgramSetSpecializationConstants(
Expand Down
1 change: 0 additions & 1 deletion source/adapters/hip/ur_interface_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetProgramExpProcAddrTable(
pDdiTable->pfnBuildExp = urProgramBuildExp;
pDdiTable->pfnCompileExp = urProgramCompileExp;
pDdiTable->pfnLinkExp = urProgramLinkExp;
pDdiTable->pfnCreateWithBinaryExp = urProgramCreateWithBinaryExp;

return UR_RESULT_SUCCESS;
}
Expand Down
46 changes: 10 additions & 36 deletions source/adapters/level_zero/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,42 +83,6 @@ ur_result_t urProgramCreateWithIL(
}

ur_result_t urProgramCreateWithBinary(
ur_context_handle_t Context, ///< [in] handle of the context instance
ur_device_handle_t
Device, ///< [in] handle to device associated with binary.
size_t Size, ///< [in] size in bytes.
const uint8_t *Binary, ///< [in] pointer to binary.
const ur_program_properties_t
*Properties, ///< [in][optional] pointer to program creation properties.
ur_program_handle_t
*Program ///< [out] pointer to handle of Program object created.
) {
// In OpenCL, clCreateProgramWithBinary() can be used to load any of the
// following: "program executable", "compiled program", or "library of
// compiled programs". In addition, the loaded program can be either
// IL (SPIR-v) or native device code. For now, we assume that
// urProgramCreateWithBinary() is only used to load a "program executable"
// as native device code.
// If we wanted to support all the same cases as OpenCL, we would need to
// somehow examine the binary image to distinguish the cases. Alternatively,
// we could change the PI interface and have the caller pass additional
// information to distinguish the cases.

try {
ur_program_handle_t_ *UrProgram =
new ur_program_handle_t_(ur_program_handle_t_::Native, Context, 1,
&Device, Properties, &Binary, &Size);
*Program = reinterpret_cast<ur_program_handle_t>(UrProgram);
} catch (const std::bad_alloc &) {
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
} catch (...) {
return UR_RESULT_ERROR_UNKNOWN;
}

return UR_RESULT_SUCCESS;
}

ur_result_t urProgramCreateWithBinaryExp(
ur_context_handle_t hContext, ///< [in] handle of the context instance
uint32_t numDevices, ///< [in] number of devices
ur_device_handle_t
Expand All @@ -135,6 +99,16 @@ ur_result_t urProgramCreateWithBinaryExp(
ur_program_handle_t
*phProgram ///< [out] pointer to handle of Program object created.
) {
// In OpenCL, clCreateProgramWithBinary() can be used to load any of the
// following: "program executable", "compiled program", or "library of
// compiled programs". In addition, the loaded program can be either
// IL (SPIR-v) or native device code. For now, we assume that
// urProgramCreateWithBinary() is only used to load a "program executable"
// as native device code.
// If we wanted to support all the same cases as OpenCL, we would need to
// somehow examine the binary image to distinguish the cases. Alternatively,
// we could change the PI interface and have the caller pass additional
// information to distinguish the cases.
try {
ur_program_handle_t_ *UrProgram = new ur_program_handle_t_(
ur_program_handle_t_::Native, hContext, numDevices, phDevices,
Expand Down
15 changes: 12 additions & 3 deletions source/adapters/native_cpu/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,19 @@ deserializeWGMetadata(const ur_program_metadata_t &MetadataElement,
}

UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary(
ur_context_handle_t hContext, ur_device_handle_t hDevice, size_t size,
const uint8_t *pBinary, const ur_program_properties_t *pProperties,
ur_context_handle_t hContext, uint32_t numDevices,
ur_device_handle_t *phDevices, size_t *pLengths, const uint8_t **ppBinaries,
const ur_program_properties_t *pProperties,
ur_program_handle_t *phProgram) {
std::ignore = size;
if (numDevices > 1)
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;

if (numDevices == 0)
return UR_RESULT_ERROR_INVALID_DEVICE;

auto hDevice = phDevices[0];
auto pBinary = ppBinaries[0];
std::ignore = pLengths;
std::ignore = pProperties;

UR_ASSERT(hContext, UR_RESULT_ERROR_INVALID_NULL_HANDLE);
Expand Down
1 change: 0 additions & 1 deletion source/adapters/native_cpu/ur_interface_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetProgramExpProcAddrTable(
pDdiTable->pfnBuildExp = urProgramBuildExp;
pDdiTable->pfnCompileExp = urProgramCompileExp;
pDdiTable->pfnLinkExp = urProgramLinkExp;
pDdiTable->pfnCreateWithBinaryExp = urProgramCreateWithBinaryExp;

return UR_RESULT_SUCCESS;
}
Expand Down
21 changes: 8 additions & 13 deletions source/adapters/opencl/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,24 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL(
}

UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary(
ur_context_handle_t hContext, ur_device_handle_t hDevice, size_t size,
const uint8_t *pBinary, const ur_program_properties_t *,
ur_context_handle_t hContext, uint32_t numDevices,
ur_device_handle_t *phDevices, size_t *pLengths, const uint8_t **ppBinaries,
const ur_program_properties_t *pProperties,
ur_program_handle_t *phProgram) {

const cl_device_id Devices[1] = {cl_adapter::cast<cl_device_id>(hDevice)};
const size_t Lengths[1] = {size};
cl_int BinaryStatus[1];
cl_device_id Devices[numDevices];
for (uint32_t i = 0; i < numDevices; ++i)
Devices[i] = cl_adapter::cast<cl_device_id>(phDevices[i]);
cl_int BinaryStatus[numDevices];
cl_int CLResult;
*phProgram = cl_adapter::cast<ur_program_handle_t>(clCreateProgramWithBinary(
cl_adapter::cast<cl_context>(hContext), cl_adapter::cast<cl_uint>(1u),
Devices, Lengths, &pBinary, BinaryStatus, &CLResult));
Devices, pLengths, ppBinaries, BinaryStatus, &CLResult));
CL_RETURN_ON_FAILURE(BinaryStatus[0]);
CL_RETURN_ON_FAILURE(CLResult);

return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinaryExp(
ur_context_handle_t, uint32_t, ur_device_handle_t *, size_t *,
const uint8_t **, const ur_program_properties_t *, ur_program_handle_t *) {
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

UR_APIEXPORT ur_result_t UR_APICALL
urProgramCompile([[maybe_unused]] ur_context_handle_t hContext,
ur_program_handle_t hProgram, const char *pOptions) {
Expand Down
1 change: 0 additions & 1 deletion source/adapters/opencl/ur_interface_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetProgramExpProcAddrTable(
pDdiTable->pfnBuildExp = urProgramBuildExp;
pDdiTable->pfnCompileExp = urProgramCompileExp;
pDdiTable->pfnLinkExp = urProgramLinkExp;
pDdiTable->pfnCreateWithBinaryExp = urProgramCreateWithBinaryExp;

return UR_RESULT_SUCCESS;
}
Expand Down
42 changes: 24 additions & 18 deletions test/adapters/cuda/kernel_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ const char *threeParamsTwoLocal = "\n\
TEST_P(cudaKernelTest, CreateProgramAndKernel) {

uur::raii::Program program = nullptr;
ASSERT_SUCCESS(urProgramCreateWithBinary(
context, device, std::strlen(ptxSource), (const uint8_t *)ptxSource,
nullptr, program.ptr()));
auto Length = std::strlen(ptxSource);
ASSERT_SUCCESS(urProgramCreateWithBinary(context, 1, &device, &Length,
(const uint8_t **)(&ptxSource),
nullptr, program.ptr()));
ASSERT_NE(program, nullptr);
ASSERT_SUCCESS(urProgramBuild(context, program, nullptr));

Expand Down Expand Up @@ -116,9 +117,10 @@ TEST_P(cudaKernelTest, CreateProgramAndKernelWithMetadata) {
ur_program_properties_t programProps{UR_STRUCTURE_TYPE_PROGRAM_PROPERTIES,
nullptr, 1, &reqdWorkGroupSizeMDProp};
uur::raii::Program program = nullptr;
ASSERT_SUCCESS(urProgramCreateWithBinary(
context, device, std::strlen(ptxSource), (const uint8_t *)ptxSource,
&programProps, program.ptr()));
auto Length = std::strlen(ptxSource);
ASSERT_SUCCESS(urProgramCreateWithBinary(context, 1, &device, &Length,
(const uint8_t **)(&ptxSource),
&programProps, program.ptr()));
ASSERT_NE(program, nullptr);

ASSERT_SUCCESS(urProgramBuild(context, program, nullptr));
Expand All @@ -138,9 +140,10 @@ TEST_P(cudaKernelTest, CreateProgramAndKernelWithMetadata) {

TEST_P(cudaKernelTest, URKernelArgumentSimple) {
uur::raii::Program program = nullptr;
ASSERT_SUCCESS(urProgramCreateWithBinary(
context, device, std::strlen(ptxSource), (const uint8_t *)ptxSource,
nullptr, program.ptr()));
auto Length = std::strlen(ptxSource);
ASSERT_SUCCESS(urProgramCreateWithBinary(context, 1, &device, &Length,
(const uint8_t **)(&ptxSource),
nullptr, program.ptr()));
ASSERT_NE(program, nullptr);
ASSERT_SUCCESS(urProgramBuild(context, program, nullptr));

Expand All @@ -160,9 +163,10 @@ TEST_P(cudaKernelTest, URKernelArgumentSimple) {

TEST_P(cudaKernelTest, URKernelArgumentSetTwice) {
uur::raii::Program program = nullptr;
ASSERT_SUCCESS(urProgramCreateWithBinary(
context, device, std::strlen(ptxSource), (const uint8_t *)ptxSource,
nullptr, program.ptr()));
auto Length = std::strlen(ptxSource);
ASSERT_SUCCESS(urProgramCreateWithBinary(context, 1, &device, &Length,
(const uint8_t **)(&ptxSource),
nullptr, program.ptr()));
ASSERT_NE(program, nullptr);
ASSERT_SUCCESS(urProgramBuild(context, program, nullptr));

Expand All @@ -189,9 +193,10 @@ TEST_P(cudaKernelTest, URKernelArgumentSetTwice) {

TEST_P(cudaKernelTest, URKernelDispatch) {
uur::raii::Program program = nullptr;
ASSERT_SUCCESS(urProgramCreateWithBinary(
context, device, std::strlen(ptxSource), (const uint8_t *)ptxSource,
nullptr, program.ptr()));
auto Length = std::strlen(ptxSource);
ASSERT_SUCCESS(urProgramCreateWithBinary(context, 1, &device, &Length,
(const uint8_t **)(&ptxSource),
nullptr, program.ptr()));
ASSERT_NE(program, nullptr);
ASSERT_SUCCESS(urProgramBuild(context, program, nullptr));

Expand All @@ -218,9 +223,10 @@ TEST_P(cudaKernelTest, URKernelDispatch) {

TEST_P(cudaKernelTest, URKernelDispatchTwo) {
uur::raii::Program program = nullptr;
ASSERT_SUCCESS(urProgramCreateWithBinary(
context, device, std::strlen(ptxSource), (const uint8_t *)twoParams,
nullptr, program.ptr()));
auto Length = std::strlen(ptxSource);
ASSERT_SUCCESS(urProgramCreateWithBinary(context, 1, &device, &Length,
(const uint8_t **)(&twoParams),
nullptr, program.ptr()));
ASSERT_NE(program, nullptr);
ASSERT_SUCCESS(urProgramBuild(context, program, nullptr));

Expand Down

0 comments on commit 584869e

Please sign in to comment.