| 1 | //===- OpDefinitionsGen.cpp - IRDL op definitions 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 | // OpDefinitionsGen uses the description of operations to generate IRDL |
| 10 | // definitions for ops. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Dialect/IRDL/IR/IRDL.h" |
| 15 | #include "mlir/IR/Attributes.h" |
| 16 | #include "mlir/IR/Builders.h" |
| 17 | #include "mlir/IR/BuiltinOps.h" |
| 18 | #include "mlir/IR/Diagnostics.h" |
| 19 | #include "mlir/IR/Dialect.h" |
| 20 | #include "mlir/IR/MLIRContext.h" |
| 21 | #include "mlir/TableGen/AttrOrTypeDef.h" |
| 22 | #include "mlir/TableGen/GenInfo.h" |
| 23 | #include "mlir/TableGen/GenNameParser.h" |
| 24 | #include "mlir/TableGen/Interfaces.h" |
| 25 | #include "mlir/TableGen/Operator.h" |
| 26 | #include "llvm/ADT/StringExtras.h" |
| 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/InitLLVM.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | #include "llvm/TableGen/Main.h" |
| 31 | #include "llvm/TableGen/Record.h" |
| 32 | #include "llvm/TableGen/TableGenBackend.h" |
| 33 | |
| 34 | using namespace llvm; |
| 35 | using namespace mlir; |
| 36 | using tblgen::NamedTypeConstraint; |
| 37 | |
| 38 | static llvm::cl::OptionCategory dialectGenCat("Options for -gen-irdl-dialect" ); |
| 39 | static llvm::cl::opt<std::string> |
| 40 | selectedDialect("dialect" , llvm::cl::desc("The dialect to gen for" ), |
| 41 | llvm::cl::cat(dialectGenCat), llvm::cl::Required); |
| 42 | |
| 43 | static Value createPredicate(OpBuilder &builder, tblgen::Pred pred) { |
| 44 | MLIRContext *ctx = builder.getContext(); |
| 45 | |
| 46 | if (pred.isCombined()) { |
| 47 | auto combiner = pred.getDef().getValueAsDef(FieldName: "kind" )->getName(); |
| 48 | if (combiner == "PredCombinerAnd" || combiner == "PredCombinerOr" ) { |
| 49 | std::vector<Value> constraints; |
| 50 | for (auto *child : pred.getDef().getValueAsListOfDefs(FieldName: "children" )) { |
| 51 | constraints.push_back(x: createPredicate(builder, pred: tblgen::Pred(child))); |
| 52 | } |
| 53 | if (combiner == "PredCombinerAnd" ) { |
| 54 | auto op = |
| 55 | irdl::AllOfOp::create(builder, location: UnknownLoc::get(context: ctx), operands: constraints); |
| 56 | return op.getOutput(); |
| 57 | } |
| 58 | auto op = |
| 59 | irdl::AnyOfOp::create(builder, location: UnknownLoc::get(context: ctx), operands: constraints); |
| 60 | return op.getOutput(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | std::string condition = pred.getCondition(); |
| 65 | // Build a CPredOp to match the C constraint built. |
| 66 | irdl::CPredOp op = irdl::CPredOp::create(builder, location: UnknownLoc::get(context: ctx), |
| 67 | pred: StringAttr::get(context: ctx, bytes: condition)); |
| 68 | return op; |
| 69 | } |
| 70 | |
| 71 | static Value typeToConstraint(OpBuilder &builder, Type type) { |
| 72 | MLIRContext *ctx = builder.getContext(); |
| 73 | auto op = |
| 74 | irdl::IsOp::create(builder, location: UnknownLoc::get(context: ctx), expected: TypeAttr::get(type)); |
| 75 | return op.getOutput(); |
| 76 | } |
| 77 | |
| 78 | static Value baseToConstraint(OpBuilder &builder, StringRef baseClass) { |
| 79 | MLIRContext *ctx = builder.getContext(); |
| 80 | auto op = irdl::BaseOp::create(builder, location: UnknownLoc::get(context: ctx), |
| 81 | base_name: StringAttr::get(context: ctx, bytes: baseClass)); |
| 82 | return op.getOutput(); |
| 83 | } |
| 84 | |
| 85 | static std::optional<Type> recordToType(MLIRContext *ctx, |
| 86 | const Record &predRec) { |
| 87 | if (predRec.isSubClassOf(Name: "I" )) { |
| 88 | auto width = predRec.getValueAsInt(FieldName: "bitwidth" ); |
| 89 | return IntegerType::get(context: ctx, width, signedness: IntegerType::Signless); |
| 90 | } |
| 91 | |
| 92 | if (predRec.isSubClassOf(Name: "SI" )) { |
| 93 | auto width = predRec.getValueAsInt(FieldName: "bitwidth" ); |
| 94 | return IntegerType::get(context: ctx, width, signedness: IntegerType::Signed); |
| 95 | } |
| 96 | |
| 97 | if (predRec.isSubClassOf(Name: "UI" )) { |
| 98 | auto width = predRec.getValueAsInt(FieldName: "bitwidth" ); |
| 99 | return IntegerType::get(context: ctx, width, signedness: IntegerType::Unsigned); |
| 100 | } |
| 101 | |
| 102 | // Index type |
| 103 | if (predRec.getName() == "Index" ) { |
| 104 | return IndexType::get(context: ctx); |
| 105 | } |
| 106 | |
| 107 | // Float types |
| 108 | if (predRec.isSubClassOf(Name: "F" )) { |
| 109 | auto width = predRec.getValueAsInt(FieldName: "bitwidth" ); |
| 110 | switch (width) { |
| 111 | case 16: |
| 112 | return Float16Type::get(context: ctx); |
| 113 | case 32: |
| 114 | return Float32Type::get(context: ctx); |
| 115 | case 64: |
| 116 | return Float64Type::get(context: ctx); |
| 117 | case 80: |
| 118 | return Float80Type::get(context: ctx); |
| 119 | case 128: |
| 120 | return Float128Type::get(context: ctx); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (predRec.getName() == "NoneType" ) { |
| 125 | return mlir::NoneType::get(context: ctx); |
| 126 | } |
| 127 | |
| 128 | if (predRec.getName() == "BF16" ) { |
| 129 | return BFloat16Type::get(context: ctx); |
| 130 | } |
| 131 | |
| 132 | if (predRec.getName() == "TF32" ) { |
| 133 | return FloatTF32Type::get(context: ctx); |
| 134 | } |
| 135 | |
| 136 | if (predRec.getName() == "F8E4M3FN" ) { |
| 137 | return Float8E4M3FNType::get(ctx); |
| 138 | } |
| 139 | |
| 140 | if (predRec.getName() == "F8E5M2" ) { |
| 141 | return Float8E5M2Type::get(ctx); |
| 142 | } |
| 143 | |
| 144 | if (predRec.getName() == "F8E4M3" ) { |
| 145 | return Float8E4M3Type::get(ctx); |
| 146 | } |
| 147 | |
| 148 | if (predRec.getName() == "F8E4M3FNUZ" ) { |
| 149 | return Float8E4M3FNUZType::get(ctx); |
| 150 | } |
| 151 | |
| 152 | if (predRec.getName() == "F8E4M3B11FNUZ" ) { |
| 153 | return Float8E4M3B11FNUZType::get(ctx); |
| 154 | } |
| 155 | |
| 156 | if (predRec.getName() == "F8E5M2FNUZ" ) { |
| 157 | return Float8E5M2FNUZType::get(ctx); |
| 158 | } |
| 159 | |
| 160 | if (predRec.getName() == "F8E3M4" ) { |
| 161 | return Float8E3M4Type::get(ctx); |
| 162 | } |
| 163 | |
| 164 | if (predRec.isSubClassOf(Name: "Complex" )) { |
| 165 | const Record *elementRec = predRec.getValueAsDef(FieldName: "elementType" ); |
| 166 | auto elementType = recordToType(ctx, predRec: *elementRec); |
| 167 | if (elementType.has_value()) { |
| 168 | return ComplexType::get(elementType: elementType.value()); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return std::nullopt; |
| 173 | } |
| 174 | |
| 175 | static Value createTypeConstraint(OpBuilder &builder, |
| 176 | tblgen::Constraint constraint) { |
| 177 | MLIRContext *ctx = builder.getContext(); |
| 178 | const Record &predRec = constraint.getDef(); |
| 179 | |
| 180 | if (predRec.isSubClassOf(Name: "Variadic" ) || predRec.isSubClassOf(Name: "Optional" )) |
| 181 | return createTypeConstraint(builder, constraint: predRec.getValueAsDef(FieldName: "baseType" )); |
| 182 | |
| 183 | if (predRec.getName() == "AnyType" ) { |
| 184 | auto op = irdl::AnyOp::create(builder, location: UnknownLoc::get(context: ctx)); |
| 185 | return op.getOutput(); |
| 186 | } |
| 187 | |
| 188 | if (predRec.isSubClassOf(Name: "TypeDef" )) { |
| 189 | auto dialect = predRec.getValueAsDef(FieldName: "dialect" )->getValueAsString(FieldName: "name" ); |
| 190 | if (dialect == selectedDialect) { |
| 191 | std::string combined = ("!" + predRec.getValueAsString(FieldName: "mnemonic" )).str(); |
| 192 | SmallVector<FlatSymbolRefAttr> nested = { |
| 193 | SymbolRefAttr::get(ctx, value: combined)}; |
| 194 | auto typeSymbol = SymbolRefAttr::get(ctx, value: dialect, nestedRefs: nested); |
| 195 | auto op = irdl::BaseOp::create(builder, location: UnknownLoc::get(context: ctx), base_ref: typeSymbol); |
| 196 | return op.getOutput(); |
| 197 | } |
| 198 | std::string typeName = ("!" + predRec.getValueAsString(FieldName: "typeName" )).str(); |
| 199 | auto op = irdl::BaseOp::create(builder, location: UnknownLoc::get(context: ctx), |
| 200 | base_name: StringAttr::get(context: ctx, bytes: typeName)); |
| 201 | return op.getOutput(); |
| 202 | } |
| 203 | |
| 204 | if (predRec.isSubClassOf(Name: "AnyTypeOf" )) { |
| 205 | std::vector<Value> constraints; |
| 206 | for (const Record *child : predRec.getValueAsListOfDefs(FieldName: "allowedTypes" )) { |
| 207 | constraints.push_back( |
| 208 | x: createTypeConstraint(builder, constraint: tblgen::Constraint(child))); |
| 209 | } |
| 210 | auto op = irdl::AnyOfOp::create(builder, location: UnknownLoc::get(context: ctx), operands: constraints); |
| 211 | return op.getOutput(); |
| 212 | } |
| 213 | |
| 214 | if (predRec.isSubClassOf(Name: "AllOfType" )) { |
| 215 | std::vector<Value> constraints; |
| 216 | for (const Record *child : predRec.getValueAsListOfDefs(FieldName: "allowedTypes" )) { |
| 217 | constraints.push_back( |
| 218 | x: createTypeConstraint(builder, constraint: tblgen::Constraint(child))); |
| 219 | } |
| 220 | auto op = irdl::AllOfOp::create(builder, location: UnknownLoc::get(context: ctx), operands: constraints); |
| 221 | return op.getOutput(); |
| 222 | } |
| 223 | |
| 224 | // Integer types |
| 225 | if (predRec.getName() == "AnyInteger" ) { |
| 226 | auto op = irdl::BaseOp::create(builder, location: UnknownLoc::get(context: ctx), |
| 227 | base_name: StringAttr::get(context: ctx, bytes: "!builtin.integer" )); |
| 228 | return op.getOutput(); |
| 229 | } |
| 230 | |
| 231 | if (predRec.isSubClassOf(Name: "AnyI" )) { |
| 232 | auto width = predRec.getValueAsInt(FieldName: "bitwidth" ); |
| 233 | std::vector<Value> types = { |
| 234 | typeToConstraint(builder, |
| 235 | type: IntegerType::get(context: ctx, width, signedness: IntegerType::Signless)), |
| 236 | typeToConstraint(builder, |
| 237 | type: IntegerType::get(context: ctx, width, signedness: IntegerType::Signed)), |
| 238 | typeToConstraint(builder, |
| 239 | type: IntegerType::get(context: ctx, width, signedness: IntegerType::Unsigned))}; |
| 240 | auto op = irdl::AnyOfOp::create(builder, location: UnknownLoc::get(context: ctx), operands: types); |
| 241 | return op.getOutput(); |
| 242 | } |
| 243 | |
| 244 | auto type = recordToType(ctx, predRec); |
| 245 | |
| 246 | if (type.has_value()) { |
| 247 | return typeToConstraint(builder, type: type.value()); |
| 248 | } |
| 249 | |
| 250 | // Confined type |
| 251 | if (predRec.isSubClassOf(Name: "ConfinedType" )) { |
| 252 | std::vector<Value> constraints; |
| 253 | constraints.push_back(x: createTypeConstraint( |
| 254 | builder, constraint: tblgen::Constraint(predRec.getValueAsDef(FieldName: "baseType" )))); |
| 255 | for (const Record *child : predRec.getValueAsListOfDefs(FieldName: "predicateList" )) { |
| 256 | constraints.push_back(x: createPredicate(builder, pred: tblgen::Pred(child))); |
| 257 | } |
| 258 | auto op = irdl::AllOfOp::create(builder, location: UnknownLoc::get(context: ctx), operands: constraints); |
| 259 | return op.getOutput(); |
| 260 | } |
| 261 | |
| 262 | return createPredicate(builder, pred: constraint.getPredicate()); |
| 263 | } |
| 264 | |
| 265 | static Value createAttrConstraint(OpBuilder &builder, |
| 266 | tblgen::Constraint constraint) { |
| 267 | MLIRContext *ctx = builder.getContext(); |
| 268 | const Record &predRec = constraint.getDef(); |
| 269 | |
| 270 | if (predRec.isSubClassOf(Name: "DefaultValuedAttr" ) || |
| 271 | predRec.isSubClassOf(Name: "DefaultValuedOptionalAttr" ) || |
| 272 | predRec.isSubClassOf(Name: "OptionalAttr" )) { |
| 273 | return createAttrConstraint(builder, constraint: predRec.getValueAsDef(FieldName: "baseAttr" )); |
| 274 | } |
| 275 | |
| 276 | if (predRec.isSubClassOf(Name: "ConfinedAttr" )) { |
| 277 | std::vector<Value> constraints; |
| 278 | constraints.push_back(x: createAttrConstraint( |
| 279 | builder, constraint: tblgen::Constraint(predRec.getValueAsDef(FieldName: "baseAttr" )))); |
| 280 | for (const Record *child : |
| 281 | predRec.getValueAsListOfDefs(FieldName: "attrConstraints" )) { |
| 282 | constraints.push_back(x: createPredicate( |
| 283 | builder, pred: tblgen::Pred(child->getValueAsDef(FieldName: "predicate" )))); |
| 284 | } |
| 285 | auto op = irdl::AllOfOp::create(builder, location: UnknownLoc::get(context: ctx), operands: constraints); |
| 286 | return op.getOutput(); |
| 287 | } |
| 288 | |
| 289 | if (predRec.isSubClassOf(Name: "AnyAttrOf" )) { |
| 290 | std::vector<Value> constraints; |
| 291 | for (const Record *child : |
| 292 | predRec.getValueAsListOfDefs(FieldName: "allowedAttributes" )) { |
| 293 | constraints.push_back( |
| 294 | x: createAttrConstraint(builder, constraint: tblgen::Constraint(child))); |
| 295 | } |
| 296 | auto op = irdl::AnyOfOp::create(builder, location: UnknownLoc::get(context: ctx), operands: constraints); |
| 297 | return op.getOutput(); |
| 298 | } |
| 299 | |
| 300 | if (predRec.getName() == "AnyAttr" ) { |
| 301 | auto op = irdl::AnyOp::create(builder, location: UnknownLoc::get(context: ctx)); |
| 302 | return op.getOutput(); |
| 303 | } |
| 304 | |
| 305 | if (predRec.isSubClassOf(Name: "AnyIntegerAttrBase" ) || |
| 306 | predRec.isSubClassOf(Name: "SignlessIntegerAttrBase" ) || |
| 307 | predRec.isSubClassOf(Name: "SignedIntegerAttrBase" ) || |
| 308 | predRec.isSubClassOf(Name: "UnsignedIntegerAttrBase" ) || |
| 309 | predRec.isSubClassOf(Name: "BoolAttr" )) { |
| 310 | return baseToConstraint(builder, baseClass: "!builtin.integer" ); |
| 311 | } |
| 312 | |
| 313 | if (predRec.isSubClassOf(Name: "FloatAttrBase" )) { |
| 314 | return baseToConstraint(builder, baseClass: "!builtin.float" ); |
| 315 | } |
| 316 | |
| 317 | if (predRec.isSubClassOf(Name: "StringBasedAttr" )) { |
| 318 | return baseToConstraint(builder, baseClass: "!builtin.string" ); |
| 319 | } |
| 320 | |
| 321 | if (predRec.getName() == "UnitAttr" ) { |
| 322 | auto op = |
| 323 | irdl::IsOp::create(builder, location: UnknownLoc::get(context: ctx), expected: UnitAttr::get(context: ctx)); |
| 324 | return op.getOutput(); |
| 325 | } |
| 326 | |
| 327 | if (predRec.isSubClassOf(Name: "AttrDef" )) { |
| 328 | auto dialect = predRec.getValueAsDef(FieldName: "dialect" )->getValueAsString(FieldName: "name" ); |
| 329 | if (dialect == selectedDialect) { |
| 330 | std::string combined = ("#" + predRec.getValueAsString(FieldName: "mnemonic" )).str(); |
| 331 | SmallVector<FlatSymbolRefAttr> nested = {SymbolRefAttr::get(ctx, value: combined) |
| 332 | |
| 333 | }; |
| 334 | auto typeSymbol = SymbolRefAttr::get(ctx, value: dialect, nestedRefs: nested); |
| 335 | auto op = irdl::BaseOp::create(builder, location: UnknownLoc::get(context: ctx), base_ref: typeSymbol); |
| 336 | return op.getOutput(); |
| 337 | } |
| 338 | std::string typeName = ("#" + predRec.getValueAsString(FieldName: "attrName" )).str(); |
| 339 | auto op = irdl::BaseOp::create(builder, location: UnknownLoc::get(context: ctx), |
| 340 | base_name: StringAttr::get(context: ctx, bytes: typeName)); |
| 341 | return op.getOutput(); |
| 342 | } |
| 343 | |
| 344 | return createPredicate(builder, pred: constraint.getPredicate()); |
| 345 | } |
| 346 | |
| 347 | static Value createRegionConstraint(OpBuilder &builder, |
| 348 | tblgen::Region constraint) { |
| 349 | MLIRContext *ctx = builder.getContext(); |
| 350 | const Record &predRec = constraint.getDef(); |
| 351 | |
| 352 | if (predRec.getName() == "AnyRegion" ) { |
| 353 | ValueRange entryBlockArgs = {}; |
| 354 | auto op = irdl::RegionOp::create( |
| 355 | builder, location: UnknownLoc::get(context: ctx), entryBlockArgs, |
| 356 | /*numberOfBlocks=*/nullptr, /*constrainedArguments=*/false); |
| 357 | return op.getResult(); |
| 358 | } |
| 359 | |
| 360 | if (predRec.isSubClassOf(Name: "SizedRegion" )) { |
| 361 | ValueRange entryBlockArgs = {}; |
| 362 | auto ty = IntegerType::get(context: ctx, width: 32); |
| 363 | auto op = irdl::RegionOp::create( |
| 364 | builder, location: UnknownLoc::get(context: ctx), entryBlockArgs, |
| 365 | numberOfBlocks: IntegerAttr::get(type: ty, value: predRec.getValueAsInt(FieldName: "blocks" ))); |
| 366 | return op.getResult(); |
| 367 | } |
| 368 | |
| 369 | return createPredicate(builder, pred: constraint.getPredicate()); |
| 370 | } |
| 371 | |
| 372 | /// Returns the name of the operation without the dialect prefix. |
| 373 | static StringRef getOperatorName(tblgen::Operator &tblgenOp) { |
| 374 | StringRef opName = tblgenOp.getDef().getValueAsString(FieldName: "opName" ); |
| 375 | return opName; |
| 376 | } |
| 377 | |
| 378 | /// Returns the name of the type without the dialect prefix. |
| 379 | static StringRef getTypeName(tblgen::TypeDef &tblgenType) { |
| 380 | StringRef opName = tblgenType.getDef()->getValueAsString(FieldName: "mnemonic" ); |
| 381 | return opName; |
| 382 | } |
| 383 | |
| 384 | /// Returns the name of the attr without the dialect prefix. |
| 385 | static StringRef getAttrName(tblgen::AttrDef &tblgenType) { |
| 386 | StringRef opName = tblgenType.getDef()->getValueAsString(FieldName: "mnemonic" ); |
| 387 | return opName; |
| 388 | } |
| 389 | |
| 390 | /// Extract an operation to IRDL. |
| 391 | static irdl::OperationOp createIRDLOperation(OpBuilder &builder, |
| 392 | tblgen::Operator &tblgenOp) { |
| 393 | MLIRContext *ctx = builder.getContext(); |
| 394 | StringRef opName = getOperatorName(tblgenOp); |
| 395 | |
| 396 | irdl::OperationOp op = irdl::OperationOp::create( |
| 397 | builder, location: UnknownLoc::get(context: ctx), sym_name: StringAttr::get(context: ctx, bytes: opName), |
| 398 | /*sym_visibility=*/nullptr); |
| 399 | |
| 400 | // Add the block in the region. |
| 401 | Block &opBlock = op.getBody().emplaceBlock(); |
| 402 | OpBuilder consBuilder = OpBuilder::atBlockBegin(block: &opBlock); |
| 403 | |
| 404 | SmallDenseSet<StringRef> usedNames; |
| 405 | for (auto &namedCons : tblgenOp.getOperands()) |
| 406 | usedNames.insert(V: namedCons.name); |
| 407 | for (auto &namedCons : tblgenOp.getResults()) |
| 408 | usedNames.insert(V: namedCons.name); |
| 409 | for (auto &namedReg : tblgenOp.getRegions()) |
| 410 | usedNames.insert(V: namedReg.name); |
| 411 | |
| 412 | size_t generateCounter = 0; |
| 413 | auto generateName = [&](StringRef prefix) -> StringAttr { |
| 414 | SmallString<16> candidate; |
| 415 | do { |
| 416 | candidate.clear(); |
| 417 | raw_svector_ostream candidateStream(candidate); |
| 418 | candidateStream << prefix << generateCounter; |
| 419 | generateCounter++; |
| 420 | } while (usedNames.contains(V: candidate)); |
| 421 | return StringAttr::get(context: ctx, bytes: candidate); |
| 422 | }; |
| 423 | auto normalizeName = [&](StringRef name) -> StringAttr { |
| 424 | if (name == "" ) |
| 425 | return generateName("unnamed" ); |
| 426 | return StringAttr::get(context: ctx, bytes: name); |
| 427 | }; |
| 428 | |
| 429 | auto getValues = [&](tblgen::Operator::const_value_range namedCons) { |
| 430 | SmallVector<Value> operands; |
| 431 | SmallVector<Attribute> names; |
| 432 | SmallVector<irdl::VariadicityAttr> variadicity; |
| 433 | |
| 434 | for (const NamedTypeConstraint &namedCons : namedCons) { |
| 435 | auto operand = createTypeConstraint(builder&: consBuilder, constraint: namedCons.constraint); |
| 436 | operands.push_back(Elt: operand); |
| 437 | |
| 438 | names.push_back(Elt: normalizeName(namedCons.name)); |
| 439 | |
| 440 | irdl::VariadicityAttr var; |
| 441 | if (namedCons.isOptional()) |
| 442 | var = consBuilder.getAttr<irdl::VariadicityAttr>( |
| 443 | args: irdl::Variadicity::optional); |
| 444 | else if (namedCons.isVariadic()) |
| 445 | var = consBuilder.getAttr<irdl::VariadicityAttr>( |
| 446 | args: irdl::Variadicity::variadic); |
| 447 | else |
| 448 | var = consBuilder.getAttr<irdl::VariadicityAttr>( |
| 449 | args: irdl::Variadicity::single); |
| 450 | |
| 451 | variadicity.push_back(Elt: var); |
| 452 | } |
| 453 | return std::make_tuple(args&: operands, args&: names, args&: variadicity); |
| 454 | }; |
| 455 | |
| 456 | auto [operands, operandNames, operandVariadicity] = |
| 457 | getValues(tblgenOp.getOperands()); |
| 458 | auto [results, resultNames, resultVariadicity] = |
| 459 | getValues(tblgenOp.getResults()); |
| 460 | |
| 461 | SmallVector<Value> attributes; |
| 462 | SmallVector<Attribute> attrNames; |
| 463 | for (auto namedAttr : tblgenOp.getAttributes()) { |
| 464 | if (namedAttr.attr.isOptional()) |
| 465 | continue; |
| 466 | attributes.push_back(Elt: createAttrConstraint(builder&: consBuilder, constraint: namedAttr.attr)); |
| 467 | attrNames.push_back(Elt: StringAttr::get(context: ctx, bytes: namedAttr.name)); |
| 468 | } |
| 469 | |
| 470 | SmallVector<Value> regions; |
| 471 | SmallVector<Attribute> regionNames; |
| 472 | for (auto namedRegion : tblgenOp.getRegions()) { |
| 473 | regions.push_back( |
| 474 | Elt: createRegionConstraint(builder&: consBuilder, constraint: namedRegion.constraint)); |
| 475 | regionNames.push_back(Elt: normalizeName(namedRegion.name)); |
| 476 | } |
| 477 | |
| 478 | // Create the operands and results operations. |
| 479 | if (!operands.empty()) |
| 480 | irdl::OperandsOp::create(builder&: consBuilder, location: UnknownLoc::get(context: ctx), args: operands, |
| 481 | names: ArrayAttr::get(context: ctx, value: operandNames), |
| 482 | variadicity: operandVariadicity); |
| 483 | if (!results.empty()) |
| 484 | irdl::ResultsOp::create(builder&: consBuilder, location: UnknownLoc::get(context: ctx), args: results, |
| 485 | names: ArrayAttr::get(context: ctx, value: resultNames), |
| 486 | variadicity: resultVariadicity); |
| 487 | if (!attributes.empty()) |
| 488 | irdl::AttributesOp::create(builder&: consBuilder, location: UnknownLoc::get(context: ctx), attributeValues: attributes, |
| 489 | attributeValueNames: ArrayAttr::get(context: ctx, value: attrNames)); |
| 490 | if (!regions.empty()) |
| 491 | irdl::RegionsOp::create(builder&: consBuilder, location: UnknownLoc::get(context: ctx), args: regions, |
| 492 | names: ArrayAttr::get(context: ctx, value: regionNames)); |
| 493 | |
| 494 | return op; |
| 495 | } |
| 496 | |
| 497 | static irdl::TypeOp createIRDLType(OpBuilder &builder, |
| 498 | tblgen::TypeDef &tblgenType) { |
| 499 | MLIRContext *ctx = builder.getContext(); |
| 500 | StringRef typeName = getTypeName(tblgenType); |
| 501 | std::string combined = ("!" + typeName).str(); |
| 502 | |
| 503 | irdl::TypeOp op = irdl::TypeOp::create(builder, location: UnknownLoc::get(context: ctx), |
| 504 | sym_name: StringAttr::get(context: ctx, bytes: combined), |
| 505 | /*sym_visibility=*/nullptr); |
| 506 | |
| 507 | op.getBody().emplaceBlock(); |
| 508 | |
| 509 | return op; |
| 510 | } |
| 511 | |
| 512 | static irdl::AttributeOp createIRDLAttr(OpBuilder &builder, |
| 513 | tblgen::AttrDef &tblgenAttr) { |
| 514 | MLIRContext *ctx = builder.getContext(); |
| 515 | StringRef attrName = getAttrName(tblgenType&: tblgenAttr); |
| 516 | std::string combined = ("#" + attrName).str(); |
| 517 | |
| 518 | irdl::AttributeOp op = irdl::AttributeOp::create( |
| 519 | builder, location: UnknownLoc::get(context: ctx), sym_name: StringAttr::get(context: ctx, bytes: combined), |
| 520 | /*sym_visibility=*/nullptr); |
| 521 | |
| 522 | op.getBody().emplaceBlock(); |
| 523 | |
| 524 | return op; |
| 525 | } |
| 526 | |
| 527 | static irdl::DialectOp createIRDLDialect(OpBuilder &builder) { |
| 528 | MLIRContext *ctx = builder.getContext(); |
| 529 | return irdl::DialectOp::create(builder, location: UnknownLoc::get(context: ctx), |
| 530 | sym_name: StringAttr::get(context: ctx, bytes: selectedDialect), |
| 531 | /*sym_visibility=*/nullptr); |
| 532 | } |
| 533 | |
| 534 | static bool emitDialectIRDLDefs(const RecordKeeper &records, raw_ostream &os) { |
| 535 | // Initialize. |
| 536 | MLIRContext ctx; |
| 537 | ctx.getOrLoadDialect<irdl::IRDLDialect>(); |
| 538 | OpBuilder builder(&ctx); |
| 539 | |
| 540 | // Create a module op and set it as the insertion point. |
| 541 | OwningOpRef<ModuleOp> module = |
| 542 | ModuleOp::create(builder, location: UnknownLoc::get(context: &ctx)); |
| 543 | builder = builder.atBlockBegin(block: module->getBody()); |
| 544 | // Create the dialect and insert it. |
| 545 | irdl::DialectOp dialect = createIRDLDialect(builder); |
| 546 | // Set insertion point to start of DialectOp. |
| 547 | builder = builder.atBlockBegin(block: &dialect.getBody().emplaceBlock()); |
| 548 | |
| 549 | for (const Record *type : |
| 550 | records.getAllDerivedDefinitionsIfDefined(ClassName: "TypeDef" )) { |
| 551 | tblgen::TypeDef tblgenType(type); |
| 552 | if (tblgenType.getDialect().getName() != selectedDialect) |
| 553 | continue; |
| 554 | createIRDLType(builder, tblgenType); |
| 555 | } |
| 556 | |
| 557 | for (const Record *attr : |
| 558 | records.getAllDerivedDefinitionsIfDefined(ClassName: "AttrDef" )) { |
| 559 | tblgen::AttrDef tblgenAttr(attr); |
| 560 | if (tblgenAttr.getDialect().getName() != selectedDialect) |
| 561 | continue; |
| 562 | createIRDLAttr(builder, tblgenAttr); |
| 563 | } |
| 564 | |
| 565 | for (const Record *def : records.getAllDerivedDefinitionsIfDefined(ClassName: "Op" )) { |
| 566 | tblgen::Operator tblgenOp(def); |
| 567 | if (tblgenOp.getDialectName() != selectedDialect) |
| 568 | continue; |
| 569 | |
| 570 | createIRDLOperation(builder, tblgenOp); |
| 571 | } |
| 572 | |
| 573 | // Print the module. |
| 574 | module->print(os); |
| 575 | |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | static mlir::GenRegistration |
| 580 | genOpDefs("gen-dialect-irdl-defs" , "Generate IRDL dialect definitions" , |
| 581 | [](const RecordKeeper &records, raw_ostream &os) { |
| 582 | return emitDialectIRDLDefs(records, os); |
| 583 | }); |
| 584 | |