1 // RUN: mlir-tblgen -gen-op-defs -I %S/../../include %s | FileCheck %s
3 include "mlir/IR/OpBase.td"
5 def TestDialect : Dialect {
7 let usePropertiesForAttributes = 0;
9 class TestFormat_Op<string fmt, list<Trait> traits = []>
10 : Op<TestDialect, "format_op", traits> {
11 let assemblyFormat = fmt;
14 //===----------------------------------------------------------------------===//
16 //===----------------------------------------------------------------------===//
18 //===----------------------------------------------------------------------===//
21 // CHECK-LABEL: CustomStringLiteralA::parse
22 // CHECK: parseFoo({{.*}}, parser.getBuilder().getI1Type())
23 // CHECK-LABEL: CustomStringLiteralA::print
24 // CHECK: printFoo({{.*}}, ::mlir::Builder(getContext()).getI1Type())
25 def CustomStringLiteralA : TestFormat_Op<[{
26 custom<Foo>("$_builder.getI1Type()") attr-dict
29 // CHECK-LABEL: CustomStringLiteralB::parse
30 // CHECK: parseFoo({{.*}}, IndexType::get(parser.getContext()))
31 // CHECK-LABEL: CustomStringLiteralB::print
32 // CHECK: printFoo({{.*}}, IndexType::get(getContext()))
33 def CustomStringLiteralB : TestFormat_Op<[{
34 custom<Foo>("IndexType::get($_ctxt)") attr-dict
37 // CHECK-LABEL: CustomStringLiteralC::parse
38 // CHECK: parseFoo({{.*}}, parser.getBuilder().getStringAttr("foo"))
39 // CHECK-LABEL: CustomStringLiteralC::print
40 // CHECK: printFoo({{.*}}, ::mlir::Builder(getContext()).getStringAttr("foo"))
41 def CustomStringLiteralC : TestFormat_Op<[{
42 custom<Foo>("$_builder.getStringAttr(\"foo\")") attr-dict
45 // CHECK-LABEL: CustomStringLiteralD::parse
46 // CHECK: parseFoo({{.*}}, result)
47 // CHECK-LABEL: CustomStringLiteralD::print
48 // CHECK: printFoo({{.*}}, getProperties())
49 def CustomStringLiteralD : TestFormat_Op<[{
50 custom<Foo>(prop-dict) attr-dict
53 //===----------------------------------------------------------------------===//
55 //===----------------------------------------------------------------------===//
57 // CHECK-LABEL: OptionalGroupA::parse
58 // CHECK: if (::mlir::succeeded(parser.parseOptionalQuestion())
60 // CHECK: parser.parseOptionalOperand
61 // CHECK-LABEL: OptionalGroupA::print
62 // CHECK: if (!getA())
63 // CHECK-NEXT: odsPrinter << ' ' << "?";
65 // CHECK: odsPrinter << value;
66 def OptionalGroupA : TestFormat_Op<[{
67 (`?`) : ($a^)? attr-dict
68 }]>, Arguments<(ins Optional<I1>:$a)>;
70 // CHECK-LABEL: OptionalGroupB::parse
71 // CHECK: if (::mlir::succeeded(parser.parseOptionalKeyword("foo")))
73 // CHECK-NEXT: result.addAttribute("a", parser.getBuilder().getUnitAttr())
74 // CHECK: parser.parseKeyword("bar")
75 // CHECK-LABEL: OptionalGroupB::print
76 // CHECK: if (!(getAAttr() && getAAttr() != ((false) ? ::mlir::OpBuilder((*this)->getContext()).getUnitAttr() : nullptr)))
77 // CHECK-NEXT: odsPrinter << ' ' << "foo"
79 // CHECK-NEXT: odsPrinter << ' ' << "bar"
80 def OptionalGroupB : TestFormat_Op<[{
81 (`foo`) : (`bar` $a^)? attr-dict
82 }]>, Arguments<(ins UnitAttr:$a)>;
84 // Optional group anchored on a default-valued attribute:
85 // CHECK-LABEL: OptionalGroupC::parse
87 // CHECK: if (getAAttr() != ::mlir::OpBuilder((*this)->getContext()).getStringAttr("default")) {
88 // CHECK-NEXT: odsPrinter << ' ';
89 // CHECK-NEXT: odsPrinter.printAttributeWithoutType(getAAttr());
91 def OptionalGroupC : TestFormat_Op<[{
93 }]>, Arguments<(ins DefaultValuedStrAttr<StrAttr, "default">:$a)>;
95 // CHECK-LABEL: OptionalGroupD::parse
96 // CHECK: if (auto optResult = [&]() -> ::mlir::OptionalParseResult {
97 // CHECK: auto odsResult = parseCustom(parser, aOperand, bOperand);
98 // CHECK: if (!odsResult.has_value()) return {};
99 // CHECK: if (::mlir::failed(*odsResult)) return ::mlir::failure();
100 // CHECK: return ::mlir::success();
101 // CHECK: }(); optResult.has_value() && ::mlir::failed(*optResult)) {
102 // CHECK: return ::mlir::failure();
103 // CHECK: } else if (optResult.has_value()) {
105 // CHECK-LABEL: OptionalGroupD::print
106 // CHECK-NEXT: if (((getA()) || (getB()))) {
107 // CHECK-NEXT: odsPrinter << ' ';
108 // CHECK-NEXT: printCustom
109 def OptionalGroupD : TestFormat_Op<[{
110 (custom<Custom>($a, $b)^)? attr-dict
111 }], [AttrSizedOperandSegments]>, Arguments<(ins Optional<I64>:$a, Optional<I64>:$b)>;