<div dir="ltr">Since clang (and arguably any other frontend that uses) should link in libdevice, could we lower these intrinsics to the libdevice code?<br><br>For example, consider compiling the simple device function below:<br><br>```<div>// /mnt/sabrent/wmoses/llvm13/build/bin/clang <a href="http://tmp.cu">tmp.cu</a> -S -emit-llvm  --cuda-path=/usr/local/cuda-11.0 -L/usr/local/cuda-11.0/lib64 --cuda-gpu-arch=sm_37<br>__device__ double f(double x) {<br>    return cos(x);<br>}<br>```<br><br>The LLVM module for it is as follows:<br><br>```<br>...<br>define dso_local double @_Z1fd(double %x) #0 {<br>entry:<br>  %__a.addr.i = alloca double, align 8<br>  %x.addr = alloca double, align 8<br>  store double %x, double* %x.addr, align 8<br>  %0 = load double, double* %x.addr, align 8<br>  store double %0, double* %__a.addr.i, align 8<br>  %1 = load double, double* %__a.addr.i, align 8<br>  %call.i = call contract double @__nv_cos(double %1) #7<br>  ret double %call.i<br>}<br><br>define internal double @__nv_cos(double %a) #1 {<br>  %q.i = alloca i32, align 4<br>```<br><br>Obviously we would need to do something to ensure these functions don't get deleted prior to their use in lowering from intrinsic to libdevice.<br>...<br><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 10, 2021 at 3:39 PM Artem Belevich <<a href="mailto:tra@google.com">tra@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 10, 2021 at 11:41 AM Johannes Doerfert <<a href="mailto:johannesdoerfert@gmail.com" target="_blank">johannesdoerfert@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Artem, Justin,<br>
<br>
I am running into a problem and I'm curious if I'm missing something or <br>
if the support is simply missing.<br>
Am I correct to assume the NVPTX backend does not deal with `llvm.sin` <br>
and friends?<br></blockquote><div><br></div><div><div class="gmail_default" style="font-family:verdana,sans-serif">Correct. It can't deal with anything that may need to lower to a standard library call. </div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
This is what I see, with some variations: <a href="https://godbolt.org/z/PxsEWs" rel="noreferrer" target="_blank">https://godbolt.org/z/PxsEWs</a><br>
<br>
If this is missing in the backend, is there a plan to get this working, <br>
I'd really like to have the<br>
intrinsics in the middle end rather than __nv_cos, not to mention that <br>
-ffast-math does emit intrinsics<br>
and crashes.<br></blockquote><div><br></div><div><div class="gmail_default" style="font-family:verdana,sans-serif">It all boils down to the fact that PTX does not have the standard libc/libm which LLVM could lower the calls to, nor does it have a 'linking' phase where we could link such a library in, if we had it.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">Libdevice bitcode does provide the implementations for some of the functions (though with a __nv_ prefix) and clang links it in in order to avoid generating IR that LLVM can't handle, but that's a workaround that does not help LLVM itself.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div></div><div class="gmail_default" style="font-family:verdana,sans-serif">--Artem<br></div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
~ Johannes<br>
<br>
<br>
-- <br>
───────────────────<br>
∽ Johannes (he/his)<br>
<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr">--Artem Belevich</div></div></div>
</blockquote></div>