[PATCH] D81580: [ADT] Add Bitfield utilities

Guillaume Chatelet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 10 09:17:46 PDT 2020


gchatelet created this revision.
gchatelet added reviewers: chandlerc, courbet.
Herald added subscribers: llvm-commits, dexonsmith, mgorny.
Herald added a project: LLVM.

Context: There are places in LLVM where we need to pack typed fields into opaque values.
For instance, the `XXXInst` classes in `llvm/include/llvm/IR/Instructions.h` that extract informations from `Value::SubclassData` via `getSubclassDataFromInstruction()`.
The bit twiddling is done manually: this impairs readability and prevent consistent handling of out of range values (e.g. https://github.com/llvm/llvm-project/blob/435b458ad0a4630e6126246a6865748104ccad06/llvm/include/llvm/IR/Instructions.h#L564)
More importantly, the bit pattern is scattered throughout the implementation making it hard to pack additionnal fields or check for overlapping bits.

Design: The Bitfield structs are to be declared together so it is clear which bits are used or not.
The code is designed with simplicity in mind, hence a few limitations:

- Storage is limited to a single integer that cannot be bigger than `uint64_t`,
- Values and storage have to be `unsigned` or `enum` (`enum class` works),
- Is is not possible to store a full `uint64_t` at once i.e. Bitfield<uint64_t, 0, 64> will fail,
- There are no automatic detection of overlapping fields (packed bitfield declaration should help though),
- The interface is C like so `storage` needs to be passed in everytime (code is simpler and lifetime considerations more obvious)

Feedback welcome


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81580

Files:
  llvm/include/llvm/ADT/Bitfields.h
  llvm/unittests/ADT/BitFieldsTest.cpp
  llvm/unittests/ADT/CMakeLists.txt

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81580.269872.patch
Type: text/x-patch
Size: 9452 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200610/9d636398/attachment.bin>


More information about the llvm-commits mailing list