Skip to content

Commit 15a52e1

Browse files
[SYCL][L0] Initialize descriptor .stype and .pNext (#4032)
Signed-off-by: Sergey V Maslov <sergey.v.maslov@intel.com>
1 parent 69a68a6 commit 15a52e1

File tree

2 files changed

+78
-25
lines changed

2 files changed

+78
-25
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,7 @@ _pi_context::getFreeSlotInExistingOrNewPool(ze_event_pool_handle_t &ZePool,
346346
std::lock_guard<std::mutex> NumEventsLiveInEventPoolGuard(
347347
NumEventsLiveInEventPoolMutex, std::adopt_lock);
348348

349-
ze_event_pool_desc_t ZeEventPoolDesc = {};
350-
ZeEventPoolDesc.stype = ZE_STRUCTURE_TYPE_EVENT_POOL_DESC;
349+
ZeStruct<ze_event_pool_desc_t> ZeEventPoolDesc;
351350
ZeEventPoolDesc.count = MaxNumEventsPerPool;
352351

353352
// Make all events visible on the host.
@@ -585,7 +584,7 @@ pi_result _pi_context::initialize() {
585584
// TODO: get rid of using Devices[0] for the context with multiple
586585
// root-devices. We should somehow make the data initialized on all devices.
587586
pi_device Device = SingleRootDevice ? SingleRootDevice : Devices[0];
588-
ze_command_queue_desc_t ZeCommandQueueDesc = {};
587+
ZeStruct<ze_command_queue_desc_t> ZeCommandQueueDesc;
589588
ZeCommandQueueDesc.ordinal = Device->ZeComputeQueueGroupIndex;
590589
ZeCommandQueueDesc.index = 0;
591590
ZeCommandQueueDesc.mode = ZE_COMMAND_QUEUE_MODE_SYNCHRONOUS;
@@ -733,10 +732,8 @@ pi_result _pi_context::getAvailableCommandList(
733732
// Each command list is paired with an associated fence to track when the
734733
// command list is available for reuse.
735734
_pi_result pi_result = PI_OUT_OF_RESOURCES;
736-
ze_command_list_desc_t ZeCommandListDesc = {};
737-
ZeCommandListDesc.stype = ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC;
738-
ze_fence_desc_t ZeFenceDesc = {};
739-
ZeFenceDesc.stype = ZE_STRUCTURE_TYPE_FENCE_DESC;
735+
ZeStruct<ze_command_list_desc_t> ZeCommandListDesc;
736+
ZeStruct<ze_fence_desc_t> ZeFenceDesc;
740737

741738
ZeCommandListDesc.commandQueueGroupOrdinal =
742739
(UseCopyEngine) ? Queue->Device->ZeCopyQueueGroupIndex
@@ -2224,7 +2221,9 @@ pi_result piContextCreate(const pi_context_properties *Properties,
22242221
PI_ASSERT(RetContext, PI_INVALID_VALUE);
22252222

22262223
pi_platform Platform = (*Devices)->Platform;
2227-
ze_context_desc_t ContextDesc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0};
2224+
ZeStruct<ze_context_desc_t> ContextDesc;
2225+
ContextDesc.flags = 0;
2226+
22282227
ze_context_handle_t ZeContext;
22292228
ZE_CALL(zeContextCreate, (Platform->ZeDriver, &ContextDesc, &ZeContext));
22302229
try {
@@ -2389,7 +2388,7 @@ pi_result piQueueCreate(pi_context Context, pi_device Device,
23892388
PI_ASSERT(Device, PI_INVALID_DEVICE);
23902389

23912390
ZeDevice = Device->ZeDevice;
2392-
ze_command_queue_desc_t ZeCommandQueueDesc = {};
2391+
ZeStruct<ze_command_queue_desc_t> ZeCommandQueueDesc;
23932392
ZeCommandQueueDesc.ordinal = Device->ZeComputeQueueGroupIndex;
23942393
ZeCommandQueueDesc.index = 0;
23952394
ZeCommandQueueDesc.mode = ZE_COMMAND_QUEUE_MODE_ASYNCHRONOUS;
@@ -2840,7 +2839,7 @@ pi_result piMemImageCreate(pi_context Context, pi_mem_flags Flags,
28402839
return PI_INVALID_VALUE;
28412840
}
28422841

2843-
ze_image_desc_t ZeImageDesc = {};
2842+
ZeStruct<ze_image_desc_t> ZeImageDesc;
28442843
ZeImageDesc.arraylevels = ZeImageDesc.flags = 0;
28452844
ZeImageDesc.type = ZeImageType;
28462845
ZeImageDesc.format = ZeFormatDesc;
@@ -3340,7 +3339,7 @@ static pi_result compileOrBuild(pi_program Program, pi_uint32 NumDevices,
33403339
}
33413340

33423341
// Ask Level Zero to build and load the native code onto the device.
3343-
ze_module_desc_t ZeModuleDesc = {};
3342+
ZeStruct<ze_module_desc_t> ZeModuleDesc;
33443343
ZeModuleDesc.format = (Program->State == _pi_program::IL)
33453344
? ZE_MODULE_FORMAT_IL_SPIRV
33463345
: ZE_MODULE_FORMAT_NATIVE;
@@ -3515,7 +3514,7 @@ static pi_result copyModule(ze_context_handle_t ZeContext,
35153514
std::unique_ptr<uint8_t[]> Code(new uint8_t[Length]);
35163515
ZE_CALL(zeModuleGetNativeBinary, (SrcMod, &Length, Code.get()));
35173516

3518-
ze_module_desc_t ZeModuleDesc = {};
3517+
ZeStruct<ze_module_desc_t> ZeModuleDesc;
35193518
ZeModuleDesc.format = ZE_MODULE_FORMAT_NATIVE;
35203519
ZeModuleDesc.inputSize = Length;
35213520
ZeModuleDesc.pInputModule = Code.get();
@@ -3602,7 +3601,7 @@ pi_result piKernelCreate(pi_program Program, const char *KernelName,
36023601
return PI_INVALID_PROGRAM_EXECUTABLE;
36033602
}
36043603

3605-
ze_kernel_desc_t ZeKernelDesc = {};
3604+
ZeStruct<ze_kernel_desc_t> ZeKernelDesc;
36063605
ZeKernelDesc.flags = 0;
36073606
ZeKernelDesc.pKernelName = KernelName;
36083607

@@ -4067,7 +4066,7 @@ pi_result piEventCreate(pi_context Context, pi_event *RetEvent) {
40674066
return Res;
40684067

40694068
ze_event_handle_t ZeEvent;
4070-
ze_event_desc_t ZeEventDesc = {};
4069+
ZeStruct<ze_event_desc_t> ZeEventDesc;
40714070
// We have to set the SIGNAL flag as HOST scope because the
40724071
// Level-Zero plugin implementation waits for the events to complete
40734072
// on the host.
@@ -4470,7 +4469,7 @@ pi_result piSamplerCreate(pi_context Context,
44704469
pi_device Device = Context->Devices[0];
44714470

44724471
ze_sampler_handle_t ZeSampler;
4473-
ze_sampler_desc_t ZeSamplerDesc = {};
4472+
ZeStruct<ze_sampler_desc_t> ZeSamplerDesc;
44744473

44754474
// Set the default values for the ZeSamplerDesc.
44764475
ZeSamplerDesc.isNormalized = PI_TRUE;
@@ -5186,7 +5185,7 @@ pi_result piEnqueueMemBufferMap(pi_queue Queue, pi_mem Buffer,
51865185
// Use the version with reference counting
51875186
PI_CALL(piextUSMHostAlloc(RetMap, Queue->Context, nullptr, Size, 1));
51885187
} else {
5189-
ze_host_mem_alloc_desc_t ZeDesc = {};
5188+
ZeStruct<ze_host_mem_alloc_desc_t> ZeDesc;
51905189
ZeDesc.flags = 0;
51915190

51925191
ZE_CALL(zeMemAllocHost,
@@ -5716,14 +5715,13 @@ static pi_result USMDeviceAllocImpl(void **ResultPtr, pi_context Context,
57165715
PI_INVALID_VALUE);
57175716

57185717
// TODO: translate PI properties to Level Zero flags
5719-
ze_device_mem_alloc_desc_t ZeDesc = {};
5718+
ZeStruct<ze_device_mem_alloc_desc_t> ZeDesc;
57205719
ZeDesc.flags = 0;
57215720
ZeDesc.ordinal = 0;
57225721

5723-
ze_relaxed_allocation_limits_exp_desc_t RelaxedDesc = {};
5722+
ZeStruct<ze_relaxed_allocation_limits_exp_desc_t> RelaxedDesc;
57245723
if (Size > Device->ZeDeviceProperties.maxMemAllocSize) {
57255724
// Tell Level-Zero to accept Size > maxMemAllocSize
5726-
RelaxedDesc.stype = ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC;
57275725
RelaxedDesc.flags = ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE;
57285726
ZeDesc.pNext = &RelaxedDesc;
57295727
}
@@ -5750,16 +5748,15 @@ static pi_result USMSharedAllocImpl(void **ResultPtr, pi_context Context,
57505748
PI_INVALID_VALUE);
57515749

57525750
// TODO: translate PI properties to Level Zero flags
5753-
ze_host_mem_alloc_desc_t ZeHostDesc = {};
5751+
ZeStruct<ze_host_mem_alloc_desc_t> ZeHostDesc;
57545752
ZeHostDesc.flags = 0;
5755-
ze_device_mem_alloc_desc_t ZeDevDesc = {};
5753+
ZeStruct<ze_device_mem_alloc_desc_t> ZeDevDesc;
57565754
ZeDevDesc.flags = 0;
57575755
ZeDevDesc.ordinal = 0;
57585756

5759-
ze_relaxed_allocation_limits_exp_desc_t RelaxedDesc = {};
5757+
ZeStruct<ze_relaxed_allocation_limits_exp_desc_t> RelaxedDesc;
57605758
if (Size > Device->ZeDeviceProperties.maxMemAllocSize) {
57615759
// Tell Level-Zero to accept Size > maxMemAllocSize
5762-
RelaxedDesc.stype = ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC;
57635760
RelaxedDesc.flags = ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE;
57645761
ZeDevDesc.pNext = &RelaxedDesc;
57655762
}
@@ -5784,7 +5781,7 @@ static pi_result USMHostAllocImpl(void **ResultPtr, pi_context Context,
57845781
PI_INVALID_VALUE);
57855782

57865783
// TODO: translate PI properties to Level Zero flags
5787-
ze_host_mem_alloc_desc_t ZeHostDesc = {};
5784+
ZeStruct<ze_host_mem_alloc_desc_t> ZeHostDesc;
57885785
ZeHostDesc.flags = 0;
57895786
ZE_CALL(zeMemAllocHost,
57905787
(Context->ZeContext, &ZeHostDesc, Size, Alignment, ResultPtr));

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,62 @@ template <> uint32_t pi_cast(uint64_t Value) {
5555
std::terminate();
5656
}
5757

58+
// Returns the ze_structure_type_t to use in .stype of a structured descriptor.
59+
// Intentionally not defined; will give an error if no proper specialization
60+
template <class T> ze_structure_type_t getZeStructureType();
61+
62+
template <> ze_structure_type_t getZeStructureType<ze_event_pool_desc_t>() {
63+
return ZE_STRUCTURE_TYPE_EVENT_POOL_DESC;
64+
}
65+
template <> ze_structure_type_t getZeStructureType<ze_fence_desc_t>() {
66+
return ZE_STRUCTURE_TYPE_FENCE_DESC;
67+
}
68+
template <> ze_structure_type_t getZeStructureType<ze_command_list_desc_t>() {
69+
return ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC;
70+
}
71+
template <> ze_structure_type_t getZeStructureType<ze_context_desc_t>() {
72+
return ZE_STRUCTURE_TYPE_CONTEXT_DESC;
73+
}
74+
template <>
75+
ze_structure_type_t
76+
getZeStructureType<ze_relaxed_allocation_limits_exp_desc_t>() {
77+
return ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC;
78+
}
79+
template <> ze_structure_type_t getZeStructureType<ze_host_mem_alloc_desc_t>() {
80+
return ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC;
81+
}
82+
template <>
83+
ze_structure_type_t getZeStructureType<ze_device_mem_alloc_desc_t>() {
84+
return ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC;
85+
}
86+
template <> ze_structure_type_t getZeStructureType<ze_command_queue_desc_t>() {
87+
return ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC;
88+
}
89+
template <> ze_structure_type_t getZeStructureType<ze_image_desc_t>() {
90+
return ZE_STRUCTURE_TYPE_IMAGE_DESC;
91+
}
92+
template <> ze_structure_type_t getZeStructureType<ze_module_desc_t>() {
93+
return ZE_STRUCTURE_TYPE_MODULE_DESC;
94+
}
95+
template <> ze_structure_type_t getZeStructureType<ze_kernel_desc_t>() {
96+
return ZE_STRUCTURE_TYPE_KERNEL_DESC;
97+
}
98+
template <> ze_structure_type_t getZeStructureType<ze_event_desc_t>() {
99+
return ZE_STRUCTURE_TYPE_EVENT_DESC;
100+
}
101+
template <> ze_structure_type_t getZeStructureType<ze_sampler_desc_t>() {
102+
return ZE_STRUCTURE_TYPE_SAMPLER_DESC;
103+
}
104+
105+
// The helper struct to properly default initialize Level-Zero descriptor
106+
// structure.
107+
template <class T> struct ZeStruct : public T {
108+
ZeStruct() {
109+
this->stype = getZeStructureType<T>();
110+
this->pNext = nullptr;
111+
}
112+
};
113+
58114
// Base class to store common data
59115
struct _pi_object {
60116
_pi_object() : RefCount{1} {}
@@ -613,7 +669,7 @@ struct _pi_image final : _pi_mem {
613669

614670
#ifndef NDEBUG
615671
// Keep the descriptor of the image (for debugging purposes)
616-
ze_image_desc_t ZeImageDesc;
672+
ZeStruct<ze_image_desc_t> ZeImageDesc;
617673
#endif // !NDEBUG
618674

619675
// Level Zero image handle.

0 commit comments

Comments
 (0)