<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
FYI, Nick just uploaded this protytype: <a href="https://reviews.llvm.org/D114174" id="LPNoLPOWALinkPreview">https://reviews.llvm.org/D114174</a></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="_Entity _EType_OWALinkPreview _EId_OWALinkPreview _EReadonly_1"></div>
<br>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> llvm-dev <llvm-dev-bounces@lists.llvm.org> on behalf of Cranmer, Joshua via llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Sent:</b> 18 November 2021 21:03<br>
<b>To:</b> llvm-dev@lists.llvm.org <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> [llvm-dev] Complex intrinsics proposal and roundtable</font>
<div> </div>
</div>
<style>
<!--
@font-face
        {font-family:"Cambria Math"}
@font-face
        {font-family:Calibri}
p.x_MsoNormal, li.x_MsoNormal, div.x_MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif}
a:link, span.x_MsoHyperlink
        {color:#0563C1;
        text-decoration:underline}
span.x_EmailStyle17
        {font-family:"Calibri",sans-serif;
        color:windowtext}
.x_MsoChpDefault
        {font-family:"Calibri",sans-serif}
@page WordSection1
        {margin:1.0in 1.0in 1.0in 1.0in}
div.x_WordSection1
        {}
-->
</style>
<div lang="EN-US" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="x_WordSection1">
<p class="x_MsoNormal">This is another proposal about introducing complex types into LLVM. Following on from
<a href="https://lists.llvm.org/pipermail/llvm-dev/2019-October/136100.html">https://lists.llvm.org/pipermail/llvm-dev/2019-October/136100.html</a>, this is different in that it doesn’t propose complex types directly but instead proposes representing complex
 numbers as vectors and using intrinsics. See also Florian’s proposal to do this (starting with complex multiply) here:
<a href="https://lists.llvm.org/pipermail/llvm-dev/2020-November/146568.html">https://lists.llvm.org/pipermail/llvm-dev/2020-November/146568.html</a>.</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">Representation of complex types</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">The proposal is to represent complex numbers as vectors of 2N floating-point types. For example, <2 x float> would represent a scalar complex number. <4 x float> would represent a vector of 2 complex floating-point numbers, with the first
 complex number living in lanes 0 and 1, and the second living in lanes 2 and 3. This representation of complex types matches the vector form in x86.</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">The basic arithmetic operations are mapped as follows:</p>
<p class="x_MsoNormal">+ or -: fadd or fsub <2 x float> %a, %b</p>
<p class="x_MsoNormal">*: call <2 x float> @llvm.complex.multiply(<2 x float> %a, <2 x float> %b)</p>
<p class="x_MsoNormal">/: call <2 x float> @llvm.complex.divide(<2 x float> %a, <2 x float> %b)</p>
<p class="x_MsoNormal">Building complex values, creal, cimag: existing extractvalue, insertvalue, and shufflevector instructions as appropriate<br>
cabs: call float @llvm.complex.abs(<2 x float> %val)<br>
cconj: call <2 x float> @llvm.complex.conj(<2 x float> %val)<br>
<br>
</p>
<p class="x_MsoNormal">One complexity that hasn’t been covered in prior proposals is what complex multiplication actually means. Among our major source languages (C/C++/Fortran), there is some variance as to the definition of multiplication, division, and complex
 absolute values. This variation is most acute when looking at division. The naïve expansion of computing (a + bi)/(c + di) is</p>
<p class="x_MsoNormal">denom = c * c + d * d<br>
real = (a * c + b * d) / denom<br>
imag = (b * c - a * d) / denom</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">If you use Fortran, there is a requirement that the division operation be scaled to prevent overflow in computing denom (at the very least, this is how I’ve seen existing Fortran compilers implement it). If you use C, there is an additional
 requirement that the resulting complex number be recomputed to infinity for certain cases where real and imaginary are both NaN (see Annex G of the C standard). Using the CX_LIMITED_RANGE pragma, or equivalent command-line option, lifts both of these requirements.
 Additionally, gcc provides a -fcx-fortran-rules that lifts only the latter requirement. My understanding is that all hardware implementations of complex multiply implement CX_LIMITED_RANGE rules.</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">My proposal is to distinguish between these situations using a mixture of existing fast-math flags and call-site attributes. Without any flags or call-site attributes, these intrinsics would expand to their compiler-rt equivalents of
 __mulsc3, __divsc3, etc., which is to say they would have full C requirements (both NaN checking and scaling). The “complex-limited-range” call-site attribute would disable both of these requirements. The “complex-no-scale” call-site attribute would disable
 the specific scaling requirement but retain the NaN checking behavior. Additionally, fast math flags can be used to generate behavior: nnan or ninf would trigger the dropping of the NaN checking code by itself.</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">Implementation experience</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">I have been able to implement patches that pattern-match for complex multiply and divide (in the CX_LIMITED_RANGE cases) early in InstCombine, and haven’t seen issues with that. Doing codegen for the non-CX_LIMITED_RANGE case, requiring
 a call to __mulsc3, is difficult because that function returns a C _Complex number, and the C ABIs for complex numbers tend to be inconsistent even among different floating-point types within the same architecture. The truly evil case is the i386 ABI for float,
 which is returned as edx:eax (or i64, as generated by clang).</p>
<p class="x_MsoNormal"> </p>
<p class="x_MsoNormal">If you want to talk more about this, I have a roundtable tomorrow, Friday, at 14:45 Eastern or 11:45 Pacific.</p>
<p class="x_MsoNormal"><span style="font-size:9.0pt"> </span></p>
<p class="x_MsoNormal"><span style="font-size:9.0pt"> </span></p>
<p class="x_MsoNormal"><span style="font-size:9.0pt">-- </span></p>
<p class="x_MsoNormal"><i><span style="font-size:9.0pt">Joshua Cranmer</span></i></p>
<p class="x_MsoNormal"> </p>
</div>
</div>
</body>
</html>