From fa97d3a2cfd590f3279cb44735e68c2aa10e0bee Mon Sep 17 00:00:00 2001 From: Jacques Pienaar Date: Fri, 10 May 2019 08:51:34 -0700 Subject: [PATCH] Emit cast instead of dyn_cast_or_null where attribute is required. If the attribute needs to exist for the validity of the op, then no need to use dyn_cast_or_null as the op would be invalid in the cases where cast fails, so just use cast. -- PiperOrigin-RevId: 247617696 --- mlir/test/mlir-tblgen/attr-enum.td | 2 +- mlir/test/mlir-tblgen/op-attribute.td | 2 +- mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp | 8 ++++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/mlir/test/mlir-tblgen/attr-enum.td b/mlir/test/mlir-tblgen/attr-enum.td index 3347165..daf31b0 100644 --- a/mlir/test/mlir-tblgen/attr-enum.td +++ b/mlir/test/mlir-tblgen/attr-enum.td @@ -24,7 +24,7 @@ def NS_OpA : NS_Op<"op_a_with_enum_attr", []> { // --- // DEF-LABEL: StringRef OpA::attr() -// DEF-NEXT: auto attr = this->getAttr("attr").dyn_cast_or_null(); +// DEF-NEXT: auto attr = this->getAttr("attr").cast(); // DEF-NEXT: return attr.getValue(); // DEF-LABEL: OpA::verify() diff --git a/mlir/test/mlir-tblgen/op-attribute.td b/mlir/test/mlir-tblgen/op-attribute.td index e1b4060..0272d0e 100644 --- a/mlir/test/mlir-tblgen/op-attribute.td +++ b/mlir/test/mlir-tblgen/op-attribute.td @@ -32,7 +32,7 @@ def AOp : NS_Op<"a_op", []> { // --- // CHECK: some-return-type AOp::aAttr() { -// CHECK-NEXT: auto attr = this->getAttr("aAttr").dyn_cast_or_null(); +// CHECK-NEXT: auto attr = this->getAttr("aAttr").cast(); // CHECK-NEXT: return attr.some-convert-from-storage(); // CHECK: some-return-type AOp::bAttr() { diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp index 36464f7..343d61f 100644 --- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp +++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp @@ -441,8 +441,12 @@ void OpEmitter::genAttrGetters() { // Emit normal emitter. // Return the queried attribute with the correct return type. - auto attrVal = formatv("this->getAttr(\"{0}\").dyn_cast_or_null<{1}>()", - name, attr.getStorageType()); + auto attrVal = + (attr.hasDefaultValueInitializer() || attr.isOptional()) + ? formatv("this->getAttr(\"{0}\").dyn_cast_or_null<{1}>()", name, + attr.getStorageType()) + : formatv("this->getAttr(\"{0}\").cast<{1}>()", name, + attr.getStorageType()); body << " auto attr = " << attrVal << ";\n"; if (attr.hasDefaultValueInitializer()) { // Returns the default value if not set. -- 2.7.4