| 1 | //===- DialectInterfacesGen.cpp - MLIR dialect interface utility generator ===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // DialectInterfaceGen generates definitions for Dialect interfaces. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "CppGenUtilities.h" |
| 14 | #include "DocGenUtilities.h" |
| 15 | #include "mlir/Support/IndentedOstream.h" |
| 16 | #include "mlir/TableGen/GenInfo.h" |
| 17 | #include "mlir/TableGen/Interfaces.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/ADT/StringExtras.h" |
| 20 | #include "llvm/Support/FormatVariadic.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include "llvm/TableGen/CodeGenHelpers.h" |
| 23 | #include "llvm/TableGen/Error.h" |
| 24 | #include "llvm/TableGen/Record.h" |
| 25 | #include "llvm/TableGen/TableGenBackend.h" |
| 26 | |
| 27 | using namespace mlir; |
| 28 | using llvm::Record; |
| 29 | using llvm::RecordKeeper; |
| 30 | using mlir::tblgen::DialectInterface; |
| 31 | using mlir::tblgen::InterfaceMethod; |
| 32 | |
| 33 | /// Emit a string corresponding to a C++ type, followed by a space if necessary. |
| 34 | static raw_ostream &emitCPPType(StringRef type, raw_ostream &os) { |
| 35 | type = type.trim(); |
| 36 | os << type; |
| 37 | if (type.back() != '&' && type.back() != '*') |
| 38 | os << " " ; |
| 39 | return os; |
| 40 | } |
| 41 | |
| 42 | /// Emit the method name and argument list for the given method. |
| 43 | static void emitMethodNameAndArgs(const InterfaceMethod &method, StringRef name, |
| 44 | raw_ostream &os) { |
| 45 | os << name << '('; |
| 46 | llvm::interleaveComma(c: method.getArguments(), os, |
| 47 | each_fn: [&](const InterfaceMethod::Argument &arg) { |
| 48 | os << arg.type << " " << arg.name; |
| 49 | }); |
| 50 | os << ") const" ; |
| 51 | } |
| 52 | |
| 53 | /// Get an array of all Dialect Interface definitions |
| 54 | static std::vector<const Record *> |
| 55 | getAllInterfaceDefinitions(const RecordKeeper &records) { |
| 56 | std::vector<const Record *> defs = |
| 57 | records.getAllDerivedDefinitions(ClassName: "DialectInterface" ); |
| 58 | |
| 59 | llvm::erase_if(C&: defs, P: [&](const Record *def) { |
| 60 | // Ignore interfaces defined outside of the top-level file. |
| 61 | return llvm::SrcMgr.FindBufferContainingLoc(Loc: def->getLoc()[0]) != |
| 62 | llvm::SrcMgr.getMainFileID(); |
| 63 | }); |
| 64 | return defs; |
| 65 | } |
| 66 | |
| 67 | namespace { |
| 68 | /// This struct is the generator used when processing tablegen dialect |
| 69 | /// interfaces. |
| 70 | class DialectInterfaceGenerator { |
| 71 | public: |
| 72 | DialectInterfaceGenerator(const RecordKeeper &records, raw_ostream &os) |
| 73 | : defs(getAllInterfaceDefinitions(records)), os(os) {} |
| 74 | |
| 75 | bool emitInterfaceDecls(); |
| 76 | |
| 77 | protected: |
| 78 | void emitInterfaceDecl(const DialectInterface &interface); |
| 79 | |
| 80 | /// The set of interface records to emit. |
| 81 | std::vector<const Record *> defs; |
| 82 | // The stream to emit to. |
| 83 | raw_ostream &os; |
| 84 | }; |
| 85 | } // namespace |
| 86 | |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | // GEN: Interface declarations |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | |
| 91 | static void emitInterfaceMethodDoc(const InterfaceMethod &method, |
| 92 | raw_ostream &os, StringRef prefix = "" ) { |
| 93 | if (std::optional<StringRef> description = method.getDescription()) |
| 94 | tblgen::emitDescriptionComment(description: *description, os, prefix); |
| 95 | else |
| 96 | os << "\n" ; |
| 97 | } |
| 98 | |
| 99 | static void emitInterfaceMethodsDef(const DialectInterface &interface, |
| 100 | raw_ostream &os) { |
| 101 | |
| 102 | raw_indented_ostream ios(os); |
| 103 | ios.indent(with: 2); |
| 104 | |
| 105 | for (auto &method : interface.getMethods()) { |
| 106 | emitInterfaceMethodDoc(method, os&: ios); |
| 107 | ios << "virtual " ; |
| 108 | emitCPPType(type: method.getReturnType(), os&: ios); |
| 109 | emitMethodNameAndArgs(method, name: method.getName(), os&: ios); |
| 110 | |
| 111 | if (method.isDeclaration()) { |
| 112 | ios << ";\n" ; |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | if (method.isPureVirtual()) { |
| 117 | ios << " = 0;\n" ; |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | // if it is not a method declaration, then it's a normal interface method. |
| 122 | ios << " {" ; |
| 123 | |
| 124 | if (auto body = method.getBody()) { |
| 125 | ios << "\n" ; |
| 126 | ios.indent(with: 4); |
| 127 | ios << body << "\n" ; |
| 128 | ios.indent(with: 2); |
| 129 | } |
| 130 | os << "}\n" ; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static void emitConstructor(const DialectInterface &interface, |
| 135 | raw_ostream &os) { |
| 136 | |
| 137 | raw_indented_ostream ios(os); |
| 138 | |
| 139 | // We consider a constructor protected if interface has at least one pure |
| 140 | // virtual method |
| 141 | auto hasProtectedConstructor = |
| 142 | llvm::any_of(Range: interface.getMethods(), P: [](const InterfaceMethod &method) { |
| 143 | return method.isPureVirtual(); |
| 144 | }); |
| 145 | |
| 146 | ios.indent(with: 0); |
| 147 | if (hasProtectedConstructor) |
| 148 | ios << "protected:\n" ; |
| 149 | |
| 150 | ios.indent(with: 2); |
| 151 | ios << llvm::formatv(Fmt: "{0}(::mlir::Dialect *dialect) : Base(dialect) {{}\n" , |
| 152 | Vals: interface.getName()); |
| 153 | } |
| 154 | |
| 155 | void DialectInterfaceGenerator::emitInterfaceDecl( |
| 156 | const DialectInterface &interface) { |
| 157 | llvm::NamespaceEmitter ns(os, interface.getCppNamespace()); |
| 158 | |
| 159 | tblgen::emitSummaryAndDescComments(os, summary: "" , |
| 160 | description: interface.getDescription().value_or(u: "" )); |
| 161 | |
| 162 | // Emit the main interface class declaration. |
| 163 | os << llvm::formatv( |
| 164 | Fmt: "class {0} : public ::mlir::DialectInterface::Base<{0}> {{\n" |
| 165 | "public:\n" , |
| 166 | Vals: interface.getName()); |
| 167 | |
| 168 | emitInterfaceMethodsDef(interface, os); |
| 169 | |
| 170 | // Emit any extra declarations. |
| 171 | if (std::optional<StringRef> = |
| 172 | interface.getExtraClassDeclaration()) { |
| 173 | raw_indented_ostream ios(os); |
| 174 | ios.indent(with: 2); |
| 175 | ios.printReindented(str: extraDecls.value()); |
| 176 | ios << "\n" ; |
| 177 | } |
| 178 | |
| 179 | os << "\n" ; |
| 180 | |
| 181 | emitConstructor(interface, os); |
| 182 | |
| 183 | os << "};\n" ; |
| 184 | } |
| 185 | |
| 186 | bool DialectInterfaceGenerator::emitInterfaceDecls() { |
| 187 | |
| 188 | llvm::emitSourceFileHeader(Desc: "Dialect Interface Declarations" , OS&: os); |
| 189 | |
| 190 | // Sort according to ID, so defs are emitted in the order in which they appear |
| 191 | // in the Tablegen file. |
| 192 | std::vector<const Record *> sortedDefs(defs); |
| 193 | llvm::sort(C&: sortedDefs, Comp: [](const Record *lhs, const Record *rhs) { |
| 194 | return lhs->getID() < rhs->getID(); |
| 195 | }); |
| 196 | |
| 197 | // `::mlir::DialectInterface`. |
| 198 | if (!sortedDefs.empty()) |
| 199 | tblgen::emitIncludes(os, headers: StringRef("mlir/IR/DialectInterface.h" )); |
| 200 | |
| 201 | for (const Record *def : sortedDefs) |
| 202 | emitInterfaceDecl(interface: DialectInterface(def)); |
| 203 | |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | //===----------------------------------------------------------------------===// |
| 208 | // GEN: Interface registration hooks |
| 209 | //===----------------------------------------------------------------------===// |
| 210 | |
| 211 | static mlir::GenRegistration genDecls( |
| 212 | "gen-dialect-interface-decls" , "Generate dialect interface declarations." , |
| 213 | [](const RecordKeeper &records, raw_ostream &os) { |
| 214 | return DialectInterfaceGenerator(records, os).emitInterfaceDecls(); |
| 215 | }); |
| 216 | |