<div dir="ltr">The issue you're seeing is actually a problem with clang integration.  Generally, clang does not understand all of the conventions required by the NVPTX back-end, and compilation from general C may not always work.<div>
<br></div><div>In this case, it's the local array "b" that is the problem.  Clang pulls this out into the global scope, but keeps it in address space 0.  In the back-end, address space 0 is the PTX generic address space and global variables cannot use it.  The index computations are not causing this issue.  The following change would work:</div>
<div><br></div><div><span style="font-family:arial,sans-serif;font-size:13px">// start here</span></div><div>__attribute__((address_space(3)))</div><div><span style="font-family:arial,sans-serif;font-size:13px">static unsigned char b[3]={0x10,0x30,0x55};</span></div>
<div><font face="arial, sans-serif"><br></font><span style="font-family:arial,sans-serif;font-size:13px">int arrtest (int x, int y) {</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">  b[0] = (255 - b[x]) + b[y];  // now it works!</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">//  b[0] = (255 - b[0]) + b[1];  // works</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">  b[1] = x - y;</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">  b[2] = x * y;</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">  return b[0];</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">}</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">// end here</span><br></div><div><br></div>
<div>If you compile LLVM in debug mode, you would have seen a failure about an unknown address space.  I just committed a change in r174808 that emits a user-visible error for address space issues.</div><div><br></div><div style>
I understand that there is a lack of documentation on the proper use of this back-end, and this is something I am working on rectifying.  GPU targets place additional restrictions and conventions on compilers, and they often require some level of custom handling.  In this case, you need to be careful how you use address spaces.  You're not really doing anything wrong, clang was just never taught how to handle this case for NVPTX.</div>
<div style><br></div><div style><br></div><div style><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Feb 9, 2013 at 6:45 AM,  <span dir="ltr"><<a href="mailto:nkavv@physics.auth.gr" target="_blank">nkavv@physics.auth.gr</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi all,<br>
<br>
i'm trying to compile some small ANSI C benchmarks to PTX assembly. For this purpose, I'm using the NVPTX backend, introduced in the LLVM 3.2 release.<br>
<br>
It appears that certain LLVM constructs cannot be compiled to PTX. The problems mostly deal with handling of arrays. I also don't get any debug info when these problems.<br>
<br>
I use "llc" for compiling regular .ll files to PTX using the "nvptx" backend. The llc options are as follows (omitting these options also present the same problems):<br>
<br>
-nacdrvtest -asm-verbose -stats -print-before-all -print-after-all -time-passes<br>
<br>
1. Only programs that make no use of arrays appear to always be processed correctly by llc (nvptx target). Some programs using arrays, make llc "hang" (runs endlessly).<br>
<br>
2. One small C function exposing this behavior is as follows:<br>
<br>
// start here<br>
int arrtest (int x, int y) {<br>
  unsigned char b[3]={0x10,0x30,0x55};<br>
  b[0] = (255 - b[x]) + b[y];  // doesn't work!<br>
//  b[0] = (255 - b[0]) + b[1];  // works<br>
  b[1] = x - y;<br>
  b[2] = x * y;<br>
  return b[0];<br>
}<br>
// end here<br>
<br>
3. A change that makes this function compilable (also visible in the comments)<br>
<br>
Change:<br>
b[0] = (255 - b[x]) + b[y];<br>
to:<br>
b[0] = (255 - b[0]) + b[1];<br>
<br>
Similarly, even the use of a single variable indexing (e.g. b[x]) doesn't work.<br>
<br>
Please note that if variable indexing is not used, then PTX assembly is generated. The problems seems related to the support/handling of variable indexing.<br>
<br>
However, the same code is always processed by other target backends (x86, mips).<br>
<br>
Is this behavior expected by the NVPTX backend?<br>
<br>
I understand that PTX is primarily targeted by OpenCL, however I would expect that the backend would issue some warnings on unimplemented features and not just run endlessly with no messages whatsoever.<br>
<br>
Best regards<br>
Nikolaos Kavvadias<br>
<br>
<br>
<br>
______________________________<u></u>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">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/<u></u>mailman/listinfo/llvmdev</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><br><div>Thanks,</div><div><br></div><div>Justin Holewinski</div>
</div></div>