[PATCH] D82058: [ADT] Add Bitfield utilities - alternative design

Guillaume Chatelet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 17 15:40:08 PDT 2020


gchatelet created this revision.
gchatelet added reviewers: courbet, serge-sans-paille.
gchatelet added a project: LLVM.
Herald added subscribers: llvm-commits, dexonsmith, mgorny.
gchatelet updated this revision to Diff 271503.
gchatelet added a comment.

- update API documentation


Alternative design for D81580 <https://reviews.llvm.org/D81580>

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. Instructions.h <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), **(edit: the new design allows signed integer but not signed enums)**
- Is is not possible to store a full `uint64_t` at once i.e. Bitfield<uint64_t, 0, 64> will fail, **(edit: the new design allows this)**
- 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 every time (code is simpler and lifetime considerations more obvious)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D82058

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: D82058.271503.patch
Type: text/x-patch
Size: 19353 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200617/c0b1cf90/attachment.bin>


More information about the llvm-commits mailing list