556 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=DEF3// RUN: mlir-tblgen -print-records -I %S/../../include %s | FileCheck %s --check-prefix=RECORD4 5include "mlir/IR/AttrTypeBase.td"6include "mlir/IR/EnumAttr.td"7include "mlir/IR/OpBase.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 SomeAttr : Attr<CPred<"some-condition">, "some attribute kind"> {17 let storageType = "some-attr-kind";18 let returnType = "some-return-type";19 let convertFromStorage = "$_self.some-convert-from-storage()";20 let constBuilderCall = "some-const-builder-call($_builder, $0)";21}22 23def SomeAttrDef : AttrDef<Test_Dialect, "SomeAttr"> {24 let attrName = "test.some_attr";25}26 27 28// Test required, optional, default-valued attributes29// ---30 31def AOp : NS_Op<"a_op", []> {32 let arguments = (ins33 SomeAttr:$aAttr,34 DefaultValuedAttr<SomeAttr, "4.2">:$bAttr,35 OptionalAttr<SomeAttr>:$cAttr,36 DefaultValuedOptionalAttr<SomeAttr, "4.2">:$dAttr37 );38}39 40// DECL-LABEL: AOp declarations41 42// Test attribute name methods43// ---44 45// DECL: static ::llvm::ArrayRef<::llvm::StringRef> getAttributeNames()46// DECL-NEXT: static ::llvm::StringRef attrNames[] =47// DECL-SAME: {::llvm::StringRef("aAttr"), ::llvm::StringRef("bAttr"), ::llvm::StringRef("cAttr"), ::llvm::StringRef("dAttr")};48// DECL-NEXT: return ::llvm::ArrayRef(attrNames);49 50// DECL: ::mlir::StringAttr getAAttrAttrName()51// DECL-NEXT: return getAttributeNameForIndex(0);52// DECL: ::mlir::StringAttr getAAttrAttrName(::mlir::OperationName name)53// DECL-NEXT: return getAttributeNameForIndex(name, 0);54 55// DECL: ::mlir::StringAttr getBAttrAttrName()56// DECL-NEXT: return getAttributeNameForIndex(1);57// DECL: ::mlir::StringAttr getBAttrAttrName(::mlir::OperationName name)58// DECL-NEXT: return getAttributeNameForIndex(name, 1);59 60// DECL: ::mlir::StringAttr getCAttrAttrName()61// DECL-NEXT: return getAttributeNameForIndex(2);62// DECL: ::mlir::StringAttr getCAttrAttrName(::mlir::OperationName name)63// DECL-NEXT: return getAttributeNameForIndex(name, 2);64 65// DEF-LABEL: AOp definitions66 67// Test verify method68// ---69 70// DEF: ::llvm::LogicalResult AOpAdaptor::verify71// DEF-NEXT: auto tblgen_aAttr = getProperties().aAttr; (void)tblgen_aAttr;72// DEF-NEXT: if (!tblgen_aAttr) return emitError(loc, "'test.a_op' op requires attribute 'aAttr'");73// DEF-NEXT: auto tblgen_bAttr = getProperties().bAttr; (void)tblgen_bAttr;74// DEF-NEXT: auto tblgen_cAttr = getProperties().cAttr; (void)tblgen_cAttr;75// DEF-NEXT: auto tblgen_dAttr = getProperties().dAttr; (void)tblgen_dAttr;76 77// DEF: if (tblgen_aAttr && !((some-condition)))78// DEF-NEXT: return emitError(loc, "'test.a_op' op attribute 'aAttr' failed to satisfy constraint: some attribute kind");79// DEF: if (tblgen_bAttr && !((some-condition)))80// DEF-NEXT: return emitError(loc, "'test.a_op' op attribute 'bAttr' failed to satisfy constraint: some attribute kind");81// DEF: if (tblgen_cAttr && !((some-condition)))82// DEF-NEXT: return emitError(loc, "'test.a_op' op attribute 'cAttr' failed to satisfy constraint: some attribute kind");83// DEF: if (tblgen_dAttr && !((some-condition)))84// DEF-NEXT: return emitError(loc, "'test.a_op' op attribute 'dAttr' failed to satisfy constraint: some attribute kind");85 86// Test getter methods87// ---88 89// DECL: some-attr-kind getAAttrAttr()90// DECL-NEXT: ::llvm::cast<some-attr-kind>(getProperties().aAttr)91// DEF: some-return-type AOp::getAAttr() {92// DEF-NEXT: auto attr = getAAttrAttr()93// DEF-NEXT: return attr.some-convert-from-storage();94 95// DECL: some-attr-kind getBAttrAttr()96// DECL-NEXT: ::llvm::dyn_cast_or_null<some-attr-kind>(getProperties().bAttr)97// DEF: some-return-type AOp::getBAttr() {98// DEF-NEXT: auto attr = getBAttrAttr();99// DEF-NEXT: return attr.some-convert-from-storage();100 101// DECL: some-attr-kind getCAttrAttr()102// DECL-NEXT: ::llvm::dyn_cast_or_null<some-attr-kind>(getProperties().cAttr)103// DEF: ::std::optional<some-return-type> AOp::getCAttr() {104// DEF-NEXT: auto attr = getCAttrAttr()105// DEF-NEXT: return attr ? ::std::optional<some-return-type>(attr.some-convert-from-storage()) : (::std::nullopt);106 107// DECL: some-attr-kind getDAttrAttr()108// DECL-NEXT: ::llvm::dyn_cast_or_null<some-attr-kind>(getProperties().dAttr)109// DEF: some-return-type AOp::getDAttr() {110// DEF-NEXT: auto attr = getDAttrAttr();111// DEF-NEXT: if (!attr)112// DEF-NEXT: return some-const-builder-call(::mlir::Builder((*this)->getContext()), 4.2).some-convert-from-storage();113// DEF-NEXT: return attr.some-convert-from-storage();114 115// Test setter methods116// ---117 118// DECL: void setAAttrAttr(some-attr-kind attr) {119// DECL-NEXT: getProperties().aAttr = attr;120// DEF: void AOp::setAAttr(some-return-type attrValue) {121// DEF-NEXT: getProperties().aAttr = some-const-builder-call(::mlir::Builder((*this)->getContext()), attrValue);122// DECL: void setBAttrAttr(some-attr-kind attr) {123// DECL-NEXT: getProperties().bAttr = attr;124// DEF: void AOp::setBAttr(some-return-type attrValue) {125// DEF-NEXT: getProperties().bAttr = some-const-builder-call(::mlir::Builder((*this)->getContext()), attrValue);126// DECL: void setCAttrAttr(some-attr-kind attr) {127// DECL-NEXT: getProperties().cAttr = attr;128// DEF: void AOp::setCAttr(::std::optional<some-return-type> attrValue) {129// DEF-NEXT: auto &odsProp = getProperties().cAttr;130// DEF-NEXT: if (attrValue)131// DEF-NEXT: odsProp = some-const-builder-call(::mlir::Builder((*this)->getContext()), *attrValue);132// DEF-NEXT: else133// DEF-NEXT: odsProp = nullptr;134 135// Test remove methods136// ---137 138// DECL: ::mlir::Attribute removeCAttrAttr() {139// DECL-NEXT: auto attr = getProperties().cAttr;140// DECL-NEXT: getProperties().cAttr = {};141// DECL-NEXT: return attr;142 143// Test build methods144// ---145 146// DEF: void AOp::build(147// DEF: odsState.getOrAddProperties<Properties>().aAttr = aAttr;148// DEF: odsState.getOrAddProperties<Properties>().bAttr = bAttr;149// DEF: if (cAttr) {150// DEF-NEXT: odsState.getOrAddProperties<Properties>().cAttr = cAttr;151 152// DEF: odsState.getOrAddProperties<Properties>().aAttr = some-const-builder-call(odsBuilder, aAttr);153// DEF-NEXT: odsState.getOrAddProperties<Properties>().bAttr = some-const-builder-call(odsBuilder, bAttr);154// DEF-NEXT: if (cAttr) {155// DEF-NEXT: odsState.getOrAddProperties<Properties>().cAttr = cAttr;156// DEF-NEXT: }157// DEF-NOT: if (dAttr)158// DEF: odsState.getOrAddProperties<Properties>().dAttr = some-const-builder-call(odsBuilder, dAttr);159 160// DEF: void AOp::build(161// DEF: some-return-type aAttr, some-return-type bAttr, /*optional*/some-attr-kind cAttr162// DEF: odsState.getOrAddProperties<Properties>().aAttr = some-const-builder-call(odsBuilder, aAttr);163 164// DEF: void AOp::build(165// DEF: ::llvm::ArrayRef<::mlir::NamedAttribute> attributes166// DEF: odsState.addAttributes(attributes);167 168// DEF: void AOp::build(169// DEF-SAME: const Properties &properties,170// DEF-SAME: ::llvm::ArrayRef<::mlir::NamedAttribute> discardableAttributes171// DEF: odsState.useProperties(const_cast<Properties&>(properties));172// DEF: odsState.addAttributes(discardableAttributes);173 174// DEF: void AOp::populateDefaultProperties175 176// Test the above but with prefix.177 178def Test2_Dialect : Dialect {179 let name = "test2";180 let cppNamespace = "foobar2";181}182def AgetOp : Op<Test2_Dialect, "a_get_op", []> {183 let arguments = (ins184 SomeAttr:$aAttr,185 DefaultValuedOptionalAttr<SomeAttr, "4.2">:$bAttr,186 OptionalAttr<SomeAttr>:$cAttr187 );188}189 190// DECL-LABEL: AgetOp declarations191 192// Test attribute name methods193// ---194 195// DECL: static ::llvm::ArrayRef<::llvm::StringRef> getAttributeNames()196// DECL-NEXT: static ::llvm::StringRef attrNames[] =197// DECL-SAME: {::llvm::StringRef("aAttr"), ::llvm::StringRef("bAttr"), ::llvm::StringRef("cAttr")};198// DECL-NEXT: return ::llvm::ArrayRef(attrNames);199 200// DECL: ::mlir::StringAttr getAAttrAttrName()201// DECL-NEXT: return getAttributeNameForIndex(0);202// DECL: ::mlir::StringAttr getAAttrAttrName(::mlir::OperationName name)203// DECL-NEXT: return getAttributeNameForIndex(name, 0);204 205// DECL: ::mlir::StringAttr getBAttrAttrName()206// DECL-NEXT: return getAttributeNameForIndex(1);207// DECL: ::mlir::StringAttr getBAttrAttrName(::mlir::OperationName name)208// DECL-NEXT: return getAttributeNameForIndex(name, 1);209 210// DECL: ::mlir::StringAttr getCAttrAttrName()211// DECL-NEXT: return getAttributeNameForIndex(2);212// DECL: ::mlir::StringAttr getCAttrAttrName(::mlir::OperationName name)213// DECL-NEXT: return getAttributeNameForIndex(name, 2);214 215// DEF-LABEL: AgetOp definitions216 217// Test verify method218// ---219 220// DEF: ::llvm::LogicalResult AgetOpAdaptor::verify221// DEF: auto tblgen_aAttr = getProperties().aAttr; (void)tblgen_aAttr;222// DEF: if (!tblgen_aAttr) return emitError(loc, "'test2.a_get_op' op requires attribute 'aAttr'");223// DEF: auto tblgen_bAttr = getProperties().bAttr; (void)tblgen_bAttr;224// DEF: auto tblgen_cAttr = getProperties().cAttr; (void)tblgen_cAttr;225// DEF: if (tblgen_bAttr && !((some-condition)))226// DEF-NEXT: return emitError(loc, "'test2.a_get_op' op attribute 'bAttr' failed to satisfy constraint: some attribute kind");227// DEF: if (tblgen_cAttr && !((some-condition)))228// DEF-NEXT: return emitError(loc, "'test2.a_get_op' op attribute 'cAttr' failed to satisfy constraint: some attribute kind");229 230// Test getter methods231// ---232 233// DECL: some-attr-kind getAAttrAttr()234// DECL-NEXT: ::llvm::cast<some-attr-kind>(getProperties().aAttr)235// DEF: some-return-type AgetOp::getAAttr() {236// DEF-NEXT: auto attr = getAAttrAttr()237// DEF-NEXT: return attr.some-convert-from-storage();238 239// DECL: some-attr-kind getBAttrAttr()240// DECL-NEXT: return ::llvm::dyn_cast_or_null<some-attr-kind>(getProperties().bAttr)241// DEF: some-return-type AgetOp::getBAttr() {242// DEF-NEXT: auto attr = getBAttrAttr();243// DEF-NEXT: if (!attr)244// DEF-NEXT: return some-const-builder-call(::mlir::Builder((*this)->getContext()), 4.2).some-convert-from-storage();245// DEF-NEXT: return attr.some-convert-from-storage();246 247// DECL: some-attr-kind getCAttrAttr()248// DECL-NEXT: return ::llvm::dyn_cast_or_null<some-attr-kind>(getProperties().cAttr)249// DEF: ::std::optional<some-return-type> AgetOp::getCAttr() {250// DEF-NEXT: auto attr = getCAttrAttr()251// DEF-NEXT: return attr ? ::std::optional<some-return-type>(attr.some-convert-from-storage()) : (::std::nullopt);252 253// Test setter methods254// ---255 256// DECL: void setAAttrAttr(some-attr-kind attr) {257// DECL-NEXT: getProperties().aAttr = attr;258// DECL: void setBAttrAttr(some-attr-kind attr) {259// DECL-NEXT: getProperties().bAttr = attr;260// DECL: void setCAttrAttr(some-attr-kind attr) {261// DECL-NEXT: getProperties().cAttr = attr;262 263// Test remove methods264// ---265 266// DECL: ::mlir::Attribute removeCAttrAttr() {267// DECL-NEXT: auto attr = getProperties().cAttr;268// DECL-NEXT: getProperties().cAttr = {};269// DECL-NEXT: return attr;270 271// Test build methods272// ---273 274// DEF: void AgetOp::build(275// DEF: odsState.getOrAddProperties<Properties>().aAttr = aAttr;276// DEF: odsState.getOrAddProperties<Properties>().bAttr = bAttr;277// DEF: if (cAttr) {278// DEF-NEXT: odsState.getOrAddProperties<Properties>().cAttr = cAttr;279 280// DEF: void AgetOp::build(281// DEF: some-return-type aAttr, /*optional*/some-return-type bAttr, /*optional*/some-attr-kind cAttr282// DEF: odsState.getOrAddProperties<Properties>().aAttr = some-const-builder-call(odsBuilder, aAttr);283 284// DEF: void AgetOp::build(285// DEF: ::llvm::ArrayRef<::mlir::NamedAttribute> attributes286// DEF: odsState.addAttributes(attributes);287 288// DEF: void AgetOp::build(289// DEF-SAME: const Properties &properties290// DEF-SAME: ::llvm::ArrayRef<::mlir::NamedAttribute> discardableAttributes291// DEF: odsState.useProperties(const_cast<Properties&>(properties));292// DEF: odsState.addAttributes(discardableAttributes);293 294// Test the above but using properties.295def ApropOp : NS_Op<"a_prop_op", []> {296 let arguments = (ins297 Property<"unsigned">:$aAttr,298 DefaultValuedAttr<SomeAttr, "4.2">:$bAttr299 );300}301 302// DEF-LABEL: ApropOp definitions303// DEF: void ApropOp::populateDefaultProperties304 305def SomeTypeAttr : TypeAttrBase<"SomeType", "some type attribute">;306 307def BOp : NS_Op<"b_op", []> {308 let arguments = (ins309 AnyAttr:$any_attr,310 BoolAttr:$bool_attr,311 I32Attr:$i32_attr,312 I64Attr:$i64_attr,313 F32Attr:$f32_attr,314 F64Attr:$f64_attr,315 StrAttr:$str_attr,316 ElementsAttr:$elements_attr,317 FlatSymbolRefAttr:$function_attr,318 SomeTypeAttr:$some_type_attr,319 ArrayAttr:$array_attr,320 TypedArrayAttrBase<SomeAttr, "SomeAttr array">:$some_attr_array,321 TypeAttr:$type_attr322 );323}324 325 326// Test common attribute kinds' constraints327// ---328 329// DEF-LABEL: BOpAdaptor::verify330// DEF: if (tblgen_any_attr && !((true)))331// DEF: if (tblgen_bool_attr && !((::llvm::isa<::mlir::BoolAttr>(tblgen_bool_attr))))332// DEF: if (tblgen_i32_attr && !(((::llvm::isa<::mlir::IntegerAttr>(tblgen_i32_attr))) && ((::llvm::cast<::mlir::IntegerAttr>(tblgen_i32_attr).getType().isSignlessInteger(32)))))333// DEF: if (tblgen_i64_attr && !(((::llvm::isa<::mlir::IntegerAttr>(tblgen_i64_attr))) && ((::llvm::cast<::mlir::IntegerAttr>(tblgen_i64_attr).getType().isSignlessInteger(64)))))334// DEF: if (tblgen_f32_attr && !(((::llvm::isa<::mlir::FloatAttr>(tblgen_f32_attr))) && ((::llvm::cast<::mlir::FloatAttr>(tblgen_f32_attr).getType().isF32()))))335// DEF: if (tblgen_f64_attr && !(((::llvm::isa<::mlir::FloatAttr>(tblgen_f64_attr))) && ((::llvm::cast<::mlir::FloatAttr>(tblgen_f64_attr).getType().isF64()))))336// DEF: if (tblgen_str_attr && !((::llvm::isa<::mlir::StringAttr>(tblgen_str_attr))))337// DEF: if (tblgen_elements_attr && !((::llvm::isa<::mlir::ElementsAttr>(tblgen_elements_attr))))338// DEF: if (tblgen_function_attr && !((::llvm::isa<::mlir::FlatSymbolRefAttr>(tblgen_function_attr))))339// DEF: if (tblgen_some_type_attr && !(((::llvm::isa<::mlir::TypeAttr>(tblgen_some_type_attr))) && ((::llvm::isa<SomeType>(::llvm::cast<::mlir::TypeAttr>(tblgen_some_type_attr).getValue()))) && ((true))))340// DEF: if (tblgen_array_attr && !((::llvm::isa<::mlir::ArrayAttr>(tblgen_array_attr))))341// DEF: if (tblgen_some_attr_array && !(((::llvm::isa<::mlir::ArrayAttr>(tblgen_some_attr_array))) && (::llvm::all_of(::llvm::cast<::mlir::ArrayAttr>(tblgen_some_attr_array), [&](::mlir::Attribute attr) { return attr && ((some-condition)); }))))342// DEF: if (tblgen_type_attr && !(((::llvm::isa<::mlir::TypeAttr>(tblgen_type_attr))) && ((::llvm::isa<::mlir::Type>(::llvm::cast<::mlir::TypeAttr>(tblgen_type_attr).getValue()))) && ((true))))343 344// Test common attribute kind getters' return types345// ---346 347// DEF: ::mlir::Attribute BOp::getAnyAttr()348// DEF: bool BOp::getBoolAttr()349// DEF: uint32_t BOp::getI32Attr()350// DEF: uint64_t BOp::getI64Attr()351// DEF: ::llvm::APFloat BOp::getF32Attr()352// DEF: ::llvm::APFloat BOp::getF64Attr()353// DEF: ::llvm::StringRef BOp::getStrAttr()354// DEF: ::mlir::ElementsAttr BOp::getElementsAttr()355// DEF: ::llvm::StringRef BOp::getFunctionAttr()356// DEF: SomeType BOp::getSomeTypeAttr()357// DEF: ::mlir::ArrayAttr BOp::getArrayAttr()358// DEF: ::mlir::ArrayAttr BOp::getSomeAttrArray()359// DEF: ::mlir::Type BOp::getTypeAttr()360 361// Test building constant values for array attribute kinds362// ---363 364def COp : NS_Op<"c_op", []> {365 let arguments = (ins366 DefaultValuedOptionalAttr<I32ArrayAttr, "{1, 2}">:$i32_array_attr,367 DefaultValuedOptionalAttr<I64ArrayAttr, "{3, 4}">:$i64_array_attr,368 DefaultValuedOptionalAttr<F32ArrayAttr, "{5.f, 6.f}">:$f32_array_attr,369 DefaultValuedOptionalAttr<F64ArrayAttr, "{7., 8.}">:$f64_array_attr,370 DefaultValuedOptionalAttr<StrArrayAttr, "{\"a\", \"b\"}">:$str_array_attr371 );372}373 374// DEF-LABEL: COp definitions375// DEF: ::mlir::Builder((*this)->getContext()).getI32ArrayAttr({1, 2})376// DEF: ::mlir::Builder((*this)->getContext()).getI64ArrayAttr({3, 4})377// DEF: ::mlir::Builder((*this)->getContext()).getF32ArrayAttr({5.f, 6.f})378// DEF: ::mlir::Builder((*this)->getContext()).getF64ArrayAttr({7., 8.})379// DEF: ::mlir::Builder((*this)->getContext()).getStrArrayAttr({"a", "b"})380 381 382// Test builder method which takes unwrapped values for attributes383// ---384 385def I32Case5: I32EnumAttrCase<"case5", 5>;386def I32Case10: I32EnumAttrCase<"case10", 10>;387 388def SomeI32Enum: I32EnumAttr<389 "SomeI32Enum", "", [I32Case5, I32Case10]>;390 391def DOp : NS_Op<"d_op", []> {392 let arguments = (ins393 I32Attr:$i32_attr,394 F64Attr:$f64_attr,395 StrAttr:$str_attr,396 BoolAttr:$bool_attr,397 SomeI32Enum:$enum_attr,398 DefaultValuedAttr<I32Attr, "42">:$dv_i32_attr,399 DefaultValuedAttr<F64Attr, "8.">:$dv_f64_attr,400 DefaultValuedStrAttr<StrAttr, "abc">:$dv_str_attr,401 DefaultValuedAttr<BoolAttr, "true">:$dv_bool_attr,402 DefaultValuedAttr<SomeI32Enum, "::SomeI32Enum::case5">:$dv_enum_attr403 );404}405 406// DECL-LABEL: DOp declarations407// DECL: static void build({{.*}}, uint32_t i32_attr, ::llvm::APFloat f64_attr, ::llvm::StringRef str_attr, bool bool_attr, ::SomeI32Enum enum_attr, uint32_t dv_i32_attr, ::llvm::APFloat dv_f64_attr, ::llvm::StringRef dv_str_attr = "abc", bool dv_bool_attr = true, ::SomeI32Enum dv_enum_attr = ::SomeI32Enum::case5)408 409// DEF-LABEL: DOp definitions410// DEF: odsState.getOrAddProperties<Properties>().str_attr = odsBuilder.getStringAttr(str_attr);411// DEF: odsState.getOrAddProperties<Properties>().dv_str_attr = odsBuilder.getStringAttr(dv_str_attr);412 413 414// Test default dictionary attribute.415// ---416 417def DefaultDictAttrOp : NS_Op<"default_dict_attr_op", []> {418 let arguments = (ins419 DefaultValuedAttr<DictionaryAttr, "{}">:$empty,420 DefaultValuedAttr<DictionaryAttr, "getDefaultDictAttrs($_builder)">:$non_empty421 );422}423 424// DEF-LABEL: DefaultDictAttrOp definitions425// DEF: if (!properties.empty)426// DEF: properties.empty = odsBuilder.getDictionaryAttr({});427// DEF: if (!properties.non_empty)428// DEF: properties.non_empty = odsBuilder.getDictionaryAttr(getDefaultDictAttrs(odsBuilder));429 430// DECL-LABEL: DefaultDictAttrOp declarations431// DECL: build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::DictionaryAttr empty = nullptr, ::mlir::DictionaryAttr non_empty = nullptr)432 433 434// Test derived type attr.435// ---436def DerivedTypeAttrOp : NS_Op<"derived_type_attr_op", []> {437 let results = (outs AnyTensor:$output);438 DerivedTypeAttr element_dtype = DerivedTypeAttr<"return output().getType();">;439}440 441// DECL: class DerivedTypeAttrOp : public ::mlir::Op442// DECL-SAME: DerivedAttributeOpInterface::Trait443// DECL: static bool isDerivedAttribute444// DEF: bool DerivedTypeAttrOp::isDerivedAttribute(::llvm::StringRef name) {445// DEF: if (name == "element_dtype") return true;446// DEF: return false;447// DEF: }448// DEF: DerivedTypeAttrOp::materializeDerivedAttributes449 450// Test that only default valued attributes at the end of the arguments451// list get default values in the builder signature452// ---453 454def EOp : NS_Op<"e_op", []> {455 let arguments = (ins456 I32Attr:$i32_attr,457 DefaultValuedAttr<I32Attr, "42">:$dv_i32_attr,458 F64Attr:$f64_attr,459 DefaultValuedAttr<F64Attr, "8.">:$dv_f64_attr,460 StrAttr:$str_attr,461 DefaultValuedStrAttr<StrAttr, "abc">:$dv_str_attr,462 BoolAttr:$bool_attr,463 DefaultValuedAttr<BoolAttr, "true">:$dv_bool_attr,464 SomeI32Enum:$enum_attr,465 DefaultValuedAttr<SomeI32Enum, "::SomeI32Enum::case5">:$dv_enum_attr466 );467}468 469// DECL-LABEL: EOp declarations470// DECL: static void build({{.*}}, uint32_t i32_attr, uint32_t dv_i32_attr, ::llvm::APFloat f64_attr, ::llvm::APFloat dv_f64_attr, ::llvm::StringRef str_attr, ::llvm::StringRef dv_str_attr, bool bool_attr, bool dv_bool_attr, ::SomeI32Enum enum_attr, ::SomeI32Enum dv_enum_attr = ::SomeI32Enum::case5)471 472 473// Test proper namespacing for AttrDef474// ---475 476def NamespaceOp : NS_Op<"namespace_op", []> {477 let arguments = (ins478 SomeAttrDef:$AttrDef479 );480}481 482// Test mixing operands and attributes in arbitrary order483// ---484 485def MixOperandsAndAttrs : NS_Op<"mix_operands_and_attrs", []> {486 let arguments = (ins F32Attr:$attr, F32:$operand, F32Attr:$otherAttr, F32:$otherArg);487}488 489// DECL-LABEL: MixOperandsAndAttrs declarations490// DECL-DAG: ::mlir::TypedValue<::mlir::FloatType> getOperand()491// DECL-DAG: ::mlir::TypedValue<::mlir::FloatType> getOtherArg()492 493// DECL-LABEL: NamespaceOp declarations494// DECL: foobar::SomeAttrAttr getAttrDef()495 496 497def OpWithDefaultAndRegion : NS_Op<"default_with_region", []> {498 let arguments = (ins499 DefaultValuedAttr<BoolAttr, "true">:$dv_bool_attr500 );501 let regions = (region VariadicRegion<AnyRegion>:$region);502}503 504// We should not have a default attribute in this case.505 506// DECL-LABEL: OpWithDefaultAndRegion declarations507// DECL: static void build({{.*}}, bool dv_bool_attr, unsigned regionCount)508 509def OpWithDefaultAndSuccessor : NS_Op<"default_with_succ", []> {510 let arguments = (ins511 DefaultValuedAttr<BoolAttr, "true">:$dv_bool_attr512 );513 let successors = (successor VariadicSuccessor<AnySuccessor>:$succ);514}515 516// We should not have a default attribute in this case.517 518// DECL-LABEL: OpWithDefaultAndSuccessor declarations519// DECL-DAG: static void build({{.*}}, bool dv_bool_attr, ::mlir::BlockRange succ)520 521// DEF-LABEL: MixOperandsAndAttrs definitions522// DEF-DAG: void MixOperandsAndAttrs::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::FloatAttr attr, ::mlir::Value operand, ::mlir::FloatAttr otherAttr, ::mlir::Value otherArg)523// DEF-DAG: ::llvm::APFloat MixOperandsAndAttrs::getAttr()524// DEF-DAG: ::llvm::APFloat MixOperandsAndAttrs::getOtherAttr()525 526// Test unit attributes.527// ---528 529def UnitAttrOp : NS_Op<"unit_attr_op", []> {530 let arguments = (ins UnitAttr:$attr);531}532 533// DEF-LABEL: UnitAttrOp definitions534// DEF: bool UnitAttrOp::getAttr() {535// DEF: return {{.*}} != nullptr536 537 538// DEF: build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, /*optional*/::mlir::UnitAttr attr)539// DEF: build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, /*optional*/bool attr)540 541// DECL-LABEL: UnitAttrOp declarations542// DECL: ::mlir::Attribute removeAttrAttr() {543// DECL-NEXT: auto attr = getProperties().attr;544// DECL-NEXT: getProperties().attr = {};545// DECL-NEXT: return attr;546// DECL: build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, /*optional*/bool attr = false)547 548 549// Test elementAttr field of TypedArrayAttr.550// ---551 552def SomeTypedArrayAttr : TypedArrayAttrBase<SomeAttr, "SomeAttr array">;553 554// RECORD-LABEL: def SomeTypedArrayAttr555// RECORD: Attr elementAttr = SomeAttr;556