From c4c04c0247f3084037d90ae74d4c0f065ea39b34 Mon Sep 17 00:00:00 2001 From: Jannik Silvanus Date: Thu, 12 Nov 2026 12:54:38 +0100 Subject: [PATCH 1/2] Add tests for nested op classes Op classes can be nested: an op class may have another op class as its superclass, so that concrete ops are members of an inner op class, which in turn is a member of an outer one. Add a nested op class to the test dialect and tests covering it. This exposes a bug: visiting an op class only covers the ops that are direct members of that op class, silently skipping the ops of nested op classes. This is because visiting is based on OpDescription::getAll, which is generated from the list of ops registered with an op class, and ops are only registered with their immediate superclass. classof does not have this problem, as it recurses into nested op classes. isa<> and visiting therefore disagree for nested op classes. The new test asserts the current, wrong behavior and is marked with a FIXME. It is fixed in the next commit. --- test/unit/dialect/TestDialect.td | 27 ++++++++ test/unit/interface/VisitorIRTests.cpp | 86 ++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/test/unit/dialect/TestDialect.td b/test/unit/dialect/TestDialect.td index bb32253..9cdc345 100644 --- a/test/unit/dialect/TestDialect.td +++ b/test/unit/dialect/TestDialect.td @@ -158,3 +158,30 @@ class ConcreteOpClassMemberOp def AddOp : ConcreteOpClassMemberOp<"add">; def MulOp : ConcreteOpClassMemberOp<"mul">; + +def SomeDerivedOpClass : OpClass { + let superclass = SomeBaseOpClass; + let arguments = (ins superclass); + + let summary = "sub-family of operations"; + let description = [{ + Illustrate nesting of the OpClass feature: this op class is itself a member of + SomeBaseOpClass, and the ops below are members of this class rather than direct + members of SomeBaseOpClass. + }]; +} + +class NestedOpClassMemberOp + : TestOp<"some.op.class.nested.subop." # op, []> { + let superclass = SomeDerivedOpClass; + let results = (outs); + let arguments = (ins superclass); + + let summary = "perform the nested " # op # " operation"; + let description = [{ + Illustrate nesting of the OpClass feature. + }]; +} + +def SubOp : NestedOpClassMemberOp<"sub">; +def DivOp : NestedOpClassMemberOp<"div">; diff --git a/test/unit/interface/VisitorIRTests.cpp b/test/unit/interface/VisitorIRTests.cpp index 50c2e2e..8b8033e 100644 --- a/test/unit/interface/VisitorIRTests.cpp +++ b/test/unit/interface/VisitorIRTests.cpp @@ -108,3 +108,89 @@ TEST_F(VisitorIRTestFixture, VisitOpClass) { EXPECT_TRUE(Op == Mul1 || Op == Mul2 || Op == Add1 || Op == Add2); } } + +// Visiting a nested op class must cover the ops of the nested class itself. +TEST_F(VisitorIRTestFixture, VisitNestedOpClass) { + llvm_dialects::Builder Builder{Context}; + Builder.SetInsertPoint(getEntryBlock()); + + Builder.create(); + Builder.create(); + auto *Sub1 = Builder.create(); + auto *Div1 = Builder.create(); + Builder.create(); + + DenseSet Ops; + static const auto Visitor = + llvm_dialects::VisitorBuilder>() + .add( + [](auto &Ops, test::SomeDerivedOpClass &Op) { Ops.insert(&Op); }) + .build(); + Visitor.visit(Ops, *Mod); + EXPECT_EQ(Ops.size(), 2); + for (const auto *Op : Ops) { + EXPECT_TRUE(Op == Sub1 || Op == Div1); + } +} + +// Visiting an op class should cover ops that are members of a nested op class, and not just +// the ops that are direct members of the visited class. +// +// FIXME: This is currently broken, and the test asserts the current (wrong) behavior. +// OpDescription::getAll, which visiting is based on, only reports the ops that are direct +// members of the visited op class, because ops are only registered with their immediate +// superclass. classof does not have this problem, as it recurses into nested op classes, so +// isa and visiting SomeBaseOpClass currently disagree: the ops of the +// nested class are silently skipped by the visitor. +TEST_F(VisitorIRTestFixture, VisitOpClassWithNestedOpClass) { + llvm_dialects::Builder Builder{Context}; + Builder.SetInsertPoint(getEntryBlock()); + + auto *Mul1 = Builder.create(); + auto *Add1 = Builder.create(); + auto *Sub1 = Builder.create(); + auto *Div1 = Builder.create(); + Builder.create(); + + // All four ops are members of SomeBaseOpClass as far as isa<> is concerned. + EXPECT_TRUE(isa(Mul1)); + EXPECT_TRUE(isa(Add1)); + EXPECT_TRUE(isa(Sub1)); + EXPECT_TRUE(isa(Div1)); + + DenseSet Ops; + static const auto Visitor = + llvm_dialects::VisitorBuilder>() + .add( + [](auto &Ops, test::SomeBaseOpClass &Op) { Ops.insert(&Op); }) + .build(); + Visitor.visit(Ops, *Mod); + + // FIXME: Should be 4, and should contain Sub1 and Div1 as well. + EXPECT_EQ(Ops.size(), 2); + for (const auto *Op : Ops) { + EXPECT_TRUE(Op == Mul1 || Op == Add1); + } +} + +// isa<> must see through the full op class hierarchy, independently of the visitor. +TEST_F(VisitorIRTestFixture, NestedOpClassIsa) { + llvm_dialects::Builder Builder{Context}; + Builder.SetInsertPoint(getEntryBlock()); + + auto *Mul1 = Builder.create(); + auto *Sub1 = Builder.create(); + auto *DialectOp1 = Builder.create(); + + // An op of the nested class is a member of both the nested and the base class. + EXPECT_TRUE(isa(Sub1)); + EXPECT_TRUE(isa(Sub1)); + + // An op that is a direct member of the base class is not a member of the nested class. + EXPECT_FALSE(isa(Mul1)); + EXPECT_TRUE(isa(Mul1)); + + // An unrelated op is a member of neither. + EXPECT_FALSE(isa(DialectOp1)); + EXPECT_FALSE(isa(DialectOp1)); +} From 308b832ba5578386725e2ebf164ceca6fbc5c77f Mon Sep 17 00:00:00 2001 From: Jannik Silvanus Date: Thu, 12 Nov 2026 13:33:24 +0100 Subject: [PATCH 2/2] Fix visiting of nested op classes Visiting an op class only covered the ops that are direct members of that op class, silently skipping the ops of nested op classes. Visiting is based on OpDescription::getAll, which is generated from the list of ops registered with an op class. Ops are only registered with their immediate superclass, so for a nested op class hierarchy, the generated array of the outer op class was missing the ops of the inner ones. classof does not have this problem, as it recurses into nested op classes, so isa<> and visiting disagreed. Collect the ops of nested op classes when generating getAll, which makes visiting agree with isa<> again. The generated code for the example dialect is unchanged, as it does not contain nested op classes. --- lib/TableGen/GenDialect.cpp | 31 +++++++++++++++++++++++--- test/unit/interface/VisitorIRTests.cpp | 14 +++--------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/TableGen/GenDialect.cpp b/lib/TableGen/GenDialect.cpp index 6d48bf7..8c187c0 100644 --- a/lib/TableGen/GenDialect.cpp +++ b/lib/TableGen/GenDialect.cpp @@ -43,6 +43,26 @@ cl::opt g_dialect("dialect", cl::desc("the dialect to generate"), c } // anonymous namespace +/// Recursively collect all operations that are members of \p opClass, including those that +/// are members of nested op classes. +/// +/// Operations are only registered with their immediate superclass, so the `operations` +/// member alone does not account for nested op classes. +static void +collectAllNestedOperations(const OpClass &opClass, + SmallVectorImpl &operations) { + for (const OpClass *subclass : opClass.subclasses) + collectAllNestedOperations(*subclass, operations); + + operations.append(opClass.operations.begin(), opClass.operations.end()); +} + +static SmallVector getAllNestedOperations(const OpClass &opClass) { + SmallVector operations; + collectAllNestedOperations(opClass, operations); + return operations; +} + static std::pair, GenDialect *> getSelectedDialect(RecordKeeperTy &records) { if (g_dialect.empty()) @@ -486,6 +506,11 @@ void llvm_dialects::genDialectDefs(raw_ostream &out, RecordKeeperTy &records) { FmtContextScope scope{fmt}; fmt.withOp(opClass.name); + // Nested op classes are part of this op class, so their operations must be included + // as well. + const SmallVector nestedOperations = + getAllNestedOperations(opClass); + // We'd prefer fully qualifying the llvm_dialects namespace below with // leading "::", but gcc/clang/MSVC reject this as they interpret the // ::llvm_dialects identifier than within the preceding ArrayRef type. The @@ -496,8 +521,8 @@ void llvm_dialects::genDialectDefs(raw_ostream &out, RecordKeeperTy &records) { ::llvm::ArrayRef<::llvm_dialects::OpDescription> llvm_dialects::OpDescription::getAll<$namespace::$_op>() { static const std::array<::llvm_dialects::OpDescription, $0> desc{)", - &fmt, opClass.operations.size()); - for (const auto &op : opClass.operations) { + &fmt, nestedOperations.size()); + for (const auto &op : nestedOperations) { fmt.addSubst("mnemonic", op->mnemonic); out << tgfmt(R"( ::llvm_dialects::OpDescription{$0, "$dialect.$mnemonic"},)", @@ -508,7 +533,7 @@ void llvm_dialects::genDialectDefs(raw_ostream &out, RecordKeeperTy &records) { return desc; } )", - &fmt, opClass.operations.size()); + &fmt, nestedOperations.size()); } out << R"( diff --git a/test/unit/interface/VisitorIRTests.cpp b/test/unit/interface/VisitorIRTests.cpp index 8b8033e..35d9396 100644 --- a/test/unit/interface/VisitorIRTests.cpp +++ b/test/unit/interface/VisitorIRTests.cpp @@ -133,15 +133,8 @@ TEST_F(VisitorIRTestFixture, VisitNestedOpClass) { } } -// Visiting an op class should cover ops that are members of a nested op class, and not just +// Visiting an op class must cover ops that are members of a nested op class, and not just // the ops that are direct members of the visited class. -// -// FIXME: This is currently broken, and the test asserts the current (wrong) behavior. -// OpDescription::getAll, which visiting is based on, only reports the ops that are direct -// members of the visited op class, because ops are only registered with their immediate -// superclass. classof does not have this problem, as it recurses into nested op classes, so -// isa and visiting SomeBaseOpClass currently disagree: the ops of the -// nested class are silently skipped by the visitor. TEST_F(VisitorIRTestFixture, VisitOpClassWithNestedOpClass) { llvm_dialects::Builder Builder{Context}; Builder.SetInsertPoint(getEntryBlock()); @@ -166,10 +159,9 @@ TEST_F(VisitorIRTestFixture, VisitOpClassWithNestedOpClass) { .build(); Visitor.visit(Ops, *Mod); - // FIXME: Should be 4, and should contain Sub1 and Div1 as well. - EXPECT_EQ(Ops.size(), 2); + EXPECT_EQ(Ops.size(), 4); for (const auto *Op : Ops) { - EXPECT_TRUE(Op == Mul1 || Op == Add1); + EXPECT_TRUE(Op == Mul1 || Op == Add1 || Op == Sub1 || Op == Div1); } }