158 lines · plain
1// RUN: mlir-tblgen -gen-op-defs -I %S/../../include %s | FileCheck %s2 3include "mlir/IR/OpBase.td"4 5def Test_Dialect : Dialect {6 let name = "test";7}8 9class NS_Op<string mnemonic, list<Trait> traits = []> :10 Op<Test_Dialect, mnemonic, traits>;11 12/// Test unique'ing of type, attribute, successor, and region constraints.13 14def ATypePred : CPred<"typePred($_self, $_op)">;15def AType : Type<ATypePred, "a type">;16def OtherType : Type<ATypePred, "another type">;17 18def AnAttrPred : CPred<"attrPred($_self, $_op)">;19def AnAttr : Attr<AnAttrPred, "an attribute (got {{reformat($_self)}})">;20def OtherAttr : Attr<AnAttrPred, "another attribute">;21 22def ASuccessorPred : CPred<"successorPred($_self, $_op)">;23def ASuccessor : Successor<ASuccessorPred, "a successor">;24def OtherSuccessor : Successor<ASuccessorPred, "another successor">;25 26def ARegionPred : CPred<"regionPred($_self, $_op)">;27def ARegion : Region<ARegionPred, "a region ({{find(foo)}})">;28def OtherRegion : Region<ARegionPred, "another region">;29 30// OpA and OpB have the same type, attribute, successor, and region constraints.31 32def OpA : NS_Op<"op_a"> {33 let arguments = (ins AType:$a, AnAttr:$b);34 let results = (outs AType:$ret);35 let successors = (successor ASuccessor:$c);36 let regions = (region ARegion:$d);37}38 39def OpB : NS_Op<"op_b"> {40 let arguments = (ins AType:$a, AnAttr:$b);41 let successors = (successor ASuccessor:$c);42 let regions = (region ARegion:$d);43}44 45// OpC has the same type, attribute, successor, and region predicates but has46// difference descriptions for them.47 48def OpC : NS_Op<"op_c"> {49 let arguments = (ins OtherType:$a, OtherAttr:$b);50 let results = (outs OtherType:$ret);51 let successors = (successor OtherSuccessor:$c);52 let regions = (region OtherRegion:$d);53}54 55/// Test that a type contraint was generated.56// CHECK: static ::llvm::LogicalResult [[$A_TYPE_CONSTRAINT:__mlir_ods_local_type_constraint.*]](57// CHECK: if (!((typePred(type, *op)))) {58// CHECK-NEXT: return op->emitOpError(valueKind) << " #" << valueIndex59// CHECK-NEXT: << " must be a type, but got " << type;60 61/// Test that duplicate type constraint was not generated.62// CHECK-NOT: << " must be a type, but got " << type;63 64/// Test that a type constraint with a different description was generated.65// CHECK: static ::llvm::LogicalResult [[$O_TYPE_CONSTRAINT:__mlir_ods_local_type_constraint.*]](66// CHECK: if (!((typePred(type, *op)))) {67// CHECK-NEXT: return op->emitOpError(valueKind) << " #" << valueIndex68// CHECK-NEXT: << " must be another type, but got " << type;69 70/// Test that an attribute contraint was generated.71// CHECK: static ::llvm::LogicalResult [[$A_ATTR_CONSTRAINT:__mlir_ods_local_attr_constraint.*]](72// CHECK: if (attr && !((attrPred(attr, *op))))73// CHECK-NEXT: return emitError() << "attribute '" << attrName74// CHECK-NEXT: << "' failed to satisfy constraint: an attribute (got " << reformat(attr) << ")";75 76/// Test that duplicate attribute constraint was not generated.77// CHECK-NOT: << "' failed to satisfy constraint: an attribute78 79/// Test that a attribute constraint with a different description was generated.80// CHECK: static ::llvm::LogicalResult [[$O_ATTR_CONSTRAINT:__mlir_ods_local_attr_constraint.*]](81// CHECK: static ::llvm::LogicalResult [[$O_ATTR_CONSTRAINT:__mlir_ods_local_attr_constraint.*]](82// CHECK: if (attr && !((attrPred(attr, *op))))83// CHECK-NEXT: return emitError() << "attribute '" << attrName84// CHECK-NEXT: << "' failed to satisfy constraint: another attribute";85 86/// Test that a successor contraint was generated.87// CHECK: static ::llvm::LogicalResult [[$A_SUCCESSOR_CONSTRAINT:__mlir_ods_local_successor_constraint.*]](88// CHECK: if (!((successorPred(successor, *op)))) {89// CHECK-NEXT: return op->emitOpError("successor #") << successorIndex << " ('"90// CHECK-NEXT: << successorName << ")' failed to verify constraint: a successor";91 92/// Test that duplicate successor constraint was not generated.93// CHECK-NOT: << successorName << ")' failed to verify constraint: a successor";94 95/// Test that a successor constraint with a different description was generated.96// CHECK: static ::llvm::LogicalResult [[$O_SUCCESSOR_CONSTRAINT:__mlir_ods_local_successor_constraint.*]](97// CHECK: if (!((successorPred(successor, *op)))) {98// CHECK-NEXT: return op->emitOpError("successor #") << successorIndex << " ('"99// CHECK-NEXT: << successorName << ")' failed to verify constraint: another successor";100 101/// Test that a region contraint was generated.102// CHECK: static ::llvm::LogicalResult [[$A_REGION_CONSTRAINT:__mlir_ods_local_region_constraint.*]](103// CHECK: if (!((regionPred(region, *op)))) {104// CHECK-NEXT: return op->emitOpError("region #") << regionIndex105// CHECK-NEXT: << (regionName.empty() ? " " : " ('" + regionName + "') ")106// CHECK-NEXT: << "failed to verify constraint: a region (" << find(foo) << ")";107 108/// Test that duplicate region constraint was not generated.109// CHECK-NOT: << "failed to verify constraint: a region";110 111/// Test that a region constraint with a different description was generated.112// CHECK: static ::llvm::LogicalResult [[$O_REGION_CONSTRAINT:__mlir_ods_local_region_constraint.*]](113// CHECK: if (!((regionPred(region, *op)))) {114// CHECK-NEXT: return op->emitOpError("region #") << regionIndex115// CHECK-NEXT: << (regionName.empty() ? " " : " ('" + regionName + "') ")116// CHECK-NEXT: << "failed to verify constraint: another region";117 118/// Test that the uniqued constraints are being used.119// CHECK-LABEL: OpA::verifyInvariantsImpl120// CHECK: auto [[$B_ATTR:.*b]] = getProperties().b;121// CHECK: if (::mlir::failed([[$A_ATTR_CONSTRAINT]](*this, [[$B_ATTR]], "b")))122// CHECK-NEXT: return ::mlir::failure();123// CHECK: auto [[$A_VALUE_GROUP:.*]] = getODSOperands(0);124// CHECK: for (auto [[$A_VALUE:.*]] : [[$A_VALUE_GROUP]])125// CHECK-NEXT: if (::mlir::failed([[$A_TYPE_CONSTRAINT]](*this, [[$A_VALUE]].getType(), "operand", index++)))126// CHECK-NEXT: return ::mlir::failure();127// CHECK: auto [[$RET_VALUE_GROUP:.*]] = getODSResults(0);128// CHECK: for (auto [[$RET_VALUE:.*]] : [[$RET_VALUE_GROUP]])129// CHECK-NEXT: if (::mlir::failed([[$A_TYPE_CONSTRAINT]](*this, [[$RET_VALUE]].getType(), "result", index++)))130// CHECK-NEXT: return ::mlir::failure();131// CHECK: for (auto ®ion : ::llvm::MutableArrayRef((*this)->getRegion(0)))132// CHECK-NEXT: if (::mlir::failed([[$A_REGION_CONSTRAINT]](*this, region, "d", index++)))133// CHECK-NEXT: return ::mlir::failure();134// CHECK: for (auto *successor : ::llvm::MutableArrayRef(c()))135// CHECK-NEXT: if (::mlir::failed([[$A_SUCCESSOR_CONSTRAINT]](*this, successor, "c", index++)))136// CHECK-NEXT: return ::mlir::failure();137 138/// Test that the op with the same predicates but different with descriptions139/// uses the different constraints.140// CHECK-LABEL: OpC::verifyInvariantsImpl141// CHECK: auto [[$B_ATTR:.*b]] = getProperties().b;142// CHECK: if (::mlir::failed([[$O_ATTR_CONSTRAINT]](*this, [[$B_ATTR]], "b")))143// CHECK-NEXT: return ::mlir::failure();144// CHECK: auto [[$A_VALUE_GROUP:.*]] = getODSOperands(0);145// CHECK: for (auto [[$A_VALUE:.*]] : [[$A_VALUE_GROUP]])146// CHECK-NEXT: if (::mlir::failed([[$O_TYPE_CONSTRAINT]](*this, [[$A_VALUE]].getType(), "operand", index++)))147// CHECK-NEXT: return ::mlir::failure();148// CHECK: auto [[$RET_VALUE_GROUP:.*]] = getODSResults(0);149// CHECK: for (auto [[$RET_VALUE:.*]] : [[$RET_VALUE_GROUP]])150// CHECK-NEXT: if (::mlir::failed([[$O_TYPE_CONSTRAINT]](*this, [[$RET_VALUE]].getType(), "result", index++)))151// CHECK-NEXT: return ::mlir::failure();152// CHECK: for (auto ®ion : ::llvm::MutableArrayRef((*this)->getRegion(0)))153// CHECK-NEXT: if (::mlir::failed([[$O_REGION_CONSTRAINT]](*this, region, "d", index++)))154// CHECK-NEXT: return ::mlir::failure();155// CHECK: for (auto *successor : ::llvm::MutableArrayRef(c()))156// CHECK-NEXT: if (::mlir::failed([[$O_SUCCESSOR_CONSTRAINT]](*this, successor, "c", index++)))157// CHECK-NEXT: return ::mlir::failure();158