<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 10, 2021 at 12:57 PM Johannes Doerfert <<a href="mailto:johannesdoerfert@gmail.com">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">Right. We could keep the definition of __nv_cos and friends<br>
around. Right now, -ffast-math might just crash on the user,<br>
which is arguably a bad thing. I can also see us benefiting<br>
in various other ways from llvm.cos uses instead of __nv_cos<br>
(assuming precision is according to the user requirements but<br>
that is always a condition).<br>
<br>
It could be as simple as introducing __nv_cos into<br>
"llvm.used" and a backend matching/rewrite pass.<br>
<br>
If the backend knew the libdevice location it could even pick<br>
the definitions from there. Maybe we could link libdevice late<br>
instead of eager?<br></blockquote><div><br></div><div><div class="gmail_default" style="font-family:verdana,sans-serif">It's possible, but it would require plumbing in CUDA SDK awareness into LLVM. While clang driver can deal with that, LLVM currently can't. The bitcode library path would have to be provided by the user.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">The standard library as bitcode raises some questions.</div><div class="gmail_default" style="font-family:verdana,sans-serif">* When do we want to do the linking? If we do it at the beginning, then the question is how to make sure unused functions are not eliminated before we may need them, as we don't know apriori what's going to be needed. We also do want the unused functions to be gone after we're done. Linking it in early would allow optimizing the code better at the expense of having to optimize a lot of code we'll throw away. Linking it in late has less overhead, but leaves the linked in bitcode unoptimized, though it's probably in the ballpark of what would happen with a real library call. I.e. no inlining, etc.</div></div><div><br></div><div><div class="gmail_default" style="font-family:verdana,sans-serif">* It incorporates linking into LLVM, which is not LLVM's job. Arguably, the line should be drawn at the lowering to libcalls as it's done for other back-ends. However, we're also constrained to by the need to have the linking done before we generate PTX which prevents doing it after LLVM is done generating an object file.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">One thing that may work within the existing compilation model is to pre-compile the standard library into PTX and then textually embed relevant functions into the generated PTX, thus pushing the 'linking' phase past the end of LLVM's compilation and make it look closer to the standard compile/link process. This way we'd only enable libcall lowering in NVPTX, assuming that the library functions will be magically available out there. Injection of PTX could be done with an external script outside of LLVM and it could be incorporated into clang driver. Bonus points for the fact that this scheme is compatible with -fgpu-rdc out of the box -- assemble the PTX with `ptxas -rdc` and then actually link with the library, instead of injecting its PTX before invoking ptxas.</div></div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></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><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Trying to figure out a good way to have the cake and eat it too.<br>
<br>
~ Johannes<br>
<br>
<br>
On 3/10/21 2:49 PM, William Moses wrote:<br>
> Since clang (and arguably any other frontend that uses) should link in<br>
> libdevice, could we lower these intrinsics to the libdevice code?<br></blockquote><div><br></div><div><div class="gmail_default" style="font-family:verdana,sans-serif">The linking happens *before* LLVM gets to work on IR.</div><div class="gmail_default" style="font-family:verdana,sans-serif">As I said, it's a workaround, not the solution. It's possible for LLVM to still attempt lowering something in the IR into a libcall and we would not be able to deal with that. It happens to work well enough in practice.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">Do you have an example where you see the problem with -ffast-math?</div></div><div><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>
> For example, consider compiling the simple device function below:<br>
><br>
> ```<br>
> // /mnt/sabrent/wmoses/llvm13/build/bin/clang <a href="http://tmp.cu" rel="noreferrer" target="_blank">tmp.cu</a> -S -emit-llvm<br>
>   --cuda-path=/usr/local/cuda-11.0 -L/usr/local/cuda-11.0/lib64<br>
> --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<br>
> deleted prior to their use in lowering from intrinsic to libdevice.<br>
> ...<br>
><br>
><br>
> On Wed, Mar 10, 2021 at 3:39 PM Artem Belevich <<a href="mailto:tra@google.com" target="_blank">tra@google.com</a>> wrote:<br>
><br>
>> On Wed, Mar 10, 2021 at 11:41 AM Johannes Doerfert <<br>
>> <a href="mailto:johannesdoerfert@gmail.com" target="_blank">johannesdoerfert@gmail.com</a>> wrote:<br>
>><br>
>>> 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>
>>><br>
>> Correct. It can't deal with anything that may need to lower to a standard<br>
>> library call.<br>
>><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>
>>><br>
>> It all boils down to the fact that PTX does not have the standard<br>
>> libc/libm which LLVM could lower the calls to, nor does it have a 'linking'<br>
>> phase where we could link such a library in, if we had it.<br>
>><br>
>> Libdevice bitcode does provide the implementations for some of the<br>
>> functions (though with a __nv_ prefix) and clang links it in in order to<br>
>> avoid generating IR that LLVM can't handle, but that's a workaround that<br>
>> does not help LLVM itself.<br>
>><br>
>> --Artem<br>
>><br>
>><br>
>><br>
>>> ~ Johannes<br>
>>><br>
>>><br>
>>> --<br>
>>> ───────────────────<br>
>>> ∽ Johannes (he/his)<br>
>>><br>
>>><br>
>> --<br>
>> --Artem Belevich<br>
>><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr">--Artem Belevich</div></div></div>