[llvm-dev] The best way of generating a good representation for an array with header?

Christoffer Lernö via llvm-dev llvm-dev at lists.llvm.org
Wed Nov 13 00:42:05 PST 2019

> On 13 Nov 2019, at 02:33, David Blaikie <dblaikie at gmail.com> wrote:
> On Tue, Nov 12, 2019 at 2:36 PM Christoffer Lernö <christoffer at aegik.com <mailto:christoffer at aegik.com>> wrote:
> The advantages:
> 1. A pointer to the struct offset can be converted to a pointer without any cost.
> 2. A nullpointer to a stretchy buffer can be treated as a zero length array. Consequently no actual struct allocation is needed to represent a zero length array.
> (2) could probably be done as well with the other representation (with the prefix data representation you'd still have to special case the null test before going backwards from the pointer to find the size - because you wouldn't want to go backwards from null and try to read bytes there to find the size).

But that runs into (1).

In the stretchy buffer, converting to a pointer is essentially a NOP.

For the case you suggest, with the solution in (2) would be the following:

1. Is it NULL? If so return NULL.
2. Otherwise return current pointer + size of header as the pointer to the data.

There is then the question of what happens to indexing.

If we do the above conversion to pointer then we are doing an addition comparison and add.

For deref we can optimize away the NULL check for indexing though (assuming UB to index an array out of bounds), for x86 I think the add is included in indexing, but for ARM it’s not.

It might be a small cost to pay.

> 3. A reference to the array is the same size as to a pointer.
> (3) Would be true with either representation I'm picturing. (pointing to the start of the struct you've described, rather tahn pointing to the trailing array and walking backwards to find the rest)

Compared to implementations like std::vector I meant.
> 4. It can be converted to and back from an pointer without losing any information about the size & capacity.
> Converting back you have to know it's the start of an array, though, right? (& you could still do that in the other representation - by subtraction, but yes, wouldn't be free/zero-cost)

Here I’m again mostly thinking about std::vector.

> The downsides are what we discuss. But it looks like I have to accept that I can only represent it as a pointer with unknown length in DWARF then?
> I imagine it'd be difficult to describe the calling convention for passing this array? Are you going to have instances of this on the stack and passed by value? or only ever individually dynamically allocated & pointed to from other places?

The array is always passed as reference, but could potentially be put on the stack but would still always be used as a reference.
> If they were individually dynamically allocated & only ever pointed to the trailing array - the DWARF for that'd be pretty simple - basically just a special pointer type. The pretty printer for it would know it was allowed to walk backwards to find some extra things (as would any member functions, if needed, be implemented in terms of walking the poniter back).

Alright, so then maybe this is solvable after all? Is there any docs on how to do this the proper way in LLVM? And perhaps also where to patch this into a fork of LLDB?

Best Regards,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20191113/9866a9dc/attachment.html>

More information about the llvm-dev mailing list