<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" id="owaParaStyle"></style>
</head>
<body fpstyle="1" ocsi="0">
<div style="direction: ltr;font-family: Tahoma;color: #000000;font-size: 10pt;">
<div style="font-size: 13.3333px;">Hi all,</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">The Sparc LLVM compiler is not lowering the return value of complex numbers according to the Sparc ABI.</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">I'll try to describe the problem as succinctly as I can.</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">I've looked at the updated Sparc ABI (https://docs.oracle.com/cd/E26502_01/html/E28387/gentextid-2734.html)</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">This document states that complex numbers should be returned from functions using two registers - in the case of the problem I'm addressing, two floating point registers.</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">LLVM treats complex numbers as a structure of two floating point values. It therefore attempts to return a complex number as a structure (of two floats).</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">When there is a cross-system call (such as where LLVM code calls into GCC's runtime library), the LLVM code is calling usingthe Sparc ABI structure return convention, whereas GCC is correctly using registers for the ABI return.
 The mismatch causes an error. Note, the relevant change to ensure GCC handled this correctly happened here: https://gcc.gnu.org/gcc-3.4/sparc-abi.html</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">The problem I've come to realise is that the information that the value passed was originally a complex number is discarded by the LLVM clang front-end. By the time the back-end sees the code, it can no longer distinguish
 between what was originally a complex number and what could reasonably have simply been a structure of two floating point values. The two are indistinguishable from each other to the back-end. See the following IR fragment declaring a function taking two complex
 numbers and returning one complex number.</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">
<div style="font-size: 13.3333px;">define void @complex_op({ float, float }* noalias nocapture sret %agg.result, { float, float }* byval nocapture readonly align 4 %a, { float, float }* byval nocapture readonly align 4 %b) #0 {</div>
<div><br>
</div>
</div>
<div style="font-size: 13.3333px;">This means that every attempt I've made to fix this problem has resulted in breaking something else in the ABI - specifically, normal structure-return ABI implementation.</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">The fix can only possibly be implemented by a combination of changes in both the front-end and back-end of LLVM.</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">I've only implemented changes in the back-end of the system so far, so I don't even know if it's possible to mark a value as being originally of "complex" type in the front-end before it's passed to the back-end.</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">Ultimately, the question is: What changes would need to be made to</div>
<div style="font-size: 13.3333px;">  (a) the front-end, to propagate this information into the back-end.</div>
<div style="font-size: 13.3333px;">  (b) the back-end, to be able to detect this information and treat the function call ABI differently?</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">I can't find any comparable situation in other processors to copy. If any can point to comparable piece(s) of code, that would be most welcome.</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div style="font-size: 13.3333px;">Best Regards,</div>
<div style="font-size: 13.3333px;">Chris Dewhurst</div>
<div style="font-size: 13.3333px;">Lero,</div>
<div style="font-size: 13.3333px;">University of Limerick,</div>
<div style="font-size: 13.3333px;">Ireland.</div>
</div>
</body>
</html>