<div dir="ltr"><div style>The lack of an open-source vector math library (which is what you suggest here) prompted me to start a project "vecmathlib", available at <<a href="https://bitbucket.org/eschnett/vecmathlib">https://bitbucket.org/eschnett/vecmathlib</a>>. This library provides almost all math functions available in libm, implemented in a vectorised manner, i.e. suitable for SSE2/AVX/MIC/PTX etc.</div>
<div style><br></div><div style>In its current state the library has rough edges, e.g. the precision of many math functions is not yet ideal, and exceptional cases (nan, inf) are probably not yet all handled correctly. I would be happy if vecmathlib could be used in LLVM.</div>
<div style><br></div><div style>For example, assuming that there is a data type "double4" containing a vector of 4 double precision values, vecmathlib provides a function double4 pow(double4, double4) that implements pow(). In the general case, i.e. if no system-specific machine instructions are available, this would use Taylor expansions to calculate pow(x,y)=exp(y*log(x)).</div>
<div style><br></div><div style>I would be happy to receive feedback on and/or contributions to vecmathlib. </div><div style><br></div><div style>-erik</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Thu, Feb 7, 2013 at 5:08 PM, Dmitry Mikushin <span dir="ltr"><<a href="mailto:dmitry@kernelgen.org" target="_blank">dmitry@kernelgen.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Justin, gentlemen,<br>
<br>
I'm afraid I have to escalate this issue at this point. Since it was discussed for the first time last summer, it was sufficient for us for a while to have lowering of math calls into intrinsics disabled at DragonEgg level, and link them against CUDA math functions at LLVM IR level. Now I can say: this is not sufficient any longer, and we need NVPTX backend to deal with GPU math.<br>


<br>
> There also is no standard libm for PTX.<br>
<br>
Yes, that's right, but there is an interesting idea to codegen CUDA math headers into LLVM IR and link it with user module at IR level.
This method gives a perfect degree of flexibility with respect to high-level languages: the user no longer needs to deal with headers and can have math right in the IR, regardless the language it was lowered from. I can confirm this method works for us very well with C and Fortran, but in order to make accurate replacements of unsupported
intrinsics calls, it needs to become aware of NVPTX backend
capabilities in the form of:<br>
<br>
bool NVPTXTargetMachine::<div>isIntrinsicSupported(Function& intrinsic) and<br>
string NVPTXTargetMachine::whichMathCallReplacesIntrinsic(Function& intrinsic)<br>
<br>
> I would prefer not to lower such things in the back-end since 
different compilers may want to implement such functions differently 
based on speed vs. accuracy trade-offs.<br>
<br>
Who are those different compilers? We are LLVM, the complete compiler
stack, which should handle these things on its specific preference.
Derived compilers may certainly think different, and it's their own business to change anything they want and never contribute back. We
should not forget there are a lot of derived projects that use LLVM
directly, like KernelGen or many of those embedded DSLs recently started flourishing. Their completeness and future relies on LLVM. For these reasons, I would strongly prefer LLVM/NVPTX should supply a reference GPU math implementation and invite you and everyone else to
form a joint roadmap to deliver it.<br>
<br>
Before we started, IANAL, but something tells me there could be a licensing issue about releasing the LLVM IR emitted from CUDA headers.<br>
Could you please check this with NVIDIA?<br>
<br>
Many thanks,<br>
- D.<br>
<br>
2012/9/6 Justin Holewinski <<a href="mailto:justin.holewinski@gmail.com" target="_blank">justin.holewinski@gmail.com</a>>:<br>
> On 09/06/2012 10:02 AM, Dmitry N. Mikushin wrote:<br>
>><br>
>> Dear all,<br>
>><br>
>> During app compilation we have a crash in NVPTX backend:<br>
>><br>
>> LLVM ERROR: Cannot select: 0x732b270: i64 = ExternalSymbol'__powisf2'<br>
>> [ID=18]<br>
>><br>
>> As I understand LLVM tries to lower the following call<br>
>><br>
>> %28 = call ptx_device float @llvm.powi.f32(float 2.000000e+00, i32 %8)<br>
>> nounwind readonly<br>
>><br>
>> to device intrinsic. The table llvm/IntrinsicsNVVM.td does not contain<br>
>> such intrinsic, however it should be builtin, according to<br>
>> cuda/include/math_functions.h<br>
><br>
><br>
> It actually gets lowered into an external function call.<br>
><br>
><br>
>><br>
>> Is my understanding correct, and we need simply add the corresponding<br>
>> definition to llvm/IntrinsicsNVVM.td ? How to do that, what are the<br>
>> rules?<br>
><br>
><br>
> PTX does not have an instruction (or simple series of instructions) that<br>
> implements pow, so this will not be handled.  I would prefer not to lower<br>
> such things in the back-end since different compilers may want to implement<br>
> such functions differently based on speed vs. accuracy trade-offs.<br>
><br>
> There also is no standard libm for PTX.  It is up to the higher-level<br>
> compiler to link against a run-time library that provides functions like pow<br>
> (see include/math_functions.h in a CUDA distribution).<br>
><br>
>><br>
>> Thanks,<br>
>> - D.<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>
><div><div><img></div></div><span class="HOEnZb"><font color="#888888"><span><font color="#888888">><br>
> --<br>
> Thanks,<br>
><br>
> Justin Holewinski<br>
></font></span></font></span></div>
<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>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br>Erik Schnetter <<a href="mailto:schnetter@cct.lsu.edu" target="_blank">schnetter@cct.lsu.edu</a>><br><a href="http://www.perimeterinstitute.ca/personal/eschnetter/" target="_blank">http://www.perimeterinstitute.ca/personal/eschnetter/</a>
</div>