216 lines · plain
1// RUN: mlir-tblgen -gen-op-defs -I %S/../../include %s | FileCheck %s2// RUN: mlir-tblgen -gen-op-decls -I %S/../../include %s | FileCheck %s --check-prefix=DECL3 4include "mlir/IR/OpBase.td"5include "mlir/Interfaces/InferTypeOpInterface.td"6 7def Test_Dialect : Dialect {8 let name = "test";9}10class NS_Op<string mnemonic, list<Trait> traits> :11 Op<Test_Dialect, mnemonic, traits>;12 13def OpA : NS_Op<"one_normal_result_op", []> {14 let results = (outs I32:$result);15}16 17// CHECK-LABEL: void OpA::build18// CHECK: ::mlir::TypeRange resultTypes, ::mlir::ValueRange operands19// CHECK: assert(resultTypes.size() == 1u && "mismatched number of return types");20// CHECK-NEXT: odsState.addTypes(resultTypes);21 22def OpB : NS_Op<"same_input_output_type_op", [SameOperandsAndResultType]> {23 let arguments = (ins I32:$x);24 let results = (outs I32:$y);25}26 27// CHECK-LABEL: OpB definitions28// CHECK: void OpB::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::Type y, ::mlir::Value x)29// CHECK: odsState.addTypes(y);30// CHECK: void OpB::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::Value x)31// CHECK: ::llvm::SmallVector<::mlir::Type, 2> inferredReturnTypes;32// CHECK: if (::mlir::succeeded(OpB::inferReturnTypes(odsBuilder.getContext(),33// CHECK: odsState.location, odsState.operands,34// CHECK: odsState.attributes.getDictionary(odsState.getContext()),35// CHECK: odsState.regions, inferredReturnTypes)))36// CHECK: odsState.addTypes(inferredReturnTypes);37 38def OpC : NS_Op<"three_normal_result_op", []> {39 let results = (outs I32:$x, /*unnamed*/I32, I32:$z);40}41 42// CHECK-LABEL: OpC definitions43// CHECK: void OpC::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::Type x, ::mlir::Type resultType1, ::mlir::Type z)44// CHECK-NEXT: odsState.addTypes(x)45// CHECK-NEXT: odsState.addTypes(resultType1)46// CHECK-NEXT: odsState.addTypes(z)47 48// CHECK: void OpC::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::TypeRange resultTypes) {49// CHECK-NEXT: assert(resultTypes.size() == 3u && "mismatched number of results");50// CHECK-NEXT: odsState.addTypes(resultTypes);51 52def IntegerTypeAttr : TypeAttrBase<"IntegerType", "Integer type attribute">;53def OpD : NS_Op<"type_attr_as_result_type", [FirstAttrDerivedResultType]> {54 let arguments = (ins I32:$x, IntegerTypeAttr:$attr, F32Attr:$f32);55 let results = (outs AnyTensor:$y);56}57 58// CHECK-LABEL: OpD definitions59// CHECK: void OpD::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::ValueRange operands, ::llvm::ArrayRef<::mlir::NamedAttribute> attributes)60// CHECK: odsState.addTypes({::llvm::cast<::mlir::TypeAttr>(typeAttr).getValue()});61// CHECK: void OpD::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::ValueRange operands, const Properties &properties, ::llvm::ArrayRef<::mlir::NamedAttribute> discardableAttributes)62// CHECK: odsState.addTypes({::llvm::cast<::mlir::TypeAttr>(typeAttr).getValue()});63 64def OpE : NS_Op<"value_attr_as_result_type", [FirstAttrDerivedResultType]> {65 let arguments = (ins I32:$x, F32Attr:$attr);66 let results = (outs AnyTensor:$y);67}68 69// CHECK-LABEL: OpE definitions70// CHECK: void OpE::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::ValueRange operands, ::llvm::ArrayRef<::mlir::NamedAttribute> attributes)71// CHECK: odsState.addTypes({::llvm::cast<::mlir::TypedAttr>(typeAttr).getType()});72// CHECK: void OpE::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::ValueRange operands, const Properties &properties, ::llvm::ArrayRef<::mlir::NamedAttribute> discardableAttributes)73// CHECK: ::mlir::Attribute typeAttr = properties.getAttr();74// CHECK: odsState.addTypes({::llvm::cast<::mlir::TypedAttr>(typeAttr).getType()});75 76def OpF : NS_Op<"one_variadic_result_op", []> {77 let results = (outs Variadic<I32>:$x);78}79 80// CHECK-LABEL: void OpF::build81// CHECK-SAME: ::mlir::TypeRange x82// CHECK-NOT: assert83// CHECK: odsState.addTypes(x);84 85def OpG : NS_Op<"one_normal_and_one_variadic_result_op", []> {86 87 let results = (outs I32:$x, Variadic<I32>:$y);88}89 90// CHECK-LABEL: OpG definitions91 92// CHECK: void OpG::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::Type x, ::mlir::TypeRange y)93// CHECK-NEXT: odsState.addTypes(x);94// CHECK-NEXT: odsState.addTypes(y);95 96// CHECK: void OpG::build97// CHECK: ::mlir::TypeRange resultTypes98// CHECK: assert(resultTypes.size() >= 1u && "mismatched number of return types");99// CHECK-NEXT: odsState.addTypes(resultTypes);100 101def OpI : NS_Op<"mix_variadic_and_normal_results_op", [SameVariadicResultSize]> {102 let results = (outs Variadic<AnyTensor>:$output1, AnyTensor:$output2, Variadic<AnyTensor>:$output3);103}104 105// DECL-LABEL: ::mlir::Operation::result_range getOutput1106// DECL-NEXT: return getODSResults(0);107 108// DECL-LABEL: ::mlir::TypedValue<::mlir::TensorType> getOutput2109// DECL-NEXT: return ::llvm::cast<::mlir::TypedValue<::mlir::TensorType>>(*getODSResults(1).begin());110 111// CHECK-LABEL: OpI::build112// CHECK-NEXT: odsState.addTypes(output1);113// CHECK-NEXT: odsState.addTypes(output2);114// CHECK-NEXT: odsState.addTypes(output3);115 116// Test that if the only operand is variadic, we access the first value in the117// pack to set result type118// ---119def OpK : NS_Op<"only_input_is_variadic_with_same_value_type_op", [SameOperandsAndResultType]> {120 let arguments = (ins Variadic<AnyTensor>:$input);121 let results = (outs AnyTensor:$result);122}123 124// CHECK-LABEL: OpK::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::ValueRange operands, ::llvm::ArrayRef<::mlir::NamedAttribute> attributes)125// CHECK: odsState.addTypes({operands[0].getType()});126// CHECK-LABEL: OpK::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::ValueRange operands, const Properties &properties, ::llvm::ArrayRef<::mlir::NamedAttribute> discardableAttributes)127// CHECK: odsState.addTypes({operands[0].getType()});128 129// Test with inferred shapes and interleaved with operands/attributes.130//131def OpL1 : NS_Op<"op_with_all_types_constraint",132 [AllTypesMatch<["a", "b"]>]> {133 let arguments = (ins I32Attr:$attr1, AnyType:$a);134 let results = (outs Res<AnyType, "output b", []>:$b);135}136 137// CHECK-LABEL: LogicalResult OpL1::inferReturnTypes138// CHECK-NOT: }139// CHECK: if (operands.size() <= 0)140// CHECK-NEXT: return ::mlir::failure();141// CHECK: ::mlir::Type odsInferredType0 = operands[0].getType();142// CHECK: inferredReturnTypes[0] = odsInferredType0;143 144def OpL2 : NS_Op<"op_with_all_types_constraint",145 [AllTypesMatch<["c", "b"]>, AllTypesMatch<["a", "d"]>]> {146 let arguments = (ins I32Attr:$attr1, AnyType:$a, AnyType:$a2, AnyType:$c);147 let results = (outs Res<AnyType, "output b", []>:$b, AnyType:$d);148}149 150// CHECK-LABEL: LogicalResult OpL2::inferReturnTypes151// CHECK-NOT: }152// CHECK: if (operands.size() <= 2)153// CHECK-NEXT: return ::mlir::failure();154// CHECK-NOT: if (operands.size() <= 0)155// CHECK: ::mlir::Type odsInferredType0 = operands[2].getType();156// CHECK: ::mlir::Type odsInferredType1 = operands[0].getType();157// CHECK: inferredReturnTypes[0] = odsInferredType0;158// CHECK: inferredReturnTypes[1] = odsInferredType1;159 160def OpL3 : NS_Op<"op_with_all_types_constraint",161 [AllTypesMatch<["a", "b"]>]> {162 let arguments = (ins I32Attr:$a);163 let results = (outs AnyType:$b);164}165 166// CHECK-LABEL: LogicalResult OpL3::inferReturnTypes167// CHECK-NOT: }168// CHECK: ::mlir::Type odsInferredType0 = odsInferredTypeAttr0.getType();169// CHECK: inferredReturnTypes[0] = odsInferredType0;170 171def OpL4 : NS_Op<"two_inference_edges", [172 TypesMatchWith<"", "a", "b", "infer0($_self)">,173 TypesMatchWith<"", "b", "c", "infer1($_self)">,174 TypesMatchWith<"", "input", "a", "fromInput($_self)">]> {175 let arguments = (ins I32:$input);176 let results = (outs AnyType:$a, AnyType:$b, AnyType:$c);177}178 179// CHECK-LABEL: LogicalResult OpL4::inferReturnTypes180// CHECK: if (operands.size() <= 0)181// CHECK-NEXT: return ::mlir::failure();182// CHECK: odsInferredType0 = fromInput(operands[0].getType())183// CHECK: odsInferredType1 = infer0(odsInferredType0)184// CHECK: odsInferredType2 = infer1(odsInferredType1)185// CHECK: inferredReturnTypes[0] = odsInferredType0186// CHECK: inferredReturnTypes[1] = odsInferredType1187// CHECK: inferredReturnTypes[2] = odsInferredType2188 189def OpL5 : NS_Op<"op_with_same_but_unconstraint_results",190 [AllTypesMatch<["result_a", "result_b"]>]> {191 let results = (outs AnyType:$result_a, AnyType:$result_b);192}193 194// CHECK-NOT: LogicalResult OpL5::inferReturnTypes195 196def OpL6 : NS_Op<"op_with_same_and_constraint_results",197 [AllTypesMatch<["result_a", "result_b", "result_c"]>]> {198 let results = (outs AnyType:$result_a, AnyType:$result_b, I32:$result_c);199}200 201// CHECK-LABEL: LogicalResult OpL6::inferReturnTypes202// CHECK-NOT: }203// CHECK: odsInferredType0 = odsBuilder.getIntegerType(32);204// CHECK: odsInferredType1 = odsBuilder.getIntegerType(32);205// CHECK: odsInferredType2 = odsBuilder.getIntegerType(32);206// CHECK: inferredReturnTypes[0] = odsInferredType0;207// CHECK: inferredReturnTypes[1] = odsInferredType1;208// CHECK: inferredReturnTypes[2] = odsInferredType2;209 210def OpM : NS_Op<"mix_diff_size_variadic_and_normal_results_op", [AttrSizedResultSegments]> {211 let results = (outs Variadic<AnyTensor>:$output1, AnyTensor:$output2, Optional<AnyTensor>:$output3);212}213 214// CHECK-LABEL: OpM::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::TypeRange output1, ::mlir::Type output2, /*optional*/::mlir::Type output3)215// CHECK: ::llvm::copy(::llvm::ArrayRef<int32_t>({static_cast<int32_t>(output1.size()), 1, (output3 ? 1 : 0)}), odsState.getOrAddProperties<Properties>().resultSegmentSizes.begin());216