[Mlir-commits] [mlir] [mlir] [irdl] Add support for regions in irdl-to-cpp (PR #158540)

Théo Degioanni llvmlistbot at llvm.org
Mon Sep 29 10:24:27 PDT 2025


================
@@ -301,6 +360,112 @@ static LogicalResult generateInclude(irdl::DialectOp dialect,
   return success();
 }
 
+static void generateVerifiers(irdl::detail::dictionary &dict,
+                              irdl::OperationOp op, const OpStrings &strings) {
+  SmallVector<std::string> verifierHelpers;
+  SmallVector<std::string> verifierCalls;
+  auto regionsOp = op.getOp<irdl::RegionsOp>();
+  if (strings.opRegionNames.empty() || !regionsOp) {
+    // Currently IRDL regions are the only reason to generate a nontrivial
+    // verifier, though this will likely change as
+    // https://github.com/llvm/llvm-project/issues/158040 is implemented
+    std::string verifierDef = llvm::formatv(R"(
+::llvm::LogicalResult {0}::verifyInvariantsImpl() {{
+  return ::mlir::success();
+})",
+                                            strings.opCppName);
+    dict["OP_VERIFIER_HELPERS"] = "";
+    dict["OP_VERIFIER"] = verifierDef;
+    return;
+  }
+
+  for (size_t i = 0; i < strings.opRegionNames.size(); ++i) {
+    std::string regionName = strings.opRegionNames[i];
+    std::string helperFnName =
+        llvm::formatv("__mlir_irdl_local_region_constraint_{0}_{1}",
+                      strings.opCppName, regionName)
+            .str();
+
+    // Extract the actual region constraint from the IRDL RegionOp
+    std::string condition = "true";
+    std::string textualConditionName = "any region";
+
+    if (auto regionDefOp =
+            dyn_cast<irdl::RegionOp>(regionsOp->getArgs()[i].getDefiningOp())) {
----------------
Moxinilian wrote:

This is not a review comment but this makes me realize the way we have RegionOp as an "fits all" constraint operation is probably not the most convenient thing for codegen. I wonder if we could do with applying layers of constraints over the constraint object (with a `%further_constrained = irdl.region_block_size 3 over %constraint` sort of model), which would render the whole thing more flexible and maybe less arbitrary.

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


More information about the Mlir-commits mailing list