133 lines · plain
1// RUN: mlir-tblgen -gen-op-decls -I %S/../../include %s | FileCheck %s --check-prefix=DECL2// RUN: mlir-tblgen -gen-op-defs -I %S/../../include %s | FileCheck %s --check-prefix=DEFS3 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 properties21// ---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 = (ins30 BoolProp:$flag,31 StringProp:$string,32 ArrayProp<StringProp>:$strings,33 DefaultValuedProp<I32Prop, "0">:$default_int,34 OptionalProp<I64Prop>:$optional,35 DefaultI64Array:$value36 );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 = (ins42 OptionalProp<I64Prop>:$mustSpecify,43 I64Prop:$required,44 OptionalProp<StringProp>:$canOmit,45 DefaultValuedProp<I64Prop, "-1">:$canOmit246 );47}48 49/// Check that the ambiguous attribute protection correctly stops optional properties50/// from getting default argument values in builders.51def OpWithOptionalPropsAndAttrs :52 NS_Op<"with_some_optional_props_and_atts"> {53 let arguments = (ins54 OptionalProp<BoolProp>:$mustSpecify,55 OptionalAttr<BoolAttr>:$ambiguous,56 OptionalAttr<I32Attr>:$canOmit,57 OptionalProp<I32Prop>:$canOmitProp58 );59}60 61// DECL: void setAttrAttr(::mlir::Attribute attr)62// DECL-NEXT: getProperties().attr = attr63// DECL: void setOptionalAttr(::mlir::Attribute attr)64// DECL-NEXT: getProperties().optional = attr65 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 expect80// DECL-LABEL: class OpWithPropsGenericAdaptorBase81// DECL: using flagTy = bool;82// DECL-NEXT: flagTy flag;83// DECL-NEXT: bool getFlag()84// DECL-NEXT: propStorage = this->flag85// 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: valueTy value = ::llvm::SmallVector<int64_t>{};98// DECL: ::llvm::ArrayRef<int64_t> getValue()99// DECL: return ::llvm::ArrayRef<int64_t>{propStorage}100// DECL: void setValue(::llvm::ArrayRef<int64_t> propValue)101// DECL: propStorage.assign102// 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> value = ::llvm::ArrayRef<int64_t>{});115 116// DEFS-LABEL: OpWithProps::computePropertiesHash117// DEFS: hash_value_118// DEFS: using ::llvm::hash_value;119// DEFS-NEXT: return hash_value(::llvm::ArrayRef<int64_t>{propStorage})120// DEFS: hash_value(prop.optional)121// DEFS: hash_value_(prop.value)122 123// -----124 125// DECL-LABEL: class OpWithSomeOptionalProperties :126// DECL: static void build(127// DECL-SAME: ::mlir::OpBuilder &odsBuilder,128// DECL-SAME: ::mlir::OperationState &odsState,129// DECL-SAME: /*optional*/std::optional<int64_t> mustSpecify,130// DECL-SAME: int64_t required,131// DECL-SAME: /*optional*/std::optional<::llvm::StringRef> canOmit = std::nullopt,132// DECL-SAME: /*optional*/int64_t canOmit2 = -1);133