<div>I would reconsider Micah's suggestion. The simple solution is to tag the variable with an address space and turn it into a global. You can do that with a simple change in CodeGenFunction::CreateStaticBlockVarDecl. It would give all the benefits you describe in that the target decides how to lower the code but do it using concepts that LLVM and some targets may already support. Kernels that call kernels with locals will also work.</div>
<div><br></div><div>Perhaps an example is useful? Our OpenCL implementation, given the code above, generates this bitcode (after optimizations that have eliminated the dead vars):</div><div><br></div><div>target datalayout = "e-p:32:32:32-f64:64:64-i64:64:64"</div>
<div>target triple = "zms-ziilabs-opencl10"</div><div><br></div><div>@foo.auto.vint = internal addrspace(2) global i32 0, align 4</div><div>@foo.auto.vvint = internal addrspace(2) global i32 0, align 4</div><div>
<br></div><div>define void @foo(i32 addrspace(1)* %A) nounwind {</div><div>entry:</div><div> %tmp1 = load i32 addrspace(1)* %A, align 4</div><div> store i32 %tmp1, i32 addrspace(2)* @foo.auto.vint, align 4</div><div> volatile store i32 %tmp1, i32 addrspace(2)* @foo.auto.vvint, align 4</div>
<div> %tmp7 = volatile load i32 addrspace(2)* @foo.auto.vvint, align 4</div><div> tail call void @llvm.memory.barrier(i1 true, i1 true, i1 true, i1 true, i1 false)</div><div> %add = add nsw i32 %tmp7, %tmp1</div><div> store i32 %add, i32 addrspace(1)* %A, align 4</div>
<div> ret void</div><div>}</div><div><br></div><div>Krister</div><div><br></div><div><br></div><br><div class="gmail_quote">On Wed, Dec 8, 2010 at 5:02 AM, David Neto <span dir="ltr"><<a href="mailto:dneto.llvm@gmail.com">dneto.llvm@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">On Mon, Dec 6, 2010 at 6:16 PM, Villmow, Micah <<a href="mailto:Micah.Villmow@amd.com">Micah.Villmow@amd.com</a>> wrote:<br>
>> -----Original Message-----<br>
>> From: <a href="mailto:llvmdev-bounces@cs.uiuc.edu">llvmdev-bounces@cs.uiuc.edu</a> [mailto:<a href="mailto:llvmdev-bounces@cs.uiuc.edu">llvmdev-bounces@cs.uiuc.edu</a>]<br>
>> On Behalf Of Peter Collingbourne<br>
>> Sent: Monday, December 06, 2010 2:56 PM<br>
>> To: David Neto<br>
>> Cc: <a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a>; <a href="mailto:llvmdev@cs.uiuc.edu">llvmdev@cs.uiuc.edu</a><br>
>> Subject: Re: [LLVMdev] [cfe-dev] OpenCL support<br>
>><br>
>> Hi David,<br>
>><br>
>> On Mon, Dec 06, 2010 at 11:14:42AM -0500, David Neto wrote:<br>
</div><div><div></div><div class="h5">>> > What do I think your patch should look like? It's true that the<br>
>> > diag::err_as_qualified_auto_decl is inappropriate for OpenCL when<br>
>> it's<br>
>> > the __local addres space.<br>
>> ><br>
>> > But we need to implement the semantics somehow. Conceptually I think<br>
>> > of it as a CL source-to-source transformation that lowers<br>
>> > function-scope-local-address-space variables into a more primitive<br>
>> > form.<br>
>> ><br>
>> > I think I disagree that the Clang is an inappropriate spot for<br>
>> > implementing this type of transform: Clang "knows" the source<br>
>> language<br>
>> > semantics, and has a lot of machinery required for the transform.<br>
>> > Also, Clang also knows a lot about the target machine (e.g. type<br>
>> > sizes, builtins, more?).<br>
>> ><br>
>> > So I believe the "auto var in different address space" case should be<br>
>> > allowed in the AST in the OpenCL case, and the local-lowering<br>
>> > transform should be applied in CodeGen. Perhaps the lowering is<br>
>> > target-specific, e.g. GPU-style, or more generic style as I proposed.<br>
>> ><br>
>> > Thoughts?<br>
>><br>
>> I've been rethinking this and perhaps coming around to this way<br>
>> of thinking. Allocating variables in the __local address space<br>
>> is really something that can't be represented at the LLVM level,<br>
>> at least in a standard form.<br>
> [Villmow, Micah] We ran across this problem in our OpenCL implementation. However, you can create a global variable with an '__local' address space and it works fine. There is an issue with collision between auto-arrays in different kernels, but that can be solved with a little name mangling. There are other ways to do this, for example, by converting local auto-arrays into kernel local pointer arguments with a known size.<br>
<br>
</div></div>Here's a little example to show the direction I was heading, with an<br>
illustration as a CL-to-C translation. I believe there are no<br>
namespace issues, but otherwise is essentially the same as the global<br>
variable solution.<br>
<br>
The idea is that the func scope local addr variables are like a stack<br>
frame that is shared between the different work items in a group. So<br>
collect all those variables in an anonymous struct, and then create a<br>
function scope private variable to point to the one copy of that<br>
struct. The pointer is returned by a system-defined intrinsic<br>
function dependent on the current work item. (The system knows what<br>
work groups are in flight, which is why you need a system-defined<br>
intrinsic.)<br>
<br>
So a kernel function like this:<br>
<br>
void foo(__global int*A) {<br>
__local int vint;<br>
__local int *vpint;<br>
__local int const *vcpint;<br>
__local int volatile vvint;<br>
int a = A[0];<br>
vint = a;<br>
vvint = a;<br>
int a2 = vint;<br>
int va2 = vvint;<br>
barrier(CLK_LOCAL_MEM_FENCE);<br>
A[0] = a2 + va2;<br>
}<br>
<br>
is translated to this, which does pass through Clang, with __local<br>
meaning attrib addrspace(2):<br>
<br>
extern __local void * __get_work_group_local_base_addr(void); // intrinsic<br>
void foo(__global int*A) {<br>
__local struct __local_vars_s {<br>
int vint;<br>
int *vpint;<br>
int const *vcpint;<br>
int volatile vvint;<br>
} * const __local_vars<br>
// this is a *private* variable, pointing to *local* addresses.<br>
// it's a const pointer because it shouldn't change; and<br>
being const may expose optimizations<br>
= __get_work_group_local_base_addr(); // the new intrinsic<br>
int a = A[0];<br>
__local_vars->vint = a; // l-values are translated as memory stores.<br>
__local_vars->vvint = a;<br>
int a2 = __local_vars->vint; // r-values are translated as memory loads<br>
int va2 = __local_vars->vvint;<br>
barrier(CLK_LOCAL_MEM_FENCE);<br>
A[0] = a2 + va2;<br>
}<br>
<br>
<br>
As an extension, the backend ought to be able to use some smarts to<br>
simplify this down in simple cases. For example if the system only<br>
ever allows one work group at a time, then the intrinsic could boil<br>
down to returning a constant, and then link time optimization can<br>
scrub away unneeded work. Similarly if you have a GPU style<br>
environment where (as Peter described) the "local" addresses are the<br>
same integer value but in different groups point to different storage,<br>
then again the intrinsic returns a constant and again LTO optimizes<br>
the result.<br>
<br>
I haven't thought through the implications of a kernel having such<br>
vars calling another kernel having such variables. At least the<br>
OpenCL spec says that the behaviour is implementation-defined for such<br>
a case. It would be nice to be able to represent any of the sane<br>
possibilities.<br>
<br>
<br>
@Anton: Regarding ARM's open-sourcing: I'm glad to see the<br>
reaffirmation, and I look forward to the contribution. Yes, I<br>
understand the virtues of patience. :-)<br>
I assume you plan to commit a document describing how OpenCL is<br>
supported. (e.g. how details like the above are handled.)<br>
<br>
thanks,<br>
<font color="#888888">david<br>
</font><div><div></div><div class="h5"><br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</div></div></blockquote></div><br>