[all-commits] [llvm/llvm-project] b56b46: [ADT] Add Bitfield utilities

Guillaume Chatelet via All-commits all-commits at lists.llvm.org
Mon Jun 29 05:49:10 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: b56b467a9a84510bd1c5a573c863cb86c98afbcd
      https://github.com/llvm/llvm-project/commit/b56b467a9a84510bd1c5a573c863cb86c98afbcd
  Author: Guillaume Chatelet <gchatelet at google.com>
  Date:   2020-06-29 (Mon, 29 Jun 2020)

  Changed paths:
    A llvm/include/llvm/ADT/Bitfields.h
    A llvm/unittests/ADT/BitFieldsTest.cpp
    M llvm/unittests/ADT/CMakeLists.txt

  Log Message:
  -----------
  [ADT] Add Bitfield utilities

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 decisions:
-----------------
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,
 - Enum values have to be `unsigned`,
 - Storage type has to be `unsigned`,
 - 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)

RFC: http://lists.llvm.org/pipermail/llvm-dev/2020-June/142196.html

Differential Revision: https://reviews.llvm.org/D81580




More information about the All-commits mailing list