<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jun 4, 2020 at 5:45 PM John McCall <<a href="mailto:rjmccall@apple.com" target="_blank">rjmccall@apple.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">These attributes would have to spell out the exact expected treatment by <br>
the backend in essentially every aggregate case, and the frontend would <br>
have to carefully select that treatment, and for many ABIs that would <br>
still require counting registers and so on.</blockquote><div><br></div><div>I don't have all the ABIs memorized, but I don't think it would be the case that the frontend would need to count registers for any of the ABIs I know of.</div><div><br></div><div>I see this as consisting of two independent pieces:</div><div>1. Examining the parameter types, and distilling the important information about each type, <i>for a given ABI</i>, into a blob of ABI-specific data.</div><div>2. Actually choosing whether to pass a given parameter in a register, or on the stack, or split up the parameter into multiple registers, etc. </div><div><br></div><div>Step 1 should be done within Clang. The amount of data generated from this step, for the ABIs I'm familiar with, is small, and can be derived based only on the frontend type (not location in parameter list, etc).</div><div>Step 2 should be done within LLVM, based on the data passed down in the IR. This of course does need to count registers, among other things.</div><div><br></div><div>So, taking an example from the RISC-V ABI. Given an argument of type:</div><div>  struct X { short s; double d; };</div><div><div>Or, similarly,</div><div>  struct X __attribute__((packed)) { struct { short i; } s[1]; double d; };</div><div></div></div><div><div><div>  struct X { short s; double __attribute__((aligned(256))) d; };</div><div></div></div><div></div></div><div><br></div><div>Clang would need to encode metadata saying that this type may be able to be passed via "INT+FLOAT" register-passing, having the INT of size 2 at offset 0, and FLOAT of size 8 at offset 8/2/256 respectively, for the 3 types above. (Or maybe the metadata should store a GEP path, rather than size+offset?)</div><div><br></div><div>Then, LLVM, seeing an argument with the INT+FLOAT ABI rule, would allocate it to registers/stack as follows:</div><div>1. If you're using hardware float, and FLEN >= 8, and XLEN >= 2, and if there is at least one floating point and one integer register available, then: Copy the data at the provided offsets into one floating point register and one integer register (with bits beyond the integer size undefined).</div><div>2. Otherwise, fallback to common aggregate handling rules:</div><div>  a. If size is < XLEN, </div><div>    i. and if there's 1 integer register available: Pass the struct (as laid out in memory) in an integer register.</div><div>    ii. otherwise: Pass on stack, with alignment min(stack_alignment, max(type_alignment, XLEN))</div><div>  b. If size < XLEN*2,</div><div>    i. and there are 2 registers available: Pass the struct (as laid out in memory) in two integer registers.<br></div><div>    ii. and there is 1 integer register available: Pass XLEN-sized half the struct in a register, and the other XLEN-sized half on the stack.</div><div><div>    iii. otherwise: Pass the aggregate on stack, with alignment as before.</div><div>  c. Otherwise, "pass by reference" -- make a copy on the stack outside the parameter-passing area, aligned appropriately for its type and then pass a pointer to that memory in the usual way for passing a scalar. </div><div><div>(leaving out the varargs rules for simplicity).</div><div><br></div><div></div></div><div>There's a lot of rules there, but the frontend shouldn't need to know about almost all of it -- the frontend only needs to evaluate whether the struct type matches the specification for INT+FLOAT (and so on, for the other categories of special handling), and encode that categorization into the IR. </div><div><br></div><div>Unfortunately, today, Clang <i>does</i> know all those rules I listed above -- and LLVM <i>also</i> has to know most of them! This is not a good situation.</div><div><br></div><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">I do actually like this <br>
approach in many ways, because it provides a path to a world where the <br>
backend stop permissively compiling everything the frontend throws at it <br>
and instead emits an error if the frontend asks for something that <br>
can’t be done, but it’s not going to make things more abstract.</blockquote><div><br></div><div>It doesn't make things more abstract, no. There's still going to be ABI-specific code in the frontend. But, it separates the concerns better, and can make the IR required from a frontend more clearly derived from the ABI.</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">
Having worked in this space for years, I am convinced that there are two <br>
meaningful points for ABI lowering: (1) the high-level source-language <br>
information and (2) the low-level register and stack conventions.  (1), <br>
for C interop, is always going to be duplicative of Clang.  You can <br>
introduce an intermediate library and make Clang copy all relevant <br>
information out of its AST into that library’s type system, but <br>
fundamentally “all relevant information” is going to just keep <br>
expanding and expanding, and Clang is still going to have a ton of <br>
target-specific ABI lowering code to do that propagation.</blockquote><div><br></div><div>I definitely think it's infeasible to provide all possibly-relevant information about the frontend language type to LLVM in a ABI-independent manner. But, providing ABI-specific metadata makes the problem feasible, because for any particular ABI, the set of parameters derived from the frontend type system will be small.</div><div> </div></div></div>