clover: Add CL_MEM_HOST_* flag checks.
authorEdB <edb+mesa@sigluy.net>
Sun, 22 Feb 2015 13:00:06 +0000 (15:00 +0200)
committerFrancisco Jerez <currojerez@riseup.net>
Tue, 24 Feb 2015 14:17:18 +0000 (16:17 +0200)
Those flags have been introduced in OpenCL 1.2.

[ Francisco Jerez: Rebase.  Throw CL_INVALID_VALUE from
  clCreateSubBuffer if the subbuffer drops access flags from its
  parent.  Use single function taking the set of allowed host access
  flags to validate memory transfer operands. ]

Reviewed-by: Francisco Jerez <currojerez@riseup.net>
src/gallium/state_trackers/clover/api/memory.cpp
src/gallium/state_trackers/clover/api/transfer.cpp

index 551a51006c3e0f7617c9b1a96839a79f21306ab8..230035b00e0fa16e3275f2b9628c3c771c380ac4 100644 (file)
@@ -32,13 +32,16 @@ namespace {
       CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY;
    const cl_mem_flags host_ptr_flags =
       CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR;
+   const cl_mem_flags host_access_flags =
+      CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS;
    const cl_mem_flags all_mem_flags =
-      dev_access_flags | host_ptr_flags;
+      dev_access_flags | host_ptr_flags | host_access_flags;
 
    void
    validate_flags(cl_mem_flags flags, cl_mem_flags valid) {
       if ((flags & ~valid) ||
-          util_bitcount(flags & dev_access_flags) > 1)
+          util_bitcount(flags & dev_access_flags) > 1 ||
+          util_bitcount(flags & host_access_flags) > 1)
          throw error(CL_INVALID_VALUE);
 
       if ((flags & CL_MEM_USE_HOST_PTR) &&
@@ -78,10 +81,10 @@ clCreateSubBuffer(cl_mem d_mem, cl_mem_flags flags,
                   const void *op_info, cl_int *r_errcode) try {
    auto &parent = obj<root_buffer>(d_mem);
 
-   validate_flags(flags, dev_access_flags);
+   validate_flags(flags, dev_access_flags | host_access_flags);
 
    if (~flags & parent.flags() &
-       (dev_access_flags & ~CL_MEM_READ_WRITE))
+       ((dev_access_flags & ~CL_MEM_READ_WRITE) | host_access_flags))
       throw error(CL_INVALID_VALUE);
 
    if (op == CL_BUFFER_CREATE_TYPE_REGION) {
index b8d7771e62afc7d5c77977b7528f26605d216616..fdb9405c918a31fb12285af2e3002625570caaf4 100644 (file)
@@ -167,14 +167,32 @@ namespace {
       }
    }
 
+   ///
+   /// Checks that the host access flags of the memory object are
+   /// within the allowed set \a flags.
+   ///
+   void
+   validate_object_access(const memory_obj &mem, const cl_mem_flags flags) {
+      if (mem.flags() & ~flags &
+          (CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_WRITE_ONLY |
+           CL_MEM_HOST_NO_ACCESS))
+         throw error(CL_INVALID_OPERATION);
+   }
+
    ///
    /// Checks that the mapping flags are correct.
    ///
    void
-   validate_flags(const cl_map_flags flags) {
+   validate_map_flags(const memory_obj &mem, const cl_map_flags flags) {
       if ((flags & (CL_MAP_WRITE | CL_MAP_READ)) &&
           (flags & CL_MAP_WRITE_INVALIDATE_REGION))
          throw error(CL_INVALID_VALUE);
+
+      if (flags & CL_MAP_READ)
+         validate_object_access(mem, CL_MEM_HOST_READ_ONLY);
+
+      if (flags & (CL_MAP_WRITE | CL_MAP_WRITE_INVALIDATE_REGION))
+         validate_object_access(mem, CL_MEM_HOST_WRITE_ONLY);
    }
 
    ///
@@ -269,6 +287,7 @@ clEnqueueReadBuffer(cl_command_queue d_q, cl_mem d_mem, cl_bool blocking,
    validate_common(q, deps);
    validate_object(q, ptr, {}, obj_pitch, region);
    validate_object(q, mem, obj_origin, obj_pitch, region);
+   validate_object_access(mem, CL_MEM_HOST_READ_ONLY);
 
    auto hev = create<hard_event>(
       q, CL_COMMAND_READ_BUFFER, deps,
@@ -298,6 +317,7 @@ clEnqueueWriteBuffer(cl_command_queue d_q, cl_mem d_mem, cl_bool blocking,
    validate_common(q, deps);
    validate_object(q, mem, obj_origin, obj_pitch, region);
    validate_object(q, ptr, {}, obj_pitch, region);
+   validate_object_access(mem, CL_MEM_HOST_WRITE_ONLY);
 
    auto hev = create<hard_event>(
       q, CL_COMMAND_WRITE_BUFFER, deps,
@@ -334,6 +354,7 @@ clEnqueueReadBufferRect(cl_command_queue d_q, cl_mem d_mem, cl_bool blocking,
    validate_common(q, deps);
    validate_object(q, ptr, host_origin, host_pitch, region);
    validate_object(q, mem, obj_origin, obj_pitch, region);
+   validate_object_access(mem, CL_MEM_HOST_READ_ONLY);
 
    auto hev = create<hard_event>(
       q, CL_COMMAND_READ_BUFFER_RECT, deps,
@@ -370,6 +391,7 @@ clEnqueueWriteBufferRect(cl_command_queue d_q, cl_mem d_mem, cl_bool blocking,
    validate_common(q, deps);
    validate_object(q, mem, obj_origin, obj_pitch, region);
    validate_object(q, ptr, host_origin, host_pitch, region);
+   validate_object_access(mem, CL_MEM_HOST_WRITE_ONLY);
 
    auto hev = create<hard_event>(
       q, CL_COMMAND_WRITE_BUFFER_RECT, deps,
@@ -474,6 +496,7 @@ clEnqueueReadImage(cl_command_queue d_q, cl_mem d_mem, cl_bool blocking,
    validate_common(q, deps);
    validate_object(q, ptr, {}, dst_pitch, region);
    validate_object(q, img, src_origin, region);
+   validate_object_access(img, CL_MEM_HOST_READ_ONLY);
 
    auto hev = create<hard_event>(
       q, CL_COMMAND_READ_IMAGE, deps,
@@ -507,6 +530,7 @@ clEnqueueWriteImage(cl_command_queue d_q, cl_mem d_mem, cl_bool blocking,
    validate_common(q, deps);
    validate_object(q, img, dst_origin, region);
    validate_object(q, ptr, {}, src_pitch, region);
+   validate_object_access(img, CL_MEM_HOST_WRITE_ONLY);
 
    auto hev = create<hard_event>(
       q, CL_COMMAND_WRITE_IMAGE, deps,
@@ -639,7 +663,7 @@ clEnqueueMapBuffer(cl_command_queue d_q, cl_mem d_mem, cl_bool blocking,
 
    validate_common(q, deps);
    validate_object(q, mem, obj_origin, obj_pitch, region);
-   validate_flags(flags);
+   validate_map_flags(mem, flags);
 
    void *map = mem.resource(q).add_map(q, flags, blocking, obj_origin, region);
 
@@ -667,7 +691,7 @@ clEnqueueMapImage(cl_command_queue d_q, cl_mem d_mem, cl_bool blocking,
 
    validate_common(q, deps);
    validate_object(q, img, origin, region);
-   validate_flags(flags);
+   validate_map_flags(img, flags);
 
    void *map = img.resource(q).add_map(q, flags, blocking, origin, region);