[LLVMdev] PROPOSAL: IR representation of detailed struct assignment information (new version)

Dan Gohman gohman at apple.com
Mon Sep 10 14:11:08 PDT 2012


On Sep 10, 2012, at 11:29 AM, Chandler Carruth <chandlerc at google.com> wrote:
> 
> 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.
> 
> 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.
> 
> 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.

This is not a problem in the current proposal, because it represents padding
completely independently from the LLVM struct type.

> 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.

I don't see how a hole in a base class which isn't being used by a subclass is
different from padding, from the optimizer's perspective. The optimizer
doesn't know about class hierarchies (unless you're proposing something
much more significant).

> 
> 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):
> 
> Remove non-packed struct types completely. Make LLVM structs represent a contiguous sequence of bytes, explicitly partitioned into fields with particular primitive types.
> 
> 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.
> 
> 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.

The current situation is not bleak. ABIs don't often vary that much in the
way they lay out structs and arrays, especially within a given architecture.

I actually think it's kind of nice that LLVM has this native concept of "normal"
struct layout built in. It encourages people to avoid doing their own custom struct
layout unless they have a good reason to.

I think your proposal would solve the original problem here, but it's not obviously
better than the metadata approach. Bytes in memory in LLVM don't have inherent types,
so optimizers can't rely on the type, or on any individual copy, to understand the
lifetimes of data in storage allocated for padding. Consequently, a type just
becomes a way to attach information to a copy. And it's not clear that using a type
is better than using metadata.

The metadata approach is nice because it separates the use cases into two families.
On one side, copies with no metadata are simple to create, simple to understand, and
simple to implement. On the other side, people who need more features can add
metadata to get there, and things are more complex all around, but that's the price
of using advanced features. This is the shape of problem that metadata was intended
to solve.

Dan




More information about the llvm-dev mailing list