786 lines · plain
1 2//===-- TestOpsSyntax.td - Operations for testing syntax ---*- tablegen -*-===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef TEST_OPS_SYNTAX11#define TEST_OPS_SYNTAX12 13include "TestAttrDefs.td"14include "TestDialect.td"15include "TestTypeDefs.td"16include "mlir/Interfaces/InferTypeOpInterface.td"17include "mlir/IR/OpBase.td"18 19class TEST_Op<string mnemonic, list<Trait> traits = []> :20 Op<Test_Dialect, mnemonic, traits>;21 22def WrappingRegionOp : TEST_Op<"wrapping_region",23 [SingleBlockImplicitTerminator<"TestReturnOp">]> {24 let summary = "wrapping region operation";25 let description = [{26 Test op wrapping another op in a region, to test calling27 parseGenericOperation from the custom parser.28 }];29 30 let results = (outs Variadic<AnyType>);31 let regions = (region SizedRegion<1>:$region);32 let hasCustomAssemblyFormat = 1;33}34 35def PrettyPrintedRegionOp : TEST_Op<"pretty_printed_region",36 [SingleBlockImplicitTerminator<"TestReturnOp">]> {37 let summary = "pretty_printed_region operation";38 let description = [{39 Test-op can be printed either in a "pretty" or "non-pretty" way based on40 some criteria. The custom parser parsers both the versions while testing41 APIs: parseCustomOperationName & parseGenericOperationAfterOpName.42 }];43 let arguments = (ins44 AnyType:$input1,45 AnyType:$input246 );47 48 let results = (outs AnyType);49 let regions = (region SizedRegion<1>:$region);50 let hasCustomAssemblyFormat = 1;51}52 53def PolyForOp : TEST_Op<"polyfor", [OpAsmOpInterface]> {54 let summary = "polyfor operation";55 let description = [{56 Test op with multiple region arguments, each argument of index type.57 }];58 let extraClassDeclaration = [{59 void getAsmBlockArgumentNames(mlir::Region ®ion,60 mlir::OpAsmSetValueNameFn setNameFn);61 }];62 let regions = (region SizedRegion<1>:$region);63 let hasCustomAssemblyFormat = 1;64}65 66def TestAttrWithLoc : TEST_Op<"attr_with_loc"> {67 let summary = "op's attribute has a location";68 let arguments = (ins AnyAttr:$loc, AnyAttr:$value);69 let assemblyFormat = "`(` $value `` custom<OptionalLoc>($loc) `)` attr-dict";70}71 72def ParseCustomOperationNameAPI : TEST_Op<"parse_custom_operation_name_api"> {73 let summary = "noop that exercises the parseCustomOperationName API";74 let arguments = (ins StrAttr:$name);75 let assemblyFormat = "`(` custom<CustomOperationNameEntry>($name) `)` attr-dict";76}77 78// -----79 80// This is used to test that the fallback for a custom op's parser and printer81// is the dialect parser and printer hooks.82def CustomFormatFallbackOp : TEST_Op<"dialect_custom_format_fallback">;83 84// Ops related to OIList primitive85def OIListTrivial : TEST_Op<"oilist_with_keywords_only"> {86 let arguments = (ins UnitAttr:$keyword, UnitAttr:$otherKeyword,87 UnitAttr:$diffNameUnitAttrKeyword);88 let assemblyFormat = [{89 oilist( `keyword` $keyword90 | `otherKeyword` $otherKeyword91 | `thirdKeyword` $diffNameUnitAttrKeyword) attr-dict92 }];93}94 95// Ops related to OIList primitive96def OIListTrivialProperties : TEST_Op<"oilist_with_keywords_only_properties"> {97 let arguments = (ins UnitProp:$keyword, UnitProp:$otherKeyword,98 UnitProp:$diffNameUnitPropKeyword);99 let assemblyFormat = [{100 oilist( `keyword` $keyword101 | `otherKeyword` $otherKeyword102 | `thirdKeyword` $diffNameUnitPropKeyword) attr-dict103 }];104}105 106def OIListSimple : TEST_Op<"oilist_with_simple_args", [AttrSizedOperandSegments]> {107 let arguments = (ins Optional<AnyType>:$arg0,108 Optional<AnyType>:$arg1,109 Optional<AnyType>:$arg2);110 let assemblyFormat = [{111 oilist( `keyword` $arg0 `:` type($arg0)112 | `otherKeyword` $arg1 `:` type($arg1)113 | `thirdKeyword` $arg2 `:` type($arg2) ) attr-dict114 }];115}116 117def OIListVariadic : TEST_Op<"oilist_variadic_with_parens", [AttrSizedOperandSegments]> {118 let arguments = (ins Variadic<AnyType>:$arg0,119 Variadic<AnyType>:$arg1,120 Variadic<AnyType>:$arg2);121 let assemblyFormat = [{122 oilist( `keyword` `(` $arg0 `:` type($arg0) `)`123 | `otherKeyword` `(` $arg1 `:` type($arg1) `)`124 | `thirdKeyword` `(` $arg2 `:` type($arg2) `)`) attr-dict125 }];126}127 128def OIListCustom : TEST_Op<"oilist_custom", [AttrSizedOperandSegments]> {129 let arguments = (ins Variadic<AnyType>:$arg0,130 Optional<I32>:$optOperand,131 UnitAttr:$nowait);132 let assemblyFormat = [{133 oilist( `private` `(` $arg0 `:` type($arg0) `)`134 | `reduction` custom<CustomOptionalOperand>($optOperand)135 | `nowait` $nowait136 ) attr-dict137 }];138}139 140def OIListAllowedLiteral : TEST_Op<"oilist_allowed_literal"> {141 let assemblyFormat = [{142 oilist( `foo` | `bar` ) `buzz` attr-dict143 }];144}145 146def TestEllipsisOp : TEST_Op<"ellipsis"> {147 let arguments = (ins Variadic<AnyType>:$operands, UnitAttr:$variadic);148 let assemblyFormat = [{149 `(` $operands (`...` $variadic^)? `)` attr-dict `:` type($operands) `...`150 }];151}152 153def ElseAnchorOp : TEST_Op<"else_anchor"> {154 let arguments = (ins Optional<AnyType>:$a);155 let assemblyFormat = "`(` (`?`) : (`` $a^ `:` type($a))? `)` attr-dict";156}157 158// This is used to test that the default dialect is not elided when printing an159// op with dots in the name to avoid parsing ambiguity.160def OpWithDotInNameOp : TEST_Op<"op.with_dot_in_name"> {161 let assemblyFormat = "attr-dict";162}163 164// --------------165 166//===----------------------------------------------------------------------===//167// Test Op Asm Format168//===----------------------------------------------------------------------===//169 170def FormatLiteralOp : TEST_Op<"format_literal_op"> {171 let assemblyFormat = [{172 `keyword_$.` `->` `:` `,` `=` `<` `>` `(` `)` `[` `]` `` `(` ` ` `)`173 `?` `+` `*` `{` `\n` `}` attr-dict174 }];175}176 177// Test that we elide attributes that are within the syntax.178def FormatAttrOp : TEST_Op<"format_attr_op"> {179 let arguments = (ins I64Attr:$attr);180 let assemblyFormat = "$attr attr-dict";181}182 183// Test that we elide optional attributes that are within the syntax.184def FormatOptAttrAOp : TEST_Op<"format_opt_attr_op_a"> {185 let arguments = (ins OptionalAttr<I64Attr>:$opt_attr);186 let assemblyFormat = "(`(` $opt_attr^ `)` )? attr-dict";187}188def FormatOptAttrBOp : TEST_Op<"format_opt_attr_op_b"> {189 let arguments = (ins OptionalAttr<I64Attr>:$opt_attr);190 let assemblyFormat = "($opt_attr^)? attr-dict";191}192 193// Test that we format symbol name attributes properly.194def FormatSymbolNameAttrOp : TEST_Op<"format_symbol_name_attr_op"> {195 let arguments = (ins SymbolNameAttr:$attr);196 let assemblyFormat = "$attr attr-dict";197}198 199// Test that we format optional symbol name attributes properly.200def FormatOptSymbolNameAttrOp : TEST_Op<"format_opt_symbol_name_attr_op"> {201 let arguments = (ins OptionalAttr<SymbolNameAttr>:$opt_attr);202 let assemblyFormat = "($opt_attr^)? attr-dict";203}204 205// Test that we format optional symbol reference attributes properly.206def FormatOptSymbolRefAttrOp : TEST_Op<"format_opt_symbol_ref_attr_op"> {207 let arguments = (ins OptionalAttr<SymbolRefAttr>:$opt_attr);208 let assemblyFormat = "($opt_attr^)? attr-dict";209}210 211// Test that we elide attributes that are within the syntax.212def FormatAttrDictWithKeywordOp : TEST_Op<"format_attr_dict_w_keyword"> {213 let arguments = (ins I64Attr:$attr, OptionalAttr<I64Attr>:$opt_attr);214 let assemblyFormat = "attr-dict-with-keyword";215}216 217// Test that we don't need to provide types in the format if they are buildable.218def FormatBuildableTypeOp : TEST_Op<"format_buildable_type_op"> {219 let arguments = (ins I64:$buildable);220 let results = (outs I64:$buildable_res);221 let assemblyFormat = "$buildable attr-dict";222}223 224// Test various mixings of region formatting.225class FormatRegionBase<string suffix, string fmt>226 : TEST_Op<"format_region_" # suffix # "_op"> {227 let regions = (region AnyRegion:$region);228 let assemblyFormat = fmt;229}230def FormatRegionAOp : FormatRegionBase<"a", [{231 regions attr-dict232}]>;233def FormatRegionBOp : FormatRegionBase<"b", [{234 $region attr-dict235}]>;236def FormatRegionCOp : FormatRegionBase<"c", [{237 (`region` $region^)? attr-dict238}]>;239class FormatVariadicRegionBase<string suffix, string fmt>240 : TEST_Op<"format_variadic_region_" # suffix # "_op"> {241 let regions = (region VariadicRegion<AnyRegion>:$regions);242 let assemblyFormat = fmt;243}244def FormatVariadicRegionAOp : FormatVariadicRegionBase<"a", [{245 $regions attr-dict246}]>;247def FormatVariadicRegionBOp : FormatVariadicRegionBase<"b", [{248 ($regions^ `found_regions`)? attr-dict249}]>;250class FormatRegionImplicitTerminatorBase<string suffix, string fmt>251 : TEST_Op<"format_implicit_terminator_region_" # suffix # "_op",252 [SingleBlockImplicitTerminator<"TestReturnOp">]> {253 let regions = (region AnyRegion:$region);254 let assemblyFormat = fmt;255}256def FormatFormatRegionImplicitTerminatorAOp257 : FormatRegionImplicitTerminatorBase<"a", [{258 $region attr-dict259}]>;260 261// Test various mixings of result type formatting.262class FormatResultBase<string suffix, string fmt>263 : TEST_Op<"format_result_" # suffix # "_op"> {264 let results = (outs I64:$buildable_res, AnyMemRef:$result);265 let assemblyFormat = fmt;266}267def FormatResultAOp : FormatResultBase<"a", [{268 type($result) attr-dict269}]>;270def FormatResultBOp : FormatResultBase<"b", [{271 type(results) attr-dict272}]>;273def FormatResultCOp : FormatResultBase<"c", [{274 functional-type($buildable_res, $result) attr-dict275}]>;276 277def FormatVariadicResult : TEST_Op<"format_variadic_result"> {278 let results = (outs Variadic<I64>:$result);279 let assemblyFormat = [{ `:` type($result) attr-dict}];280}281 282def FormatMultipleVariadicResults : TEST_Op<"format_multiple_variadic_results",283 [AttrSizedResultSegments]> {284 let results = (outs Variadic<I64>:$result0, Variadic<AnyType>:$result1);285 let assemblyFormat = [{286 `:` `(` type($result0) `)` `,` `(` type($result1) `)` attr-dict287 }];288}289 290// Test various mixings of operand type formatting.291class FormatOperandBase<string suffix, string fmt>292 : TEST_Op<"format_operand_" # suffix # "_op"> {293 let arguments = (ins I64:$buildable, AnyMemRef:$operand);294 let assemblyFormat = fmt;295}296 297def FormatOperandAOp : FormatOperandBase<"a", [{298 operands `:` type(operands) attr-dict299}]>;300def FormatOperandBOp : FormatOperandBase<"b", [{301 operands `:` type($operand) attr-dict302}]>;303def FormatOperandCOp : FormatOperandBase<"c", [{304 $buildable `,` $operand `:` type(operands) attr-dict305}]>;306def FormatOperandDOp : FormatOperandBase<"d", [{307 $buildable `,` $operand `:` type($operand) attr-dict308}]>;309def FormatOperandEOp : FormatOperandBase<"e", [{310 $buildable `,` $operand `:` type($buildable) `,` type($operand) attr-dict311}]>;312 313def FormatSuccessorAOp : TEST_Op<"format_successor_a_op", [Terminator]> {314 let successors = (successor VariadicSuccessor<AnySuccessor>:$targets);315 let assemblyFormat = "$targets attr-dict";316}317 318def FormatVariadicOperand : TEST_Op<"format_variadic_operand"> {319 let arguments = (ins Variadic<I64>:$operand);320 let assemblyFormat = [{ $operand `:` type($operand) attr-dict}];321}322def FormatVariadicOfVariadicOperand323 : TEST_Op<"format_variadic_of_variadic_operand"> {324 let arguments = (ins325 VariadicOfVariadic<I64, "operand_segments">:$operand,326 DenseI32ArrayAttr:$operand_segments327 );328 let assemblyFormat = [{ $operand `:` type($operand) attr-dict}];329}330 331def FormatMultipleVariadicOperands :332 TEST_Op<"format_multiple_variadic_operands", [AttrSizedOperandSegments]> {333 let arguments = (ins Variadic<I64>:$operand0, Variadic<AnyType>:$operand1);334 let assemblyFormat = [{335 ` ` `(` $operand0 `)` `,` `(` $operand1 `:` type($operand1) `)` attr-dict336 }];337}338 339// Test various mixings of optional operand and result type formatting.340class FormatOptionalOperandResultOpBase<string suffix, string fmt>341 : TEST_Op<"format_optional_operand_result_" # suffix # "_op",342 [AttrSizedOperandSegments]> {343 let arguments = (ins Optional<I64>:$optional, Variadic<I64>:$variadic);344 let results = (outs Optional<I64>:$optional_res);345 let assemblyFormat = fmt;346}347 348def FormatOptionalOperandResultAOp : FormatOptionalOperandResultOpBase<"a", [{349 `(` $optional `:` type($optional) `)` `:` type($optional_res)350 (`[` $variadic^ `]`)? attr-dict351}]>;352 353def FormatOptionalOperandResultBOp : FormatOptionalOperandResultOpBase<"b", [{354 (`(` $optional^ `:` type($optional) `)`)? `:` type($optional_res)355 (`[` $variadic^ `]`)? attr-dict356}]>;357 358// Test optional result type formatting.359class FormatOptionalResultOpBase<string suffix, string fmt>360 : TEST_Op<"format_optional_result_" # suffix # "_op",361 [AttrSizedResultSegments]> {362 let results = (outs Optional<I64>:$optional, Variadic<I64>:$variadic);363 let assemblyFormat = fmt;364}365def FormatOptionalResultAOp : FormatOptionalResultOpBase<"a", [{366 (`:` type($optional)^ `->` type($variadic))? attr-dict367}]>;368 369def FormatOptionalResultBOp : FormatOptionalResultOpBase<"b", [{370 (`:` type($optional) `->` type($variadic)^)? attr-dict371}]>;372 373def FormatOptionalResultCOp : FormatOptionalResultOpBase<"c", [{374 (`:` functional-type($optional, $variadic)^)? attr-dict375}]>;376 377def FormatOptionalResultDOp378 : TEST_Op<"format_optional_result_d_op" > {379 let results = (outs Optional<F80>:$optional);380 let assemblyFormat = "(`:` type($optional)^)? attr-dict";381}382 383def FormatTwoVariadicOperandsNoBuildableTypeOp384 : TEST_Op<"format_two_variadic_operands_no_buildable_type_op",385 [AttrSizedOperandSegments]> {386 let arguments = (ins Variadic<AnyType>:$a,387 Variadic<AnyType>:$b);388 let assemblyFormat = [{389 `(` $a `:` type($a) `)` `->` `(` $b `:` type($b) `)` attr-dict390 }];391}392 393def FormatInferVariadicTypeFromNonVariadic394 : TEST_Op<"format_infer_variadic_type_from_non_variadic",395 [SameOperandsAndResultType]> {396 let arguments = (ins Variadic<AnyType>:$args);397 let results = (outs AnyType:$result);398 let assemblyFormat = "operands attr-dict `:` type($result)";399}400 401def FormatOptionalUnitAttr : TEST_Op<"format_optional_unit_attribute"> {402 let arguments = (ins UnitAttr:$is_optional);403 let assemblyFormat = "(`is_optional` $is_optional^)? attr-dict";404}405 406def FormatOptionalUnitAttrNoElide407 : TEST_Op<"format_optional_unit_attribute_no_elide"> {408 let arguments = (ins UnitAttr:$is_optional);409 let assemblyFormat = "($is_optional^)? attr-dict";410}411 412def FormatOptionalUnitProperty : TEST_Op<"format_optional_unit_property"> {413 let arguments = (ins UnitProp:$is_optional);414 let assemblyFormat = "(`is_optional` $is_optional^)? attr-dict";415}416 417def FormatOptionalUnitPropertyNoElide418 : TEST_Op<"format_optional_unit_property_no_elide"> {419 let arguments = (ins UnitProp:$is_optional);420 let assemblyFormat = "($is_optional^)? attr-dict";421}422 423def FormatOptionalEnumAttr : TEST_Op<"format_optional_enum_attr"> {424 let arguments = (ins OptionalAttr<SomeI64Enum>:$attr);425 let assemblyFormat = "($attr^)? attr-dict";426}427 428def FormatOptionalDefaultAttrs : TEST_Op<"format_optional_default_attrs"> {429 let arguments = (ins DefaultValuedStrAttr<StrAttr, "default">:$str,430 DefaultValuedStrAttr<SymbolNameAttr, "default">:$sym,431 DefaultValuedAttr<SomeI64Enum, "SomeI64Enum::case5">:$e);432 let assemblyFormat = "($str^)? ($sym^)? ($e^)? attr-dict";433}434 435def FormatOptionalWithElse : TEST_Op<"format_optional_else"> {436 let arguments = (ins UnitAttr:$isFirstBranchPresent);437 let assemblyFormat = "(`then` $isFirstBranchPresent^):(`else`)? attr-dict";438}439 440def FormatOptionalPropDict : TEST_Op<"format_optional_prop_dict"> {441 let arguments = (ins442 OptionalProp<StringProp>:$a,443 DefaultValuedProp<I32Prop, "1">:$b);444 let assemblyFormat = "prop-dict attr-dict";445}446 447def FormatCompoundAttr : TEST_Op<"format_compound_attr"> {448 let arguments = (ins CompoundAttrA:$compound);449 let assemblyFormat = "$compound attr-dict-with-keyword";450}451 452def FormatNestedAttr : TEST_Op<"format_nested_attr"> {453 let arguments = (ins CompoundAttrNested:$nested);454 let assemblyFormat = "$nested attr-dict-with-keyword";455}456 457def FormatNestedCompoundAttr : TEST_Op<"format_cpmd_nested_attr"> {458 let arguments = (ins CompoundNestedOuter:$nested);459 let assemblyFormat = "`nested` $nested attr-dict-with-keyword";460}461 462def FormatMaybeEmptyType : TEST_Op<"format_maybe_empty_type"> {463 let arguments = (ins TestTypeOptionalValueType:$in);464 let assemblyFormat = "$in `:` type($in) attr-dict";465}466 467def FormatQualifiedCompoundAttr : TEST_Op<"format_qual_cpmd_nested_attr"> {468 let arguments = (ins CompoundNestedOuter:$nested);469 let assemblyFormat = "`nested` qualified($nested) attr-dict-with-keyword";470}471 472def FormatNestedType : TEST_Op<"format_cpmd_nested_type"> {473 let arguments = (ins CompoundNestedOuterType:$nested);474 let assemblyFormat = "$nested `nested` type($nested) attr-dict-with-keyword";475}476 477def FormatQualifiedNestedType : TEST_Op<"format_qual_cpmd_nested_type"> {478 let arguments = (ins CompoundNestedOuterType:$nested);479 let assemblyFormat = "$nested `nested` qualified(type($nested)) attr-dict-with-keyword";480}481 482//===----------------------------------------------------------------------===//483// Custom Directives484//===----------------------------------------------------------------------===//485 486def FormatCustomDirectiveOperands487 : TEST_Op<"format_custom_directive_operands", [AttrSizedOperandSegments]> {488 let arguments = (ins I64:$operand, Optional<I64>:$optOperand,489 Variadic<I64>:$varOperands);490 let assemblyFormat = [{491 custom<CustomDirectiveOperands>(492 $operand, $optOperand, $varOperands493 )494 attr-dict495 }];496}497 498def FormatCustomDirectiveOperandsAndTypes499 : TEST_Op<"format_custom_directive_operands_and_types",500 [AttrSizedOperandSegments]> {501 let arguments = (ins AnyType:$operand, Optional<AnyType>:$optOperand,502 Variadic<AnyType>:$varOperands);503 let assemblyFormat = [{504 custom<CustomDirectiveOperandsAndTypes>(505 $operand, $optOperand, $varOperands,506 type($operand), type($optOperand), type($varOperands)507 )508 attr-dict509 }];510}511 512def FormatCustomDirectiveRegions : TEST_Op<"format_custom_directive_regions"> {513 let regions = (region AnyRegion:$region, VariadicRegion<AnyRegion>:$other_regions);514 let assemblyFormat = [{515 custom<CustomDirectiveRegions>(516 $region, $other_regions517 )518 attr-dict519 }];520}521 522def FormatCustomDirectiveResults523 : TEST_Op<"format_custom_directive_results", [AttrSizedResultSegments]> {524 let results = (outs AnyType:$result, Optional<AnyType>:$optResult,525 Variadic<AnyType>:$varResults);526 let assemblyFormat = [{527 custom<CustomDirectiveResults>(528 type($result), type($optResult), type($varResults)529 )530 attr-dict531 }];532}533 534def FormatCustomDirectiveResultsWithTypeRefs535 : TEST_Op<"format_custom_directive_results_with_type_refs",536 [AttrSizedResultSegments]> {537 let results = (outs AnyType:$result, Optional<AnyType>:$optResult,538 Variadic<AnyType>:$varResults);539 let assemblyFormat = [{540 custom<CustomDirectiveResults>(541 type($result), type($optResult), type($varResults)542 )543 custom<CustomDirectiveWithTypeRefs>(544 ref(type($result)), ref(type($optResult)), ref(type($varResults))545 )546 attr-dict547 }];548}549 550def FormatCustomDirectiveWithOptionalOperandRef551 : TEST_Op<"format_custom_directive_with_optional_operand_ref"> {552 let arguments = (ins Optional<I64>:$optOperand);553 let assemblyFormat = [{554 ($optOperand^)? `:`555 custom<CustomDirectiveOptionalOperandRef>(ref($optOperand))556 attr-dict557 }];558}559 560def FormatCustomDirectiveSuccessors561 : TEST_Op<"format_custom_directive_successors", [Terminator]> {562 let successors = (successor AnySuccessor:$successor,563 VariadicSuccessor<AnySuccessor>:$successors);564 let assemblyFormat = [{565 custom<CustomDirectiveSuccessors>(566 $successor, $successors567 )568 attr-dict569 }];570}571 572def FormatCustomDirectiveAttributes573 : TEST_Op<"format_custom_directive_attributes"> {574 let arguments = (ins I64Attr:$attr, OptionalAttr<I64Attr>:$optAttr);575 let assemblyFormat = [{576 custom<CustomDirectiveAttributes>(577 $attr, $optAttr578 )579 attr-dict580 }];581}582 583def FormatCustomDirectiveSpacing584 : TEST_Op<"format_custom_directive_spacing"> {585 let arguments = (ins StrAttr:$attr1, StrAttr:$attr2);586 let assemblyFormat = [{587 custom<CustomDirectiveSpacing>($attr1)588 custom<CustomDirectiveSpacing>($attr2)589 attr-dict590 }];591}592 593def FormatCustomDirectiveAttrDict594 : TEST_Op<"format_custom_directive_attrdict"> {595 let arguments = (ins I64Attr:$attr, OptionalAttr<I64Attr>:$optAttr);596 let assemblyFormat = [{597 custom<CustomDirectiveAttrDict>( attr-dict )598 }];599}600 601def FormatLiteralFollowingOptionalGroup602 : TEST_Op<"format_literal_following_optional_group"> {603 let arguments = (ins TypeAttr:$type, OptionalAttr<AnyAttr>:$value);604 let assemblyFormat = "(`(` $value^ `)`)? `:` $type attr-dict";605}606 607//===----------------------------------------------------------------------===//608// AllTypesMatch type inference609//===----------------------------------------------------------------------===//610 611def FormatAllTypesMatchVarOp : TEST_Op<"format_all_types_match_var", [612 AllTypesMatch<["value1", "value2", "result"]>613 ]> {614 let arguments = (ins AnyType:$value1, AnyType:$value2);615 let results = (outs AnyType:$result);616 let assemblyFormat = "attr-dict $value1 `,` $value2 `:` type($value1)";617}618 619def FormatAllTypesMatchAttrOp : TEST_Op<"format_all_types_match_attr", [620 AllTypesMatch<["value1", "value2", "result"]>621 ]> {622 let arguments = (ins TypedAttrInterface:$value1, AnyType:$value2);623 let results = (outs AnyType:$result);624 let assemblyFormat = "attr-dict $value1 `,` $value2";625}626 627//===----------------------------------------------------------------------===//628// TypesMatchWith type inference629//===----------------------------------------------------------------------===//630 631def FormatTypesMatchVarOp : TEST_Op<"format_types_match_var", [632 TypesMatchWith<"result type matches operand", "value", "result", "$_self">633 ]> {634 let arguments = (ins AnyType:$value);635 let results = (outs AnyType:$result);636 let assemblyFormat = "attr-dict $value `:` type($value)";637}638 639def FormatTypesMatchVariadicOp : TEST_Op<"format_types_match_variadic", [640 RangedTypesMatchWith<"result type matches operand", "value", "result",641 "llvm::make_range($_self.begin(), $_self.end())">642 ]> {643 let arguments = (ins Variadic<AnyType>:$value);644 let results = (outs Variadic<AnyType>:$result);645 let assemblyFormat = "attr-dict $value `:` type($value)";646}647 648def FormatTypesMatchAttrOp : TEST_Op<"format_types_match_attr", [649 TypesMatchWith<"result type matches constant", "value", "result", "$_self">650 ]> {651 let arguments = (ins TypedAttrInterface:$value);652 let results = (outs AnyType:$result);653 let assemblyFormat = "attr-dict $value";654}655 656def FormatTypesMatchContextOp : TEST_Op<"format_types_match_context", [657 TypesMatchWith<"tuple result type matches operand type", "value", "result",658 "::mlir::TupleType::get($_ctxt, $_self)">659 ]> {660 let arguments = (ins AnyType:$value);661 let results = (outs AnyType:$result);662 let assemblyFormat = "attr-dict $value `:` type($value)";663}664 665//===----------------------------------------------------------------------===//666// InferTypeOpInterface type inference in assembly format667//===----------------------------------------------------------------------===//668 669def FormatInferTypeOp : TEST_Op<"format_infer_type", [InferTypeOpInterface]> {670 let results = (outs AnyType);671 let assemblyFormat = "attr-dict";672 673 let extraClassDeclaration = [{674 static ::llvm::LogicalResult inferReturnTypes(::mlir::MLIRContext *context,675 ::std::optional<::mlir::Location> location, ::mlir::ValueRange operands,676 ::mlir::DictionaryAttr attributes, mlir::OpaqueProperties properties, ::mlir::RegionRange regions,677 ::llvm::SmallVectorImpl<::mlir::Type> &inferredReturnTypes) {678 inferredReturnTypes.assign({::mlir::IntegerType::get(context, 16)});679 return ::mlir::success();680 }681 }];682}683 684// Check that formatget supports DeclareOpInterfaceMethods.685def FormatInferType2Op : TEST_Op<"format_infer_type2", [DeclareOpInterfaceMethods<InferTypeOpInterface>]> {686 let results = (outs AnyType);687 let assemblyFormat = "attr-dict";688}689 690// Base class for testing mixing allOperandTypes, allOperands, and691// inferResultTypes.692class FormatInferAllTypesBaseOp<string mnemonic, list<Trait> traits = []>693 : TEST_Op<mnemonic, [InferTypeOpInterface] # traits> {694 let arguments = (ins Variadic<AnyType>:$args);695 let results = (outs Variadic<AnyType>:$outs);696 let extraClassDeclaration = [{697 static ::llvm::LogicalResult inferReturnTypes(::mlir::MLIRContext *context,698 ::std::optional<::mlir::Location> location, ::mlir::ValueRange operands,699 ::mlir::DictionaryAttr attributes, mlir::OpaqueProperties properties, ::mlir::RegionRange regions,700 ::llvm::SmallVectorImpl<::mlir::Type> &inferredReturnTypes) {701 ::mlir::TypeRange operandTypes = operands.getTypes();702 inferredReturnTypes.assign(operandTypes.begin(), operandTypes.end());703 return ::mlir::success();704 }705 }];706}707 708// Test inferReturnTypes is called when allOperandTypes and allOperands is true.709def FormatInferTypeAllOperandsAndTypesOp710 : FormatInferAllTypesBaseOp<"format_infer_type_all_operands_and_types"> {711 let assemblyFormat = "`(` operands `)` attr-dict `:` type(operands)";712}713 714// Test inferReturnTypes is called when allOperandTypes is true and there is one715// ODS operand.716def FormatInferTypeAllOperandsAndTypesOneOperandOp717 : FormatInferAllTypesBaseOp<"format_infer_type_all_types_one_operand"> {718 let assemblyFormat = "`(` $args `)` attr-dict `:` type(operands)";719}720 721// Test inferReturnTypes is called when allOperandTypes is true and there are722// more than one ODS operands.723def FormatInferTypeAllOperandsAndTypesTwoOperandsOp724 : FormatInferAllTypesBaseOp<"format_infer_type_all_types_two_operands",725 [SameVariadicOperandSize]> {726 let arguments = (ins Variadic<AnyType>:$args0, Variadic<AnyType>:$args1);727 let assemblyFormat = "`(` $args0 `)` `(` $args1 `)` attr-dict `:` type(operands)";728}729 730// Test inferReturnTypes is called when allOperands is true and operand types731// are separately specified.732def FormatInferTypeAllTypesOp733 : FormatInferAllTypesBaseOp<"format_infer_type_all_types"> {734 let assemblyFormat = "`(` operands `)` attr-dict `:` type($args)";735}736 737// Test inferReturnTypes coupled with regions.738def FormatInferTypeRegionsOp739 : TEST_Op<"format_infer_type_regions", [InferTypeOpInterface]> {740 let results = (outs Variadic<AnyType>:$outs);741 let regions = (region AnyRegion:$region);742 let assemblyFormat = "$region attr-dict";743 let extraClassDeclaration = [{744 static ::llvm::LogicalResult inferReturnTypes(::mlir::MLIRContext *context,745 ::std::optional<::mlir::Location> location, ::mlir::ValueRange operands,746 ::mlir::DictionaryAttr attributes, mlir::OpaqueProperties properties, ::mlir::RegionRange regions,747 ::llvm::SmallVectorImpl<::mlir::Type> &inferredReturnTypes) {748 if (regions.empty())749 return ::mlir::failure();750 auto types = regions.front()->getArgumentTypes();751 inferredReturnTypes.assign(types.begin(), types.end());752 return ::mlir::success();753 }754 }];755}756 757// Test inferReturnTypes coupled with variadic operands (operandSegmentSizes).758def FormatInferTypeVariadicOperandsOp759 : TEST_Op<"format_infer_type_variadic_operands",760 [InferTypeOpInterface, AttrSizedOperandSegments]> {761 let arguments = (ins Variadic<I32>:$a, Variadic<I64>:$b);762 let results = (outs Variadic<AnyType>:$outs);763 let assemblyFormat = "`(` $a `:` type($a) `)` `(` $b `:` type($b) `)` attr-dict";764 let extraClassDeclaration = [{765 static ::llvm::LogicalResult inferReturnTypes(::mlir::MLIRContext *context,766 ::std::optional<::mlir::Location> location, ::mlir::ValueRange operands,767 ::mlir::DictionaryAttr attributes, mlir::OpaqueProperties properties, ::mlir::RegionRange regions,768 ::llvm::SmallVectorImpl<::mlir::Type> &inferredReturnTypes) {769 FormatInferTypeVariadicOperandsOpAdaptor adaptor(770 operands, attributes, *properties.as<Properties *>(), {});771 auto aTypes = adaptor.getA().getTypes();772 auto bTypes = adaptor.getB().getTypes();773 inferredReturnTypes.append(aTypes.begin(), aTypes.end());774 inferredReturnTypes.append(bTypes.begin(), bTypes.end());775 return ::mlir::success();776 }777 }];778}779 780def VariadicArgsTypesSplit : TEST_Op<"variadic_args_types_split"> {781 let arguments = (ins StrAttr:$str, Variadic<AnyType>:$args);782 let assemblyFormat = "$str (`,` $args^)? attr-dict (`:` type($args)^)?";783}784 785#endif // TEST_OPS_SYNTAX786