<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/208203>208203</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [lldb] lldb ignores DW_AT_bit_size when evaluating DW_OP_convert to _BitInt base types
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          firmiana402
      </td>
    </tr>
</table>

<pre>
    Clang and GCC emit natural DWARF for C `_BitInt(31)` as a `DW_TAG_base_type` carrying both `DW_AT_byte_size = 4` and `DW_AT_bit_size = 31`.

When a value does not fully occupy the storage described by `DW_AT_byte_size`, `DW_AT_bit_size` describes the actual number of bits used to represent values of the type. DWARF specification defines `DW_OP_convert` as converting the top stack entry to the referenced base type.

LLDB derives the conversion width from `DW_AT_byte_size * 8` whenever a byte size is present, and only falls back to `DW_AT_bit_size` when there is no byte size. So a natural `_BitInt(31)` base type is converted as a 32-bit integer, ignoring its `DW_AT_bit_size = 31`.

The width is computed in `DWARFUnit::GetDIEBitSizeAndSign` (`DWARFUnit.cpp`), on the `DW_OP_convert` path:

```cpp
uint64_t bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
if (!bit_size)
  bit_size = die.GetAttributeValueAsUnsigned(DW_AT_bit_size, 0);
```

`DW_AT_byte_size` takes priority: `DW_AT_bit_size` is consulted only when the byte size is absent. For `_BitInt(31)` emitted as `DW_AT_byte_size = 4, DW_AT_bit_size = 31`, this yields a width of `32` instead of `31`.

## Possible Fix

When a type carries both `DW_AT_bit_size` and `DW_AT_byte_size`, `DW_AT_bit_size` should take priority, since it describes the actual value width — rather than deriving the width from `DW_AT_byte_size * 8`. In other words, prefer `DW_AT_bit_size` when it is present and fall back to `DW_AT_byte_size * 8` only when it is absent, so that types like `_BitInt(31)` are converted at their true 31-bit width.
</pre>
<img width="1" height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