[llvm] [ADT] Fix a bug in PackedVector::setValue for signed types (PR #159239)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 17 07:21:24 PDT 2025


kazutakahirata wrote:

> I'd think that in order to support signed types, we have to take care of sign extension and that merely manipulating the MSB is not enough? I haven't looked at the logic carefully, but this sounds fishy...

`getValue` handles sign extension with `~` in the signed case:

```
  static T getValue(const BitVectorTy &Bits, unsigned Idx) {
    T val = T();
    for (unsigned i = 0; i != BitNum-1; ++i)
      val = T(val | ((Bits[(Idx * BitNum) + i] ? 1UL : 0UL) << i));
    if (Bits[(Idx * BitNum) + BitNum - 1])  //  <---- Look
      val = ~val;                           //  <---- Look
    return val;
  }
```

That is, if the sign bit is set, we flip the whole value with `val = ~val`.  Again, we do not use two's complement in `PackedVector`.


https://github.com/llvm/llvm-project/pull/159239


More information about the llvm-commits mailing list