Skip to content

Commit ea2e634

Browse files
Add clCreateBufferWithPropertiesIntel() API
Change-Id: Icfbbbc2479c1bc94008e0ccf90bcb25adddf0b61 Signed-off-by: Filip Hazubski <filip.hazubski@intel.com>
1 parent f583ceb commit ea2e634

File tree

12 files changed

+286
-108
lines changed

12 files changed

+286
-108
lines changed

public/cl_ext_private.h

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
*/
77

88
#pragma once
9+
#include "CL/cl.h"
910

10-
/***************************************
11-
* * Internal only queue properties *
12-
* ****************************************/
11+
/**********************************
12+
* Internal only queue properties *
13+
**********************************/
1314
// Intel evaluation now. Remove it after approval for public release
1415
#define CL_DEVICE_DRIVER_VERSION_INTEL 0x10010
1516

1617
#define CL_DEVICE_DRIVER_VERSION_INTEL_NEO1 0x454E4831 // Driver version is ENH1
1718

18-
/***************************************
19-
* * cl_intel_debug_info extension *
20-
* ****************************************/
19+
/*********************************
20+
* cl_intel_debug_info extension *
21+
*********************************/
2122
#define cl_intel_debug_info 1
2223

2324
// New queries for clGetProgramInfo:
@@ -29,8 +30,26 @@
2930
#define CL_KERNEL_BINARIES_INTEL 0x4102
3031
#define CL_KERNEL_BINARY_SIZES_INTEL 0x4103
3132

32-
/***************************************
33-
* * event properties for performance counter *
34-
* ****************************************/
33+
/********************************************
34+
* event properties for performance counter *
35+
********************************************/
3536
/* performance counter */
3637
#define CL_PROFILING_COMMAND_PERFCOUNTERS_INTEL 0x407F
38+
39+
/**************************
40+
* Internal only cl types *
41+
**************************/
42+
43+
using cl_mem_properties_intel = cl_bitfield;
44+
using cl_mem_flags_intel = cl_mem_flags;
45+
46+
struct MemoryProperties {
47+
cl_mem_flags flags = 0;
48+
cl_mem_flags_intel flags_intel = 0;
49+
};
50+
51+
/******************************
52+
* Internal only cl_mem_flags *
53+
******************************/
54+
55+
#define CL_MEM_FLAGS_INTEL 0x10001

