[TableGen] SIGSEGV in AddParentClass for a multiclass with dump instantiated via defm X : MC, Class
Summary
llvm-tblgen segfaults when a multiclass body contains a dump statement and a defm instantiates that multiclass together with a regular class parent (defm X : MC, C;). TGParser::AddParentClass(RecordsEntry&, ClassReference&) handles Record and Assertion entries but then unconditionally dereferences Entry.Loop; a Dump entry has Loop == nullptr.
Reproducer
class C;
multiclass MC { dump "hi"; def D; }
defm X : MC, C;
Command and output (assertions build, main @ fd64c07f9ae1):
$ llvm-tblgen -print-records test.td
Stack dump:
0. Program arguments: llvm-tblgen -print-records test.td
...
4 libLLVMTableGen.so ... llvm::TGParser::AddParentClass(llvm::RecordsEntry&, llvm::ClassReference&) + 76
5 libLLVMTableGen.so ... llvm::TGParser::ParseDefm() + 2415
Segmentation fault (core dumped)
# exit code 139
Also reproduces with:
- an anonymous
defm : MC, C; dumpas the only entry in the multiclass bodydumpinside aforeachin the multiclass body
Does not reproduce with:
defm X : MC;(no class parent, so theRecordsEntryoverload is not called)- an
assertentry (explicitly skipped atTGParser.cpp:390-391) - a
defvar(not part ofNewEntries)
Analysis
dumpis a legal multiclass body statement (whitelist atTGParser.cpp:4530-4537).ParseDefmcallsAddParentClass(Entry, ParentClass)for every entry when thedefmhas a class parent (TGParser.cpp:4636-4655, call at:4647).AddParentClass(RecordsEntry&)is atTGParser.cpp:385-401:
if (Entry.Rec)
return AddParentClass(Entry.Rec.get(), ParentClass);
if (Entry.Assertion)
return false;
for (auto &E : Entry.Loop->Entries) { // Entry.Loop is null for Dump
if (AddParentClass(E, ParentClass))
return true;
}
RecordsEntryalready has aDumpmember (TGParser.h:57-70); this overload was never updated for it.
History: the recursion was introduced by 7d69e0f37d28 (2018-06-21, "TableGen: Allow foreach in multiclass to depend on template args"); the Assertion special case by 985ab6e1fa57 (2021-04-30, "Fix two bugs in 'defm' when complex 'assert' is involved"). Dump has been legal in multiclass bodies throughout.
Note: this is distinct from PR #236438, which keeps dumps when records are copied/instantiated but does not touch this RecordsEntry overload.
Suggested fix
Treat Dump like Assertion:
if (Entry.Assertion || Entry.Dump)
return false;
(dump messages are emitted later during resolve()), plus a lit regression test in llvm/test/TableGen/ that checks the exit code and the note: output.
Version
llvm-tblgen 24.0.0git, llvm-project main @ fd64c07f9ae1. llvm/test/TableGen passes 479/479, so this path is currently untested.
Related but independent crash found in the same area: #238797 (!head/!tail on deferred-empty lists → assert/UB).