Index: llvm/lib/TableGen/Record.cpp =================================================================== --- llvm/lib/TableGen/Record.cpp +++ llvm/lib/TableGen/Record.cpp @@ -1304,8 +1304,14 @@ Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const { if (RecordVal *Val = R.getValue(VarName)) - if (RV == Val || (!RV && !isa(Val->getValue()))) - return Val->getValue(); + if (RV == Val || (!RV && !isa(Val->getValue()))) { + Init *TheInit = Val->getValue(); + // Prevent recursion if this variable refers to itself. + if (TheInit == this || VarName->getAsUnquotedString() == "NAME") + return TheInit; + // Otherwise resolve all downstream references. + return TheInit->resolveReferences(R, RV); + } return const_cast(this); } Index: llvm/test/TableGen/var-eval.td =================================================================== --- /dev/null +++ llvm/test/TableGen/var-eval.td @@ -0,0 +1,41 @@ +// RUN: llvm-tblgen %s | FileCheck %s + +// Make sure that tablegen evaluates all values of the record fields. +def r0; +def r1; +class A { + dag a = (r0 r1); +} + +class B : A { + dag b0 = (r0 ); + dag b1 = (r1 ); + dag b2 = !if(i, b1, b0); + + let a = b2; + dag b = b2; +} + +// CHECK-LABEL: def b +def b : B<1>; +// both a and b should be evaluated. +// CHECK: dag a = (r1); +// CHECK: dag b = (r1); + +// NAME is a special case for the field evaluation. Make sure we don't recurse +// infinitely and that NAME has the right value. +multiclass M { + def NAME : B<0>; + def NAME # "a" : B<1>; +} + +defm m : M; +// CHECK-LABEL: def m +// CHECK: dag a = (r0); +// CHECK: dag b = (r0); +// CHECK: string NAME = "m" + +// CHECK-LABEL: def ma +// CHECK: dag a = (r1); +// CHECK: dag b = (r1); +// CHECK: string NAME = "m"