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/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..35d9396 100644 --- a/test/unit/interface/VisitorIRTests.cpp +++ b/test/unit/interface/VisitorIRTests.cpp @@ -108,3 +108,81 @@ 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 must cover ops that are members of a nested op class, and not just +// the ops that are direct members of the visited class. +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); + + EXPECT_EQ(Ops.size(), 4); + for (const auto *Op : Ops) { + EXPECT_TRUE(Op == Mul1 || Op == Add1 || Op == Sub1 || Op == Div1); + } +} + +// 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)); +}