1bf215546Sopenharmony_ci//
2bf215546Sopenharmony_ci// Copyright 2012 Francisco Jerez
3bf215546Sopenharmony_ci//
4bf215546Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci// copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci// to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci// and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci// Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci//
11bf215546Sopenharmony_ci// The above copyright notice and this permission notice shall be included in
12bf215546Sopenharmony_ci// all copies or substantial portions of the Software.
13bf215546Sopenharmony_ci//
14bf215546Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15bf215546Sopenharmony_ci// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bf215546Sopenharmony_ci// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17bf215546Sopenharmony_ci// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18bf215546Sopenharmony_ci// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19bf215546Sopenharmony_ci// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20bf215546Sopenharmony_ci// OTHER DEALINGS IN THE SOFTWARE.
21bf215546Sopenharmony_ci//
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci#include "api/util.hpp"
24bf215546Sopenharmony_ci#include "core/kernel.hpp"
25bf215546Sopenharmony_ci#include "core/event.hpp"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ciusing namespace clover;
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ciCLOVER_API cl_kernel
30bf215546Sopenharmony_ciclCreateKernel(cl_program d_prog, const char *name, cl_int *r_errcode) try {
31bf215546Sopenharmony_ci   auto &prog = obj(d_prog);
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci   if (!name)
34bf215546Sopenharmony_ci      throw error(CL_INVALID_VALUE);
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci   auto &sym = find(name_equals(name), prog.symbols());
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   ret_error(r_errcode, CL_SUCCESS);
39bf215546Sopenharmony_ci   return new kernel(prog, name, range(sym.args));
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci} catch (std::out_of_range &) {
42bf215546Sopenharmony_ci   ret_error(r_errcode, CL_INVALID_KERNEL_NAME);
43bf215546Sopenharmony_ci   return NULL;
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci} catch (error &e) {
46bf215546Sopenharmony_ci   ret_error(r_errcode, e);
47bf215546Sopenharmony_ci   return NULL;
48bf215546Sopenharmony_ci}
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ciCLOVER_API cl_int
51bf215546Sopenharmony_ciclCreateKernelsInProgram(cl_program d_prog, cl_uint count,
52bf215546Sopenharmony_ci                         cl_kernel *rd_kerns, cl_uint *r_count) try {
53bf215546Sopenharmony_ci   auto &prog = obj(d_prog);
54bf215546Sopenharmony_ci   auto &syms = prog.symbols();
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   if (rd_kerns && count < syms.size())
57bf215546Sopenharmony_ci      throw error(CL_INVALID_VALUE);
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci   if (rd_kerns)
60bf215546Sopenharmony_ci      copy(map([&](const binary::symbol &sym) {
61bf215546Sopenharmony_ci               return desc(new kernel(prog,
62bf215546Sopenharmony_ci                                      std::string(sym.name.begin(),
63bf215546Sopenharmony_ci                                                  sym.name.end()),
64bf215546Sopenharmony_ci                                      range(sym.args)));
65bf215546Sopenharmony_ci            }, syms),
66bf215546Sopenharmony_ci         rd_kerns);
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   if (r_count)
69bf215546Sopenharmony_ci      *r_count = syms.size();
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   return CL_SUCCESS;
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci} catch (error &e) {
74bf215546Sopenharmony_ci   return e.get();
75bf215546Sopenharmony_ci}
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ciCLOVER_API cl_int
78bf215546Sopenharmony_ciclRetainKernel(cl_kernel d_kern) try {
79bf215546Sopenharmony_ci   obj(d_kern).retain();
80bf215546Sopenharmony_ci   return CL_SUCCESS;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci} catch (error &e) {
83bf215546Sopenharmony_ci   return e.get();
84bf215546Sopenharmony_ci}
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ciCLOVER_API cl_int
87bf215546Sopenharmony_ciclReleaseKernel(cl_kernel d_kern) try {
88bf215546Sopenharmony_ci   if (obj(d_kern).release())
89bf215546Sopenharmony_ci      delete pobj(d_kern);
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   return CL_SUCCESS;
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci} catch (error &e) {
94bf215546Sopenharmony_ci   return e.get();
95bf215546Sopenharmony_ci}
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ciCLOVER_API cl_int
98bf215546Sopenharmony_ciclSetKernelArg(cl_kernel d_kern, cl_uint idx, size_t size,
99bf215546Sopenharmony_ci               const void *value) try {
100bf215546Sopenharmony_ci   obj(d_kern).args().at(idx).set(size, value);
101bf215546Sopenharmony_ci   return CL_SUCCESS;
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci} catch (std::out_of_range &) {
104bf215546Sopenharmony_ci   return CL_INVALID_ARG_INDEX;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci} catch (error &e) {
107bf215546Sopenharmony_ci   return e.get();
108bf215546Sopenharmony_ci}
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ciCLOVER_API cl_int
111bf215546Sopenharmony_ciclGetKernelInfo(cl_kernel d_kern, cl_kernel_info param,
112bf215546Sopenharmony_ci                size_t size, void *r_buf, size_t *r_size) try {
113bf215546Sopenharmony_ci   property_buffer buf { r_buf, size, r_size };
114bf215546Sopenharmony_ci   auto &kern = obj(d_kern);
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   switch (param) {
117bf215546Sopenharmony_ci   case CL_KERNEL_FUNCTION_NAME:
118bf215546Sopenharmony_ci      buf.as_string() = kern.name();
119bf215546Sopenharmony_ci      break;
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   case CL_KERNEL_NUM_ARGS:
122bf215546Sopenharmony_ci      buf.as_scalar<cl_uint>() = kern.args().size();
123bf215546Sopenharmony_ci      break;
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   case CL_KERNEL_REFERENCE_COUNT:
126bf215546Sopenharmony_ci      buf.as_scalar<cl_uint>() = kern.ref_count();
127bf215546Sopenharmony_ci      break;
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   case CL_KERNEL_CONTEXT:
130bf215546Sopenharmony_ci      buf.as_scalar<cl_context>() = desc(kern.program().context());
131bf215546Sopenharmony_ci      break;
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci   case CL_KERNEL_PROGRAM:
134bf215546Sopenharmony_ci      buf.as_scalar<cl_program>() = desc(kern.program());
135bf215546Sopenharmony_ci      break;
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   case CL_KERNEL_ATTRIBUTES:
138bf215546Sopenharmony_ci      buf.as_string() = find(name_equals(kern.name()), kern.program().symbols()).attributes;
139bf215546Sopenharmony_ci      break;
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   default:
142bf215546Sopenharmony_ci      throw error(CL_INVALID_VALUE);
143bf215546Sopenharmony_ci   }
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   return CL_SUCCESS;
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci} catch (error &e) {
148bf215546Sopenharmony_ci   return e.get();
149bf215546Sopenharmony_ci}
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ciCLOVER_API cl_int
152bf215546Sopenharmony_ciclGetKernelWorkGroupInfo(cl_kernel d_kern, cl_device_id d_dev,
153bf215546Sopenharmony_ci                         cl_kernel_work_group_info param,
154bf215546Sopenharmony_ci                         size_t size, void *r_buf, size_t *r_size) try {
155bf215546Sopenharmony_ci   property_buffer buf { r_buf, size, r_size };
156bf215546Sopenharmony_ci   auto &kern = obj(d_kern);
157bf215546Sopenharmony_ci   auto &dev = (d_dev ? *pobj(d_dev) : unique(kern.program().devices()));
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci   if (!count(dev, kern.program().devices()))
160bf215546Sopenharmony_ci      throw error(CL_INVALID_DEVICE);
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci   switch (param) {
163bf215546Sopenharmony_ci   case CL_KERNEL_WORK_GROUP_SIZE:
164bf215546Sopenharmony_ci      buf.as_scalar<size_t>() = dev.max_threads_per_block();
165bf215546Sopenharmony_ci      break;
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci   case CL_KERNEL_COMPILE_WORK_GROUP_SIZE:
168bf215546Sopenharmony_ci      buf.as_vector<size_t>() = kern.required_block_size();
169bf215546Sopenharmony_ci      break;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   case CL_KERNEL_LOCAL_MEM_SIZE:
172bf215546Sopenharmony_ci      buf.as_scalar<cl_ulong>() = kern.mem_local();
173bf215546Sopenharmony_ci      break;
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci   case CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE:
176bf215546Sopenharmony_ci      buf.as_scalar<size_t>() = dev.subgroup_size();
177bf215546Sopenharmony_ci      break;
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci   case CL_KERNEL_PRIVATE_MEM_SIZE:
180bf215546Sopenharmony_ci      buf.as_scalar<cl_ulong>() = kern.mem_private();
181bf215546Sopenharmony_ci      break;
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci   default:
184bf215546Sopenharmony_ci      throw error(CL_INVALID_VALUE);
185bf215546Sopenharmony_ci   }
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   return CL_SUCCESS;
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci} catch (error &e) {
190bf215546Sopenharmony_ci   return e.get();
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci} catch (std::out_of_range &) {
193bf215546Sopenharmony_ci   return CL_INVALID_DEVICE;
194bf215546Sopenharmony_ci}
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ciCLOVER_API cl_int
197bf215546Sopenharmony_ciclGetKernelArgInfo(cl_kernel d_kern,
198bf215546Sopenharmony_ci                   cl_uint idx, cl_kernel_arg_info param,
199bf215546Sopenharmony_ci                   size_t size, void *r_buf, size_t *r_size) try {
200bf215546Sopenharmony_ci   property_buffer buf { r_buf, size, r_size };
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci   auto info = obj(d_kern).args_infos().at(idx);
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   if (info.arg_name.empty())
205bf215546Sopenharmony_ci      return CL_KERNEL_ARG_INFO_NOT_AVAILABLE;
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci   switch (param) {
208bf215546Sopenharmony_ci   case CL_KERNEL_ARG_ADDRESS_QUALIFIER:
209bf215546Sopenharmony_ci      buf.as_scalar<cl_kernel_arg_address_qualifier>() = info.address_qualifier;
210bf215546Sopenharmony_ci      break;
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_ci   case CL_KERNEL_ARG_ACCESS_QUALIFIER:
213bf215546Sopenharmony_ci      buf.as_scalar<cl_kernel_arg_access_qualifier>() = info.access_qualifier;
214bf215546Sopenharmony_ci      break;
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_ci   case CL_KERNEL_ARG_TYPE_NAME:
217bf215546Sopenharmony_ci      buf.as_string() = info.type_name;
218bf215546Sopenharmony_ci      break;
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci   case CL_KERNEL_ARG_TYPE_QUALIFIER:
221bf215546Sopenharmony_ci      buf.as_scalar<cl_kernel_arg_type_qualifier>() = info.type_qualifier;
222bf215546Sopenharmony_ci      break;
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci   case CL_KERNEL_ARG_NAME:
225bf215546Sopenharmony_ci      buf.as_string() = info.arg_name;
226bf215546Sopenharmony_ci      break;
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci   default:
229bf215546Sopenharmony_ci      throw error(CL_INVALID_VALUE);
230bf215546Sopenharmony_ci   }
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci   return CL_SUCCESS;
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci} catch (std::out_of_range &) {
235bf215546Sopenharmony_ci   return CL_INVALID_ARG_INDEX;
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci} catch (error &e) {
238bf215546Sopenharmony_ci   return e.get();
239bf215546Sopenharmony_ci}
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_cinamespace {
242bf215546Sopenharmony_ci   ///
243bf215546Sopenharmony_ci   /// Common argument checking shared by kernel invocation commands.
244bf215546Sopenharmony_ci   ///
245bf215546Sopenharmony_ci   void
246bf215546Sopenharmony_ci   validate_common(const command_queue &q, kernel &kern,
247bf215546Sopenharmony_ci                   const ref_vector<event> &deps) {
248bf215546Sopenharmony_ci      if (kern.program().context() != q.context() ||
249bf215546Sopenharmony_ci          any_of([&](const event &ev) {
250bf215546Sopenharmony_ci                return ev.context() != q.context();
251bf215546Sopenharmony_ci             }, deps))
252bf215546Sopenharmony_ci         throw error(CL_INVALID_CONTEXT);
253bf215546Sopenharmony_ci
254bf215546Sopenharmony_ci      if (any_of([](kernel::argument &arg) {
255bf215546Sopenharmony_ci               return !arg.set();
256bf215546Sopenharmony_ci            }, kern.args()))
257bf215546Sopenharmony_ci         throw error(CL_INVALID_KERNEL_ARGS);
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_ci      // If the command queue's device is not associated to the program, we get
260bf215546Sopenharmony_ci      // a binary, with no sections, which will also fail the following test.
261bf215546Sopenharmony_ci      auto &b = kern.program().build(q.device()).bin;
262bf215546Sopenharmony_ci      if (!any_of(type_equals(binary::section::text_executable), b.secs))
263bf215546Sopenharmony_ci         throw error(CL_INVALID_PROGRAM_EXECUTABLE);
264bf215546Sopenharmony_ci   }
265bf215546Sopenharmony_ci
266bf215546Sopenharmony_ci   std::vector<size_t>
267bf215546Sopenharmony_ci   validate_grid_size(const command_queue &q, cl_uint dims,
268bf215546Sopenharmony_ci                      const size_t *d_grid_size) {
269bf215546Sopenharmony_ci      auto grid_size = range(d_grid_size, dims);
270bf215546Sopenharmony_ci
271bf215546Sopenharmony_ci      if (dims < 1 || dims > q.device().max_block_size().size())
272bf215546Sopenharmony_ci         throw error(CL_INVALID_WORK_DIMENSION);
273bf215546Sopenharmony_ci
274bf215546Sopenharmony_ci      return grid_size;
275bf215546Sopenharmony_ci   }
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_ci   std::vector<size_t>
278bf215546Sopenharmony_ci   validate_grid_offset(const command_queue &q, cl_uint dims,
279bf215546Sopenharmony_ci                        const size_t *d_grid_offset) {
280bf215546Sopenharmony_ci      if (d_grid_offset)
281bf215546Sopenharmony_ci         return range(d_grid_offset, dims);
282bf215546Sopenharmony_ci      else
283bf215546Sopenharmony_ci         return std::vector<size_t>(dims, 0);
284bf215546Sopenharmony_ci   }
285bf215546Sopenharmony_ci
286bf215546Sopenharmony_ci   std::vector<size_t>
287bf215546Sopenharmony_ci   validate_block_size(const command_queue &q, const kernel &kern,
288bf215546Sopenharmony_ci                       cl_uint dims, const size_t *d_grid_size,
289bf215546Sopenharmony_ci                       const size_t *d_block_size) {
290bf215546Sopenharmony_ci      auto grid_size = range(d_grid_size, dims);
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_ci      if (d_block_size) {
293bf215546Sopenharmony_ci         auto block_size = range(d_block_size, dims);
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci         if (any_of(is_zero(), block_size) ||
296bf215546Sopenharmony_ci             any_of(greater(), block_size, q.device().max_block_size()))
297bf215546Sopenharmony_ci            throw error(CL_INVALID_WORK_ITEM_SIZE);
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ci         if (any_of(modulus(), grid_size, block_size))
300bf215546Sopenharmony_ci            throw error(CL_INVALID_WORK_GROUP_SIZE);
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_ci         if (fold(multiplies(), 1u, block_size) >
303bf215546Sopenharmony_ci             q.device().max_threads_per_block())
304bf215546Sopenharmony_ci            throw error(CL_INVALID_WORK_GROUP_SIZE);
305bf215546Sopenharmony_ci
306bf215546Sopenharmony_ci         return block_size;
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_ci      } else {
309bf215546Sopenharmony_ci         return kern.optimal_block_size(q, grid_size);
310bf215546Sopenharmony_ci      }
311bf215546Sopenharmony_ci   }
312bf215546Sopenharmony_ci}
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_ciCLOVER_API cl_int
315bf215546Sopenharmony_ciclEnqueueNDRangeKernel(cl_command_queue d_q, cl_kernel d_kern,
316bf215546Sopenharmony_ci                       cl_uint dims, const size_t *d_grid_offset,
317bf215546Sopenharmony_ci                       const size_t *d_grid_size, const size_t *d_block_size,
318bf215546Sopenharmony_ci                       cl_uint num_deps, const cl_event *d_deps,
319bf215546Sopenharmony_ci                       cl_event *rd_ev) try {
320bf215546Sopenharmony_ci   auto &q = obj(d_q);
321bf215546Sopenharmony_ci   auto &kern = obj(d_kern);
322bf215546Sopenharmony_ci   auto deps = objs<wait_list_tag>(d_deps, num_deps);
323bf215546Sopenharmony_ci   auto grid_size = validate_grid_size(q, dims, d_grid_size);
324bf215546Sopenharmony_ci   auto grid_offset = validate_grid_offset(q, dims, d_grid_offset);
325bf215546Sopenharmony_ci   auto block_size = validate_block_size(q, kern, dims,
326bf215546Sopenharmony_ci                                         d_grid_size, d_block_size);
327bf215546Sopenharmony_ci
328bf215546Sopenharmony_ci   validate_common(q, kern, deps);
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci   auto hev = create<hard_event>(
331bf215546Sopenharmony_ci      q, CL_COMMAND_NDRANGE_KERNEL, deps,
332bf215546Sopenharmony_ci      [=, &kern, &q](event &) {
333bf215546Sopenharmony_ci         kern.launch(q, grid_offset, grid_size, block_size);
334bf215546Sopenharmony_ci      });
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_ci   ret_object(rd_ev, hev);
337bf215546Sopenharmony_ci   return CL_SUCCESS;
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci} catch (error &e) {
340bf215546Sopenharmony_ci   return e.get();
341bf215546Sopenharmony_ci}
342bf215546Sopenharmony_ci
343bf215546Sopenharmony_ciCLOVER_API cl_int
344bf215546Sopenharmony_ciclEnqueueTask(cl_command_queue d_q, cl_kernel d_kern,
345bf215546Sopenharmony_ci              cl_uint num_deps, const cl_event *d_deps,
346bf215546Sopenharmony_ci              cl_event *rd_ev) try {
347bf215546Sopenharmony_ci   auto &q = obj(d_q);
348bf215546Sopenharmony_ci   auto &kern = obj(d_kern);
349bf215546Sopenharmony_ci   auto deps = objs<wait_list_tag>(d_deps, num_deps);
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_ci   validate_common(q, kern, deps);
352bf215546Sopenharmony_ci
353bf215546Sopenharmony_ci   auto hev = create<hard_event>(
354bf215546Sopenharmony_ci      q, CL_COMMAND_TASK, deps,
355bf215546Sopenharmony_ci      [=, &kern, &q](event &) {
356bf215546Sopenharmony_ci         kern.launch(q, { 0 }, { 1 }, { 1 });
357bf215546Sopenharmony_ci      });
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_ci   ret_object(rd_ev, hev);
360bf215546Sopenharmony_ci   return CL_SUCCESS;
361bf215546Sopenharmony_ci
362bf215546Sopenharmony_ci} catch (error &e) {
363bf215546Sopenharmony_ci   return e.get();
364bf215546Sopenharmony_ci}
365bf215546Sopenharmony_ci
366bf215546Sopenharmony_ciCLOVER_API cl_int
367bf215546Sopenharmony_ciclEnqueueNativeKernel(cl_command_queue d_q, void (*func)(void *),
368bf215546Sopenharmony_ci                      void *args, size_t args_size,
369bf215546Sopenharmony_ci                      cl_uint num_mems, const cl_mem *d_mems,
370bf215546Sopenharmony_ci                      const void **mem_handles, cl_uint num_deps,
371bf215546Sopenharmony_ci                      const cl_event *d_deps, cl_event *rd_ev) {
372bf215546Sopenharmony_ci   return CL_INVALID_OPERATION;
373bf215546Sopenharmony_ci}
374bf215546Sopenharmony_ci
375bf215546Sopenharmony_ciCLOVER_API cl_int
376bf215546Sopenharmony_ciclSetKernelArgSVMPointer(cl_kernel d_kern,
377bf215546Sopenharmony_ci                         cl_uint arg_index,
378bf215546Sopenharmony_ci                         const void *arg_value) try {
379bf215546Sopenharmony_ci  if (!any_of(std::mem_fn(&device::svm_support), obj(d_kern).program().devices()))
380bf215546Sopenharmony_ci      return CL_INVALID_OPERATION;
381bf215546Sopenharmony_ci   obj(d_kern).args().at(arg_index).set_svm(arg_value);
382bf215546Sopenharmony_ci   return CL_SUCCESS;
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci} catch (std::out_of_range &) {
385bf215546Sopenharmony_ci   return CL_INVALID_ARG_INDEX;
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_ci} catch (error &e) {
388bf215546Sopenharmony_ci   return e.get();
389bf215546Sopenharmony_ci}
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ciCLOVER_API cl_int
392bf215546Sopenharmony_ciclSetKernelExecInfo(cl_kernel d_kern,
393bf215546Sopenharmony_ci                    cl_kernel_exec_info param_name,
394bf215546Sopenharmony_ci                    size_t param_value_size,
395bf215546Sopenharmony_ci                    const void *param_value) try {
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ci   if (!any_of(std::mem_fn(&device::svm_support), obj(d_kern).program().devices()))
398bf215546Sopenharmony_ci      return CL_INVALID_OPERATION;
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_ci   auto &kern = obj(d_kern);
401bf215546Sopenharmony_ci
402bf215546Sopenharmony_ci   const bool has_system_svm = all_of(std::mem_fn(&device::has_system_svm),
403bf215546Sopenharmony_ci                                      kern.program().context().devices());
404bf215546Sopenharmony_ci
405bf215546Sopenharmony_ci   if (!param_value)
406bf215546Sopenharmony_ci      return CL_INVALID_VALUE;
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_ci   switch (param_name) {
409bf215546Sopenharmony_ci   case CL_KERNEL_EXEC_INFO_SVM_FINE_GRAIN_SYSTEM:
410bf215546Sopenharmony_ci   case CL_KERNEL_EXEC_INFO_SVM_FINE_GRAIN_SYSTEM_ARM: {
411bf215546Sopenharmony_ci      if (param_value_size != sizeof(cl_bool))
412bf215546Sopenharmony_ci         return CL_INVALID_VALUE;
413bf215546Sopenharmony_ci
414bf215546Sopenharmony_ci      cl_bool val = *static_cast<const cl_bool*>(param_value);
415bf215546Sopenharmony_ci      if (val == CL_TRUE && !has_system_svm)
416bf215546Sopenharmony_ci         return CL_INVALID_OPERATION;
417bf215546Sopenharmony_ci      else
418bf215546Sopenharmony_ci         return CL_SUCCESS;
419bf215546Sopenharmony_ci   }
420bf215546Sopenharmony_ci
421bf215546Sopenharmony_ci   case CL_KERNEL_EXEC_INFO_SVM_PTRS:
422bf215546Sopenharmony_ci   case CL_KERNEL_EXEC_INFO_SVM_PTRS_ARM:
423bf215546Sopenharmony_ci      if (has_system_svm)
424bf215546Sopenharmony_ci         return CL_SUCCESS;
425bf215546Sopenharmony_ci
426bf215546Sopenharmony_ci      CLOVER_NOT_SUPPORTED_UNTIL("2.0");
427bf215546Sopenharmony_ci      return CL_INVALID_VALUE;
428bf215546Sopenharmony_ci
429bf215546Sopenharmony_ci   default:
430bf215546Sopenharmony_ci      return CL_INVALID_VALUE;
431bf215546Sopenharmony_ci   }
432bf215546Sopenharmony_ci
433bf215546Sopenharmony_ci} catch (error &e) {
434bf215546Sopenharmony_ci   return e.get();
435bf215546Sopenharmony_ci}
436