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

    <tr>
        <th>Summary</th>
        <td>
            [Tablegen] CodeEmitterGen::run in CodeEmitterGen.cpp  doesn't grow Bitwidth
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            tablegen
      </td>
    </tr>

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

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

<pre>
    ## Bug
If all instruction records are TargetOpcode or isPseudo we never grows BitWidth above zero.

## Why? 
On line 487 BitWidth is set to 0. 
https://github.com/llvm/llvm-project/blob/de209fa11b5455155228bcdba012b6074388b917/llvm/utils/TableGen/CodeEmitterGen.cpp#L487

We never get to line 506
https://github.com/llvm/llvm-project/blob/de209fa11b5455155228bcdba012b6074388b917/llvm/utils/TableGen/CodeEmitterGen.cpp#L506-L506

 that could have set BitWidth Because of
https://github.com/llvm/llvm-project/blob/de209fa11b5455155228bcdba012b6074388b917/llvm/utils/TableGen/CodeEmitterGen.cpp#L490-L492



The reason why  it is like this is because `TargetOpcode` do not have a `Inst` field.

## The problem 

If we only have TargetOpcode or  isPseudo then We never add any `UINT64_C(0)`
to  `NEW_TARGETGenMCCodeEmitter.inc` in 

```cpp
uint64_t DirectXMCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,
    SmallVectorImpl<MCFixup> &Fixups,
    const MCSubtargetInfo &STI) const {
  static const uint64_t InstBits[] = {
```

That causes a compile error because `InstBits` is just full of commas with no data in the array.

## How to debug

```bash
lldb -- /mnt/DevDrive/projects/llvm_debug_build/bin/llvm-tblgen -gen-emitter -I /mnt/DevDrive/projects/llvm-project/llvm/lib/Target/NEW_TARGET -I/mnt/DevDrive/projects/llvm_debug_build/include -I/mnt/DevDrive/projects/llvm-project/llvm/include -I /mnt/DevDrive/projects/llvm-project/llvm/lib/Target /mnt/DevDrive/projects/llvm-project/llvm/lib/Target/NEW_TARGET/NEW_TARGET.td --write-if-changed -o /mnt/DevDrive/projects/llvm_debug_build/lib/Target/NEW_TARGET/NEW_TARGETGenMCCodeEmitter.inc -d /mnt/DevDrive/projects/llvm_debug_build/lib/Target/NEW_TARGET/NEW_TARGETGenMCCodeEmiter.inc
```

## Potential Fixes
### Fix 1
On line 487 Set BitWidth to 1. 
https://github.com/llvm/llvm-project/blob/de209fa11b5455155228bcdba012b6074388b917/llvm/utils/TableGen/CodeEmitterGen.cpp#L487

### Fix 2
Maybe you are thinking what does it mean to have a target with only TargetOpcode. Well then at least  Pseudo instructions need to set the Bit width

```cpp
if( R->getValueAsBit("isPseudo")) {
    const BitsInit *BI = R->getValueAsBitsInit("Inst");
    BitWidth = std::max(BitWidth, BI->getNumBits());
}
``` 

Of the Two I think fix one is more correct. a 0 bitwidth doesn't make any sense and could cause problems for others. I don't know if 1 for the bitwidth makes sense though. 
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMV01v27oS_TX0ZiBDoizJXnhhO1GegKYtGr_m7QJKHElsaNIgqTjur3-gJH-k6aK99-KigIBYIedwvs4ckVkrGoW4JMmaJDcT1rlWm2XNzHet5KTU_LgkNCY0hnXXkHBV1MCkBKGsM13lhFZgsNKGW2AGYctMg-7TvtIcQRsQ9rPFjms4ICh8QQON0QcLa-EeBXctsFK_IHxHo6ckXPlnOO2xPZI4BxKuPimQQiHM5tnFTFiw6MBpCKd-U-vc3pJ4RWhOaN4I13bltNI7QnMpX05_gr3R37ByhOal1CWhOUcaLmoWRWUyS5IoSSidlxUvWRjRMg2zWTyfl4sou-B0TkhLaL5lpcQ7VITmG83xdiecQ3OHalrt94TGH2bzbIjo8Rz64HEfTRKmf6bbSZgGHwbvSLgC1zIHle4kh5a9YJ_1cxXWWLHOIuj6z4xltgiDD7MFHTurf7YtgkFmtYJDewQQzjeTFM8IrhXWv5RjWCQNr_uZpCFwDUq7IRXMbyiUdX6hFij52xb2J-2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