[PATCH] D76526: Add an algorithm for performing "optimal" layout of a struct.

John McCall via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 20 14:07:14 PDT 2020


rjmccall created this revision.
Herald added subscribers: llvm-commits, dexonsmith, modocache, hiraditya, mgorny.
Herald added a project: LLVM.

The algorithm supports both assigning a fixed offset to a field prior to layout and allowing fields to have sizes that aren't multiples of their required alignments.  This means that the well-known algorithm of sorting by decreasing alignment isn't always good enough.  Still, we start with that, and only if that leaves padding around do we fall back on a greedy padding-minimizing algorithm.

      

There is no known efficient algorithm for producing a guaranteed-minimal layout in all cases.  In fact, allowing arbitrary fixed-offset fields means there's a straightforward reduction from bin-packing, making this NP-hard. But as usual with such problems, we can still efficiently produce adequate solutions to the cases that matter most to us.

      

I intend to use this in coroutine frame layout, where the retcon lowerings very badly want to minimize total space usage, and where the switch lowering can indeed produce a header with interior padding if the promise field is highly-aligned.  But it may be useful in a much wider variety of situations.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76526

Files:
  llvm/include/llvm/Support/OptimalLayout.h
  llvm/lib/Support/CMakeLists.txt
  llvm/lib/Support/OptimalLayout.cpp
  llvm/unittests/Support/CMakeLists.txt
  llvm/unittests/Support/OptimalLayoutTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76526.251755.patch
Type: text/x-patch
Size: 27949 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200320/69dcdea4/attachment.bin>


More information about the llvm-commits mailing list