runtime/api/api.cpp

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -536,59 +536,49 @@ cl_mem CL_API_CALL clCreateBuffer(cl_context context,
536536
size_t size,
537537
void *hostPtr,
538538
cl_int *errcodeRet) {
539-
cl_int retVal = CL_SUCCESS;
540-
API_ENTER(&retVal);
541539
DBG_LOG_INPUTS("cl_context", context,
542540
"cl_mem_flags", flags,
543541
"size", size,
544542
"hostPtr", DebugManager.infoPointerToString(hostPtr, size));
545-
cl_mem buffer = nullptr;
546543

547-
do {
548-
if (size == 0) {
549-
retVal = CL_INVALID_BUFFER_SIZE;
550-
break;
551-
}
552-
553-
/* Are there some invalid flag bits? */
554-
if (!MemObjHelper::checkMemFlagsForBuffer(flags)) {
555-
retVal = CL_INVALID_VALUE;
556-
break;
557-
}
544+
cl_int retVal = CL_SUCCESS;
545+
API_ENTER(&retVal);
546+
cl_mem buffer = nullptr;
547+
ErrorCodeHelper err(errcodeRet, CL_SUCCESS);
558548

559-
/* Check all the invalid flags combination. */
560-
if (((flags & CL_MEM_READ_WRITE) && (flags & (CL_MEM_READ_ONLY | CL_MEM_WRITE_ONLY))) ||
561-
((flags & CL_MEM_READ_ONLY) && (flags & (CL_MEM_WRITE_ONLY))) ||
562-
((flags & CL_MEM_ALLOC_HOST_PTR) && (flags & CL_MEM_USE_HOST_PTR)) ||
563-
((flags & CL_MEM_COPY_HOST_PTR) && (flags & CL_MEM_USE_HOST_PTR)) ||
564-
((flags & CL_MEM_HOST_READ_ONLY) && (flags & CL_MEM_HOST_NO_ACCESS)) ||
565-
((flags & CL_MEM_HOST_READ_ONLY) && (flags & CL_MEM_HOST_WRITE_ONLY)) ||
566-
((flags & CL_MEM_HOST_WRITE_ONLY) && (flags & CL_MEM_HOST_NO_ACCESS))) {
567-
retVal = CL_INVALID_VALUE;
568-
break;
569-
}
549+
MemoryProperties propertiesStruct;
550+
propertiesStruct.flags = flags;
551+
Buffer::validateInputAndCreateBuffer(context, propertiesStruct, size, hostPtr, retVal, buffer);
570552

571-
/* Check the host ptr and data */
572-
if ((((flags & CL_MEM_COPY_HOST_PTR) || (flags & CL_MEM_USE_HOST_PTR)) && hostPtr == nullptr) ||
573-
(!(flags & (CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR)) && (hostPtr != nullptr))) {
574-
retVal = CL_INVALID_HOST_PTR;
575-
break;
576-
}
553+
err.set(retVal);
554+
DBG_LOG_INPUTS("buffer", buffer);
555+
return buffer;
556+
}
577557

578-
Context *pContext = nullptr;
558+
cl_mem CL_API_CALL clCreateBufferWithPropertiesINTEL(cl_context context,
559+
const cl_mem_properties_intel *properties,
560+
size_t size,
561+
void *hostPtr,
562+
cl_int *errcodeRet) {
579563

580-
retVal = validateObjects(WithCastToInternal(context, &pContext));
581-
if (retVal != CL_SUCCESS) {
582-
break;
583-
}
564+
DBG_LOG_INPUTS("cl_context", context,
565+
"cl_mem_properties_intel", properties,
566+
"size", size,
567+
"hostPtr", DebugManager.infoPointerToString(hostPtr, size));
584568

585-
// create the buffer
586-
buffer = Buffer::create(pContext, flags, size, hostPtr, retVal);
587-
} while (false);
569+
cl_int retVal = CL_SUCCESS;
570+
API_ENTER(&retVal);
571+
cl_mem buffer = nullptr;
572+
ErrorCodeHelper err(errcodeRet, CL_SUCCESS);
588573

589-
if (errcodeRet) {
590-
*errcodeRet = retVal;
574+
MemoryProperties propertiesStruct;
575+
if (!MemObjHelper::parseMemoryProperties(properties, propertiesStruct)) {
576+
retVal = CL_INVALID_VALUE;
577+
} else {
578+
Buffer::validateInputAndCreateBuffer(context, propertiesStruct, size, hostPtr, retVal, buffer);
591579
}
580+
581+
err.set(retVal);
592582
DBG_LOG_INPUTS("buffer", buffer);
593583
return buffer;
594584
}

runtime/api/api.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "CL/cl.h"
99
#include "CL/cl_gl.h"
10+
#include "public/cl_ext_private.h"
1011
#include "runtime/api/dispatch.h"
1112

1213
#ifdef __cplusplus
@@ -120,6 +121,13 @@ cl_mem CL_API_CALL clCreateBuffer(
120121
void *hostPtr,
121122
cl_int *errcodeRet);
122123

124+
cl_mem CL_API_CALL clCreateBufferWithPropertiesINTEL(
125+
cl_context context,
126+
const cl_mem_properties_intel *properties,
127+
size_t size,
128+
void *hostPtr,
129+
cl_int *errcodeRet);
130+
123131
cl_mem CL_API_CALL clCreateSubBuffer(
124132
cl_mem buffer,
125133
cl_mem_flags flags,

runtime/mem_obj/buffer.cpp

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,54 @@ bool Buffer::isValidSubBufferOffset(size_t offset) {
6868
return false;
6969
}
7070

71+
void Buffer::validateInputAndCreateBuffer(cl_context &context,
72+
MemoryProperties properties,
73+
size_t size,
74+
void *hostPtr,
75+
cl_int &retVal,
76+
cl_mem &buffer) {
77+
if (size == 0) {
78+
retVal = CL_INVALID_BUFFER_SIZE;
79+
return;
80+
}
81+
82+
if (!MemObjHelper::validateMemoryProperties(properties)) {
83+
retVal = CL_INVALID_VALUE;
84+
return;
85+
}
86+
87+
/* Check the host ptr and data */
88+
bool expectHostPtr = (properties.flags & (CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR)) != 0;
89+
if ((hostPtr == nullptr) == expectHostPtr) {
90+
retVal = CL_INVALID_HOST_PTR;
91+
return;
92+
}
93+
94+
Context *pContext = nullptr;
95+
retVal = validateObjects(WithCastToInternal(context, &pContext));
96+
if (retVal != CL_SUCCESS) {
97+
return;
98+
}
99+
100+
// create the buffer
101+
buffer = create(pContext, properties, size, hostPtr, retVal);
102+
}
103+
71104
Buffer *Buffer::create(Context *context,
72105
cl_mem_flags flags,
73106
size_t size,
74107
void *hostPtr,
75108
cl_int &errcodeRet) {
109+
MemoryProperties properties;
110+
properties.flags = flags;
111+
return create(context, properties, size, hostPtr, errcodeRet);
112+
}
113+
114+
Buffer *Buffer::create(Context *context,
115+
MemoryProperties properties,
116+
size_t size,
117+
void *hostPtr,
118+
cl_int &errcodeRet) {
76119
Buffer *pBuffer = nullptr;
77120
errcodeRet = CL_SUCCESS;
78121

@@ -84,14 +127,14 @@ Buffer *Buffer::create(Context *context,
84127
bool allocateMemory = true;
85128
bool copyMemoryFromHostPtr = false;
86129
GraphicsAllocation::AllocationType allocationType = getGraphicsAllocationType(
87-
flags,
130+
properties.flags,
88131
context->isSharedContext,
89132
context->getDevice(0)->getHardwareInfo().capabilityTable.ftrRenderCompressedBuffers);
90133

91134
MemoryManager *memoryManager = context->getMemoryManager();
92135
UNRECOVERABLE_IF(!memoryManager);
93136

94-
checkMemory(flags, size, hostPtr, errcodeRet, alignementSatisfied, copyMemoryFromHostPtr, memoryManager);
137+
checkMemory(properties.flags, size, hostPtr, errcodeRet, alignementSatisfied, copyMemoryFromHostPtr, memoryManager);
95138

96139
if (errcodeRet != CL_SUCCESS) {
97140
return nullptr;
@@ -100,16 +143,16 @@ Buffer *Buffer::create(Context *context,
100143
if (allocationType == GraphicsAllocation::AllocationType::BUFFER_COMPRESSED) {
101144
zeroCopyAllowed = false;
102145
allocateMemory = true;
103-
if (flags & CL_MEM_USE_HOST_PTR) {
146+
if (properties.flags & CL_MEM_USE_HOST_PTR) {
104147
copyMemoryFromHostPtr = true;
105148
}
106149
}
107150

108151
if (allocationType == GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY) {
109-
if (flags & CL_MEM_ALLOC_HOST_PTR) {
152+
if (properties.flags & CL_MEM_ALLOC_HOST_PTR) {
110153
zeroCopyAllowed = true;
111154
allocateMemory = true;
112-
} else if (flags & CL_MEM_USE_HOST_PTR) {
155+
} else if (properties.flags & CL_MEM_USE_HOST_PTR) {
113156
allocateMemory = false;
114157
if (!alignementSatisfied || DebugManager.flags.DisableZeroCopyForUseHostPtr.get()) {
115158
zeroCopyAllowed = false;
@@ -124,7 +167,7 @@ Buffer *Buffer::create(Context *context,
124167
allocateMemory = false;
125168
}
126169

127-
if (flags & CL_MEM_USE_HOST_PTR) {
170+
if (properties.flags & CL_MEM_USE_HOST_PTR) {
128171
memory = context->getSVMAllocsManager()->getSVMAlloc(hostPtr);
129172
if (memory) {
130173
allocationType = GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY;
@@ -152,8 +195,8 @@ Buffer *Buffer::create(Context *context,
152195
}
153196

154197
if (!memory) {
155-
AllocationFlags allocFlags = MemObjHelper::getAllocationFlags(flags, allocateMemory);
156-
DevicesBitfield devices = MemObjHelper::getDevicesBitfield(flags);
198+
AllocationFlags allocFlags = MemObjHelper::getAllocationFlags(properties.flags, allocateMemory);
199+
DevicesBitfield devices = MemObjHelper::getDevicesBitfield(properties.flags_intel);
157200
memory = memoryManager->allocateGraphicsMemoryInPreferredPool(allocFlags, devices, hostPtr, static_cast<size_t>(size), allocationType);
158201
}
159202

@@ -163,12 +206,12 @@ Buffer *Buffer::create(Context *context,
163206

164207
// if memory pointer should not be allcoated and graphics allocation is nullptr
165208
// and cl_mem flags allow, create non-zerocopy buffer
166-
if (!allocateMemory && !memory && Buffer::isReadOnlyMemoryPermittedByFlags(flags)) {
209+
if (!allocateMemory && !memory && Buffer::isReadOnlyMemoryPermittedByFlags(properties.flags)) {
167210
allocationType = GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY;
168211
zeroCopyAllowed = false;
169212
copyMemoryFromHostPtr = true;
170-
AllocationFlags allocFlags = MemObjHelper::getAllocationFlags(flags, true);
171-
DevicesBitfield devices = MemObjHelper::getDevicesBitfield(flags);
213+
AllocationFlags allocFlags = MemObjHelper::getAllocationFlags(properties.flags, true);
214+
DevicesBitfield devices = MemObjHelper::getDevicesBitfield(properties.flags_intel);
172215
memory = memoryManager->allocateGraphicsMemoryInPreferredPool(allocFlags, devices, nullptr, static_cast<size_t>(size), allocationType);
173216
}
174217

@@ -184,12 +227,12 @@ Buffer *Buffer::create(Context *context,
184227
}
185228

186229
memory->setAllocationType(allocationType);
187-
memory->setMemObjectsAllocationWithWritableFlags(!(flags & (CL_MEM_READ_ONLY | CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS)));
230+
memory->setMemObjectsAllocationWithWritableFlags(!(properties.flags & (CL_MEM_READ_ONLY | CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS)));
188231

189232
DBG_LOG(LogMemoryObject, __FUNCTION__, "hostPtr:", hostPtr, "size:", size, "memoryStorage:", memory->getUnderlyingBuffer(), "GPU address:", std::hex, memory->getGpuAddress());
190233

191234
pBuffer = createBufferHw(context,
192-
flags,
235+
properties.flags,
193236
size,
194237
memory->getUnderlyingBuffer(),
195238
const_cast<void *>(hostPtr),

runtime/mem_obj/buffer.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "runtime/memory_manager/memory_constants.h"
1010
#include "runtime/mem_obj/mem_obj.h"
1111
#include "runtime/helpers/basic_math.h"
12+
#include "public/cl_ext_private.h"
1213
#include "igfxfmid.h"
1314

1415
namespace OCLRT {
@@ -40,12 +41,26 @@ class Buffer : public MemObj {
4041
bool forceDisallowCPUCopy = false;
4142

4243
~Buffer() override;
44+
45+
static void validateInputAndCreateBuffer(cl_context &context,
46+
MemoryProperties properties,
47+
size_t size,
48+
void *hostPtr,
49+
cl_int &retVal,
50+
cl_mem &buffer);
51+
4352
static Buffer *create(Context *context,
4453
cl_mem_flags flags,
4554
size_t size,
4655
void *hostPtr,
4756
cl_int &errcodeRet);
4857

58+
static Buffer *create(Context *context,
59+
MemoryProperties properties,
60+
size_t size,
61+
void *hostPtr,
62+
cl_int &errcodeRet);
63+
4964
static Buffer *createSharedBuffer(Context *context,
5065
cl_mem_flags flags,
5166
SharingHandler *sharingHandler,

runtime/mem_obj/mem_obj_helper.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,28 @@
99

1010
namespace OCLRT {
1111

12-
bool MemObjHelper::checkExtraMemFlagsForBuffer(cl_mem_flags flags) {
13-
return false;
12+
bool MemObjHelper::parseMemoryProperties(const cl_mem_properties_intel *properties, MemoryProperties &propertiesStruct) {
13+
if (properties == nullptr) {
14+
return true;
15+
}
16+
17+
for (int i = 0; properties[i] != 0; i += 2) {
18+
switch (properties[i]) {
19+
case CL_MEM_FLAGS:
20+
propertiesStruct.flags |= static_cast<cl_mem_flags>(properties[i + 1]);
21+
break;
22+
case CL_MEM_FLAGS_INTEL:
23+
propertiesStruct.flags_intel |= static_cast<cl_mem_flags_intel>(properties[i + 1]);
24+
break;
25+
default:
26+
return false;
27+
}
28+
}
29+
return true;
30+
}
31+
32+
bool MemObjHelper::validateExtraMemoryProperties(const MemoryProperties &properties) {
33+
return true;
1434
}
1535

1636
AllocationFlags MemObjHelper::getAllocationFlags(cl_mem_flags flags, bool allocateMemory) {

0 commit comments

Comments
 (0)