Issue 124067
Summary [Tablegen] CodeEmitterGen::run in CodeEmitterGen.cpp doesn't grow Bitwidth
Labels tablegen
Assignees
Reporter farzonl
    ## 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. 
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to