[llvm-branch-commits] [mlir] 071e483 - Add verifier for the LHLO TupleOp

Uday Bondhugula via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Nov 5 03:30:15 PDT 2021


Author: Prateek Gupta
Date: 2021-09-23T05:53:52+05:30
New Revision: 071e483b072ee7ff59ec3386b6bc0155c7a230b3

URL: https://github.com/llvm/llvm-project/commit/071e483b072ee7ff59ec3386b6bc0155c7a230b3
DIFF: https://github.com/llvm/llvm-project/commit/071e483b072ee7ff59ec3386b6bc0155c7a230b3.diff

LOG: Add verifier for the LHLO TupleOp
Added verification support for LHLO TupleOp, so that inconsistency between input and output types for the LHLO TupleOp can be checked.

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/LHLO/IR/LHLOOps.td
    mlir/lib/Dialect/LHLO/IR/LHLOOps.cc
    mlir/test/Dialect/LHLO/invalid.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/LHLO/IR/LHLOOps.td b/mlir/include/mlir/Dialect/LHLO/IR/LHLOOps.td
index 104e9c011de6..752992761485 100644
--- a/mlir/include/mlir/Dialect/LHLO/IR/LHLOOps.td
+++ b/mlir/include/mlir/Dialect/LHLO/IR/LHLOOps.td
@@ -600,6 +600,8 @@ def LHLO_TupleOp : LHLO_ReadOnlyOp<"tuple", [NoSideEffect]>, BASE_HLO_TupleOp {
   let results = (outs NestedTupleOf<[LHLO_BufferOrIntOrFP]>);
 
   let builders = [OpBuilder<(ins "ValueRange":$values)>];
+
+  let verifier = [{ return::verify(*this);}];
 }
 
 def LHLO_WhileOp

diff  --git a/mlir/lib/Dialect/LHLO/IR/LHLOOps.cc b/mlir/lib/Dialect/LHLO/IR/LHLOOps.cc
index 01b0da948dea..27d2596f167e 100644
--- a/mlir/lib/Dialect/LHLO/IR/LHLOOps.cc
+++ b/mlir/lib/Dialect/LHLO/IR/LHLOOps.cc
@@ -122,6 +122,17 @@ void TupleOp::build(OpBuilder& builder, OperationState& result,
   build(builder, result, builder.getTupleType(types), values);
 }
 
+static LogicalResult verify(TupleOp op) {
+  SmallVector<Type, 4> operandTypes = {op.operand_type_begin(),
+                                       op.operand_type_end()};
+  auto expectedType = TupleType::get(op.getContext(), operandTypes);
+  if (op.getType() != expectedType) {
+    return op.emitOpError(llvm::formatv("has return type {0}, but expected {1}",
+                                        op.getType(), expectedType));
+  }
+  return success();
+}
+
 //===----------------------------------------------------------------------===//
 // GetTupleElementOp
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/LHLO/invalid.mlir b/mlir/test/Dialect/LHLO/invalid.mlir
index 0e269535d00c..98e744c0167c 100644
--- a/mlir/test/Dialect/LHLO/invalid.mlir
+++ b/mlir/test/Dialect/LHLO/invalid.mlir
@@ -8,3 +8,30 @@ func @passthrough(%arg : memref<8xi32>) {
   %mem = "xla_lhlo.get_tuple_element"(%tuple) {index = 0 : i32} : (tuple<i32, memref<8xi32>>) -> memref<8xi32>
   return
 }
+
+// -----
+
+func @pass_wrong_number_of_arguments(%arg : memref<8xi32>){
+    %c0 = constant 0 : i32
+    %c1 = constant 1 : i32
+    // expected-error at +1{{'xla_lhlo.tuple' op has return type tuple<i32>, but expected tuple<i32, memref<8xi32>, i32>}}
+    %tuple = "xla_lhlo.tuple"(%c0, %arg, %c1) : (i32, memref<8xi32>, i32) -> (tuple<i32>)
+}
+
+// -----
+
+func @pass_wrong_type(%arg : i32){
+    %c = constant 0 : i32
+    // expected-error at +1{{'xla_lhlo.tuple' op has return type tuple<i32, memref<8xi32>>, but expected tuple<i32, i32>}}
+    %tuple = "xla_lhlo.tuple"(%c, %arg) : (i32, i32) -> (tuple<i32, memref<8xi32>>)
+    return 
+}
+
+// -----
+
+func @pass_wrong_order(%arg : memref<8xi32>){
+    %c = constant 0 : i32
+    // expected-error at +1{{'xla_lhlo.tuple' op has return type tuple<memref<8xi32>, i32>, but expected tuple<i32, memref<8xi32>>}}
+    %tuple = "xla_lhlo.tuple"(%c, %arg) : (i32, memref<8xi32>) -> (tuple<memref<8xi32>, i32>)
+    return 
+}
\ No newline at end of file


        


More information about the llvm-branch-commits mailing list