[Mlir-commits] [mlir] 4eaaf05 - Add sanity check in MLIR ODS to catch case where two results have the same name

Mehdi Amini llvmlistbot at llvm.org
Wed Sep 8 16:53:17 PDT 2021


Author: Mehdi Amini
Date: 2021-09-08T23:38:50Z
New Revision: 4eaaf053945986f0ea1af6c914e92893cd5bb66a

URL: https://github.com/llvm/llvm-project/commit/4eaaf053945986f0ea1af6c914e92893cd5bb66a
DIFF: https://github.com/llvm/llvm-project/commit/4eaaf053945986f0ea1af6c914e92893cd5bb66a.diff

LOG: Add sanity check in MLIR ODS to catch case where two results have the same name

This is making a tablegen crash with a more friendly error.

Differential Revision: https://reviews.llvm.org/D109456

Added: 
    

Modified: 
    mlir/test/mlir-tblgen/op-error.td
    mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/test/mlir-tblgen/op-error.td b/mlir/test/mlir-tblgen/op-error.td
index 0be7d33c1ef3..587738e8f111 100644
--- a/mlir/test/mlir-tblgen/op-error.td
+++ b/mlir/test/mlir-tblgen/op-error.td
@@ -1,6 +1,8 @@
 // RUN: not mlir-tblgen -gen-op-decls -I %S/../../include -DERROR1 %s 2>&1 | FileCheck --check-prefix=ERROR1 %s
 // RUN: not mlir-tblgen -gen-op-decls -I %S/../../include -DERROR2 %s 2>&1 | FileCheck --check-prefix=ERROR2 %s
 // RUN: not mlir-tblgen -gen-op-decls -I %S/../../include -DERROR3 %s 2>&1 | FileCheck --check-prefix=ERROR3 %s
+// RUN: not mlir-tblgen -gen-op-decls -I %S/../../include -DERROR4 %s 2>&1 | FileCheck --check-prefix=ERROR4 %s
+// RUN: not mlir-tblgen -gen-op-decls -I %S/../../include -DERROR5 %s 2>&1 | FileCheck --check-prefix=ERROR5 %s
 
 include "mlir/IR/OpBase.td"
 
@@ -34,3 +36,17 @@ def OpDefaultValueNotTrailing : Op<Test_Dialect, "default_value"> {
   ];
 }
 #endif
+
+#ifdef ERROR4
+// ERROR4: error: op has two operands with the same name: 'tensor'
+def OpWithDuplicatedArgNames : Op<Test_Dialect, "default_value"> {
+  let arguments = (ins AnyTensor:$tensor, AnyTensor:$tensor);
+}
+#endif
+
+#ifdef ERROR5
+// ERROR5: error: op has two results with the same name: 'tensor'
+def OpWithDuplicatedResultNames : Op<Test_Dialect, "default_value"> {
+  let results = (outs AnyTensor:$tensor, AnyTensor:$tensor);
+}
+#endif

diff  --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index a182e268be3c..b9d63dc483cc 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -857,7 +857,8 @@ static void generateNamedOperandGetters(const Operator &op, Class &opClass,
     if (operand.name.empty())
       continue;
     if (!operandNames.insert(operand.name).second)
-      PrintFatalError(op.getLoc(), "op has two operands with the same name");
+      PrintFatalError(op.getLoc(), "op has two operands with the same name: '" +
+                                       operand.name + "'");
 
     if (operand.isOptional()) {
       m = opClass.addMethodAndPrune("::mlir::Value", operand.name);
@@ -991,10 +992,14 @@ void OpEmitter::genNamedResultGetters() {
   m->body() << formatv(valueRangeReturnCode, "getOperation()->result_begin()",
                        "getODSResultIndexAndLength(index)");
 
+  SmallDenseSet<StringRef> resultNames;
   for (int i = 0; i != numResults; ++i) {
     const auto &result = op.getResult(i);
     if (result.name.empty())
       continue;
+    if (!resultNames.insert(result.name).second)
+      PrintFatalError(op.getLoc(), "op has two results with the same name: '" +
+                                       result.name + "'");
 
     if (result.isOptional()) {
       m = opClass.addMethodAndPrune("::mlir::Value", result.name);


        


More information about the Mlir-commits mailing list