From: Christian Sigg Date: Thu, 3 Oct 2019 19:59:42 +0000 (-0700) Subject: Generalize parse/printBinaryOp to parse/printOneResultOp. X-Git-Tag: llvmorg-11-init~1466^2~625 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=496f4590a1ab17615fe798de3266da3f34dfbddb;p=platform%2Fupstream%2Fllvm.git Generalize parse/printBinaryOp to parse/printOneResultOp. PiperOrigin-RevId: 272722539 --- diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td index 002d261..6b81af5 100644 --- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td +++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td @@ -130,8 +130,8 @@ class LLVM_ArithmeticOp, 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. diff --git a/mlir/include/mlir/Dialect/SPIRV/SPIRVBase.td b/mlir/include/mlir/Dialect/SPIRV/SPIRVBase.td index b405988..3893361 100644 --- a/mlir/include/mlir/Dialect/SPIRV/SPIRVBase.td +++ b/mlir/include/mlir/Dialect/SPIRV/SPIRVBase.td @@ -1288,8 +1288,8 @@ class SPV_BinaryOp:$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(); }]; } diff --git a/mlir/include/mlir/Dialect/SPIRV/SPIRVGLSLOps.td b/mlir/include/mlir/Dialect/SPIRV/SPIRVGLSLOps.td index 25a2d65..ea4b38b 100644 --- a/mlir/include/mlir/Dialect/SPIRV/SPIRVGLSLOps.td +++ b/mlir/include/mlir/Dialect/SPIRV/SPIRVGLSLOps.td @@ -76,9 +76,9 @@ class SPV_GLSLBinaryOp:$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(); }]; } diff --git a/mlir/include/mlir/Dialect/StandardOps/Ops.td b/mlir/include/mlir/Dialect/StandardOps/Ops.td index 1c548f2..7afa782 100644 --- a/mlir/include/mlir/Dialect/StandardOps/Ops.td +++ b/mlir/include/mlir/Dialect/StandardOps/Ops.td @@ -82,7 +82,7 @@ class ArithmeticOp traits = []> : let results = (outs AnyType); let parser = [{ - return impl::parseBinaryOp(parser, result); + return impl::parseOneResultSameOperandTypeOp(parser, result); }]; let printer = [{ diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h index 6d99206..0b0c187 100644 --- a/mlir/include/mlir/IR/OpDefinition.h +++ b/mlir/include/mlir/IR/OpDefinition.h @@ -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, diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp index 27681d3..2c05bb4 100644 --- a/mlir/lib/IR/Operation.cpp +++ b/mlir/lib/IR/Operation.cpp @@ -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 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; } //===----------------------------------------------------------------------===// diff --git a/mlir/test/IR/invalid-ops.mlir b/mlir/test/IR/invalid-ops.mlir index ca645e2..381e5d5 100644 --- a/mlir/test/IR/invalid-ops.mlir +++ b/mlir/test/IR/invalid-ops.mlir @@ -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}} } // -----