1//===-- TestOps.td - Test dialect operation definitions ----*- tablegen -*-===// 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#ifndef TEST_OPS 10#define TEST_OPS 11 12include "TestDialect.td" 13include "mlir/Dialect/DLTI/DLTIBase.td" 14include "mlir/IR/EnumAttr.td" 15include "mlir/IR/OpBase.td" 16include "mlir/IR/OpAsmInterface.td" 17include "mlir/IR/RegionKindInterface.td" 18include "mlir/IR/SymbolInterfaces.td" 19include "mlir/Interfaces/CallInterfaces.td" 20include "mlir/Interfaces/ControlFlowInterfaces.td" 21include "mlir/Interfaces/CopyOpInterface.td" 22include "mlir/Interfaces/DataLayoutInterfaces.td" 23include "mlir/Interfaces/InferTypeOpInterface.td" 24include "mlir/Interfaces/SideEffectInterfaces.td" 25include "mlir/Dialect/Linalg/IR/LinalgInterfaces.td" 26include "TestInterfaces.td" 27 28 29// Include the attribute definitions. 30include "TestAttrDefs.td" 31// Include the type definitions. 32include "TestTypeDefs.td" 33 34 35class TEST_Op<string mnemonic, list<Trait> traits = []> : 36 Op<Test_Dialect, mnemonic, traits>; 37 38//===----------------------------------------------------------------------===// 39// Test Types 40//===----------------------------------------------------------------------===// 41 42def IntTypesOp : TEST_Op<"int_types"> { 43 let results = (outs 44 AnyI16:$any_i16, 45 SI32:$si32, 46 UI64:$ui64, 47 AnyInteger:$any_int 48 ); 49} 50 51def ComplexF64 : Complex<F64>; 52def ComplexOp : TEST_Op<"complex_f64"> { 53 let results = (outs ComplexF64); 54} 55 56def ComplexTensorOp : TEST_Op<"complex_f64_tensor"> { 57 let results = (outs TensorOf<[ComplexF64]>); 58} 59 60def TupleOp : TEST_Op<"tuple_32_bit"> { 61 let results = (outs TupleOf<[I32, F32]>); 62} 63 64def NestedTupleOp : TEST_Op<"nested_tuple_32_bit"> { 65 let results = (outs NestedTupleOf<[I32, F32]>); 66} 67 68def TakesStaticMemRefOp : TEST_Op<"takes_static_memref"> { 69 let arguments = (ins AnyStaticShapeMemRef:$x); 70} 71 72def RankLessThan2I8F32MemRefOp : TEST_Op<"rank_less_than_2_I8_F32_memref"> { 73 let results = (outs MemRefRankOf<[I8, F32], [0, 1]>); 74} 75 76def NDTensorOfOp : TEST_Op<"nd_tensor_of"> { 77 let arguments = (ins 78 0DTensorOf<[F32]>:$arg0, 79 1DTensorOf<[F32]>:$arg1, 80 2DTensorOf<[I16]>:$arg2, 81 3DTensorOf<[I16]>:$arg3, 82 4DTensorOf<[I16]>:$arg4 83 ); 84} 85 86def RankedTensorOp : TEST_Op<"ranked_tensor_op"> { 87 let arguments = (ins AnyRankedTensor:$input); 88} 89 90def MultiTensorRankOf : TEST_Op<"multi_tensor_rank_of"> { 91 let arguments = (ins 92 TensorRankOf<[I8, I32, F32], [0, 1]>:$arg0 93 ); 94} 95 96def TEST_TestType : DialectType<Test_Dialect, 97 CPred<"$_self.isa<::test::TestType>()">, "test">, 98 BuildableType<"$_builder.getType<::test::TestType>()">; 99 100//===----------------------------------------------------------------------===// 101// Test Symbols 102//===----------------------------------------------------------------------===// 103 104def SymbolOp : TEST_Op<"symbol", [Symbol]> { 105 let summary = "operation which defines a new symbol"; 106 let arguments = (ins StrAttr:$sym_name, 107 OptionalAttr<StrAttr>:$sym_visibility); 108} 109 110def SymbolScopeOp : TEST_Op<"symbol_scope", 111 [SymbolTable, SingleBlockImplicitTerminator<"TerminatorOp">]> { 112 let summary = "operation which defines a new symbol table"; 113 let regions = (region SizedRegion<1>:$region); 114} 115 116def SymbolTableRegionOp : TEST_Op<"symbol_table_region", [SymbolTable]> { 117 let summary = "operation which defines a new symbol table without a " 118 "restriction on a terminator"; 119 let regions = (region SizedRegion<1>:$region); 120} 121 122//===----------------------------------------------------------------------===// 123// Test Operands 124//===----------------------------------------------------------------------===// 125 126def MixedNormalVariadicOperandOp : TEST_Op< 127 "mixed_normal_variadic_operand", [SameVariadicOperandSize]> { 128 let arguments = (ins 129 Variadic<AnyTensor>:$input1, 130 AnyTensor:$input2, 131 Variadic<AnyTensor>:$input3 132 ); 133} 134def VariadicWithSameOperandsResult : 135 TEST_Op<"variadic_with_same_operand_results", 136 [SameOperandsAndResultType]> { 137 let arguments = (ins Variadic<AnySignlessInteger>); 138 let results = (outs AnySignlessInteger:$result); 139} 140 141def SameOperandsResultType : TEST_Op< 142 "same_operand_result_type", [SameOperandsAndResultType]> { 143 let arguments = (ins AnyTensor:$operand); 144 let results = (outs AnyTensor:$result); 145} 146 147//===----------------------------------------------------------------------===// 148// Test Results 149//===----------------------------------------------------------------------===// 150 151def MixedNormalVariadicResults : TEST_Op< 152 "mixed_normal_variadic_result", [SameVariadicResultSize]> { 153 let results = (outs 154 Variadic<AnyTensor>:$output1, 155 AnyTensor:$output2, 156 Variadic<AnyTensor>:$output3 157 ); 158} 159 160//===----------------------------------------------------------------------===// 161// Test Attributes 162//===----------------------------------------------------------------------===// 163 164def AnyAttrOfOp : TEST_Op<"any_attr_of_i32_str"> { 165 let arguments = (ins AnyAttrOf<[I32Attr, StrAttr]>:$attr); 166} 167 168def NonNegIntAttrOp : TEST_Op<"non_negative_int_attr"> { 169 let arguments = (ins 170 Confined<I32Attr, [IntNonNegative]>:$i32attr, 171 Confined<I64Attr, [IntNonNegative]>:$i64attr 172 ); 173} 174 175def PositiveIntAttrOp : TEST_Op<"positive_int_attr"> { 176 let arguments = (ins 177 Confined<I32Attr, [IntPositive]>:$i32attr, 178 Confined<I64Attr, [IntPositive]>:$i64attr 179 ); 180} 181 182def TypeArrayAttrOp : TEST_Op<"type_array_attr"> { 183 let arguments = (ins TypeArrayAttr:$attr); 184} 185def TypeArrayAttrWithDefaultOp : TEST_Op<"type_array_attr_with_default"> { 186 let arguments = (ins DefaultValuedAttr<TypeArrayAttr, "{}">:$attr); 187} 188def TypeStringAttrWithTypeOp : TEST_Op<"string_attr_with_type"> { 189 let arguments = (ins TypedStrAttr<AnyType>:$attr); 190 let assemblyFormat = "$attr attr-dict"; 191} 192 193def StrCaseA: StrEnumAttrCase<"A">; 194def StrCaseB: StrEnumAttrCase<"B">; 195 196def SomeStrEnum: StrEnumAttr< 197 "SomeStrEnum", "", [StrCaseA, StrCaseB]>; 198 199def StrEnumAttrOp : TEST_Op<"str_enum_attr"> { 200 let arguments = (ins SomeStrEnum:$attr); 201 let results = (outs I32:$val); 202} 203 204def I32Case5: I32EnumAttrCase<"case5", 5>; 205def I32Case10: I32EnumAttrCase<"case10", 10>; 206 207def SomeI32Enum: I32EnumAttr< 208 "SomeI32Enum", "", [I32Case5, I32Case10]>; 209 210def I32EnumAttrOp : TEST_Op<"i32_enum_attr"> { 211 let arguments = (ins SomeI32Enum:$attr); 212 let results = (outs I32:$val); 213} 214 215def I64Case5: I64EnumAttrCase<"case5", 5>; 216def I64Case10: I64EnumAttrCase<"case10", 10>; 217 218def SomeI64Enum: I64EnumAttr< 219 "SomeI64Enum", "", [I64Case5, I64Case10]>; 220 221def I64EnumAttrOp : TEST_Op<"i64_enum_attr"> { 222 let arguments = (ins SomeI64Enum:$attr); 223 let results = (outs I32:$val); 224} 225 226def SomeStructAttr : StructAttr<"SomeStructAttr", Test_Dialect, [ 227 StructFieldAttr<"some_field", I64Attr>, 228 StructFieldAttr<"some_other_field", I64Attr> 229]> {} 230 231def StructAttrOp : TEST_Op<"struct_attr"> { 232 let arguments = (ins SomeStructAttr:$the_struct_attr); 233 let results = (outs); 234} 235 236def IntAttrOp : TEST_Op<"int_attrs"> { 237 let arguments = (ins 238 AnyI32Attr:$any_i32_attr, 239 IndexAttr:$index_attr, 240 UI32Attr:$ui32_attr, 241 SI32Attr:$si32_attr 242 ); 243} 244 245def FloatElementsAttrOp : TEST_Op<"float_elements_attr"> { 246 let arguments = (ins 247 RankedF32ElementsAttr<[2]>:$scalar_f32_attr, 248 RankedF64ElementsAttr<[4, 8]>:$tensor_f64_attr 249 ); 250} 251 252// A pattern that updates dense<[3.0, 4.0]> to dense<[5.0, 6.0]>. 253// This tests both matching and generating float elements attributes. 254def UpdateFloatElementsAttr : Pat< 255 (FloatElementsAttrOp 256 ConstantAttr<RankedF32ElementsAttr<[2]>, "{3.0f, 4.0f}">:$f32attr, 257 $f64attr), 258 (FloatElementsAttrOp 259 ConstantAttr<RankedF32ElementsAttr<[2]>, "{5.0f, 6.0f}">:$f32attr, 260 $f64attr)>; 261 262def IntElementsAttrOp : TEST_Op<"int_elements_attr"> { 263 let arguments = (ins 264 AnyI32ElementsAttr:$any_i32_attr, 265 I32ElementsAttr:$i32_attr 266 ); 267} 268 269def RankedIntElementsAttrOp : TEST_Op<"ranked_int_elements_attr"> { 270 let arguments = (ins 271 RankedI32ElementsAttr<[2]>:$vector_i32_attr, 272 RankedI64ElementsAttr<[4, 8]>:$matrix_i64_attr 273 ); 274} 275 276def DerivedTypeAttrOp : TEST_Op<"derived_type_attr", []> { 277 let results = (outs AnyTensor:$output); 278 DerivedTypeAttr element_dtype = 279 DerivedTypeAttr<"return getElementTypeOrSelf(getOutput().getType());">; 280 DerivedAttr size = DerivedAttr<"int", 281 "return getOutput().getType().cast<ShapedType>().getSizeInBits();", 282 "$_builder.getI32IntegerAttr($_self)">; 283} 284 285def StringElementsAttrOp : TEST_Op<"string_elements_attr"> { 286 let arguments = (ins 287 StringElementsAttr:$scalar_string_attr 288 ); 289} 290 291//===----------------------------------------------------------------------===// 292// Test Enum Attributes 293//===----------------------------------------------------------------------===// 294 295// Define the C++ enum. 296def TestEnum 297 : I32EnumAttr<"TestEnum", "a test enum", [ 298 I32EnumAttrCase<"First", 0, "first">, 299 I32EnumAttrCase<"Second", 1, "second">, 300 I32EnumAttrCase<"Third", 2, "third">, 301 ]> { 302 let genSpecializedAttr = 0; 303 let cppNamespace = "test"; 304} 305 306// Define the enum attribute. 307def TestEnumAttr : EnumAttr<Test_Dialect, TestEnum, "enum">; 308 309// Define an op that contains the enum attribute. 310def OpWithEnum : TEST_Op<"op_with_enum"> { 311 let arguments = (ins TestEnumAttr:$value, OptionalAttr<AnyAttr>:$tag); 312 let assemblyFormat = "$value (`tag` $tag^)? attr-dict"; 313} 314 315// Define a pattern that matches and creates an enum attribute. 316def : Pat<(OpWithEnum ConstantAttr<TestEnumAttr, 317 "::test::TestEnum::First">:$value, 318 ConstantAttr<I32Attr, "0">:$tag), 319 (OpWithEnum ConstantAttr<TestEnumAttr, 320 "::test::TestEnum::Second">, 321 ConstantAttr<I32Attr, "1">)>; 322 323//===----------------------------------------------------------------------===// 324// Test Attribute Constraints 325//===----------------------------------------------------------------------===// 326 327def SymbolRefOp : TEST_Op<"symbol_ref_attr"> { 328 let arguments = (ins 329 Confined<FlatSymbolRefAttr, [ReferToOp<"FuncOp">]>:$symbol 330 ); 331} 332 333//===----------------------------------------------------------------------===// 334// Test Regions 335//===----------------------------------------------------------------------===// 336 337def OneRegionOp : TEST_Op<"one_region_op", []> { 338 let regions = (region AnyRegion); 339} 340 341def TwoRegionOp : TEST_Op<"two_region_op", []> { 342 let regions = (region AnyRegion, AnyRegion); 343} 344 345def SizedRegionOp : TEST_Op<"sized_region_op", []> { 346 let regions = (region SizedRegion<2>:$my_region, SizedRegion<1>); 347} 348 349//===----------------------------------------------------------------------===// 350// NoTerminator Operation 351//===----------------------------------------------------------------------===// 352 353def SingleNoTerminatorOp : TEST_Op<"single_no_terminator_op", 354 GraphRegionNoTerminator.traits> { 355 let regions = (region SizedRegion<1>:$my_region); 356 357 let assemblyFormat = "attr-dict `:` $my_region"; 358} 359 360def SingleNoTerminatorCustomAsmOp : TEST_Op<"single_no_terminator_custom_asm_op", 361 [SingleBlock, NoTerminator]> { 362 let regions = (region SizedRegion<1>); 363 let hasCustomAssemblyFormat = 1; 364} 365 366def VariadicNoTerminatorOp : TEST_Op<"variadic_no_terminator_op", 367 GraphRegionNoTerminator.traits> { 368 let regions = (region VariadicRegion<SizedRegion<1>>:$my_regions); 369 370 let assemblyFormat = "attr-dict `:` $my_regions"; 371} 372 373//===----------------------------------------------------------------------===// 374// Test Call Interfaces 375//===----------------------------------------------------------------------===// 376 377def ConversionCallOp : TEST_Op<"conversion_call_op", 378 [CallOpInterface]> { 379 let arguments = (ins Variadic<AnyType>:$arg_operands, SymbolRefAttr:$callee); 380 let results = (outs Variadic<AnyType>); 381 382 let extraClassDeclaration = [{ 383 /// Return the callee of this operation. 384 ::mlir::CallInterfaceCallable getCallableForCallee(); 385 }]; 386 let extraClassDefinition = [{ 387 ::mlir::CallInterfaceCallable $cppClass::getCallableForCallee() { 388 return (*this)->getAttrOfType<::mlir::SymbolRefAttr>("callee"); 389 } 390 }]; 391} 392 393def FunctionalRegionOp : TEST_Op<"functional_region_op", 394 [CallableOpInterface]> { 395 let regions = (region AnyRegion:$body); 396 let results = (outs FunctionType); 397 398 let extraClassDeclaration = [{ 399 ::mlir::Region *getCallableRegion() { return &getBody(); } 400 ::llvm::ArrayRef<::mlir::Type> getCallableResults() { 401 return getType().cast<::mlir::FunctionType>().getResults(); 402 } 403 }]; 404} 405 406 407def FoldToCallOp : TEST_Op<"fold_to_call_op"> { 408 let arguments = (ins FlatSymbolRefAttr:$callee); 409 let hasCanonicalizer = 1; 410} 411 412//===----------------------------------------------------------------------===// 413// Test Traits 414//===----------------------------------------------------------------------===// 415 416def SameOperandElementTypeOp : TEST_Op<"same_operand_element_type", 417 [SameOperandsElementType]> { 418 let arguments = (ins AnyType, AnyType); 419 let results = (outs AnyType); 420} 421 422def SameOperandAndResultElementTypeOp : 423 TEST_Op<"same_operand_and_result_element_type", 424 [SameOperandsAndResultElementType]> { 425 let arguments = (ins Variadic<AnyType>); 426 let results = (outs Variadic<AnyType>); 427} 428 429def SameOperandShapeOp : TEST_Op<"same_operand_shape", [SameOperandsShape]> { 430 let arguments = (ins Variadic<AnyShaped>); 431} 432 433def SameOperandAndResultShapeOp : TEST_Op<"same_operand_and_result_shape", 434 [SameOperandsAndResultShape]> { 435 let arguments = (ins Variadic<AnyShaped>); 436 let results = (outs Variadic<AnyShaped>); 437} 438 439def SameOperandAndResultTypeOp : TEST_Op<"same_operand_and_result_type", 440 [SameOperandsAndResultType]> { 441 let arguments = (ins Variadic<AnyType>); 442 let results = (outs Variadic<AnyType>); 443} 444 445def ElementwiseMappableOp : TEST_Op<"elementwise_mappable", 446 ElementwiseMappable.traits> { 447 let arguments = (ins Variadic<AnyType>); 448 let results = (outs Variadic<AnyType>); 449} 450 451def ArgAndResHaveFixedElementTypesOp : 452 TEST_Op<"arg_and_res_have_fixed_element_types", 453 [PredOpTrait<"fixed type combination", 454 And<[ElementTypeIsPred<"x", I32>, 455 ElementTypeIsPred<"y", F32>]>>, 456 ElementTypeIs<"res", I16>]> { 457 let arguments = (ins 458 AnyShaped:$x, AnyShaped:$y); 459 let results = (outs AnyShaped:$res); 460} 461 462def OperandsHaveSameElementType : TEST_Op<"operands_have_same_element_type", [ 463 AllElementTypesMatch<["x", "y"]>]> { 464 let arguments = (ins AnyType:$x, AnyType:$y); 465} 466 467def OperandZeroAndResultHaveSameElementType : TEST_Op< 468 "operand0_and_result_have_same_element_type", 469 [AllElementTypesMatch<["x", "res"]>]> { 470 let arguments = (ins AnyType:$x, AnyType:$y); 471 let results = (outs AnyType:$res); 472} 473 474def OperandsHaveSameType : 475 TEST_Op<"operands_have_same_type", [AllTypesMatch<["x", "y"]>]> { 476 let arguments = (ins AnyType:$x, AnyType:$y); 477} 478 479def ResultHasSameTypeAsAttr : 480 TEST_Op<"result_has_same_type_as_attr", 481 [AllTypesMatch<["attr", "result"]>]> { 482 let arguments = (ins AnyAttr:$attr); 483 let results = (outs AnyType:$result); 484 let assemblyFormat = "$attr `->` type($result) attr-dict"; 485} 486 487def OperandZeroAndResultHaveSameType : 488 TEST_Op<"operand0_and_result_have_same_type", 489 [AllTypesMatch<["x", "res"]>]> { 490 let arguments = (ins AnyType:$x, AnyType:$y); 491 let results = (outs AnyType:$res); 492} 493 494def OperandsHaveSameRank : 495 TEST_Op<"operands_have_same_rank", [AllRanksMatch<["x", "y"]>]> { 496 let arguments = (ins AnyShaped:$x, AnyShaped:$y); 497} 498 499def OperandZeroAndResultHaveSameRank : 500 TEST_Op<"operand0_and_result_have_same_rank", 501 [AllRanksMatch<["x", "res"]>]> { 502 let arguments = (ins AnyShaped:$x, AnyShaped:$y); 503 let results = (outs AnyShaped:$res); 504} 505 506def OperandZeroAndResultHaveSameShape : 507 TEST_Op<"operand0_and_result_have_same_shape", 508 [AllShapesMatch<["x", "res"]>]> { 509 let arguments = (ins AnyShaped:$x, AnyShaped:$y); 510 let results = (outs AnyShaped:$res); 511} 512 513def OperandZeroAndResultHaveSameElementCount : 514 TEST_Op<"operand0_and_result_have_same_element_count", 515 [AllElementCountsMatch<["x", "res"]>]> { 516 let arguments = (ins AnyShaped:$x, AnyShaped:$y); 517 let results = (outs AnyShaped:$res); 518} 519 520def FourEqualsFive : 521 TEST_Op<"four_equals_five", [AllMatch<["5", "4"], "4 equals 5">]>; 522 523def OperandRankEqualsResultSize : 524 TEST_Op<"operand_rank_equals_result_size", 525 [AllMatch<[Rank<"operand">.result, ElementCount<"result">.result], 526 "operand rank equals result size">]> { 527 let arguments = (ins AnyShaped:$operand); 528 let results = (outs AnyShaped:$result); 529} 530 531def IfFirstOperandIsNoneThenSoIsSecond : 532 TEST_Op<"if_first_operand_is_none_then_so_is_second", [PredOpTrait< 533 "has either both none type operands or first is not none", 534 Or<[ 535 And<[TypeIsPred<"x", NoneType>, TypeIsPred<"y", NoneType>]>, 536 Neg<TypeIsPred<"x", NoneType>>]>>]> { 537 let arguments = (ins AnyType:$x, AnyType:$y); 538} 539 540def BroadcastableOp : TEST_Op<"broadcastable", [ResultsBroadcastableShape]> { 541 let arguments = (ins Variadic<AnyTensor>); 542 let results = (outs AnyTensor); 543} 544 545// HasParent trait 546def ParentOp : TEST_Op<"parent"> { 547 let regions = (region AnyRegion); 548} 549def ChildOp : TEST_Op<"child", [HasParent<"ParentOp">]>; 550 551// ParentOneOf trait 552def ParentOp1 : TEST_Op<"parent1"> { 553 let regions = (region AnyRegion); 554} 555def ChildWithParentOneOf : TEST_Op<"child_with_parent_one_of", 556 [ParentOneOf<["ParentOp", "ParentOp1"]>]>; 557 558def TerminatorOp : TEST_Op<"finish", [Terminator]>; 559def SingleBlockImplicitTerminatorOp : TEST_Op<"SingleBlockImplicitTerminator", 560 [SingleBlockImplicitTerminator<"TerminatorOp">]> { 561 let regions = (region SizedRegion<1>:$region); 562} 563 564def I32ElementsAttrOp : TEST_Op<"i32ElementsAttr"> { 565 let arguments = (ins I32ElementsAttr:$attr); 566} 567 568def IndexElementsAttrOp : TEST_Op<"indexElementsAttr"> { 569 let arguments = (ins IndexElementsAttr:$attr); 570} 571 572def OpWithInferTypeInterfaceOp : TEST_Op<"op_with_infer_type_if", [ 573 DeclareOpInterfaceMethods<InferTypeOpInterface, 574 ["inferReturnTypeComponents"]>]> { 575 let arguments = (ins AnyTensor, AnyTensor); 576 let results = (outs AnyTensor); 577} 578 579def OpWithShapedTypeInferTypeInterfaceOp : TEST_Op<"op_with_shaped_type_infer_type_if", 580 [InferTensorTypeWithReify]> { 581 let arguments = (ins AnyTensor, AnyTensor); 582 let results = (outs AnyTensor); 583} 584 585def OpWithResultShapeInterfaceOp : TEST_Op<"op_with_result_shape_interface", 586 [DeclareOpInterfaceMethods<InferShapedTypeOpInterface, 587 ["reifyReturnTypeShapes"]>]> { 588 let arguments = (ins AnyRankedTensor:$operand1, AnyRankedTensor:$operand2); 589 let results = (outs AnyRankedTensor:$result1, AnyRankedTensor:$result2); 590} 591 592def OpWithResultShapePerDimInterfaceOp : 593 TEST_Op<"op_with_result_shape_per_dim_interface", 594 [DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>]> { 595 let arguments = (ins AnyRankedTensor:$operand1, AnyRankedTensor:$operand2); 596 let results = (outs AnyRankedTensor:$result1, AnyRankedTensor:$result2); 597} 598 599def IsNotScalar : Constraint<CPred<"$0.getType().getRank() != 0">>; 600 601def UpdateAttr : Pat<(I32ElementsAttrOp $attr), 602 (I32ElementsAttrOp ConstantAttr<I32ElementsAttr, "0">), 603 [(IsNotScalar $attr)]>; 604 605def TestBranchOp : TEST_Op<"br", 606 [DeclareOpInterfaceMethods<BranchOpInterface>, Terminator]> { 607 let arguments = (ins Variadic<AnyType>:$targetOperands); 608 let successors = (successor AnySuccessor:$target); 609} 610 611def AttrSizedOperandOp : TEST_Op<"attr_sized_operands", 612 [AttrSizedOperandSegments]> { 613 let arguments = (ins 614 Variadic<I32>:$a, 615 Variadic<I32>:$b, 616 I32:$c, 617 Variadic<I32>:$d, 618 I32ElementsAttr:$operand_segment_sizes 619 ); 620} 621 622def AttrSizedResultOp : TEST_Op<"attr_sized_results", 623 [AttrSizedResultSegments]> { 624 let arguments = (ins 625 I32ElementsAttr:$result_segment_sizes 626 ); 627 let results = (outs 628 Variadic<I32>:$a, 629 Variadic<I32>:$b, 630 I32:$c, 631 Variadic<I32>:$d 632 ); 633} 634 635// This is used to test that the fallback for a custom op's parser and printer 636// is the dialect parser and printer hooks. 637def CustomFormatFallbackOp : TEST_Op<"dialect_custom_format_fallback">; 638 639// This is used to test encoding of a string attribute into an SSA name of a 640// pretty printed value name. 641def StringAttrPrettyNameOp 642 : TEST_Op<"string_attr_pretty_name", 643 [DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]> { 644 let arguments = (ins StrArrayAttr:$names); 645 let results = (outs Variadic<I32>:$r); 646 let hasCustomAssemblyFormat = 1; 647} 648 649// This is used to test the OpAsmOpInterface::getDefaultDialect() feature: 650// operations nested in a region under this op will drop the "test." dialect 651// prefix. 652def DefaultDialectOp : TEST_Op<"default_dialect", [OpAsmOpInterface]> { 653 let regions = (region AnyRegion:$body); 654 let extraClassDeclaration = [{ 655 static ::llvm::StringRef getDefaultDialect() { 656 return "test"; 657 } 658 void getAsmResultNames(::llvm::function_ref<void(::mlir::Value, ::llvm::StringRef)> setNameFn) {} 659 }]; 660 let assemblyFormat = "regions attr-dict-with-keyword"; 661} 662 663// This is used to test the OpAsmOpInterface::getAsmBlockName() feature: 664// blocks nested in a region under this op will have a name defined by the 665// interface. 666def AsmBlockNameOp : TEST_Op<"block_names", [OpAsmOpInterface]> { 667 let regions = (region AnyRegion:$body); 668 let extraClassDeclaration = [{ 669 void getAsmBlockNames(mlir::OpAsmSetBlockNameFn setNameFn) { 670 std::string name; 671 int count = 0; 672 for (::mlir::Block &block : getRegion().getBlocks()) { 673 name = "foo" + std::to_string(count++); 674 setNameFn(&block, name); 675 } 676 } 677 }]; 678 let assemblyFormat = "regions attr-dict-with-keyword"; 679} 680 681// This operation requires its return type to have the trait 'TestTypeTrait'. 682def ResultTypeWithTraitOp : TEST_Op<"result_type_with_trait", []> { 683 let results = (outs AnyType); 684 let hasVerifier = 1; 685} 686 687// This operation requires its "attr" attribute to have the 688// trait 'TestAttrTrait'. 689def AttrWithTraitOp : TEST_Op<"attr_with_trait", []> { 690 let arguments = (ins AnyAttr:$attr); 691 let hasVerifier = 1; 692} 693 694//===----------------------------------------------------------------------===// 695// Test Locations 696//===----------------------------------------------------------------------===// 697 698def TestLocationSrcOp : TEST_Op<"loc_src"> { 699 let arguments = (ins I32:$input); 700 let results = (outs I32:$output); 701} 702 703def TestLocationDstOp : TEST_Op<"loc_dst", [SameOperandsAndResultType]> { 704 let arguments = (ins I32:$input); 705 let results = (outs I32:$output); 706} 707 708def TestLocationSrcNoResOp : TEST_Op<"loc_src_no_res"> { 709 let arguments = (ins I32:$input); 710 let results = (outs); 711} 712 713def TestLocationDstNoResOp : TEST_Op<"loc_dst_no_res"> { 714 let arguments = (ins I32:$input); 715 let results = (outs); 716} 717 718//===----------------------------------------------------------------------===// 719// Test Patterns 720//===----------------------------------------------------------------------===// 721 722def OpA : TEST_Op<"op_a"> { 723 let arguments = (ins I32, I32Attr:$attr); 724 let results = (outs I32); 725} 726 727def OpB : TEST_Op<"op_b"> { 728 let arguments = (ins I32, I32Attr:$attr); 729 let results = (outs I32); 730} 731 732// Test named pattern. 733def TestNamedPatternRule : Pat<(OpA $input, $attr), (OpB $input, $attr)>; 734 735// Test with fused location. 736def : Pat<(OpA (OpA $input, $attr), $bttr), (OpB $input, $bttr)>; 737 738// Test added benefit. 739def OpD : TEST_Op<"op_d">, Arguments<(ins I32)>, Results<(outs I32)>; 740def OpE : TEST_Op<"op_e">, Arguments<(ins I32)>, Results<(outs I32)>; 741def OpF : TEST_Op<"op_f">, Arguments<(ins I32)>, Results<(outs I32)>; 742def OpG : TEST_Op<"op_g">, Arguments<(ins I32)>, Results<(outs I32)>; 743// Verify that bumping benefit results in selecting different op. 744def : Pat<(OpD $input), (OpE $input)>; 745def : Pat<(OpD $input), (OpF $input), [], (addBenefit 10)>; 746// Verify that patterns with more source nodes are selected before those with fewer. 747def : Pat<(OpG $input), (OpB $input, ConstantAttr<I32Attr, "20">:$attr)>; 748def : Pat<(OpG (OpG $input)), (OpB $input, ConstantAttr<I32Attr, "34">:$attr)>; 749 750// Test patterns for zero-result op. 751def OpH : TEST_Op<"op_h">, Arguments<(ins I32)>, Results<(outs)>; 752def OpI : TEST_Op<"op_i">, Arguments<(ins I32)>, Results<(outs)>; 753def : Pat<(OpH $input), (OpI $input)>; 754 755// Test patterns for zero-input op. 756def OpJ : TEST_Op<"op_j">, Arguments<(ins)>, Results<(outs I32)>; 757def OpK : TEST_Op<"op_k">, Arguments<(ins)>, Results<(outs I32)>; 758def : Pat<(OpJ), (OpK)>; 759 760// Test that natives calls are only called once during rewrites. 761def OpM : TEST_Op<"op_m"> { 762 let arguments = (ins I32, OptionalAttr<I32Attr>:$optional_attr); 763 let results = (outs I32); 764} 765 766def OpN : TEST_Op<"op_n"> { 767 let arguments = (ins I32, I32); 768 let results = (outs I32); 769} 770 771def OpO : TEST_Op<"op_o"> { 772 let arguments = (ins I32); 773 let results = (outs I32); 774} 775 776def OpP : TEST_Op<"op_p"> { 777 let arguments = (ins I32, I32, I32, I32, I32, I32); 778 let results = (outs I32); 779} 780 781// Test same operand name enforces equality condition check. 782def TestEqualArgsPattern : Pat<(OpN $a, $a), (OpO $a)>; 783 784// Test when equality is enforced at different depth. 785def TestNestedOpEqualArgsPattern : 786 Pat<(OpN $b, (OpP $a, $b, $c, $d, $e, $f)), (replaceWithValue $b)>; 787 788// Test when equality is enforced on same op and same operand but at different 789// depth. We only bound one of the $x to the second operand of outer OpN and 790// left another be the default value (which is the value of first operand of 791// outer OpN). As a result, it ended up comparing wrong values in some cases. 792def TestNestedSameOpAndSameArgEqualityPattern : 793 Pat<(OpN (OpN $_, $x), $x), (replaceWithValue $x)>; 794 795// Test multiple equal arguments check enforced. 796def TestMultipleEqualArgsPattern : 797 Pat<(OpP $a, $b, $a, $a, $b, $c), (OpN $c, $b)>; 798 799// Test for memrefs normalization of an op with normalizable memrefs. 800def OpNorm : TEST_Op<"op_norm", [MemRefsNormalizable]> { 801 let arguments = (ins AnyMemRef:$X, AnyMemRef:$Y); 802} 803// Test for memrefs normalization of an op without normalizable memrefs. 804def OpNonNorm : TEST_Op<"op_nonnorm"> { 805 let arguments = (ins AnyMemRef:$X, AnyMemRef:$Y); 806} 807// Test for memrefs normalization of an op that has normalizable memref results. 808def OpNormRet : TEST_Op<"op_norm_ret", [MemRefsNormalizable]> { 809 let arguments = (ins AnyMemRef:$X); 810 let results = (outs AnyMemRef:$Y, AnyMemRef:$Z); 811} 812 813// Test for memrefs normalization of an op with a reference to a function 814// symbol. 815def OpFuncRef : TEST_Op<"op_funcref"> { 816 let summary = "Test op with a reference to a function symbol"; 817 let description = [{ 818 The "test.op_funcref" is a test op with a reference to a function symbol. 819 }]; 820 let builders = [OpBuilder<(ins "::mlir::FuncOp":$function)>]; 821} 822 823// Pattern add the argument plus a increasing static number hidden in 824// OpMTest function. That value is set into the optional argument. 825// That way, we will know if operations is called once or twice. 826def OpMGetNullAttr : NativeCodeCall<"Attribute()">; 827def OpMAttributeIsNull : Constraint<CPred<"! ($_self)">, "Attribute is null">; 828def OpMVal : NativeCodeCall<"opMTest($_builder, $0)">; 829def : Pat<(OpM $attr, $optAttr), (OpM $attr, (OpMVal $attr) ), 830 [(OpMAttributeIsNull:$optAttr)]>; 831 832// Test `$_` for ignoring op argument match. 833def TestIgnoreArgMatchSrcOp : TEST_Op<"ignore_arg_match_src"> { 834 let arguments = (ins 835 AnyType:$a, AnyType:$b, AnyType:$c, 836 AnyAttr:$d, AnyAttr:$e, AnyAttr:$f); 837} 838def TestIgnoreArgMatchDstOp : TEST_Op<"ignore_arg_match_dst"> { 839 let arguments = (ins AnyType:$b, AnyAttr:$f); 840} 841def : Pat<(TestIgnoreArgMatchSrcOp $_, $b, I32, I64Attr:$_, $_, $f), 842 (TestIgnoreArgMatchDstOp $b, $f)>; 843 844def OpInterleavedOperandAttribute1 : TEST_Op<"interleaved_operand_attr1"> { 845 let arguments = (ins 846 I32:$input1, 847 I64Attr:$attr1, 848 I32:$input2, 849 I64Attr:$attr2 850 ); 851} 852 853def OpInterleavedOperandAttribute2 : TEST_Op<"interleaved_operand_attr2"> { 854 let arguments = (ins 855 I32:$input1, 856 I64Attr:$attr1, 857 I32:$input2, 858 I64Attr:$attr2 859 ); 860} 861 862def ManyArgsOp : TEST_Op<"many_arguments"> { 863 let arguments = (ins 864 I32:$input1, I32:$input2, I32:$input3, I32:$input4, I32:$input5, 865 I32:$input6, I32:$input7, I32:$input8, I32:$input9, 866 I64Attr:$attr1, I64Attr:$attr2, I64Attr:$attr3, I64Attr:$attr4, 867 I64Attr:$attr5, I64Attr:$attr6, I64Attr:$attr7, I64Attr:$attr8, 868 I64Attr:$attr9 869 ); 870} 871 872// Test that DRR does not blow up when seeing lots of arguments. 873def : Pat<(ManyArgsOp 874 $input1, $input2, $input3, $input4, $input5, 875 $input6, $input7, $input8, $input9, 876 ConstantAttr<I64Attr, "42">, 877 $attr2, $attr3, $attr4, $attr5, $attr6, 878 $attr7, $attr8, $attr9), 879 (ManyArgsOp 880 $input1, $input2, $input3, $input4, $input5, 881 $input6, $input7, $input8, $input9, 882 ConstantAttr<I64Attr, "24">, 883 $attr2, $attr3, $attr4, $attr5, $attr6, 884 $attr7, $attr8, $attr9)>; 885 886// Test that we can capture and reference interleaved operands and attributes. 887def : Pat<(OpInterleavedOperandAttribute1 $input1, $attr1, $input2, $attr2), 888 (OpInterleavedOperandAttribute2 $input1, $attr1, $input2, $attr2)>; 889 890// Test NativeCodeCall. 891def OpNativeCodeCall1 : TEST_Op<"native_code_call1"> { 892 let arguments = (ins 893 I32:$input1, I32:$input2, 894 BoolAttr:$choice, 895 I64Attr:$attr1, I64Attr:$attr2 896 ); 897 let results = (outs I32); 898} 899def OpNativeCodeCall2 : TEST_Op<"native_code_call2"> { 900 let arguments = (ins I32:$input, I64ArrayAttr:$attr); 901 let results = (outs I32); 902} 903// Native code call to invoke a C++ function 904def CreateOperand: NativeCodeCall<"chooseOperand($0, $1, $2)">; 905// Native code call to invoke a C++ expression 906def CreateArrayAttr: NativeCodeCall<"$_builder.getArrayAttr({$0, $1})">; 907// Test that we can use NativeCodeCall to create operand and attribute. 908// This pattern chooses between $input1 and $input2 according to $choice and 909// it combines $attr1 and $attr2 into an array attribute. 910def : Pat<(OpNativeCodeCall1 $input1, $input2, 911 ConstBoolAttrTrue:$choice, $attr1, $attr2), 912 (OpNativeCodeCall2 (CreateOperand $input1, $input2, $choice), 913 (CreateArrayAttr $attr1, $attr2))>; 914// Note: the following is just for testing purpose. 915// Should use the replaceWithValue directive instead. 916def UseOpResult: NativeCodeCall<"$0">; 917// Test that we can use NativeCodeCall to create result. 918def : Pat<(OpNativeCodeCall1 $input1, $input2, 919 ConstBoolAttrFalse, $attr1, $attr2), 920 (UseOpResult $input2)>; 921 922def OpNativeCodeCall3 : TEST_Op<"native_code_call3"> { 923 let arguments = (ins I32:$input); 924 let results = (outs I32); 925} 926// Test that NativeCodeCall is not ignored if it is not used to directly 927// replace the matched root op. 928def : Pattern<(OpNativeCodeCall3 $input), 929 [(NativeCodeCallVoid<"createOpI($_builder, $_loc, $0)"> $input), 930 (OpK)]>; 931 932def OpNativeCodeCall4 : TEST_Op<"native_code_call4"> { 933 let arguments = (ins AnyType:$input1); 934 let results = (outs I32:$output1, I32:$output2); 935} 936def OpNativeCodeCall5 : TEST_Op<"native_code_call5"> { 937 let arguments = (ins I32:$input1, I32:$input2); 938 let results = (outs I32:$output1, I32:$output2); 939} 940 941def GetFirstI32Result : NativeCodeCall<"success(getFirstI32Result($_self, $0))">; 942def BindNativeCodeCallResult : NativeCodeCall<"bindNativeCodeCallResult($0)">; 943def : Pat<(OpNativeCodeCall4 (GetFirstI32Result $ret)), 944 (OpNativeCodeCall5 (BindNativeCodeCallResult:$native $ret), $native)>; 945 946def OpNativeCodeCall6 : TEST_Op<"native_code_call6"> { 947 let arguments = (ins I32:$input1, I32:$input2); 948 let results = (outs I32:$output1, I32:$output2); 949} 950def OpNativeCodeCall7 : TEST_Op<"native_code_call7"> { 951 let arguments = (ins I32:$input); 952 let results = (outs I32); 953} 954def BindMultipleNativeCodeCallResult : NativeCodeCall<"bindMultipleNativeCodeCallResult($0, $1)", 2>; 955def : Pattern<(OpNativeCodeCall6 $arg1, $arg2), 956 [(OpNativeCodeCall7 (BindMultipleNativeCodeCallResult:$native__0 $arg1, $arg2)), 957 (OpNativeCodeCall7 $native__1)]>; 958 959// Test AllAttrConstraintsOf. 960def OpAllAttrConstraint1 : TEST_Op<"all_attr_constraint_of1"> { 961 let arguments = (ins I64ArrayAttr:$attr); 962 let results = (outs I32); 963} 964def OpAllAttrConstraint2 : TEST_Op<"all_attr_constraint_of2"> { 965 let arguments = (ins I64ArrayAttr:$attr); 966 let results = (outs I32); 967} 968def Constraint0 : AttrConstraint< 969 CPred<"$_self.cast<ArrayAttr>()[0]." 970 "cast<::mlir::IntegerAttr>().getInt() == 0">, 971 "[0] == 0">; 972def Constraint1 : AttrConstraint< 973 CPred<"$_self.cast<ArrayAttr>()[1].cast<::mlir::IntegerAttr>().getInt() == 1">, 974 "[1] == 1">; 975def : Pat<(OpAllAttrConstraint1 976 AllAttrConstraintsOf<[Constraint0, Constraint1]>:$attr), 977 (OpAllAttrConstraint2 $attr)>; 978 979// Op for testing RewritePattern removing op with inner ops. 980def TestOpWithRegionPattern : TEST_Op<"op_with_region_pattern"> { 981 let regions = (region SizedRegion<1>:$region); 982 let hasCanonicalizer = 1; 983} 984 985def TestOpConstant : TEST_Op<"constant", [ConstantLike, NoSideEffect]> { 986 let arguments = (ins AnyAttr:$value); 987 let results = (outs AnyType); 988 989 let hasFolder = 1; 990} 991 992def OpR : TEST_Op<"op_r">, Arguments<(ins AnyInteger, AnyInteger)>, Results<(outs AnyInteger)>; 993def OpS : TEST_Op<"op_s">, Arguments<(ins AnyInteger, AnyAttr:$value)>, Results<(outs AnyInteger)>; 994 995def : Pat<(OpR $input1, (ConstantLikeMatcher I32Attr:$input2)), 996 (OpS:$unused $input1, $input2)>; 997 998// Op for testing trivial removal via folding of op with inner ops and no uses. 999def TestOpWithRegionFoldNoSideEffect : TEST_Op< 1000 "op_with_region_fold_no_side_effect", [NoSideEffect]> { 1001 let regions = (region SizedRegion<1>:$region); 1002} 1003 1004// Op for testing folding of outer op with inner ops. 1005def TestOpWithRegionFold : TEST_Op<"op_with_region_fold"> { 1006 let arguments = (ins I32:$operand); 1007 let results = (outs I32); 1008 let regions = (region SizedRegion<1>:$region); 1009 let hasFolder = 1; 1010} 1011 1012def TestOpWithVariadicResultsAndFolder: TEST_Op<"op_with_variadic_results_and_folder"> { 1013 let arguments = (ins Variadic<I32>); 1014 let results = (outs Variadic<I32>); 1015 let hasFolder = 1; 1016} 1017 1018def TestCommutativeOp : TEST_Op<"op_commutative", [Commutative]> { 1019 let arguments = (ins I32:$op1, I32:$op2, I32:$op3, I32:$op4); 1020 let results = (outs I32); 1021} 1022 1023def TestIdempotentTraitOp 1024 : TEST_Op<"op_idempotent_trait", 1025 [SameOperandsAndResultType, NoSideEffect, Idempotent]> { 1026 let arguments = (ins I32:$op1); 1027 let results = (outs I32); 1028} 1029 1030def TestIdempotentTraitBinaryOp 1031 : TEST_Op<"op_idempotent_trait_binary", 1032 [SameOperandsAndResultType, NoSideEffect, Idempotent]> { 1033 let arguments = (ins I32:$op1, I32:$op2); 1034 let results = (outs I32); 1035} 1036 1037def TestInvolutionTraitNoOperationFolderOp 1038 : TEST_Op<"op_involution_trait_no_operation_fold", 1039 [SameOperandsAndResultType, NoSideEffect, Involution]> { 1040 let arguments = (ins I32:$op1); 1041 let results = (outs I32); 1042} 1043 1044def TestInvolutionTraitFailingOperationFolderOp 1045 : TEST_Op<"op_involution_trait_failing_operation_fold", 1046 [SameOperandsAndResultType, NoSideEffect, Involution]> { 1047 let arguments = (ins I32:$op1); 1048 let results = (outs I32); 1049 let hasFolder = 1; 1050} 1051 1052def TestInvolutionTraitSuccesfulOperationFolderOp 1053 : TEST_Op<"op_involution_trait_succesful_operation_fold", 1054 [SameOperandsAndResultType, NoSideEffect, Involution]> { 1055 let arguments = (ins I32:$op1); 1056 let results = (outs I32); 1057 let hasFolder = 1; 1058} 1059 1060def TestOpInPlaceFoldAnchor : TEST_Op<"op_in_place_fold_anchor"> { 1061 let arguments = (ins I32); 1062 let results = (outs I32); 1063} 1064 1065def TestOpInPlaceFold : TEST_Op<"op_in_place_fold"> { 1066 let arguments = (ins I32:$op, I32Attr:$attr); 1067 let results = (outs I32); 1068 let hasFolder = 1; 1069} 1070 1071// An op that always fold itself. 1072def TestPassthroughFold : TEST_Op<"passthrough_fold"> { 1073 let arguments = (ins AnyType:$op); 1074 let results = (outs AnyType); 1075 let hasFolder = 1; 1076} 1077 1078def TestDialectCanonicalizerOp : TEST_Op<"dialect_canonicalizable"> { 1079 let arguments = (ins); 1080 let results = (outs I32); 1081} 1082 1083//===----------------------------------------------------------------------===// 1084// Test Patterns (Symbol Binding) 1085 1086// Test symbol binding. 1087def OpSymbolBindingA : TEST_Op<"symbol_binding_a", []> { 1088 let arguments = (ins I32:$operand, I64Attr:$attr); 1089 let results = (outs I32); 1090} 1091def OpSymbolBindingB : TEST_Op<"symbol_binding_b", []> { 1092 let arguments = (ins I32:$operand); 1093 let results = (outs I32); 1094} 1095def OpSymbolBindingC : TEST_Op<"symbol_binding_c", []> { 1096 let arguments = (ins I32:$operand); 1097 let results = (outs I32); 1098 let builders = OpSymbolBindingB.builders; 1099} 1100def OpSymbolBindingD : TEST_Op<"symbol_binding_d", []> { 1101 let arguments = (ins I32:$input1, I32:$input2, I64Attr:$attr); 1102 let results = (outs I32); 1103} 1104def HasOneUse: Constraint<CPred<"$0.hasOneUse()">, "has one use">; 1105def : Pattern< 1106 // Bind to source pattern op operand/attribute/result 1107 (OpSymbolBindingA:$res_a $operand, $attr), [ 1108 // Bind to auxiliary op result 1109 (OpSymbolBindingC:$res_c (OpSymbolBindingB:$res_b $operand)), 1110 1111 // Use bound symbols in resultant ops 1112 (OpSymbolBindingD $res_b, $res_c, $attr)], 1113 // Use bound symbols in additional constraints 1114 [(HasOneUse $res_a)]>; 1115 1116def OpSymbolBindingNoResult : TEST_Op<"symbol_binding_no_result", []> { 1117 let arguments = (ins I32:$operand); 1118} 1119 1120// Test that we can bind to an op without results and reference it later. 1121def : Pat<(OpSymbolBindingNoResult:$op $operand), 1122 (NativeCodeCallVoid<"handleNoResultOp($_builder, $0)"> $op)>; 1123 1124//===----------------------------------------------------------------------===// 1125// Test Patterns (Attributes) 1126 1127// Test matching against op attributes. 1128def OpAttrMatch1 : TEST_Op<"match_op_attribute1"> { 1129 let arguments = (ins 1130 I32Attr:$required_attr, 1131 OptionalAttr<I32Attr>:$optional_attr, 1132 DefaultValuedAttr<I32Attr, "42">:$default_valued_attr, 1133 I32Attr:$more_attr 1134 ); 1135 let results = (outs I32); 1136} 1137def OpAttrMatch2 : TEST_Op<"match_op_attribute2"> { 1138 let arguments = OpAttrMatch1.arguments; 1139 let results = (outs I32); 1140} 1141def MoreConstraint : AttrConstraint< 1142 CPred<"$_self.cast<IntegerAttr>().getInt() == 4">, "more constraint">; 1143def : Pat<(OpAttrMatch1 $required, $optional, $default_valued, 1144 MoreConstraint:$more), 1145 (OpAttrMatch2 $required, $optional, $default_valued, $more)>; 1146 1147// Test unit attrs. 1148def OpAttrMatch3 : TEST_Op<"match_op_attribute3"> { 1149 let arguments = (ins UnitAttr:$attr); 1150 let results = (outs I32); 1151} 1152def OpAttrMatch4 : TEST_Op<"match_op_attribute4"> { 1153 let arguments = (ins UnitAttr:$attr1, UnitAttr:$attr2); 1154 let results = (outs I32); 1155} 1156def : Pat<(OpAttrMatch3 $attr), (OpAttrMatch4 ConstUnitAttr, $attr)>; 1157 1158// Test with constant attr. 1159def OpC : TEST_Op<"op_c">, Arguments<(ins I32)>, Results<(outs I32)>; 1160def : Pat<(OpC $input), (OpB $input, ConstantAttr<I32Attr, "17">:$attr)>; 1161 1162// Test string enum attribute in rewrites. 1163def : Pat<(StrEnumAttrOp StrCaseA), (StrEnumAttrOp StrCaseB)>; 1164// Test integer enum attribute in rewrites. 1165def : Pat<(I32EnumAttrOp I32Case5), (I32EnumAttrOp I32Case10)>; 1166def : Pat<(I64EnumAttrOp I64Case5), (I64EnumAttrOp I64Case10)>; 1167 1168//===----------------------------------------------------------------------===// 1169// Test Patterns (Multi-result Ops) 1170 1171def MultiResultOpKind1: I64EnumAttrCase<"kind1", 1>; 1172def MultiResultOpKind2: I64EnumAttrCase<"kind2", 2>; 1173def MultiResultOpKind3: I64EnumAttrCase<"kind3", 3>; 1174def MultiResultOpKind4: I64EnumAttrCase<"kind4", 4>; 1175def MultiResultOpKind5: I64EnumAttrCase<"kind5", 5>; 1176def MultiResultOpKind6: I64EnumAttrCase<"kind6", 6>; 1177 1178def MultiResultOpEnum: I64EnumAttr< 1179 "MultiResultOpEnum", "Multi-result op kinds", [ 1180 MultiResultOpKind1, MultiResultOpKind2, MultiResultOpKind3, 1181 MultiResultOpKind4, MultiResultOpKind5, MultiResultOpKind6 1182 ]>; 1183 1184def ThreeResultOp : TEST_Op<"three_result"> { 1185 let arguments = (ins MultiResultOpEnum:$kind); 1186 let results = (outs I32:$result1, F32:$result2, F32:$result3); 1187} 1188 1189def AnotherThreeResultOp 1190 : TEST_Op<"another_three_result", 1191 [DeclareOpInterfaceMethods<InferTypeOpInterface>]> { 1192 let arguments = (ins MultiResultOpEnum:$kind); 1193 let results = (outs I32:$result1, F32:$result2, F32:$result3); 1194} 1195 1196def TwoResultOp : TEST_Op<"two_result"> { 1197 let arguments = (ins MultiResultOpEnum:$kind); 1198 let results = (outs I32:$result1, F32:$result2); 1199} 1200 1201def AnotherTwoResultOp : TEST_Op<"another_two_result"> { 1202 let arguments = (ins MultiResultOpEnum:$kind); 1203 let results = (outs F32:$result1, F32:$result2); 1204} 1205 1206def OneResultOp1 : TEST_Op<"one_result1"> { 1207 let arguments = (ins MultiResultOpEnum:$kind); 1208 let results = (outs F32:$result1); 1209} 1210 1211def OneResultOp2 : TEST_Op<"one_result2"> { 1212 let arguments = (ins MultiResultOpEnum:$kind); 1213 let results = (outs I32:$result1); 1214} 1215 1216def OneResultOp3 : TEST_Op<"one_result3"> { 1217 let arguments = (ins F32); 1218 let results = (outs I32:$result1); 1219} 1220 1221// Test using multi-result op as a whole 1222def : Pat<(ThreeResultOp MultiResultOpKind1:$kind), 1223 (AnotherThreeResultOp $kind)>; 1224 1225// Test using multi-result op as a whole for partial replacement 1226def : Pattern<(ThreeResultOp MultiResultOpKind2:$kind), 1227 [(TwoResultOp $kind), 1228 (OneResultOp1 $kind)]>; 1229def : Pattern<(ThreeResultOp MultiResultOpKind3:$kind), 1230 [(OneResultOp2 $kind), 1231 (AnotherTwoResultOp $kind)]>; 1232 1233// Test using results separately in a multi-result op 1234def : Pattern<(ThreeResultOp MultiResultOpKind4:$kind), 1235 [(TwoResultOp:$res1__0 $kind), 1236 (OneResultOp1 $kind), 1237 (TwoResultOp:$res2__1 $kind)]>; 1238 1239// Test referencing a single value in the value pack 1240// This rule only matches TwoResultOp if its second result has no use. 1241def : Pattern<(TwoResultOp:$res MultiResultOpKind5:$kind), 1242 [(OneResultOp2 $kind), 1243 (OneResultOp1 $kind)], 1244 [(HasNoUseOf:$res__1)]>; 1245 1246// Test using auxiliary ops for replacing multi-result op 1247def : Pattern< 1248 (ThreeResultOp MultiResultOpKind6:$kind), [ 1249 // Auxiliary op generated to help building the final result but not 1250 // directly used to replace the source op's results. 1251 (TwoResultOp:$interm $kind), 1252 1253 (OneResultOp3 $interm__1), 1254 (AnotherTwoResultOp $kind) 1255 ]>; 1256 1257//===----------------------------------------------------------------------===// 1258// Test Patterns (Variadic Ops) 1259 1260def OneVResOneVOperandOp1 : TEST_Op<"one_variadic_out_one_variadic_in1"> { 1261 let arguments = (ins Variadic<I32>); 1262 let results = (outs Variadic<I32>); 1263} 1264def OneVResOneVOperandOp2 : TEST_Op<"one_variadic_out_one_variadic_in2"> { 1265 let arguments = (ins Variadic<I32>); 1266 let results = (outs Variadic<I32>); 1267} 1268 1269// Rewrite an op with one variadic operand and one variadic result to 1270// another similar op. 1271def : Pat<(OneVResOneVOperandOp1 $inputs), (OneVResOneVOperandOp2 $inputs)>; 1272 1273def MixedVOperandOp1 : TEST_Op<"mixed_variadic_in1", 1274 [SameVariadicOperandSize]> { 1275 let arguments = (ins 1276 Variadic<I32>:$input1, 1277 F32:$input2, 1278 Variadic<I32>:$input3 1279 ); 1280} 1281 1282def MixedVOperandOp2 : TEST_Op<"mixed_variadic_in2", 1283 [SameVariadicOperandSize]> { 1284 let arguments = (ins 1285 Variadic<I32>:$input1, 1286 F32:$input2, 1287 Variadic<I32>:$input3 1288 ); 1289} 1290 1291// Rewrite an op with both variadic operands and normal operands. 1292def : Pat<(MixedVOperandOp1 $input1, $input2, $input3), 1293 (MixedVOperandOp2 $input1, $input2, $input3)>; 1294 1295def MixedVResultOp1 : TEST_Op<"mixed_variadic_out1", [SameVariadicResultSize]> { 1296 let results = (outs 1297 Variadic<I32>:$output1, 1298 F32:$output2, 1299 Variadic<I32>:$output3 1300 ); 1301} 1302 1303def MixedVResultOp2 : TEST_Op<"mixed_variadic_out2", [SameVariadicResultSize]> { 1304 let results = (outs 1305 Variadic<I32>:$output1, 1306 F32:$output2, 1307 Variadic<I32>:$output3 1308 ); 1309} 1310 1311// Rewrite an op with both variadic results and normal results. 1312// Note that because we are generating the op with a top-level result pattern, 1313// we are able to deduce the correct result types for the generated op using 1314// the information from the matched root op. 1315def : Pat<(MixedVResultOp1), (MixedVResultOp2)>; 1316 1317def OneI32ResultOp : TEST_Op<"one_i32_out"> { 1318 let results = (outs I32); 1319} 1320 1321def MixedVOperandOp3 : TEST_Op<"mixed_variadic_in3", 1322 [SameVariadicOperandSize]> { 1323 let arguments = (ins 1324 I32:$input1, 1325 Variadic<I32>:$input2, 1326 Variadic<I32>:$input3, 1327 I32Attr:$count 1328 ); 1329 1330 let results = (outs I32); 1331} 1332 1333def MixedVResultOp3 : TEST_Op<"mixed_variadic_out3", 1334 [SameVariadicResultSize]> { 1335 let arguments = (ins I32Attr:$count); 1336 1337 let results = (outs 1338 I32:$output1, 1339 Variadic<I32>:$output2, 1340 Variadic<I32>:$output3 1341 ); 1342 1343 // We will use this op in a nested result pattern, where we cannot deduce the 1344 // result type. So need to provide a builder not requiring result types. 1345 let builders = [ 1346 OpBuilder<(ins "::mlir::IntegerAttr":$count), 1347 [{ 1348 auto i32Type = $_builder.getIntegerType(32); 1349 $_state.addTypes(i32Type); // $output1 1350 SmallVector<Type, 4> types(count.getInt(), i32Type); 1351 $_state.addTypes(types); // $output2 1352 $_state.addTypes(types); // $output3 1353 $_state.addAttribute("count", count); 1354 }]> 1355 ]; 1356} 1357 1358// Generates an op with variadic results using nested pattern. 1359def : Pat<(OneI32ResultOp), 1360 (MixedVOperandOp3 1361 (MixedVResultOp3:$results__0 ConstantAttr<I32Attr, "2">), 1362 (replaceWithValue $results__1), 1363 (replaceWithValue $results__2), 1364 ConstantAttr<I32Attr, "2">)>; 1365 1366//===----------------------------------------------------------------------===// 1367// Test Patterns (either) 1368 1369def TestEitherOpA : TEST_Op<"either_op_a"> { 1370 let arguments = (ins AnyInteger:$arg0, AnyInteger:$arg1, AnyInteger:$arg2); 1371 let results = (outs I32:$output); 1372} 1373 1374def TestEitherOpB : TEST_Op<"either_op_b"> { 1375 let arguments = (ins AnyInteger:$arg0); 1376 let results = (outs I32:$output); 1377} 1378 1379def : Pat<(TestEitherOpA (either I32:$arg1, I16:$arg2), $_), 1380 (TestEitherOpB $arg2)>; 1381 1382def : Pat<(TestEitherOpA (either (TestEitherOpB I32:$arg1), I16:$arg2), $_), 1383 (TestEitherOpB $arg2)>; 1384 1385def : Pat<(TestEitherOpA (either (TestEitherOpB I32:$arg1), 1386 (TestEitherOpB I16:$arg2)), 1387 $_), 1388 (TestEitherOpB $arg2)>; 1389 1390//===----------------------------------------------------------------------===// 1391// Test Patterns (Location) 1392 1393// Test that we can specify locations for generated ops. 1394def : Pat<(TestLocationSrcOp:$res1 1395 (TestLocationSrcOp:$res2 1396 (TestLocationSrcOp:$res3 $input))), 1397 (TestLocationDstOp 1398 (TestLocationDstOp 1399 (TestLocationDstOp $input, (location $res1)), 1400 (location "named")), 1401 (location "fused", $res2, $res3))>; 1402 1403// Test that we can use the location of an op without results 1404def : Pat<(TestLocationSrcNoResOp:$loc 1405 (TestLocationSrcOp (TestLocationSrcOp $input))), 1406 (TestLocationDstNoResOp $input, (location $loc))>; 1407 1408//===----------------------------------------------------------------------===// 1409// Test Patterns (Type Builders) 1410 1411def SourceOp : TEST_Op<"source_op"> { 1412 let arguments = (ins AnyInteger:$arg, AnyI32Attr:$tag); 1413 let results = (outs AnyInteger); 1414} 1415 1416// An op without return type deduction. 1417def OpX : TEST_Op<"op_x"> { 1418 let arguments = (ins AnyInteger:$input); 1419 let results = (outs AnyInteger); 1420} 1421 1422// Test that ops without built-in type deduction can be created in the 1423// replacement DAG with an explicitly specified type. 1424def : Pat<(SourceOp $val, ConstantAttr<I32Attr, "11">:$attr), 1425 (OpX (OpX $val, (returnType "$_builder.getI32Type()")))>; 1426// Test NativeCodeCall type builder can accept arguments. 1427def SameTypeAs : NativeCodeCall<"$0.getType()">; 1428 1429def : Pat<(SourceOp $val, ConstantAttr<I32Attr, "22">:$attr), 1430 (OpX (OpX $val, (returnType (SameTypeAs $val))))>; 1431 1432// Test multiple return types. 1433def MakeI64Type : NativeCodeCall<"$_builder.getI64Type()">; 1434def MakeI32Type : NativeCodeCall<"$_builder.getI32Type()">; 1435 1436def OneToTwo : TEST_Op<"one_to_two"> { 1437 let arguments = (ins AnyInteger); 1438 let results = (outs AnyInteger, AnyInteger); 1439} 1440 1441def TwoToOne : TEST_Op<"two_to_one"> { 1442 let arguments = (ins AnyInteger, AnyInteger); 1443 let results = (outs AnyInteger); 1444} 1445 1446def : Pat<(SourceOp $val, ConstantAttr<I32Attr, "33">:$attr), 1447 (TwoToOne (OpX (OneToTwo:$res__0 $val, (returnType (MakeI64Type), (MakeI32Type))), (returnType (MakeI32Type))), 1448 (OpX $res__1, (returnType (MakeI64Type))))>; 1449 1450// Test copy value return type. 1451def : Pat<(SourceOp $val, ConstantAttr<I32Attr, "44">:$attr), 1452 (OpX (OpX $val, (returnType $val)))>; 1453 1454// Test create multiple return types with different methods. 1455def : Pat<(SourceOp $val, ConstantAttr<I32Attr, "55">:$attr), 1456 (TwoToOne (OneToTwo:$res__0 $val, (returnType $val, "$_builder.getI64Type()")), $res__1)>; 1457 1458//===----------------------------------------------------------------------===// 1459// Test Patterns (Trailing Directives) 1460 1461// Test that we can specify both `location` and `returnType` directives. 1462def : Pat<(SourceOp $val, ConstantAttr<I32Attr, "66">:$attr), 1463 (TwoToOne (OpX $val, (returnType $val), (location "loc1")), 1464 (OpX $val, (location "loc2"), (returnType $val)))>; 1465 1466//===----------------------------------------------------------------------===// 1467// Test Legalization 1468//===----------------------------------------------------------------------===// 1469 1470def Test_LegalizerEnum_Success : StrEnumAttrCase<"Success">; 1471def Test_LegalizerEnum_Failure : StrEnumAttrCase<"Failure">; 1472 1473def Test_LegalizerEnum : StrEnumAttr<"Success", "Failure", 1474 [Test_LegalizerEnum_Success, Test_LegalizerEnum_Failure]>; 1475 1476def ILLegalOpA : TEST_Op<"illegal_op_a">, Results<(outs I32)>; 1477def ILLegalOpB : TEST_Op<"illegal_op_b">, Results<(outs I32)>; 1478def ILLegalOpC : TEST_Op<"illegal_op_c">, Results<(outs I32)>; 1479def ILLegalOpD : TEST_Op<"illegal_op_d">, Results<(outs I32)>; 1480def ILLegalOpE : TEST_Op<"illegal_op_e">, Results<(outs I32)>; 1481def ILLegalOpF : TEST_Op<"illegal_op_f">, Results<(outs I32)>; 1482def ILLegalOpG : TEST_Op<"illegal_op_g">, Results<(outs I32)>; 1483def LegalOpA : TEST_Op<"legal_op_a">, 1484 Arguments<(ins Test_LegalizerEnum:$status)>, Results<(outs I32)>; 1485def LegalOpB : TEST_Op<"legal_op_b">, Results<(outs I32)>; 1486def LegalOpC : TEST_Op<"legal_op_c">, 1487 Arguments<(ins I32)>, Results<(outs I32)>; 1488 1489// Check that the conversion infrastructure can properly undo the creation of 1490// operations where an operation was created before its parent, in this case, 1491// in the parent's builder. 1492def IllegalOpTerminator : TEST_Op<"illegal_op_terminator", [Terminator]>; 1493def IllegalOpWithRegion : TEST_Op<"illegal_op_with_region"> { 1494 let skipDefaultBuilders = 1; 1495 let builders = [OpBuilder<(ins), 1496 [{ 1497 Region *bodyRegion = $_state.addRegion(); 1498 OpBuilder::InsertionGuard g($_builder); 1499 Block *body = $_builder.createBlock(bodyRegion); 1500 $_builder.setInsertionPointToEnd(body); 1501 $_builder.create<IllegalOpTerminator>($_state.location); 1502 }]>]; 1503} 1504def IllegalOpWithRegionAnchor : TEST_Op<"illegal_op_with_region_anchor">; 1505 1506// Check that smaller pattern depths are chosen, i.e. prioritize more direct 1507// mappings. 1508def : Pat<(ILLegalOpA), (LegalOpA Test_LegalizerEnum_Success)>; 1509 1510def : Pat<(ILLegalOpA), (ILLegalOpB)>; 1511def : Pat<(ILLegalOpB), (LegalOpA Test_LegalizerEnum_Failure)>; 1512 1513// Check that the higher benefit pattern is taken for multiple legalizations 1514// with the same depth. 1515def : Pat<(ILLegalOpC), (ILLegalOpD)>; 1516def : Pat<(ILLegalOpD), (LegalOpA Test_LegalizerEnum_Failure)>; 1517 1518def : Pat<(ILLegalOpC), (ILLegalOpE), [], (addBenefit 10)>; 1519def : Pat<(ILLegalOpE), (LegalOpA Test_LegalizerEnum_Success)>; 1520 1521// Check that patterns use the most up-to-date value when being replaced. 1522def TestRewriteOp : TEST_Op<"rewrite">, 1523 Arguments<(ins AnyType)>, Results<(outs AnyType)>; 1524def : Pat<(TestRewriteOp $input), (replaceWithValue $input)>; 1525 1526// Check that patterns can specify bounded recursion when rewriting. 1527def TestRecursiveRewriteOp : TEST_Op<"recursive_rewrite"> { 1528 let arguments = (ins I64Attr:$depth); 1529 let assemblyFormat = "$depth attr-dict"; 1530} 1531 1532// Test legalization pattern: this op will be erase and will also erase the 1533// producer of its operand. 1534def BlackHoleOp : TEST_Op<"blackhole">, 1535 Arguments<(ins AnyType)>; 1536 1537//===----------------------------------------------------------------------===// 1538// Test Type Legalization 1539//===----------------------------------------------------------------------===// 1540 1541def TestRegionBuilderOp : TEST_Op<"region_builder">; 1542def TestReturnOp : TEST_Op<"return", [ReturnLike, Terminator]> { 1543 let arguments = (ins Variadic<AnyType>); 1544 let builders = [OpBuilder<(ins), 1545 [{ build($_builder, $_state, {}); }]> 1546 ]; 1547} 1548def TestCastOp : TEST_Op<"cast">, 1549 Arguments<(ins Variadic<AnyType>)>, Results<(outs AnyType)>; 1550def TestInvalidOp : TEST_Op<"invalid", [Terminator]>, 1551 Arguments<(ins Variadic<AnyType>)>; 1552def TestTypeProducerOp : TEST_Op<"type_producer">, 1553 Results<(outs AnyType)>; 1554def TestAnotherTypeProducerOp : TEST_Op<"another_type_producer">, 1555 Results<(outs AnyType)>; 1556def TestTypeConsumerOp : TEST_Op<"type_consumer">, 1557 Arguments<(ins AnyType)>; 1558def TestValidOp : TEST_Op<"valid", [Terminator]>, 1559 Arguments<(ins Variadic<AnyType>)>; 1560 1561def TestMergeBlocksOp : TEST_Op<"merge_blocks"> { 1562 let summary = "merge_blocks operation"; 1563 let description = [{ 1564 Test op with multiple blocks that are merged with Dialect Conversion 1565 }]; 1566 1567 let regions = (region AnyRegion:$body); 1568 let results = (outs Variadic<AnyType>:$result); 1569} 1570 1571def TestRemappedValueRegionOp : TEST_Op<"remapped_value_region", 1572 [SingleBlock]> { 1573 let summary = "remapped_value_region operation"; 1574 let description = [{ 1575 Test op that remaps values that haven't yet been converted in Dialect 1576 Conversion. 1577 }]; 1578 1579 let regions = (region SizedRegion<1>:$body); 1580 let results = (outs Variadic<AnyType>:$result); 1581} 1582 1583def TestSignatureConversionUndoOp : TEST_Op<"signature_conversion_undo"> { 1584 let regions = (region AnyRegion); 1585} 1586 1587def TestSignatureConversionNoConverterOp 1588 : TEST_Op<"signature_conversion_no_converter"> { 1589 let regions = (region AnyRegion); 1590} 1591 1592//===----------------------------------------------------------------------===// 1593// Test parser. 1594//===----------------------------------------------------------------------===// 1595 1596def ParseIntegerLiteralOp : TEST_Op<"parse_integer_literal"> { 1597 let results = (outs Variadic<Index>:$results); 1598 let hasCustomAssemblyFormat = 1; 1599} 1600 1601def ParseWrappedKeywordOp : TEST_Op<"parse_wrapped_keyword"> { 1602 let arguments = (ins StrAttr:$keyword); 1603 let hasCustomAssemblyFormat = 1; 1604} 1605 1606//===----------------------------------------------------------------------===// 1607// Test region argument list parsing. 1608 1609def IsolatedRegionOp : TEST_Op<"isolated_region", [IsolatedFromAbove]> { 1610 let summary = "isolated region operation"; 1611 let description = [{ 1612 Test op with an isolated region, to test passthrough region arguments. Each 1613 argument is of index type. 1614 }]; 1615 1616 let arguments = (ins Index); 1617 let regions = (region SizedRegion<1>:$region); 1618 let hasCustomAssemblyFormat = 1; 1619} 1620 1621def SSACFGRegionOp : TEST_Op<"ssacfg_region", [ 1622 DeclareOpInterfaceMethods<RegionKindInterface>]> { 1623 let summary = "operation with an SSACFG region"; 1624 let description = [{ 1625 Test op that defines an SSACFG region. 1626 }]; 1627 1628 let regions = (region VariadicRegion<AnyRegion>:$regions); 1629 let arguments = (ins Variadic<AnyType>); 1630 let results = (outs Variadic<AnyType>); 1631} 1632 1633def GraphRegionOp : TEST_Op<"graph_region", [ 1634 DeclareOpInterfaceMethods<RegionKindInterface>]> { 1635 let summary = "operation with a graph region"; 1636 let description = [{ 1637 Test op that defines a graph region. 1638 }]; 1639 1640 let regions = (region AnyRegion:$region); 1641 let hasCustomAssemblyFormat = 1; 1642} 1643 1644def AffineScopeOp : TEST_Op<"affine_scope", [AffineScope]> { 1645 let summary = "affine scope operation"; 1646 let description = [{ 1647 Test op that defines a new affine scope. 1648 }]; 1649 1650 let regions = (region SizedRegion<1>:$region); 1651 let hasCustomAssemblyFormat = 1; 1652} 1653 1654def WrappingRegionOp : TEST_Op<"wrapping_region", 1655 [SingleBlockImplicitTerminator<"TestReturnOp">]> { 1656 let summary = "wrapping region operation"; 1657 let description = [{ 1658 Test op wrapping another op in a region, to test calling 1659 parseGenericOperation from the custom parser. 1660 }]; 1661 1662 let results = (outs Variadic<AnyType>); 1663 let regions = (region SizedRegion<1>:$region); 1664 let hasCustomAssemblyFormat = 1; 1665} 1666 1667def PrettyPrintedRegionOp : TEST_Op<"pretty_printed_region", 1668 [SingleBlockImplicitTerminator<"TestReturnOp">]> { 1669 let summary = "pretty_printed_region operation"; 1670 let description = [{ 1671 Test-op can be printed either in a "pretty" or "non-pretty" way based on 1672 some criteria. The custom parser parsers both the versions while testing 1673 APIs: parseCustomOperationName & parseGenericOperationAfterOpName. 1674 }]; 1675 let arguments = (ins 1676 AnyType:$input1, 1677 AnyType:$input2 1678 ); 1679 1680 let results = (outs AnyType); 1681 let regions = (region SizedRegion<1>:$region); 1682 let hasCustomAssemblyFormat = 1; 1683} 1684 1685def PolyForOp : TEST_Op<"polyfor", [OpAsmOpInterface]> { 1686 let summary = "polyfor operation"; 1687 let description = [{ 1688 Test op with multiple region arguments, each argument of index type. 1689 }]; 1690 let extraClassDeclaration = [{ 1691 void getAsmBlockArgumentNames(mlir::Region ®ion, 1692 mlir::OpAsmSetValueNameFn setNameFn); 1693 }]; 1694 let regions = (region SizedRegion<1>:$region); 1695 let hasCustomAssemblyFormat = 1; 1696} 1697 1698//===----------------------------------------------------------------------===// 1699// Test OpAsmInterface. 1700 1701def AsmInterfaceOp : TEST_Op<"asm_interface_op"> { 1702 let results = (outs AnyType:$first, Variadic<AnyType>:$middle_results, 1703 AnyType); 1704} 1705 1706def AsmDialectInterfaceOp : TEST_Op<"asm_dialect_interface_op"> { 1707 let results = (outs AnyType); 1708} 1709 1710//===----------------------------------------------------------------------===// 1711// Test Op Asm Format 1712//===----------------------------------------------------------------------===// 1713 1714def FormatLiteralOp : TEST_Op<"format_literal_op"> { 1715 let assemblyFormat = [{ 1716 `keyword_$.` `->` `:` `,` `=` `<` `>` `(` `)` `[` `]` `` `(` ` ` `)` 1717 `?` `+` `*` `{` `\n` `}` attr-dict 1718 }]; 1719} 1720 1721// Test that we elide attributes that are within the syntax. 1722def FormatAttrOp : TEST_Op<"format_attr_op"> { 1723 let arguments = (ins I64Attr:$attr); 1724 let assemblyFormat = "$attr attr-dict"; 1725} 1726 1727// Test that we elide optional attributes that are within the syntax. 1728def FormatOptAttrAOp : TEST_Op<"format_opt_attr_op_a"> { 1729 let arguments = (ins OptionalAttr<I64Attr>:$opt_attr); 1730 let assemblyFormat = "(`(` $opt_attr^ `)` )? attr-dict"; 1731} 1732def FormatOptAttrBOp : TEST_Op<"format_opt_attr_op_b"> { 1733 let arguments = (ins OptionalAttr<I64Attr>:$opt_attr); 1734 let assemblyFormat = "($opt_attr^)? attr-dict"; 1735} 1736 1737// Test that we format symbol name attributes properly. 1738def FormatSymbolNameAttrOp : TEST_Op<"format_symbol_name_attr_op"> { 1739 let arguments = (ins SymbolNameAttr:$attr); 1740 let assemblyFormat = "$attr attr-dict"; 1741} 1742 1743// Test that we format optional symbol name attributes properly. 1744def FormatOptSymbolNameAttrOp : TEST_Op<"format_opt_symbol_name_attr_op"> { 1745 let arguments = (ins OptionalAttr<SymbolNameAttr>:$opt_attr); 1746 let assemblyFormat = "($opt_attr^)? attr-dict"; 1747} 1748 1749// Test that we elide attributes that are within the syntax. 1750def FormatAttrDictWithKeywordOp : TEST_Op<"format_attr_dict_w_keyword"> { 1751 let arguments = (ins I64Attr:$attr, OptionalAttr<I64Attr>:$opt_attr); 1752 let assemblyFormat = "attr-dict-with-keyword"; 1753} 1754 1755// Test that we don't need to provide types in the format if they are buildable. 1756def FormatBuildableTypeOp : TEST_Op<"format_buildable_type_op"> { 1757 let arguments = (ins I64:$buildable); 1758 let results = (outs I64:$buildable_res); 1759 let assemblyFormat = "$buildable attr-dict"; 1760} 1761 1762// Test various mixings of region formatting. 1763class FormatRegionBase<string suffix, string fmt> 1764 : TEST_Op<"format_region_" # suffix # "_op"> { 1765 let regions = (region AnyRegion:$region); 1766 let assemblyFormat = fmt; 1767} 1768def FormatRegionAOp : FormatRegionBase<"a", [{ 1769 regions attr-dict 1770}]>; 1771def FormatRegionBOp : FormatRegionBase<"b", [{ 1772 $region attr-dict 1773}]>; 1774def FormatRegionCOp : FormatRegionBase<"c", [{ 1775 (`region` $region^)? attr-dict 1776}]>; 1777class FormatVariadicRegionBase<string suffix, string fmt> 1778 : TEST_Op<"format_variadic_region_" # suffix # "_op"> { 1779 let regions = (region VariadicRegion<AnyRegion>:$regions); 1780 let assemblyFormat = fmt; 1781} 1782def FormatVariadicRegionAOp : FormatVariadicRegionBase<"a", [{ 1783 $regions attr-dict 1784}]>; 1785def FormatVariadicRegionBOp : FormatVariadicRegionBase<"b", [{ 1786 ($regions^ `found_regions`)? attr-dict 1787}]>; 1788class FormatRegionImplicitTerminatorBase<string suffix, string fmt> 1789 : TEST_Op<"format_implicit_terminator_region_" # suffix # "_op", 1790 [SingleBlockImplicitTerminator<"TestReturnOp">]> { 1791 let regions = (region AnyRegion:$region); 1792 let assemblyFormat = fmt; 1793} 1794def FormatFormatRegionImplicitTerminatorAOp 1795 : FormatRegionImplicitTerminatorBase<"a", [{ 1796 $region attr-dict 1797}]>; 1798 1799// Test various mixings of result type formatting. 1800class FormatResultBase<string suffix, string fmt> 1801 : TEST_Op<"format_result_" # suffix # "_op"> { 1802 let results = (outs I64:$buildable_res, AnyMemRef:$result); 1803 let assemblyFormat = fmt; 1804} 1805def FormatResultAOp : FormatResultBase<"a", [{ 1806 type($result) attr-dict 1807}]>; 1808def FormatResultBOp : FormatResultBase<"b", [{ 1809 type(results) attr-dict 1810}]>; 1811def FormatResultCOp : FormatResultBase<"c", [{ 1812 functional-type($buildable_res, $result) attr-dict 1813}]>; 1814 1815def FormatVariadicResult : TEST_Op<"format_variadic_result"> { 1816 let results = (outs Variadic<I64>:$result); 1817 let assemblyFormat = [{ `:` type($result) attr-dict}]; 1818} 1819 1820def FormatMultipleVariadicResults : TEST_Op<"format_multiple_variadic_results", 1821 [AttrSizedResultSegments]> { 1822 let results = (outs Variadic<I64>:$result0, Variadic<AnyType>:$result1); 1823 let assemblyFormat = [{ 1824 `:` `(` type($result0) `)` `,` `(` type($result1) `)` attr-dict 1825 }]; 1826} 1827 1828// Test various mixings of operand type formatting. 1829class FormatOperandBase<string suffix, string fmt> 1830 : TEST_Op<"format_operand_" # suffix # "_op"> { 1831 let arguments = (ins I64:$buildable, AnyMemRef:$operand); 1832 let assemblyFormat = fmt; 1833} 1834 1835def FormatOperandAOp : FormatOperandBase<"a", [{ 1836 operands `:` type(operands) attr-dict 1837}]>; 1838def FormatOperandBOp : FormatOperandBase<"b", [{ 1839 operands `:` type($operand) attr-dict 1840}]>; 1841def FormatOperandCOp : FormatOperandBase<"c", [{ 1842 $buildable `,` $operand `:` type(operands) attr-dict 1843}]>; 1844def FormatOperandDOp : FormatOperandBase<"d", [{ 1845 $buildable `,` $operand `:` type($operand) attr-dict 1846}]>; 1847def FormatOperandEOp : FormatOperandBase<"e", [{ 1848 $buildable `,` $operand `:` type($buildable) `,` type($operand) attr-dict 1849}]>; 1850 1851def FormatSuccessorAOp : TEST_Op<"format_successor_a_op", [Terminator]> { 1852 let successors = (successor VariadicSuccessor<AnySuccessor>:$targets); 1853 let assemblyFormat = "$targets attr-dict"; 1854} 1855 1856def FormatVariadicOperand : TEST_Op<"format_variadic_operand"> { 1857 let arguments = (ins Variadic<I64>:$operand); 1858 let assemblyFormat = [{ $operand `:` type($operand) attr-dict}]; 1859} 1860def FormatVariadicOfVariadicOperand 1861 : TEST_Op<"format_variadic_of_variadic_operand"> { 1862 let arguments = (ins 1863 VariadicOfVariadic<I64, "operand_segments">:$operand, 1864 I32ElementsAttr:$operand_segments 1865 ); 1866 let assemblyFormat = [{ $operand `:` type($operand) attr-dict}]; 1867} 1868 1869def FormatMultipleVariadicOperands : 1870 TEST_Op<"format_multiple_variadic_operands", [AttrSizedOperandSegments]> { 1871 let arguments = (ins Variadic<I64>:$operand0, Variadic<AnyType>:$operand1); 1872 let assemblyFormat = [{ 1873 ` ` `(` $operand0 `)` `,` `(` $operand1 `:` type($operand1) `)` attr-dict 1874 }]; 1875} 1876 1877// Test various mixings of optional operand and result type formatting. 1878class FormatOptionalOperandResultOpBase<string suffix, string fmt> 1879 : TEST_Op<"format_optional_operand_result_" # suffix # "_op", 1880 [AttrSizedOperandSegments]> { 1881 let arguments = (ins Optional<I64>:$optional, Variadic<I64>:$variadic); 1882 let results = (outs Optional<I64>:$optional_res); 1883 let assemblyFormat = fmt; 1884} 1885 1886def FormatOptionalOperandResultAOp : FormatOptionalOperandResultOpBase<"a", [{ 1887 `(` $optional `:` type($optional) `)` `:` type($optional_res) 1888 (`[` $variadic^ `]`)? attr-dict 1889}]>; 1890 1891def FormatOptionalOperandResultBOp : FormatOptionalOperandResultOpBase<"b", [{ 1892 (`(` $optional^ `:` type($optional) `)`)? `:` type($optional_res) 1893 (`[` $variadic^ `]`)? attr-dict 1894}]>; 1895 1896// Test optional result type formatting. 1897class FormatOptionalResultOpBase<string suffix, string fmt> 1898 : TEST_Op<"format_optional_result_" # suffix # "_op", 1899 [AttrSizedResultSegments]> { 1900 let results = (outs Optional<I64>:$optional, Variadic<I64>:$variadic); 1901 let assemblyFormat = fmt; 1902} 1903def FormatOptionalResultAOp : FormatOptionalResultOpBase<"a", [{ 1904 (`:` type($optional)^ `->` type($variadic))? attr-dict 1905}]>; 1906 1907def FormatOptionalResultBOp : FormatOptionalResultOpBase<"b", [{ 1908 (`:` type($optional) `->` type($variadic)^)? attr-dict 1909}]>; 1910 1911def FormatOptionalResultCOp : FormatOptionalResultOpBase<"c", [{ 1912 (`:` functional-type($optional, $variadic)^)? attr-dict 1913}]>; 1914 1915def FormatTwoVariadicOperandsNoBuildableTypeOp 1916 : TEST_Op<"format_two_variadic_operands_no_buildable_type_op", 1917 [AttrSizedOperandSegments]> { 1918 let arguments = (ins Variadic<AnyType>:$a, 1919 Variadic<AnyType>:$b); 1920 let assemblyFormat = [{ 1921 `(` $a `:` type($a) `)` `->` `(` $b `:` type($b) `)` attr-dict 1922 }]; 1923} 1924 1925def FormatInferVariadicTypeFromNonVariadic 1926 : TEST_Op<"format_infer_variadic_type_from_non_variadic", 1927 [SameOperandsAndResultType]> { 1928 let arguments = (ins Variadic<AnyType>:$args); 1929 let results = (outs AnyType:$result); 1930 let assemblyFormat = "$args attr-dict `:` type($result)"; 1931} 1932 1933def FormatOptionalUnitAttr : TEST_Op<"format_optional_unit_attribute"> { 1934 let arguments = (ins UnitAttr:$is_optional); 1935 let assemblyFormat = "(`is_optional` $is_optional^)? attr-dict"; 1936} 1937 1938def FormatOptionalUnitAttrNoElide 1939 : TEST_Op<"format_optional_unit_attribute_no_elide"> { 1940 let arguments = (ins UnitAttr:$is_optional); 1941 let assemblyFormat = "($is_optional^)? attr-dict"; 1942} 1943 1944def FormatOptionalEnumAttr : TEST_Op<"format_optional_enum_attr"> { 1945 let arguments = (ins OptionalAttr<SomeI64Enum>:$attr); 1946 let assemblyFormat = "($attr^)? attr-dict"; 1947} 1948 1949def FormatOptionalWithElse : TEST_Op<"format_optional_else"> { 1950 let arguments = (ins UnitAttr:$isFirstBranchPresent); 1951 let assemblyFormat = "(`then` $isFirstBranchPresent^):(`else`)? attr-dict"; 1952} 1953 1954def FormatCompoundAttr : TEST_Op<"format_compound_attr"> { 1955 let arguments = (ins CompoundAttrA:$compound); 1956 let assemblyFormat = "$compound attr-dict-with-keyword"; 1957} 1958 1959def FormatNestedAttr : TEST_Op<"format_nested_attr"> { 1960 let arguments = (ins CompoundAttrNested:$nested); 1961 let assemblyFormat = "$nested attr-dict-with-keyword"; 1962} 1963 1964def FormatNestedCompoundAttr : TEST_Op<"format_cpmd_nested_attr"> { 1965 let arguments = (ins CompoundNestedOuter:$nested); 1966 let assemblyFormat = "`nested` $nested attr-dict-with-keyword"; 1967} 1968 1969def FormatQualifiedCompoundAttr : TEST_Op<"format_qual_cpmd_nested_attr"> { 1970 let arguments = (ins CompoundNestedOuter:$nested); 1971 let assemblyFormat = "`nested` qualified($nested) attr-dict-with-keyword"; 1972} 1973 1974def FormatNestedType : TEST_Op<"format_cpmd_nested_type"> { 1975 let arguments = (ins CompoundNestedOuterType:$nested); 1976 let assemblyFormat = "$nested `nested` type($nested) attr-dict-with-keyword"; 1977} 1978 1979def FormatQualifiedNestedType : TEST_Op<"format_qual_cpmd_nested_type"> { 1980 let arguments = (ins CompoundNestedOuterType:$nested); 1981 let assemblyFormat = "$nested `nested` qualified(type($nested)) attr-dict-with-keyword"; 1982} 1983 1984//===----------------------------------------------------------------------===// 1985// Custom Directives 1986 1987def FormatCustomDirectiveOperands 1988 : TEST_Op<"format_custom_directive_operands", [AttrSizedOperandSegments]> { 1989 let arguments = (ins I64:$operand, Optional<I64>:$optOperand, 1990 Variadic<I64>:$varOperands); 1991 let assemblyFormat = [{ 1992 custom<CustomDirectiveOperands>( 1993 $operand, $optOperand, $varOperands 1994 ) 1995 attr-dict 1996 }]; 1997} 1998 1999def FormatCustomDirectiveOperandsAndTypes 2000 : TEST_Op<"format_custom_directive_operands_and_types", 2001 [AttrSizedOperandSegments]> { 2002 let arguments = (ins AnyType:$operand, Optional<AnyType>:$optOperand, 2003 Variadic<AnyType>:$varOperands); 2004 let assemblyFormat = [{ 2005 custom<CustomDirectiveOperandsAndTypes>( 2006 $operand, $optOperand, $varOperands, 2007 type($operand), type($optOperand), type($varOperands) 2008 ) 2009 attr-dict 2010 }]; 2011} 2012 2013def FormatCustomDirectiveRegions : TEST_Op<"format_custom_directive_regions"> { 2014 let regions = (region AnyRegion:$region, VariadicRegion<AnyRegion>:$other_regions); 2015 let assemblyFormat = [{ 2016 custom<CustomDirectiveRegions>( 2017 $region, $other_regions 2018 ) 2019 attr-dict 2020 }]; 2021} 2022 2023def FormatCustomDirectiveResults 2024 : TEST_Op<"format_custom_directive_results", [AttrSizedResultSegments]> { 2025 let results = (outs AnyType:$result, Optional<AnyType>:$optResult, 2026 Variadic<AnyType>:$varResults); 2027 let assemblyFormat = [{ 2028 custom<CustomDirectiveResults>( 2029 type($result), type($optResult), type($varResults) 2030 ) 2031 attr-dict 2032 }]; 2033} 2034 2035def FormatCustomDirectiveResultsWithTypeRefs 2036 : TEST_Op<"format_custom_directive_results_with_type_refs", 2037 [AttrSizedResultSegments]> { 2038 let results = (outs AnyType:$result, Optional<AnyType>:$optResult, 2039 Variadic<AnyType>:$varResults); 2040 let assemblyFormat = [{ 2041 custom<CustomDirectiveResults>( 2042 type($result), type($optResult), type($varResults) 2043 ) 2044 custom<CustomDirectiveWithTypeRefs>( 2045 ref(type($result)), ref(type($optResult)), ref(type($varResults)) 2046 ) 2047 attr-dict 2048 }]; 2049} 2050 2051def FormatCustomDirectiveWithOptionalOperandRef 2052 : TEST_Op<"format_custom_directive_with_optional_operand_ref"> { 2053 let arguments = (ins Optional<I64>:$optOperand); 2054 let assemblyFormat = [{ 2055 ($optOperand^)? `:` 2056 custom<CustomDirectiveOptionalOperandRef>(ref($optOperand)) 2057 attr-dict 2058 }]; 2059} 2060 2061def FormatCustomDirectiveSuccessors 2062 : TEST_Op<"format_custom_directive_successors", [Terminator]> { 2063 let successors = (successor AnySuccessor:$successor, 2064 VariadicSuccessor<AnySuccessor>:$successors); 2065 let assemblyFormat = [{ 2066 custom<CustomDirectiveSuccessors>( 2067 $successor, $successors 2068 ) 2069 attr-dict 2070 }]; 2071} 2072 2073def FormatCustomDirectiveAttributes 2074 : TEST_Op<"format_custom_directive_attributes"> { 2075 let arguments = (ins I64Attr:$attr, OptionalAttr<I64Attr>:$optAttr); 2076 let assemblyFormat = [{ 2077 custom<CustomDirectiveAttributes>( 2078 $attr, $optAttr 2079 ) 2080 attr-dict 2081 }]; 2082} 2083 2084def FormatCustomDirectiveAttrDict 2085 : TEST_Op<"format_custom_directive_attrdict"> { 2086 let arguments = (ins I64Attr:$attr, OptionalAttr<I64Attr>:$optAttr); 2087 let assemblyFormat = [{ 2088 custom<CustomDirectiveAttrDict>( attr-dict ) 2089 }]; 2090} 2091 2092//===----------------------------------------------------------------------===// 2093// AllTypesMatch type inference 2094 2095def FormatAllTypesMatchVarOp : TEST_Op<"format_all_types_match_var", [ 2096 AllTypesMatch<["value1", "value2", "result"]> 2097 ]> { 2098 let arguments = (ins AnyType:$value1, AnyType:$value2); 2099 let results = (outs AnyType:$result); 2100 let assemblyFormat = "attr-dict $value1 `,` $value2 `:` type($value1)"; 2101} 2102 2103def FormatAllTypesMatchAttrOp : TEST_Op<"format_all_types_match_attr", [ 2104 AllTypesMatch<["value1", "value2", "result"]> 2105 ]> { 2106 let arguments = (ins AnyAttr:$value1, AnyType:$value2); 2107 let results = (outs AnyType:$result); 2108 let assemblyFormat = "attr-dict $value1 `,` $value2"; 2109} 2110 2111//===----------------------------------------------------------------------===// 2112// TypesMatchWith type inference 2113 2114def FormatTypesMatchVarOp : TEST_Op<"format_types_match_var", [ 2115 TypesMatchWith<"result type matches operand", "value", "result", "$_self"> 2116 ]> { 2117 let arguments = (ins AnyType:$value); 2118 let results = (outs AnyType:$result); 2119 let assemblyFormat = "attr-dict $value `:` type($value)"; 2120} 2121 2122def FormatTypesMatchVariadicOp : TEST_Op<"format_types_match_variadic", [ 2123 RangedTypesMatchWith<"result type matches operand", "value", "result", 2124 "llvm::make_range($_self.begin(), $_self.end())"> 2125 ]> { 2126 let arguments = (ins Variadic<AnyType>:$value); 2127 let results = (outs Variadic<AnyType>:$result); 2128 let assemblyFormat = "attr-dict $value `:` type($value)"; 2129} 2130 2131def FormatTypesMatchAttrOp : TEST_Op<"format_types_match_attr", [ 2132 TypesMatchWith<"result type matches constant", "value", "result", "$_self"> 2133 ]> { 2134 let arguments = (ins AnyAttr:$value); 2135 let results = (outs AnyType:$result); 2136 let assemblyFormat = "attr-dict $value"; 2137} 2138 2139def FormatTypesMatchContextOp : TEST_Op<"format_types_match_context", [ 2140 TypesMatchWith<"tuple result type matches operand type", "value", "result", 2141 "::mlir::TupleType::get($_ctxt, $_self)"> 2142 ]> { 2143 let arguments = (ins AnyType:$value); 2144 let results = (outs AnyType:$result); 2145 let assemblyFormat = "attr-dict $value `:` type($value)"; 2146} 2147 2148//===----------------------------------------------------------------------===// 2149// InferTypeOpInterface type inference in assembly format 2150 2151def FormatInferTypeOp : TEST_Op<"format_infer_type", [InferTypeOpInterface]> { 2152 let results = (outs AnyType); 2153 let assemblyFormat = "attr-dict"; 2154 2155 let extraClassDeclaration = [{ 2156 static ::mlir::LogicalResult inferReturnTypes(::mlir::MLIRContext *context, 2157 ::llvm::Optional<::mlir::Location> location, ::mlir::ValueRange operands, 2158 ::mlir::DictionaryAttr attributes, ::mlir::RegionRange regions, 2159 ::llvm::SmallVectorImpl<::mlir::Type> &inferredReturnTypes) { 2160 inferredReturnTypes.assign({::mlir::IntegerType::get(context, 16)}); 2161 return ::mlir::success(); 2162 } 2163 }]; 2164} 2165 2166// Check that formatget supports DeclareOpInterfaceMethods. 2167def FormatInferType2Op : TEST_Op<"format_infer_type2", [DeclareOpInterfaceMethods<InferTypeOpInterface>]> { 2168 let results = (outs AnyType); 2169 let assemblyFormat = "attr-dict"; 2170} 2171 2172// Base class for testing mixing allOperandTypes, allOperands, and 2173// inferResultTypes. 2174class FormatInferAllTypesBaseOp<string mnemonic, list<Trait> traits = []> 2175 : TEST_Op<mnemonic, [InferTypeOpInterface] # traits> { 2176 let arguments = (ins Variadic<AnyType>:$args); 2177 let results = (outs Variadic<AnyType>:$outs); 2178 let extraClassDeclaration = [{ 2179 static ::mlir::LogicalResult inferReturnTypes(::mlir::MLIRContext *context, 2180 ::llvm::Optional<::mlir::Location> location, ::mlir::ValueRange operands, 2181 ::mlir::DictionaryAttr attributes, ::mlir::RegionRange regions, 2182 ::llvm::SmallVectorImpl<::mlir::Type> &inferredReturnTypes) { 2183 ::mlir::TypeRange operandTypes = operands.getTypes(); 2184 inferredReturnTypes.assign(operandTypes.begin(), operandTypes.end()); 2185 return ::mlir::success(); 2186 } 2187 }]; 2188} 2189 2190// Test inferReturnTypes is called when allOperandTypes and allOperands is true. 2191def FormatInferTypeAllOperandsAndTypesOp 2192 : FormatInferAllTypesBaseOp<"format_infer_type_all_operands_and_types"> { 2193 let assemblyFormat = "`(` operands `)` attr-dict `:` type(operands)"; 2194} 2195 2196// Test inferReturnTypes is called when allOperandTypes is true and there is one 2197// ODS operand. 2198def FormatInferTypeAllOperandsAndTypesOneOperandOp 2199 : FormatInferAllTypesBaseOp<"format_infer_type_all_types_one_operand"> { 2200 let assemblyFormat = "`(` $args `)` attr-dict `:` type(operands)"; 2201} 2202 2203// Test inferReturnTypes is called when allOperandTypes is true and there are 2204// more than one ODS operands. 2205def FormatInferTypeAllOperandsAndTypesTwoOperandsOp 2206 : FormatInferAllTypesBaseOp<"format_infer_type_all_types_two_operands", 2207 [SameVariadicOperandSize]> { 2208 let arguments = (ins Variadic<AnyType>:$args0, Variadic<AnyType>:$args1); 2209 let assemblyFormat = "`(` $args0 `)` `(` $args1 `)` attr-dict `:` type(operands)"; 2210} 2211 2212// Test inferReturnTypes is called when allOperands is true and operand types 2213// are separately specified. 2214def FormatInferTypeAllTypesOp 2215 : FormatInferAllTypesBaseOp<"format_infer_type_all_types"> { 2216 let assemblyFormat = "`(` operands `)` attr-dict `:` type($args)"; 2217} 2218 2219// Test inferReturnTypes coupled with regions. 2220def FormatInferTypeRegionsOp 2221 : TEST_Op<"format_infer_type_regions", [InferTypeOpInterface]> { 2222 let results = (outs Variadic<AnyType>:$outs); 2223 let regions = (region AnyRegion:$region); 2224 let assemblyFormat = "$region attr-dict"; 2225 let extraClassDeclaration = [{ 2226 static ::mlir::LogicalResult inferReturnTypes(::mlir::MLIRContext *context, 2227 ::llvm::Optional<::mlir::Location> location, ::mlir::ValueRange operands, 2228 ::mlir::DictionaryAttr attributes, ::mlir::RegionRange regions, 2229 ::llvm::SmallVectorImpl<::mlir::Type> &inferredReturnTypes) { 2230 if (regions.empty()) 2231 return ::mlir::failure(); 2232 auto types = regions.front()->getArgumentTypes(); 2233 inferredReturnTypes.assign(types.begin(), types.end()); 2234 return ::mlir::success(); 2235 } 2236 }]; 2237} 2238 2239// Test inferReturnTypes coupled with variadic operands (operand_segment_sizes). 2240def FormatInferTypeVariadicOperandsOp 2241 : TEST_Op<"format_infer_type_variadic_operands", 2242 [InferTypeOpInterface, AttrSizedOperandSegments]> { 2243 let arguments = (ins Variadic<I32>:$a, Variadic<I64>:$b); 2244 let results = (outs Variadic<AnyType>:$outs); 2245 let assemblyFormat = "`(` $a `:` type($a) `)` `(` $b `:` type($b) `)` attr-dict"; 2246 let extraClassDeclaration = [{ 2247 static ::mlir::LogicalResult inferReturnTypes(::mlir::MLIRContext *context, 2248 ::llvm::Optional<::mlir::Location> location, ::mlir::ValueRange operands, 2249 ::mlir::DictionaryAttr attributes, ::mlir::RegionRange regions, 2250 ::llvm::SmallVectorImpl<::mlir::Type> &inferredReturnTypes) { 2251 FormatInferTypeVariadicOperandsOpAdaptor adaptor(operands, attributes); 2252 auto aTypes = adaptor.getA().getTypes(); 2253 auto bTypes = adaptor.getB().getTypes(); 2254 inferredReturnTypes.append(aTypes.begin(), aTypes.end()); 2255 inferredReturnTypes.append(bTypes.begin(), bTypes.end()); 2256 return ::mlir::success(); 2257 } 2258 }]; 2259} 2260 2261//===----------------------------------------------------------------------===// 2262// Test SideEffects 2263//===----------------------------------------------------------------------===// 2264 2265def SideEffectOp : TEST_Op<"side_effect_op", 2266 [DeclareOpInterfaceMethods<MemoryEffectsOpInterface>, 2267 DeclareOpInterfaceMethods<TestEffectOpInterface>]> { 2268 let results = (outs AnyType:$result); 2269} 2270 2271//===----------------------------------------------------------------------===// 2272// Test CopyOpInterface 2273//===----------------------------------------------------------------------===// 2274 2275def CopyOp : TEST_Op<"copy", [CopyOpInterface]> { 2276 let description = [{ 2277 Represents a copy operation. 2278 }]; 2279 let arguments = (ins Res<AnyRankedOrUnrankedMemRef, "", [MemRead]>:$source, 2280 Res<AnyRankedOrUnrankedMemRef, "", [MemWrite]>:$target); 2281 let assemblyFormat = [{ 2282 `(` $source `,` $target `)` `:` `(` type($source) `,` type($target) `)` 2283 attr-dict 2284 }]; 2285} 2286 2287//===----------------------------------------------------------------------===// 2288// Test Buffer/Tensor 2289//===----------------------------------------------------------------------===// 2290 2291def RegionYieldOp : TEST_Op<"region_yield", 2292 [NoSideEffect, ReturnLike, Terminator]> { 2293 let description = [{ 2294 This operation is used in a region and yields the corresponding type for 2295 that operation. 2296 }]; 2297 let arguments = (ins AnyType:$result); 2298 let assemblyFormat = [{ 2299 $result `:` type($result) attr-dict 2300 }]; 2301 let builders = [OpBuilder<(ins), 2302 [{ build($_builder, $_state, {}); }]> 2303 ]; 2304} 2305 2306class BufferBasedOpBase<string mnemonic, list<Trait> traits> 2307 : TEST_Op<mnemonic, traits> { 2308 let description = [{ 2309 A buffer based operation, that uses memRefs as input and output. 2310 }]; 2311 let arguments = (ins AnyRankedOrUnrankedMemRef:$input, 2312 AnyRankedOrUnrankedMemRef:$output); 2313} 2314 2315def BufferBasedOp : BufferBasedOpBase<"buffer_based", []>{ 2316 let assemblyFormat = [{ 2317 `in` `(` $input`:` type($input) `)` `out` `(` $output`:` type($output) `)` 2318 attr-dict 2319 }]; 2320} 2321 2322def RegionBufferBasedOp : BufferBasedOpBase<"region_buffer_based", 2323 [SingleBlockImplicitTerminator<"RegionYieldOp">]> { 2324 let regions = (region AnyRegion:$region); 2325 let assemblyFormat = [{ 2326 `in` `(` $input`:` type($input) `)` `out` `(` $output`:` type($output) `)` 2327 $region attr-dict 2328 }]; 2329} 2330 2331def TensorBasedOp : TEST_Op<"tensor_based", []> { 2332 let description = [{ 2333 A tensor based operation, that uses a tensor as an input and results in a 2334 tensor again. 2335 }]; 2336 let arguments = (ins AnyRankedTensor:$input); 2337 let results = (outs AnyRankedTensor:$result); 2338 let assemblyFormat = [{ 2339 `in` `(` $input`:` type($input) `)` `->` type($result) attr-dict 2340 }]; 2341} 2342 2343//===----------------------------------------------------------------------===// 2344// Test RegionBranchOpInterface 2345//===----------------------------------------------------------------------===// 2346 2347def RegionIfYieldOp : TEST_Op<"region_if_yield", 2348 [NoSideEffect, ReturnLike, Terminator]> { 2349 let arguments = (ins Variadic<AnyType>:$results); 2350 let assemblyFormat = [{ 2351 $results `:` type($results) attr-dict 2352 }]; 2353} 2354 2355def RegionIfOp : TEST_Op<"region_if", 2356 [DeclareOpInterfaceMethods<RegionBranchOpInterface, 2357 ["getRegionInvocationBounds"]>, 2358 SingleBlockImplicitTerminator<"RegionIfYieldOp">, 2359 RecursiveSideEffects]> { 2360 let description =[{ 2361 Represents an abstract if-then-else-join pattern. In this context, the then 2362 and else regions jump to the join region, which finally returns to its 2363 parent op. 2364 }]; 2365 2366 let arguments = (ins Variadic<AnyType>); 2367 let results = (outs Variadic<AnyType>:$results); 2368 let regions = (region SizedRegion<1>:$thenRegion, 2369 AnyRegion:$elseRegion, 2370 AnyRegion:$joinRegion); 2371 let extraClassDeclaration = [{ 2372 ::mlir::Block::BlockArgListType getThenArgs() { 2373 return getBody(0)->getArguments(); 2374 } 2375 ::mlir::Block::BlockArgListType getElseArgs() { 2376 return getBody(1)->getArguments(); 2377 } 2378 ::mlir::Block::BlockArgListType getJoinArgs() { 2379 return getBody(2)->getArguments(); 2380 } 2381 ::mlir::OperandRange getSuccessorEntryOperands(unsigned index); 2382 }]; 2383 let hasCustomAssemblyFormat = 1; 2384} 2385 2386def AnyCondOp : TEST_Op<"any_cond", 2387 [DeclareOpInterfaceMethods<RegionBranchOpInterface, 2388 ["getRegionInvocationBounds"]>, 2389 RecursiveSideEffects]> { 2390 let results = (outs Variadic<AnyType>:$results); 2391 let regions = (region AnyRegion:$region); 2392} 2393 2394//===----------------------------------------------------------------------===// 2395// Test TableGen generated build() methods 2396//===----------------------------------------------------------------------===// 2397 2398def TableGenConstant : TEST_Op<"tblgen_constant"> { 2399 let results = (outs AnyType); 2400} 2401 2402// No variadic args or results. 2403def TableGenBuildOp0 : TEST_Op<"tblgen_build_0"> { 2404 let arguments = (ins AnyType:$value); 2405 let results = (outs AnyType:$result); 2406} 2407 2408// Sigle variadic arg and single variadic results. 2409def TableGenBuildOp1 : TEST_Op<"tblgen_build_1"> { 2410 let arguments = (ins Variadic<AnyType>:$inputs); 2411 let results = (outs Variadic<AnyType>:$results); 2412} 2413 2414// Single variadic arg and non-variadic results. 2415def TableGenBuildOp2 : TEST_Op<"tblgen_build_2"> { 2416 let arguments = (ins Variadic<AnyType>:$inputs); 2417 let results = (outs AnyType:$result); 2418} 2419 2420// Single variadic arg and multiple variadic results. 2421def TableGenBuildOp3 : TEST_Op<"tblgen_build_3", [SameVariadicResultSize]> { 2422 let arguments = (ins Variadic<AnyType>:$inputs); 2423 let results = (outs Variadic<AnyType>:$resultA, Variadic<AnyType>:$resultB); 2424} 2425 2426// Single variadic arg, non variadic results, with SameOperandsAndResultType. 2427// Tests suppression of ambiguous build methods for operations with 2428// SameOperandsAndResultType trait. 2429def TableGenBuildOp4 : TEST_Op<"tblgen_build_4", [SameOperandsAndResultType]> { 2430 let arguments = (ins Variadic<AnyType>:$inputs); 2431 let results = (outs AnyType:$result); 2432} 2433 2434// Base class for testing `build` methods for ops with 2435// InferReturnTypeOpInterface. 2436class TableGenBuildInferReturnTypeBaseOp<string mnemonic, 2437 list<Trait> traits = []> 2438 : TEST_Op<mnemonic, [InferTypeOpInterface] # traits> { 2439 let arguments = (ins Variadic<AnyType>:$inputs); 2440 let results = (outs AnyType:$result); 2441 2442 let extraClassDeclaration = [{ 2443 static ::mlir::LogicalResult inferReturnTypes(::mlir::MLIRContext *, 2444 ::llvm::Optional<::mlir::Location> location, ::mlir::ValueRange operands, 2445 ::mlir::DictionaryAttr attributes, ::mlir::RegionRange regions, 2446 ::llvm::SmallVectorImpl<::mlir::Type> &inferredReturnTypes) { 2447 inferredReturnTypes.assign({operands[0].getType()}); 2448 return ::mlir::success(); 2449 } 2450 }]; 2451} 2452 2453// Single variadic arg with SameOperandsAndResultType and InferTypeOpInterface. 2454// Tests suppression of ambiguous build methods for operations with 2455// SameOperandsAndResultType and InferTypeOpInterface. 2456def TableGenBuildOp5 : TableGenBuildInferReturnTypeBaseOp< 2457 "tblgen_build_5", [SameOperandsAndResultType]>; 2458 2459// Op with InferTypeOpInterface and regions. 2460def TableGenBuildOp6 : TableGenBuildInferReturnTypeBaseOp< 2461 "tblgen_build_6", [InferTypeOpInterface]> { 2462 let regions = (region AnyRegion:$body); 2463} 2464 2465//===----------------------------------------------------------------------===// 2466// Test BufferPlacement 2467//===----------------------------------------------------------------------===// 2468 2469def GetTupleElementOp: TEST_Op<"get_tuple_element"> { 2470 let description = [{ 2471 Test op that returns a specified element of the tuple. 2472 }]; 2473 2474 let arguments = (ins 2475 TupleOf<[AnyType]>, 2476 I32Attr:$index 2477 ); 2478 let results = (outs AnyType); 2479} 2480 2481def MakeTupleOp: TEST_Op<"make_tuple"> { 2482 let description = [{ 2483 Test op that creates a tuple value from a list of values. 2484 }]; 2485 2486 let arguments = (ins 2487 Variadic<AnyType>:$inputs 2488 ); 2489 let results = (outs TupleOf<[AnyType]>); 2490} 2491 2492//===----------------------------------------------------------------------===// 2493// Test Target DataLayout 2494//===----------------------------------------------------------------------===// 2495 2496def OpWithDataLayoutOp : TEST_Op<"op_with_data_layout", 2497 [HasDefaultDLTIDataLayout, DataLayoutOpInterface]> { 2498 let summary = 2499 "An op that uses DataLayout implementation from the Target dialect"; 2500 let regions = (region VariadicRegion<AnyRegion>:$regions); 2501} 2502 2503def DataLayoutQueryOp : TEST_Op<"data_layout_query"> { 2504 let summary = "A token op recognized by data layout query test pass"; 2505 let description = [{ 2506 The data layout query pass pattern-matches this op and attaches to it an 2507 array attribute containing the result of data layout query of the result 2508 type of this op. 2509 }]; 2510 2511 let results = (outs AnyType:$res); 2512} 2513 2514//===----------------------------------------------------------------------===// 2515// Test Reducer Patterns 2516//===----------------------------------------------------------------------===// 2517 2518def OpCrashLong : TEST_Op<"op_crash_long"> { 2519 let arguments = (ins I32, I32, I32); 2520 let results = (outs I32); 2521} 2522 2523def OpCrashShort : TEST_Op<"op_crash_short"> { 2524 let results = (outs I32); 2525} 2526 2527def : Pat<(OpCrashLong $_, $_, $_), (OpCrashShort)>; 2528 2529//===----------------------------------------------------------------------===// 2530// Test LinalgConvolutionOpInterface. 2531//===----------------------------------------------------------------------===// 2532 2533def TestLinalgConvOpNotLinalgOp : TEST_Op<"conv_op_not_linalg_op", [ 2534 LinalgConvolutionOpInterface]> { 2535 let arguments = (ins 2536 AnyType:$image, AnyType:$filter, AnyType:$output); 2537 let results = (outs AnyRankedTensor:$result); 2538} 2539 2540def TestLinalgConvOp : 2541 TEST_Op<"linalg_conv_op", [AttrSizedOperandSegments, SingleBlock, 2542 LinalgStructuredInterface, LinalgConvolutionOpInterface]> { 2543 2544 let arguments = (ins Variadic<AnyType>:$inputs, 2545 Variadic<AnyType>:$outputs); 2546 let results = (outs Variadic<AnyType>:$results); 2547 let regions = (region AnyRegion:$region); 2548 2549 let assemblyFormat = [{ 2550 attr-dict (`ins` `(` $inputs^ `:` type($inputs) `)`)? 2551 `outs` `(` $outputs `:` type($outputs) `)` 2552 $region (`->` type($results)^)? 2553 }]; 2554 2555 let extraClassDeclaration = [{ 2556 bool hasIndexSemantics() { return false; } 2557 2558 static void regionBuilder(mlir::ImplicitLocOpBuilder &b, mlir::Block &block, 2559 mlir::ArrayRef<mlir::NamedAttribute> attrs) { 2560 b.create<mlir::linalg::YieldOp>(block.getArguments().back()); 2561 } 2562 2563 static std::function<void(mlir::ImplicitLocOpBuilder &, mlir::Block &, 2564 mlir::ArrayRef<mlir::NamedAttribute>)> 2565 getRegionBuilder() { 2566 return ®ionBuilder; 2567 } 2568 2569 mlir::ArrayAttr iterator_types() { 2570 return getOperation()->getAttrOfType<mlir::ArrayAttr>("iterator_types"); 2571 } 2572 2573 mlir::ArrayAttr indexing_maps() { 2574 return getOperation()->getAttrOfType<mlir::ArrayAttr>("indexing_maps"); 2575 } 2576 2577 std::string getLibraryCallName() { 2578 return ""; 2579 } 2580 2581 // To conform with interface requirement on operand naming. 2582 mlir::ValueRange inputs() { return getInputs(); } 2583 mlir::ValueRange outputs() { return getOutputs(); } 2584 }]; 2585} 2586 2587//===----------------------------------------------------------------------===// 2588// Test Ops with Default-Valued String Attributes 2589//===----------------------------------------------------------------------===// 2590 2591def TestDefaultStrAttrNoValueOp : TEST_Op<"no_str_value"> { 2592 let arguments = (ins DefaultValuedAttr<StrAttr, "">:$value); 2593 let assemblyFormat = "attr-dict"; 2594} 2595 2596def TestDefaultStrAttrHasValueOp : TEST_Op<"has_str_value"> { 2597 let arguments = (ins DefaultValuedStrAttr<StrAttr, "">:$value); 2598 let assemblyFormat = "attr-dict"; 2599} 2600 2601def : Pat<(TestDefaultStrAttrNoValueOp $value), 2602 (TestDefaultStrAttrHasValueOp ConstantStrAttr<StrAttr, "foo">)>; 2603 2604//===----------------------------------------------------------------------===// 2605// Test Ops with effects 2606//===----------------------------------------------------------------------===// 2607 2608def TestResource : Resource<"TestResource">; 2609 2610def TestEffectsOpA : TEST_Op<"op_with_effects_a"> { 2611 let arguments = (ins 2612 Arg<Variadic<AnyMemRef>, "", [MemRead]>, 2613 Arg<FlatSymbolRefAttr, "", [MemRead]>:$first, 2614 Arg<SymbolRefAttr, "", [MemWrite]>:$second, 2615 Arg<OptionalAttr<SymbolRefAttr>, "", [MemRead]>:$optional_symbol 2616 ); 2617 2618 let results = (outs Res<AnyMemRef, "", [MemAlloc<TestResource>]>); 2619} 2620 2621def TestEffectsOpB : TEST_Op<"op_with_effects_b", 2622 [MemoryEffects<[MemWrite<TestResource>]>]>; 2623 2624#endif // TEST_OPS 2625