[TableGen] !head/!tail on a deferred-empty list aborts in assert builds
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 asserts 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
class C<int N> {
int h = !head(!filter(x, [1, 2], !gt(x, N)));
}
def D : C<5>;
A more direct form:
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::ParseOperationonly rejects a concrete empty list (TGParser.cpp:1380-1383). Deferred expressions are notListInitat parse time.UnOpInit::Foldhas no error path for an empty list: theHEADcase isRecord.cpp:874-877,TAILisRecord.cpp:881-888.- Existing tests do not cover this:
llvm/test/TableGenpasses 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/.
Related but independent crash found in the same area: #238799 (multiclass dump + defm X : MC, Class → SIGSEGV in AddParentClass).