<div class="gmail_extra"><div class="gmail_quote">On Thu, Sep 6, 2012 at 4:24 PM, Dan Gohman <span dir="ltr"><<a href="mailto:gohman@apple.com" target="_blank" class="cremed">gohman@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello,<br>
<br>
Persuant to feedback,<br>
<br>
<a href="http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-August/052927.html" target="_blank" class="cremed">http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-August/052927.html</a><br>
<br>
here is a new proposal for detailed struct assignment information.<br>
<br>
Here's the example showing the basic problem:<br>
<br>
struct bar {<br>
 char x;<br>
 float y;<br>
 double z;<br>
};<br>
void copy_bar(struct bar *a, struct bar *b) {<br>
 *a = *b;<br>
}<br>
<br>
The solution I now propose here is to have front-ends describe the copy<br>
using metadata. For example:<br>
<br>
 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* %1, i64 16, i32 8, i1 false), !tbaa.struct !4<br>
 […]<br>
 !0 = metadata !{metadata !"Simple C/C++ TBAA"}<br>
 !1 = metadata !{metadata !"omnipotent char", metadata !0}<br>
 !2 = metadata !{metadata !"float", metadata !1}<br>
 !3 = metadata !{metadata !"double", metadata !1}<br>
 !4 = metadata !{metadata !5, i64 3, metadata !6, metadata !7}<br>
 !5 = metadata !{i64 1, metadata !1}<br>
 !6 = metadata !{i64 4, metadata !2}<br>
 !7 = metadata !{i64 8, metadata !3}<br>
<br>
Metadata nodes !0 through !3 are regular TBAA nodes as are already in use.<br>
<br>
Metadata node !4 here is a top-level description of the memcpy. It holds a<br>
list of virtual members. An integer represents a padding field of that<br>
size. A metadata tuple represents an actual data field. The tuple's members<br>
are an integer size and a TBAA tag for the field.<br></blockquote><div><br></div><div>Hey Dan, I've talked with you about this in person and on IRC, but I've not yet laid out my thoughts on a single place, so I'll put them here.</div>
<div><br></div><div>TL;DR: I really like the idea of using metadata to tag each member of a struct with TBAA, and re-using the TBAA metadata nodes we already have. I'm not as fond of the description of padding in the metadata node.</div>
<div><br></div><div>Currently padding is really hard to represent because there is sometimes a member of an LLVM struct which represents padding (packed structs and cases where the frontend type requires more alignment than the datalayout string specifies) and other times there isn't. The current proposal doesn't entirely fix this because we still will need some way to annotate the members of structs inserted purely for the purpose of padding.</div>
<div><br></div><div>Further, we have the problem that sometimes what is needed is a representation of a "hole", that is a region which is neither padding nor part of the struct itself. The canonical example is the tail padding of a base class where the derived class's first member has low alignent constraints.</div>
<div><br></div><div>I would propose that we solve these problems by a somewhat more invasive change, but one which will significantly simplify both LLVM and frontends (at least Clang, I suspect other frontends):</div><div>
<br></div><div>Remove non-packed struct types completely. Make LLVM structs represent a contiguous sequence of bytes, explicitly partitioned into fields with particular primitive types.</div><div><br></div><div>The idea would be to make all struct types be packed[1], and to represent padding as explicit members of the struct. These could in turn have a "padding" TBAA metadata node which would specify that member as being padding. This would simplify the metadata representation because there would *always* be a member to hang the padding tag off of. It would simplify struct layout analysis in LLVM because the difference between alloc-size and type-size would be irrelevant. It would dramatically simplify Clang's record layout building, which already has to fall back to packed LLVM structs in many cases because  normal structs produce offsets that conflict with the ABI's layout requirements.</div>
<div><br></div><div>Essentially, LLVM is trying to simplify ABI layout by providing a datalayout summary description of target alignments, and building structs with that algorithm. But unless this *exactly* matches the ABI in question, it actually makes the job harder because now we have to try, potentially fail, and end up with all the code to use the packed mode anyways. My theory is that there are too many ABIs in the world (and too weird rules within them) for us to ever really get this right at the LLVM layer. Instead, we should force the frontend to explicitly layout the bytes as it sees fit.</div>
<div><br></div><div><br></div><div>Ok, now to the "how does this all work" part:</div><div><br></div><div>- No more alignment needed in the datalayout string[2].</div><div>- Other places where today we have optional alignment, if omitted the alignment will be '1' instead of '0'. This will essentially require alignment to be specified in more places.</div>
<div>- Array elements are packed[3]. If the elements of an array must be padded out to a particular alignment, the array should be of a struct containing the element and a padding member of the appropriate size. This will allow us to tag that member with metadata as padding as well.</div>
<div>- Auto-upgrade uses old datalayout with alignments to synthesize necessary align specifiers on instructions etc.</div><div>- TBAA metadata will identify members of a struct type which are padding and hold no interesting data.</div>
<div><br></div><div>This would at least remove one dimension of complexity from Clang's record layout building by removing the need to try non-packed structs and fallback to packed. It should even allow us to retain the struct type for a base class with derived class members packed into previously "padding" bytes at the end. Currently, even the current proposal doesn't seem to support retaining the llvm struct type for the base class in this case, or easily annotating the fields of that base class with TBAA information.</div>
<div><br></div><div>Thoughts?</div><div>-Chandler</div><div><br></div><div>Some points of clarification:</div><div>[1]: I say "packed" repeatedly but never "bit packed" or "byte packed". My inclination is to make the rule within LLVM "byte packed" and fix the idea of a byte as an i8. I think its hopeless to support non-8-bit-bytes in LLVM, and we should just move past that illusion. However, it would certainly be possible to make this be "bit packed" and add bit padding with appropriate metadata. I might even like that if it gives us a cleaner semantic model, or helps tag certain bits as undef.</div>
<div><br></div><div>[2]: We could potentially keep some of this information here if there are other parts of LLVM that use it... I'm not deeply familiar with all the consumers of the datalayout string.</div><div><br></div>
<div>[3]: I'm torn on this one. It might be nice to have arrays get an optional alignment that establishes the stride of the elements, particularly if we want the semantics to be that between array elements we have a "hole" rather than padding. However, I'm not aware of any place where this is a practical or important constraint, and it seems to add complexity that we don't need. If needed, it could always be added later.</div>
</div></div>