<div class="gmail_quote">On Wed, Nov 16, 2011 at 8:05 AM, Alberto Magni <span dir="ltr"><<a href="mailto:alberto.magni86@gmail.com" target="_blank">alberto.magni86@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Dear Justin,<br>
<br>
I am trying to add the support for some OpenCL builtin functions to<br>
the PTX backend.<br>
The attached file represent the first stub of a patch for the fmax<br>
builtin function.<br></blockquote><div><br></div><div>First off, thanks for helping to improve the PTX back-end!</div><div><br></div><div>There are really two main issues here.  First, OpenCL built-in functions do not belong in the PTX back-end.  These will be implemented in the libclc library (<a href="http://www.pcc.me.uk/~peter/libclc/" target="_blank">http://www.pcc.me.uk/~peter/libclc</a>).  The back-end will only implement PTX intrinsics, which may be used by the OpenCL built-in functions in libclc.  However, this particular function (max) corresponds to a PTX instruction, so it makes sense to implement it as an intrinsic in the back-end.</div>

<div><br></div><div>Second, intrinsic functions require a bit more work.  You're off to a great start, but intrinsics are implemented a bit differently.  It looks like LLVM does not have a max intrinsic, so we'll need to create one.  Have a look at include/llvm/IntrinsicsPTX.td.  This file defines the PTX-specific intrinsics.  You can add an intrinsic for max here, and then implement a pattern-match in the PTXInstrInfo.td file.  There is no need to create a new SDNode type for intrinsics, unless they require some special handling in the C++ code, which I do not see being the case here.</div>
<div><br></div><div>When you define a new intrinsic, use the following template as a name: int_ptx_max.  This will define the LLVM intrinsic as @llvm.ptx.max().  Please follow the same convention when naming the __builtin_* function.</div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<br>
The test case I am trying is the following:<br>
<br>
define ptx_device float @f(float %x, float %y) {<br>
entry:<br>
  %z = call float @fmax(float %x, float %y)<br>
  ret float %z<br>
}<br>
<br>
declare float @fmax(float, float)<br>
<br>
But at the moment llc crashes saying that "calls are not supported",<br>
this does not<br>
happens with llvm builtins like llvm.sqrt.f32<br></blockquote><div><br></div><div>Which version of LLVM are you using?  Calls to PTX device functions have been implemented for a little while now, so I'm surprised to see that error.  Perhaps it's because the fmax function is not defined as ptx_device.</div>

<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Can you please give me a hint on what I am missing, or some general<br>
advice on how<br>
to add builtin functions.<br>
<br>
Thank you in advance,<br>
<br>
Alberto.<br>
<br>_______________________________________________<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/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><br><div>Thanks,</div><div><br></div><div>Justin Holewinski</div><br>