xref: /llvm-project/mlir/test/mlir-tblgen/op-properties.td (revision 378e1793379c9c63a4265ecf55c47308410ed25d)
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
3
4include "mlir/IR/AttrTypeBase.td"
5include "mlir/IR/EnumAttr.td"
6include "mlir/IR/OpBase.td"
7include "mlir/IR/Properties.td"
8
9def Test_Dialect : Dialect {
10  let name = "test";
11  let cppNamespace = "foobar";
12}
13class NS_Op<string mnemonic, list<Trait> traits = []> :
14    Op<Test_Dialect, mnemonic, traits>;
15
16def OpWithAttr : NS_Op<"op_with_attr">{
17  let arguments = (ins AnyAttr:$attr, OptionalAttr<AnyAttr>:$optional);
18}
19
20// Test required and optional properties
21// ---
22
23def DefaultI64Array : IntArrayProp<I64Prop> {
24  let defaultValue = "::llvm::ArrayRef<int64_t>{}";
25  let storageTypeValueOverride = "::llvm::SmallVector<int64_t>{}";
26}
27
28def OpWithProps : NS_Op<"op_with_props"> {
29  let arguments = (ins
30    BoolProp:$flag,
31    StringProp:$string,
32    ArrayProp<StringProp>:$strings,
33    DefaultValuedProp<I32Prop, "0">:$default_int,
34    OptionalProp<I64Prop>:$optional,
35    DefaultI64Array:$intArray
36  );
37}
38
39/// Check that optional arguments to builders only go at the end.
40def OpWithSomeOptionalProperties : NS_Op<"op_with_some_optional_props"> {
41  let arguments = (ins
42    OptionalProp<I64Prop>:$mustSpecify,
43    I64Prop:$required,
44    OptionalProp<StringProp>:$canOmit,
45    DefaultValuedProp<I64Prop, "-1">:$canOmit2
46  );
47}
48
49/// Check that the ambiguous attribute protection correctly stops optional properties
50/// from getting default argument values in builders.
51def OpWithOptionalPropsAndAttrs :
52    NS_Op<"with_some_optional_props_and_atts"> {
53  let arguments = (ins
54    OptionalProp<BoolProp>:$mustSpecify,
55    OptionalAttr<BoolAttr>:$ambiguous,
56    OptionalAttr<I32Attr>:$canOmit,
57    OptionalProp<I32Prop>:$canOmitProp
58  );
59}
60
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
65
66// -----
67
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);
76
77// -----
78
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)
105
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>{});
115
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)
121
122// -----
123
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);
132