<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size: 12pt; color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;" dir="ltr">
<p style="margin-top:0;margin-bottom:0">Hi Sam,</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">There is a restriction in OpenCL spec I have referenced in my previous email -
<span lang="en-US"><font size="2" face="Calibri,sans-serif"><span style="font-size:11pt;"><font color="black" size="3"><span style="font-size:12pt;">s6.13.17.2</span></font></span></font></span>, which is implemented by Clang. If you look in the file
<span>test/SemaOpenCL/cl20-device-side-enqueue.cl</span> around line 117, you will see that
<span>block_B</span> is rejected to be passed into enqueue_kernel because it has a parameter which isn't "local void*". If you think this is wrong perhaps it would make sense to revisit this bit and understand whether the current spec should be changed to allow
 more optimal implementations to exist. But as for the current state, I don't think we can implement what you are suggesting because we can only have one block argument type for a block in enqueue.</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">Cheer,</p>
<p style="margin-top:0;margin-bottom:0">Anastasia<br>
</p>
<br>
<br>
<div style="color: rgb(0, 0, 0);">
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" color="#000000" face="Calibri, sans-serif"><b>From:</b> Liu, Yaxun (Sam) <Yaxun.Liu@amd.com><br>
<b>Sent:</b> 08 January 2018 22:25<br>
<b>To:</b> Anastasia Stulova; cfe-dev (cfe-dev@lists.llvm.org); Bader, Alexey (alexey.bader@intel.com)<br>
<b>Cc:</b> Sumner, Brian; nd<br>
<b>Subject:</b> RE: [RFC][OpenCL] Pass alignment of arguments in local addr space for device-side enqueued kernel to __enqueue_kernel functions</font>
<div> </div>
</div>
<div link="blue" vlink="purple" lang="EN-US">
<div class="x_WordSection1">
<p class="x_MsoNormal">My comments are below.</p>
<p class="x_MsoNormal"> </p>
<div>
<div style="border:none; border-top:solid #E1E1E1 1.0pt; padding:3.0pt 0in 0in 0in">
<p class="x_MsoNormal"><b>From:</b> Anastasia Stulova [mailto:Anastasia.Stulova@arm.com]
<br>
<b>Sent:</b> Tuesday, December 19, 2017 10:21 AM<br>
<b>To:</b> Liu, Yaxun (Sam) <Yaxun.Liu@amd.com>; cfe-dev (cfe-dev@lists.llvm.org) <cfe-dev@lists.llvm.org>; Bader, Alexey (alexey.bader@intel.com) <alexey.bader@intel.com><br>
<b>Cc:</b> Sumner, Brian <Brian.Sumner@amd.com>; nd <nd@arm.com><br>
<b>Subject:</b> Re: [RFC][OpenCL] Pass alignment of arguments in local addr space for device-side enqueued kernel to __enqueue_kernel functions</p>
</div>
</div>
<p class="x_MsoNormal"> </p>
<div id="x_divtagdefaultwrapper">
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black">> For example, if a block kernel has argument local int4*. Its alignment should be 16 bytes. </span></p>
<p class="x_MsoNormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_MsoNormal" style="margin-bottom:12.0pt"><span style="font-size:12.0pt; color:black">Perhaps I am missing something but I still don't see anything in the spec that requires pointers themselves to take alignment from the pointee type. In your example
 int4* should be aligned to the pointer size (either 4 or 8 bites) while int4 should be 16 byte aligned. Clang will set the alignment of load and store operations correctly according to their data types specified in the source code (which is mainly inherited
 from C implementation apart from some special data types like vectors). The arguments passed to kernels are allocated elsewhere and OpenCL compiler has no control over this.<br>
<br>
</span><span style="font-size:12.0pt"></span></p>
<p class="x_MsoNormal" style="margin-bottom:12.0pt">  Sam: The spec (v2.0s6.1.5) requires “the
<span style="font-size:12.0pt; color:black">pointee is always appropriately aligned as required by the data type”, which means the pointee of the kernel argument of int4* type should be aligned at 16 bytes.</span></p>
<p class="x_MsoNormal" style="margin-bottom:12.0pt"><span style="font-size:12.0pt; color:black"><br>
Regarding enqueued kernels as far as I understand you suggest to add block argument alignment info into builtin? Even though it shouldn't be strictly necessary I believe some implementation can indeed be done more efficiently using this. So I don't see any
 problem adding this. However, spec (s6.13.17.2) mandates that the enqueued block function only has void* types as parameters: "Each argument must be declared to be a void pointer to local memory."  So could you elaborate please where exactly do you plan to
 get the optimal alignment from?<br>
<br>
</span><span style="font-size:12.0pt"></span></p>
<p class="x_MsoNormal" style="margin-bottom:12.0pt">  Sam: The block function is passed to the builtin. The argument of the block function has the proper data type instead of void* type. Clang can deduce the alignment of the pointee of the kernel argument from
 the block function type.</p>
