[Mlir-commits] [mlir] [mlir][tblgen] Add custom parsing and printing within struct (PR #133939)

River Riddle llvmlistbot at llvm.org
Tue Apr 8 22:27:30 PDT 2025


================
@@ -551,22 +557,36 @@ void DefFormat::genStructParser(StructDirective *el, FmtContext &ctx,
     while (!$_parser.parseOptionalComma()) {
 )";
 
+  const char *const checkParamKey = R"(
+  if (!_seen_{0} && _paramKey == "{0}") {
+    _seen_{0} = true;
+)";
+
   os << "// Parse parameter struct\n";
 
   // Declare a "seen" variable for each key.
-  for (ParameterElement *param : el->getParams())
+  for (FormatElement *arg : el->getElements()) {
+    ParameterElement *param = getStructParameterElement(arg);
+    assert(param != nullptr &&
+           "expected `struct` directive to have a parameter");
     os << formatv("bool _seen_{0} = false;\n", param->getName());
+  }
 
   // Generate the body of the parsing loop inside a lambda.
   os << "{\n";
   os.indent()
       << "const auto _loop_body = [&](::llvm::StringRef _paramKey) -> bool {\n";
   genLiteralParser("=", ctx, os.indent());
-  for (ParameterElement *param : el->getParams()) {
-    os << formatv("if (!_seen_{0} && _paramKey == \"{0}\") {\n"
-                  "  _seen_{0} = true;\n",
-                  param->getName());
-    genVariableParser(param, ctx, os.indent());
+  for (FormatElement *arg : el->getElements()) {
+    ParameterElement *param = getStructParameterElement(arg);
+    assert(param != nullptr &&
+           "expected `struct` directive to have a parameter");
+    os.getStream().printReindented(strfmt(checkParamKey, param->getName()));
+    if (auto realParam = dyn_cast<ParameterElement>(arg)) {
+      genVariableParser(param, ctx, os.indent());
+    } else if (auto custom = dyn_cast<CustomDirective>(arg)) {
+      genCustomParser(custom, ctx, os.indent());
+    }
----------------
River707 wrote:

```suggestion
    if (auto realParam = dyn_cast<ParameterElement>(arg))
      genVariableParser(param, ctx, os.indent());
    else if (auto custom = dyn_cast<CustomDirective>(arg))
      genCustomParser(custom, ctx, os.indent());
```

style: Please strip trivial braces

https://github.com/llvm/llvm-project/pull/133939


More information about the Mlir-commits mailing list