This is an archive of the discontinued LLVM Phabricator instance.

[COFF] Store alignment in log2 form, NFC
ClosedPublic

Authored by rnk on May 8 2019, 1:53 PM.

Details

Summary

Valid section or chunk alignments are powers of 2 in the range [1,
8192]. These can be stored more canonically in log2 form to free up some
bits in Chunk. Combined with D61696, SectionChunk gets 8 bytes smaller.

Diff Detail

Repository
rL LLVM

Event Timeline

rnk created this revision.May 8 2019, 1:53 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 8 2019, 1:53 PM
ruiu added inline comments.May 8 2019, 6:42 PM
lld/COFF/Chunks.cpp
860 ↗(On Diff #198721)

I'd think we should always pass a power-of-two instead of log2 to constructors for the sake of consistency.

lld/COFF/Chunks.h
47–48 ↗(On Diff #198721)

Is this enforced by the file format?

73 ↗(On Diff #198721)

If this can be triggered by feeding a bad file, this should be a call of error() instead of assert().

136 ↗(On Diff #198721)

Instead of emphasizing that the maximum alignment is 2^13, can we support an alignment up to a reasonable upper limit, say, 2^32? That's apparently large enough, and by doing that, we do not have to explain a trivia that what value is the maximum alignment value we can express by COFF. (And the runtime cost of doing that is virtually nothing.)

lld/COFF/Driver.cpp
1744–1745 ↗(On Diff #198721)

Can you eliminate {set,get}Log2Alignment and consistently use {set,get}Alignment, so that the details about how it is represented internally is hidden from the user?

aganea added inline comments.May 9 2019, 1:53 PM
lld/COFF/Chunks.h
47–48 ↗(On Diff #198721)

Yes there's a hard limit in the SectionCharacteristics, see PE Format

rnk marked 3 inline comments as done.May 9 2019, 2:14 PM

In the long term, I think we want to hoist section characteristics up into chunk, and that's going to create the 8192 max alignment constraint. This is just a first step to refactor the code to use the accessors.

lld/COFF/Chunks.h
73 ↗(On Diff #198721)

I don't think it's possible. I think it might be possible with -aligncomm or -functionpadmin, though, since those raise alignment on the command line and take arbitrary input.

136 ↗(On Diff #198721)

Originally I was planning to make this a bitfield, in which case, the comment is relevant, but if we store the characteristics in the on-disk format here, then this comment isn't really needed. It makes more sense to move it to the setter, since that has to deal with the edge case of invalid alignments.

rnk updated this revision to Diff 198920.May 9 2019, 3:16 PM
  • remove [gs]etLog2Alignment
aganea added inline comments.May 13 2019, 2:06 PM
lld/COFF/Chunks.cpp
865 ↗(On Diff #198920)

This is essentially the same code as Chunk::setAlignment, can you make it so that a static version of that function is used here instead?

lld/COFF/Chunks.h
71 ↗(On Diff #198920)

assert(P2Align<=Log2MaxSectionAlignment) ?

ruiu added a comment.May 15 2019, 6:13 AM

By the way, the other idea I had in mind for this problem is to define a new integer-ish class that can hold only a 2^n value, by overriding cast from/to an integer. I don't know if that is a good idea or a disgusting idea, though.

rnk added a comment.May 15 2019, 4:38 PM

By the way, the other idea I had in mind for this problem is to define a new integer-ish class that can hold only a 2^n value, by overriding cast from/to an integer. I don't know if that is a good idea or a disgusting idea, though.

Well, the next logical step, IMO, is to store the section characteristics directly in Chunk. I don't think that would work well with the custom type idea.

ruiu accepted this revision.May 17 2019, 4:58 AM

LGTM

lld/COFF/Chunks.h
70 ↗(On Diff #198920)

A little off-topic, but llvm::Log2_32 seems to be a redundant function, as I think we can just use countTrailingZeros.

This revision is now accepted and ready to land.May 17 2019, 4:58 AM
rnk marked 2 inline comments as done.May 20 2019, 3:50 PM
rnk added inline comments.
lld/COFF/Chunks.cpp
865 ↗(On Diff #198920)

I'm not sure it's worth it, it's only combining the assert with the log. The simplification I would prefer is to add back the log2 accessor, which is what I had originally, but I did this since it was requested.

lld/COFF/Chunks.h
71 ↗(On Diff #198920)

Added

rnk updated this revision to Diff 200369.May 20 2019, 3:51 PM
  • assert
This revision was automatically updated to reflect the committed changes.