<p class="x_MsoNormal" style="margin-bottom:12.0pt"><span style="font-size:12.0pt; color:black"><br>
Thanks,<br>
Anastasia</span></p>
<div>
<div class="x_MsoNormal" style="text-align:center" align="center"><span style="font-size:12.0pt; color:black">
<hr size="2" align="center" width="98%">
</span></div>
<div id="x_divRplyFwdMsg">
<p class="x_MsoNormal"><b><span style="color:black">From:</span></b><span style="color:black"> Liu, Yaxun (Sam) <<a href="mailto:Yaxun.Liu@amd.com">Yaxun.Liu@amd.com</a>><br>
<b>Sent:</b> 15 December 2017 19:08<br>
<b>To:</b> Anastasia Stulova; cfe-dev (<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>); Bader, Alexey (<a href="mailto:alexey.bader@intel.com">alexey.bader@intel.com</a>)<br>
<b>Cc:</b> Sumner, Brian; nd<br>
<b>Subject:</b> RE: [RFC][OpenCL] Pass alignment of arguments in local addr space for device-side enqueued kernel to __enqueue_kernel functions</span><span style="font-size:12.0pt; color:black">
</span></p>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; color:black"> </span></p>
</div>
</div>
<div>
<div>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black">Spec reference:</span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black">OpenCL v2.0 s6.1.5</span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black">The OpenCL compiler is responsible for aligning data items to the appropriate alignment as required by the data type. For arguments to a __kernel function declared to be a pointer to a data
 type, the OpenCL compiler can assume that the pointee is always appropriately aligned as required by the data type. The behavior of an unaligned load or store is undefined, except for the vloadn, vload_halfn, vstoren, and vstore_halfn functions defined in
 section 6.13.7.</span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black">s6.2.5</span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black">Casting a pointer to a new type represents an unchecked assertion that the address is correctly aligned.</span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black">The C Standard, 6.3.2.3, paragraph 7 [ISO/IEC 9899:2011], states</span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black">A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the referenced type,
 the behavior is undefined.</span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black">For example, if a block kernel has argument local int4*. Its alignment should be 16 bytes. Passing a pointer aligned to 1 byte may result in undefined behavior. Most hardware can still load
 from the unaligned memory but will a performance hit. If runtime wants to avoid the performance hit, it has to allocate the buffer at maximum possible alignment e.g. 32 bytes, which will result in wasted memory.</span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black">Sam</span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<div>
<div style="border:none; border-top:solid #E1E1E1 1.0pt; padding:3.0pt 0in 0in 0in">
<p class="x_xmsonormal"><b><span style="font-size:12.0pt; color:black">From:</span></b><span style="font-size:12.0pt; color:black"> Anastasia Stulova [<a href="mailto:Anastasia.Stulova@arm.com">mailto:Anastasia.Stulova@arm.com</a>]
<br>
<b>Sent:</b> Friday, December 15, 2017 10:40 AM<br>
<b>To:</b> Liu, Yaxun (Sam) <<a href="mailto:Yaxun.Liu@amd.com">Yaxun.Liu@amd.com</a>>; cfe-dev (<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>) <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>>; Bader, Alexey (<a href="mailto:alexey.bader@intel.com">alexey.bader@intel.com</a>)
 <<a href="mailto:alexey.bader@intel.com">alexey.bader@intel.com</a>><br>
<b>Cc:</b> Sumner, Brian <<a href="mailto:Brian.Sumner@amd.com">Brian.Sumner@amd.com</a>>; nd <<a href="mailto:nd@arm.com">nd@arm.com</a>><br>
<b>Subject:</b> Re: [RFC][OpenCL] Pass alignment of arguments in local addr space for device-side enqueued kernel to __enqueue_kernel functions</span></p>
</div>
</div>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<div id="x_x_divtagdefaultwrapper">
<p><span style="font-size:12.0pt; color:black"> </span></p>
<p><span style="font-size:12.0pt; color:black">><span class="x_xcurrenthithighlight"> OpenCL</span> spec requires that a pointer should be aligned to at least the pointee type.</span></p>
<p><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black">So a pointer to int16 would be 64 byte aligned? Seems strange though. Can you give me the spec reference?<br>
<br>
> Otherwise, __enqueue_kernel has to either allocate unaligned local buffer, which degrades performance, or allocates local buffer with extra alignment therefore wasted memory space.<br>
<br>
Can you explain in more details here, please.<br>
<br>
Cheer,<br>
Anastasia</span></p>
<div>
<div class="x_MsoNormal" style="text-align:center" align="center"><span style="font-size:12.0pt; color:black">
<hr size="2" align="center" width="98%">
</span></div>
<div id="x_x_divRplyFwdMsg">
<p class="x_xmsonormal"><b><span style="font-size:12.0pt; color:black">From:</span></b><span style="font-size:12.0pt; color:black"> Liu, Yaxun (Sam) <<a href="mailto:Yaxun.Liu@amd.com">Yaxun.Liu@amd.com</a>><br>
<b>Sent:</b> 01 December 2017 19:45<br>
<b>To:</b> Anastasia Stulova; cfe-dev (<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>); Bader, Alexey (<a href="mailto:alexey.bader@intel.com">alexey.bader@intel.com</a>)<br>
<b>Cc:</b> Sumner, Brian<br>
<b>Subject:</b> [RFC][OpenCL] Pass alignment of arguments in local addr space for device-side enqueued kernel to __enqueue_kernel functions
</span></p>
<div>
<p class="x_xmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
</div>
</div>
<div>
<div>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black">Hi,</span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black">OpenCL spec requires that a pointer should be aligned to at least the pointee type. Therefore, if a device-side enqueued kernel has a local int* argument, it should be aligned to 4 bytes.</span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black">Since these buffers in local addr space are allocated by __enqueue_kernel, it needs to know the alignment of these buffers, not just their sizes.</span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black">Although such information is not passed to the original OpenCL builtin function enqueue_kernel, it can be obtained by checking the prototype of the block invoke function at compile time.</span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black">I would like to create a patch to pass this information to  __enqueue_kernel. Otherwise, __enqueue_kernel has to either allocate unaligned local buffer, which degrades performance, or allocates
 local buffer with extra alignment therefore wasted memory space.</span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black">Any comments?</span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black">Thanks.</span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black"> </span></p>
<p class="x_xxmsonormal"><span style="font-size:12.0pt; color:black">Sam</span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>