Issue 238796
Summary [TableGen] !head/!tail on a deferred-empty list aborts in assert builds
Labels new issue
Assignees
Reporter nodecoda
    ## Summary

`llvm-tblgen` aborts on an assertion when `!head` or `!tail` is applied to a list that is empty only after final resolution. The parser rejects a literal empty list, but lists produced by fields, template arguments, or `!filter`/`!foreach`/`!listremove` reach `UnOpInit::Fold` as an empty `ListInit`, where only `assert`s guard `getElement(0)`/`slice(1)`.

In release builds (`NDEBUG`) the asserts are compiled out, so this is an out-of-bounds access (UB) rather than a diagnostic.

## Reproducer

```tablegen
class C<int N> {
  int h = !head(!filter(x, [1, 2], !gt(x, N)));
}
def D : C<5>;
```

A more direct form:

```tablegen
def D {
  list<int> e = [];
  int h = !head(e);
}
```

Command and output (assertions build, `main` @ `fd64c07f9ae1`):

```
$ llvm-tblgen -print-records test.td
llvm-tblgen: .../llvm/lib/TableGen/Record.cpp:875: const llvm::Init *llvm::UnOpInit::Fold(const llvm::Record *, bool) const: Assertion `!LHSl->empty() && "Empty list in head"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ ...
```

`!tail` fails the same way at `Record.cpp:882` (`Empty list in tail`).

Relevant backtrace frames:

```
9  libLLVMTableGen ... llvm::UnOpInit::Fold(llvm::Record const*, bool) const + 3631
10 libLLVMTableGen ... llvm::Record::resolveReferences(llvm::Resolver&, llvm::RecordVal const*) + 218
11 libLLVMTableGen ... llvm::TGParser::AddParentClass(llvm::Record*, llvm::ClassReference&) + 744
12 libLLVMTableGen ... llvm::TGParser::ParseObjectBody(llvm::Record*) + 1021
```

## Analysis

* `TGParser::ParseOperation` only rejects a concrete empty list (`TGParser.cpp:1380-1383`). Deferred expressions are not `ListInit` at parse time.
* `UnOpInit::Fold` has no error path for an empty list: the `HEAD` case is `Record.cpp:874-877`, `TAIL` is `Record.cpp:881-888`.
* Existing tests do not cover this: `llvm/test/TableGen` passes 479/479 with this build.

## Suggested fix

Replace the two asserts with `PrintFatalError(CurRec->getLoc(), ...)`, following the existing `LOG2` precedent at `Record.cpp:930-937`, and add a negative lit test in `llvm/test/TableGen/`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to