1 // RUN: mlir-tblgen -gen-op-decls -I %S/../../include %s | FileCheck %s --check-prefix=DECL
2 // RUN: mlir-tblgen -gen-op-defs -I %S/../../include %s | FileCheck %s --check-prefix=DEFS
4 include "mlir/IR/AttrTypeBase.td"
5 include "mlir/IR/EnumAttr.td"
6 include "mlir/IR/OpBase.td"
7 include "mlir/IR/Properties.td"
9 def Test_Dialect : Dialect {
11 let cppNamespace = "foobar";
13 class NS_Op<string mnemonic, list<Trait> traits = []> :
14 Op<Test_Dialect, mnemonic, traits>;
16 def OpWithAttr : NS_Op<"op_with_attr">{
17 let arguments = (ins AnyAttr:$attr, OptionalAttr<AnyAttr>:$optional);
20 // Test required and optional properties
23 def DefaultI64Array : IntArrayProperty<"int64_t"> {
24 let defaultValue = "::llvm::ArrayRef<int64_t>{}";
25 let storageTypeValueOverride = "::llvm::SmallVector<int64_t>{}";
28 def OpWithProps : NS_Op<"op_with_props"> {
31 StringProperty:$string,
32 ArrayProperty<StringProperty>:$strings,
33 DefaultValuedProperty<I32Property, "0">:$default_int,
34 OptionalProperty<I64Property>:$optional,
35 DefaultI64Array:$intArray
39 /// Check that optional arguments to builders only go at the end.
40 def OpWithSomeOptionalProperties : NS_Op<"op_with_some_optional_props"> {
42 OptionalProperty<I64Property>:$mustSpecify,
43 I64Property:$required,
44 OptionalProperty<StringProperty>:$canOmit,
45 DefaultValuedProperty<I64Property, "-1">:$canOmit2
49 /// Check that the ambiguous attribute protection correctly stops optional properties
50 /// from getting default argument values in builders.
51 def OpWithOptionalPropsAndAttrs :
52 NS_Op<"with_some_optional_props_and_atts"> {
54 OptionalProperty<BoolProperty>:$mustSpecify,
55 OptionalAttr<BoolAttr>:$ambiguous,
56 OptionalAttr<I32Attr>:$canOmit,
57 OptionalProperty<I32Property>:$canOmitProp
61 // DECL: void setAttrAttr(::mlir::Attribute attr)
62 // DECL-NEXT: getProperties().attr = attr
63 // DECL: void setOptionalAttr(::mlir::Attribute attr)
64 // DECL-NEXT: getProperties().optional = attr
68 // DECL-LABEL: class OpWithOptionalPropsAndAttrs :
69 // DECL: static void build(
70 // DECL-SAME: ::mlir::OpBuilder &odsBuilder,
71 // DECL-SAME: ::mlir::OperationState &odsState,
72 // DECL-SAME: /*optional*/std::optional<bool> mustSpecify,
73 // DECL-SAME: /*optional*/::mlir::BoolAttr ambiguous,
74 // DECL-SAME: /*optional*/::mlir::IntegerAttr canOmit,
75 // DECL-SAME: /*optional*/std::optional<int32_t> canOmitProp = std::nullopt);
79 // COM: Ensure the struct is set up how we expect
80 // DECL-LABEL: class OpWithPropsGenericAdaptorBase
81 // DECL: using flagTy = bool;
82 // DECL-NEXT: flagTy flag;
83 // DECL-NEXT: bool getFlag()
84 // DECL-NEXT: propStorage = this->flag
85 // DECL-NEXT: return propStorage;
86 // DECL: void setFlag(bool propValue)
87 // DECL-NEXT: propStorage = this->flag;
88 // DECL-NEXT: propStorage = propValue;
89 // DECL: using stringTy = std::string;
90 // DECL: llvm::StringRef getString()
91 // DECL: auto &propStorage = this->string;
92 // DECL-NEXT: return ::llvm::StringRef{propStorage};
93 // DECL: using stringsTy = ::llvm::SmallVector<std::string>
94 // DECL: ::llvm::ArrayRef<std::string> getStrings()
95 // DECL: using default_intTy = int32_t;
96 // DECL: default_intTy default_int = 0;
97 // DECL: intArrayTy intArray = ::llvm::SmallVector<int64_t>{};
98 // DECL: ::llvm::ArrayRef<int64_t> getIntArray()
99 // DECL: return ::llvm::ArrayRef<int64_t>{propStorage}
100 // DECL: void setIntArray(::llvm::ArrayRef<int64_t> propValue)
101 // DECL: propStorage.assign
102 // DECL-LABEL: class OpWithProps :
103 // DECL: setString(::llvm::StringRef newString)
104 // DECL-NEXT: getProperties().setString(newString)
106 // DECL: static void build(
107 // DECL-SAME: ::mlir::OpBuilder &odsBuilder,
108 // DECL-SAME: ::mlir::OperationState &odsState,
109 // DECL-SAME: bool flag,
110 // DECL-SAME: ::llvm::StringRef string,
111 // DECL-SAME: ::llvm::ArrayRef<std::string> strings,
112 // DECL-SAME: /*optional*/int32_t default_int = 0,
113 // DECL-SAME: /*optional*/std::optional<int64_t> optional = std::nullopt,
114 // DECL-SAME: /*optional*/::llvm::ArrayRef<int64_t> intArray = ::llvm::ArrayRef<int64_t>{});
116 // DEFS-LABEL: OpWithProps::computePropertiesHash
117 // DEFS: hash_intArray
118 // DEFS-NEXT: return ::llvm::hash_value(::llvm::ArrayRef<int64_t>{propStorage})
119 // DEFS: ::llvm::hash_value(prop.optional)
120 // DEFS: hash_intArray(prop.intArray)
124 // DECL-LABEL: class OpWithSomeOptionalProperties :
125 // DECL: static void build(
126 // DECL-SAME: ::mlir::OpBuilder &odsBuilder,
127 // DECL-SAME: ::mlir::OperationState &odsState,
128 // DECL-SAME: /*optional*/std::optional<int64_t> mustSpecify,
129 // DECL-SAME: int64_t required,
130 // DECL-SAME: /*optional*/std::optional<::llvm::StringRef> canOmit = std::nullopt,
131 // DECL-SAME: /*optional*/int64_t canOmit2 = -1);