[PATCH] D77104: [mlir] Add a pre-check before checking constraints of an optional attribute.

Han-Chung Wang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 30 16:57:17 PDT 2020


hanchung created this revision.
hanchung added reviewers: rriddle, mehdi_amini, jpienaar, antiagainst.
Herald added subscribers: llvm-commits, grosul1, Joonsoo, liufengdb, lucyrfox, mgester, arpith-jacob, nicolasvasilache, shauheen, burmako.
Herald added a project: LLVM.

When an attribute is optional and if it's missing, a mlir::Attribute() is
created to signal the missing state. In this case, it is a nullptr. This patch
adds a pre-check before checking constraints because there are bunch of
`tblgen_attr.isa<...>` in a condition template. Otherwise, it fails with an
assertion because it is not valid to apply `isa` method on a nullptr.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77104

Files:
  mlir/tools/mlir-tblgen/RewriterGen.cpp


Index: mlir/tools/mlir-tblgen/RewriterGen.cpp
===================================================================
--- mlir/tools/mlir-tblgen/RewriterGen.cpp
+++ mlir/tools/mlir-tblgen/RewriterGen.cpp
@@ -369,6 +369,11 @@
 
     // If a constraint is specified, we need to generate C++ statements to
     // check the constraint.
+    // Only check the constraint of an optional attribute if it has value.
+    if (attr.isOptional()) {
+      os.indent(indent) << "if (tblgen_attr) {\n";
+      indent += 2;
+    }
     emitMatchCheck(
         depth,
         tgfmt(matcher.getConditionTemplate(), &fmtCtx.withSelf("tblgen_attr")),
@@ -376,6 +381,10 @@
                 "{2}\"",
                 op.getOperationName(), namedAttr->name,
                 matcher.getAsConstraint().getDescription()));
+    if (attr.isOptional()) {
+      os.indent(indent) << "}\n";
+      indent -= 2;
+    }
   }
 
   // Capture the value


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77104.253740.patch
Type: text/x-patch
Size: 922 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200330/42e014e6/attachment-0001.bin>


More information about the llvm-commits mailing list