This is an archive of the discontinued LLVM Phabricator instance.

[lld][Alignment] Use Align in ELF Sections
AbandonedPublic

Authored by gchatelet on Dec 2 2022, 7:57 AM.

Details

Reviewers
MaskRay
Summary

Follow up on D139181.
Here we assume that alignExpr contains a valid value (i.e. non zero power of 2).
It was already somewhat assumed but not checked. This will now crash if it's not the case.

Diff Detail

Event Timeline

gchatelet created this revision.Dec 2 2022, 7:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 2 2022, 7:57 AM
gchatelet requested review of this revision.Dec 2 2022, 7:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 2 2022, 7:57 AM
arichardson added inline comments.Dec 2 2022, 8:01 AM
lld/ELF/InputSection.h
74

Moving it after partition should allow it to fit into padding bytes

gchatelet added inline comments.Dec 2 2022, 8:48 AM
lld/ELF/InputSection.h
74

I moved the field around but it didn't improve the situation
https://godbolt.org/z/vYchbnME5

If you look at the -Xclang -fdump-record-layouts output you can see that it does improve the requirerd size, but due to the 8 byte alignment requirement it is still rounded up to 48 (for 64-bit architectures). However, for 32-bit arch reordering does help: 36 vs 40 bytes.

*** Dumping AST Record Layout
         0 | struct OutputSegment
     0:0-2 |   uint8_t sectionKind
     0:3-3 |   uint8_t bss
     0:4-4 |   uint8_t keepUnique
         1 |   uint8_t partition
         2 |   struct llvm::Align addralign
         2 |     uint8_t ShiftValue
         4 |   uint32_t type
         8 |   class llvm::StringRef name
         8 |     const char * Data
        16 |     size_t Length
        24 |   uint64_t flags
        32 |   uint32_t entsize
        36 |   uint32_t link
        40 |   uint32_t info
           | [sizeof=48, dsize=44, align=8,
           |  nvsize=44, nvalign=8]

Whereas the current patch has sizeof==dsize:

*** Dumping AST Record Layout
         0 | struct OutputSegment
     0:0-2 |   uint8_t sectionKind
     0:3-3 |   uint8_t bss
     0:4-4 |   uint8_t keepUnique
         1 |   uint8_t partition
         4 |   uint32_t type
         8 |   class llvm::StringRef name
         8 |     const char * Data
        16 |     size_t Length
        24 |   uint64_t flags
        32 |   struct llvm::Align addralign
        32 |     uint8_t ShiftValue
        36 |   uint32_t entsize
        40 |   uint32_t link
        44 |   uint32_t info
           | [sizeof=48, dsize=48, align=8,
           |  nvsize=48, nvalign=8]
MaskRay added a comment.EditedDec 2 2022, 10:40 AM

With this patch, the final lld executable is larger and some call sites are more complex.
llvm::Align probably won't improve lld/ELF (and the extra dependency llvm/Support/Alignment.h and MathExtras.h is somewhat unnecessary).
I've checked using p2align for lld::elf::Symbol, but I think it won't decrease the struct size and will likely lead to no less code size in most call sites.

Note: I think struct size for 32-bit systems isn't really necessary. The 64-bit system behavior is much more interesting.

MaskRay requested changes to this revision.Dec 2 2022, 10:40 AM
This revision now requires changes to proceed.Dec 2 2022, 10:40 AM
gchatelet abandoned this revision.Dec 19 2022, 5:44 AM