Background
An array of structures in memory is laid out in a way that is not very efficient when cache accesses are taken into account. In the following example, when the element ‘a’ of successive structures are accessed in a loop, the cache misses often after a few accesses.
What we are trying to add here is an optimization that would better utilize the cache and results in misses less often.
Structure Peeling
In the above image, we see that the elements of different structures are now rearranged so that all the different elements from all the structure in the original array are now laid out together. This would mean that whenever an element ‘a’ is accessed, the subsequent elements ‘a’ from the subsequent original array elements are loaded to the cache, resulting in misses that occur less often. This kind of layout rearrangement can also be beneficial in enabling loop optimizations like vectorization.
Proposed Implementation
There are multiple research papers with implementation details that have been adapted by a few compilers. But they are heavily reliant on type escape analyses which do not provide for a good basis when dealing with a language like C in which types can be freely casted to and from. Even if we were to attempt to implement such an analysis, it would be very limiting. Moreover, with LLVM adapting opaque pointers this has been made impossible to perform reliably as well. So we take an alternate approach of analysing allocations and determining if the contents can be rearranged.
Analysis
The objective of the analysis is to determine which allocations can be rearranged as described above. This analysis does not limit itself to structures and would work equally well in situations where the memory accesses follow the pattern of strided accesses within the boundaries of the allocation.
We start with the allocation and look at its users and their users recursively. We are particularly interested in all the GEPs, loads and stores - we use them to essentially reconstruct a view of the structure by collecting all the sizes present at different offsets of the allocation that are being accessed.
We check, for each user of the allocation, if it is a:
- GEP - collect its constant offset and variable offset multiplier. The variable offset multiplier of the GEP following the first GEP should all be the same - this means that we are indexing into GEPs of the same structure
- Load/Store - this user would be following a GEP, hence a constant offset would be present. Collect the size being accessed at this offset. This size should be the same for any further access (load/store) at the same offset.
If the conditions are met, this allocation is safe for rearrangement.
Transformation
The transformation is just a rewriting of GEPs so that the accesses now happen in a way that rearranges the elements in memory with every write. The uses of the GEPs are subsequently replaced with the new GEPs.
Implementation
Link to the code on my fork - Add basic Structure Layout Optimization · rahulana-quic/llvm-project@79e8dc9 · GitHub
This is a very limited implementation intended to showcase the kind of analysis and the transformation that needs to be performed - more complex cases will be handled shortly based on the feedback for the proposal.
Example
For the above C example, the diff with and without the proposed transformation:

