<div dir="ltr"><div class="gmail_default" style="font-family:verdana,sans-serif">+CC: jlebar, llvm-dev@ as this may be of some interest for other users of NVPTX back-end.<br></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Jun 14, 2018 at 9:33 AM Gheorghe-Teod Bercea <<a href="mailto:Gheorghe-Teod.Bercea@ibm.com">Gheorghe-Teod.Bercea@ibm.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"><font size="2" face="sans-serif">Hi Artem,</font><br><br><font size="2" face="sans-serif">I hope things are well.</font><br><br><font size="2" face="sans-serif">Just touching base regarding the patch
I posted last week: </font><a href="https://reviews.llvm.org/D47849" target="_blank"><font size="2" color="blue" face="sans-serif">https://reviews.llvm.org/D47849</font></a><br><br><font size="2" face="sans-serif">Based on your expertise of the CUDA
toolchain in Clang, are math functions for optimizations levels of O1 or
higher translated to device functions at all?</font><br><br><font size="2" face="sans-serif">I have been having a mixed experience
with that. On the device side, for CUDA, some functions (like pow) will
be translated to a device version but some functions like sqrt will use
the llvm intrinsic version even though an nvvm version of the function
exists.</font></blockquote><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font size="2" face="sans-serif"> I have been trying to leverage the existing CUDA functionality
for OpenMP device toolchain. I've been able to get OpenMP to do exactly
what CUDA does but my question, does CUDA do the right thing by using llvm
intrinsics on the device side? Or do we perhaps need to fix CUDA too.</font><br><br></blockquote><div><br></div><div><div class="gmail_default" style="font-family:verdana,sans-serif">​AFAICT, clang does not do anything special about translating math library calls into libdevice calls. We do include CUDA SDK headers that end up providing device-side overloads for at subset of libm calls. See include/math_functions.hpp in CUDA SDK.</div><div class="gmail_default" style="font-family:verdana,sans-serif">That maps math functions to __nv_* functions that come with CUDA SDK's libdevice bitcode. We link in necessary bits of bitcode before passing it to LLVM.<br></div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">Those libdevice functions in turn sometimes use NVPTX-specific intrinsics. E.g. fsqrt has this IR:</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="verdana, sans-serif"><span style="font-size:13px">define float @__nv_fsqrt_rn(float %x) #0 {<br></span></font><font face="verdana, sans-serif"><span style="font-size:13px">...<br></span></font><span style="font-family:verdana,sans-serif">  %3 = call float @llvm.nvvm.sqrt.rn.ftz.f(float %x)</span></blockquote><div class="gmail_default"><span style="font-family:verdana,sans-serif"><br></span></div><div class="gmail_default"><span style="font-family:verdana,sans-serif">Then LLVM replaces calls to some of those intrinsics to their LLVM counterparts:</span></div><div class="gmail_default"><font face="verdana, sans-serif"><span style="font-size:13px"><a href="https://github.com/llvm-mirror/llvm/blob/master/lib/Transforms/InstCombine/InstCombineCalls.cpp#L1466">https://github.com/llvm-mirror/llvm/blob/master/lib/Transforms/InstCombine/InstCombineCalls.cpp#L1466</a></span></font><br></div><div class="gmail_default"><span style="font-family:verdana,sans-serif"><br></span></div><div class="gmail_default"><span style="font-family:verdana,sans-serif">This way LLVM has ability to reason about these calls and can optimize some of them.</span></div><div class="gmail_default"><br></div><div class="gmail_default"><span style="font-family:verdana,sans-serif">So, depending on optimizations you may or may not see some of these transformations and hopefully it explains the inconsistencies you have seen.</span><br></div><div class="gmail_default"><span style="font-family:verdana,sans-serif"><br></span></div><div class="gmail_default"> <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"><font size="2" face="sans-serif">Please let me know your thoughts on
this.</font><br><br><font size="2" face="sans-serif">Thanks,</font><br><br><font size="2" face="sans-serif">--Doru</font><br><br><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>