[cfe-dev] [RFC] Block captured variables in OpenCL 2.X dynamic parallelism

Anastasia Stulova via cfe-dev cfe-dev at lists.llvm.org
Sun Sep 18 12:51:43 PDT 2016

Hi Bekket,

Could you please elaborate what benefits passing captured variables as separate argument gives in comparison to passing a pointer to a chunk of captures located somewhere in contiguous memory. Perhaps examples could be helpful or if you already have a patch feel free to setup the review.

Current design passes the whole block literal structure into __enqueue_kernel_XXX and relies on it to make capturing data available before enqueueing the block. Using memcpy should be sufficient here to copy thread local (private address space) data into some global memory. I am guessing the same should be done with your approach but in the format of parameter passing if blocks are to be enqueued anywhere globally on the device?

In your proposal does frontend generate definitions of cap_copy_helper functions then?

From: Bekket McClane <bekket.mcclane at gmail.com>
Sent: 17 September 2016 05:12:03
To: cfe-dev at lists.llvm.org
Cc: Anastasia Stulova; aleksey.bader at mail.ru; yaxun.liu at amd.com
Subject: [RFC] Block captured variables in OpenCL 2.X dynamic parallelism

OpenCL 2.X dynamic parallelism in clang 3.9 leverages most of the Objective-C's block structure to save captured variables from parent kernel. However, there is a disadvantage on this design: The block literal structure, which needs to be passed to child kernel by address later, is allocated in parent kernel context by default. But parent kernels and child kernels only shares the global address space, so we either need to put block literal structures in global address space, which brings other problems like memory management due to the large amount of block literals potentially, or find another way to figure this out.

We propose a way that passes all the captured variables as function arguments to the child kernel invoke function, which is the "main body" function of child kernel. OpenCL 2.X spec doesn't allowed __local variables to be captured into child kernel block, so we can almost assert that there are only two kinds of captured variables: constants(i.e by-value variables) and __global pointers. Also, OpenCL 2.X allows child kernels shared chunks of memory which are passed as arguments in(and only in) __local pointer type to child kernel block. Thus, if we insert all the captured variables to the head of the child block invoke function argument list, which originally contains only __local pointers if there are shared memory chunks, we can not only conveniently distinguish captured variables from shared memory pointers based on their types in the argument list, but also remove the need of maintaining memory for block literal structures since we don't pass it to child kernel anymore.

Nevertheless, captured variables still needed to be extracted inside most of the __enqueue_kernel_XXX implementations. We came up with a design that adds two new fields into the block literal struct: cap_num, which tells the amount of captured variables, and cap_copy_helper, which is a function with prototype:
    size_t cap_copy_helper(void* block_literal, unsigned int arg_index, void* dest_memory)
The second parameter arg_index requires index of the desired captured variables in block invoke function argument list mentioned above. This helper would copy your desired variable value from block_literal to dest_memory and return its size. Actually we had previously built a version of helper function that still took block_literal and arg_index but return the captured variables's pointer(in void* type) directly. In that approach neither can we know the size of variable nor could we retrieve value from the returning pointer due to the lack of type information and one can not directly de-reference a void pointer either. The new cap_copy_helper approach can solve both of the problems, although it seems to be more suitable for copying all the captured variables adjacently into a chunk of memory, I think it still can be used to fetch individual captured variable.

We have implemented a prototype for all of the design above except the cap_copy_helper, but it could be finished soon. Also, discussions for captured variables in advanced type like image_t are welcomed - since we haven't covered those types yet.

Yours sincerely,

Bekket McClane
Department of Computer Science,
National Tsing Hua University
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160918/cbc8b0b6/attachment.html>

More information about the cfe-dev mailing list