Generalize parse/printBinaryOp to parse/printOneResultOp.
authorChristian Sigg <csigg@google.com>
Thu, 3 Oct 2019 19:59:42 +0000 (12:59 -0700)
committerA. Unique TensorFlower <gardener@tensorflow.org>
Thu, 3 Oct 2019 20:00:12 +0000 (13:00 -0700)
PiperOrigin-RevId: 272722539

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
mlir/include/mlir/Dialect/SPIRV/SPIRVBase.td
mlir/include/mlir/Dialect/SPIRV/SPIRVGLSLOps.td
mlir/include/mlir/Dialect/StandardOps/Ops.td
mlir/include/mlir/IR/OpDefinition.h
mlir/lib/IR/Operation.cpp
mlir/test/IR/invalid-ops.mlir

index 002d261..6b81af5 100644 (file)
@@ -130,8 +130,8 @@ class LLVM_ArithmeticOp<string mnemonic, string builderFunc,
            !listconcat([NoSideEffect, SameOperandsAndResultType], traits)>,
     Arguments<(ins LLVM_Type:$lhs, LLVM_Type:$rhs)>,
     LLVM_Builder<"$res = builder." # builderFunc # "($lhs, $rhs);"> {
-  let parser = [{ return impl::parseBinaryOp(parser, result); }];
-  let printer = [{ mlir::impl::printBinaryOp(this->getOperation(), p); }];
+  let parser = [{ return impl::parseOneResultSameOperandTypeOp(parser, result); }];
+  let printer = [{ mlir::impl::printOneResultOp(this->getOperation(), p); }];
 }
 
 // Integer binary operations.
index b405988..3893361 100644 (file)
@@ -1288,8 +1288,8 @@ class SPV_BinaryOp<string mnemonic, Type resultType, Type operandsType,
     SPV_ScalarOrVectorOf<resultType>:$result
   );
 
-  let parser = [{ return impl::parseBinaryOp(parser, result); }];
-  let printer = [{ return impl::printBinaryOp(getOperation(), p); }];
+  let parser = [{ return impl::parseOneResultSameOperandTypeOp(parser, result); }];
+  let printer = [{ return impl::printOneResultOp(getOperation(), p); }];
   // No additional verification needed in addition to the ODS-generated ones.
   let verifier = [{ return success(); }];
 }
index 25a2d65..ea4b38b 100644 (file)
@@ -76,9 +76,9 @@ class SPV_GLSLBinaryOp<string mnemonic, int opcode, Type resultType,
     SPV_ScalarOrVectorOf<resultType>:$result
   );
 
-  let parser = [{ return impl::parseBinaryOp(parser, result); }];
+  let parser = [{ return impl::parseOneResultSameOperandTypeOp(parser, result); }];
 
-  let printer = [{ return impl::printBinaryOp(getOperation(), p); }];
+  let printer = [{ return impl::printOneResultOp(getOperation(), p); }];
 
   let verifier = [{ return success(); }];
 }
index 1c548f2..7afa782 100644 (file)
@@ -82,7 +82,7 @@ class ArithmeticOp<string mnemonic, list<OpTrait> traits = []> :
   let results = (outs AnyType);
 
   let parser = [{
-    return impl::parseBinaryOp(parser, result);
+    return impl::parseOneResultSameOperandTypeOp(parser, result);
   }];
 
   let printer = [{
index 6d99206..0b0c187 100644 (file)
@@ -1144,11 +1144,12 @@ private:
 namespace impl {
 void buildBinaryOp(Builder *builder, OperationState &result, Value *lhs,
                    Value *rhs);
-ParseResult parseBinaryOp(OpAsmParser &parser, OperationState &result);
+ParseResult parseOneResultSameOperandTypeOp(OpAsmParser &parser,
+                                            OperationState &result);
 // Prints the given binary `op` in custom assembly form if both the two operands
 // and the result have the same time. Otherwise, prints the generic assembly
 // form.
-void printBinaryOp(Operation *op, OpAsmPrinter &p);
+void printOneResultOp(Operation *op, OpAsmPrinter &p);
 } // namespace impl
 
 // These functions are out-of-line implementations of the methods in CastOp,
index 27681d3..2c05bb4 100644 (file)
@@ -953,33 +953,34 @@ void impl::buildBinaryOp(Builder *builder, OperationState &result, Value *lhs,
   result.types.push_back(lhs->getType());
 }
 
-ParseResult impl::parseBinaryOp(OpAsmParser &parser, OperationState &result) {
+ParseResult impl::parseOneResultSameOperandTypeOp(OpAsmParser &parser,
+                                                  OperationState &result) {
   SmallVector<OpAsmParser::OperandType, 2> ops;
   Type type;
-  return failure(parser.parseOperandList(ops, 2) ||
+  return failure(parser.parseOperandList(ops) ||
                  parser.parseOptionalAttributeDict(result.attributes) ||
                  parser.parseColonType(type) ||
                  parser.resolveOperands(ops, type, result.operands) ||
                  parser.addTypeToList(type, result.types));
 }
 
-void impl::printBinaryOp(Operation *op, OpAsmPrinter &p) {
-  assert(op->getNumOperands() == 2 && "binary op should have two operands");
-  assert(op->getNumResults() == 1 && "binary op should have one result");
+void impl::printOneResultOp(Operation *op, OpAsmPrinter &p) {
+  assert(op->getNumResults() == 1 && "op should have one result");
 
   // If not all the operand and result types are the same, just use the
   // generic assembly form to avoid omitting information in printing.
   auto resultType = op->getResult(0)->getType();
-  if (op->getOperand(0)->getType() != resultType ||
-      op->getOperand(1)->getType() != resultType) {
+  if (llvm::any_of(op->getOperandTypes(),
+                   [&](Type type) { return type != resultType; })) {
     p.printGenericOp(op);
     return;
   }
 
-  p << op->getName() << ' ' << *op->getOperand(0) << ", " << *op->getOperand(1);
+  p << op->getName() << ' ';
+  p.printOperands(op->getOperands());
   p.printOptionalAttrDict(op->getAttrs());
   // Now we can output only one type for all operands and the result.
-  p << " : " << op->getResult(0)->getType();
+  p << " : " << resultType;
 }
 
 //===----------------------------------------------------------------------===//
index ca645e2..381e5d5 100644 (file)
@@ -165,21 +165,21 @@ func @calls(%arg0: i32) {
 
 func @func_with_ops(f32) {
 ^bb0(%a : f32):
-  %sf = addf %a, %a, %a : f32  // expected-error {{custom op 'std.addf' expected 2 operands}}
+  %sf = addf %a, %a, %a : f32  // expected-error {{'std.addf' op expected 2 operands}}
 }
 
 // -----
 
 func @func_with_ops(f32) {
 ^bb0(%a : f32):
-  %sf = addf(%a, %a) : f32  // expected-error {{unexpected delimiter}}
+  %sf = addf(%a, %a) : f32  // expected-error {{expected ':'}}
 }
 
 // -----
 
 func @func_with_ops(f32) {
 ^bb0(%a : f32):
-  %sf = addf{%a, %a} : f32  // expected-error {{invalid operand}}
+  %sf = addf{%a, %a} : f32  // expected-error {{expected attribute name}}
 }
 
 // -----