diff options
| author | Lei Zhang <antiagainst@google.com> | 2019-03-06 12:50:01 -0800 |
|---|---|---|
| committer | jpienaar <jpienaar@google.com> | 2019-03-29 17:02:26 -0700 |
| commit | 4fc9b51727b331933158ef2122d0d04909202562 (patch) | |
| tree | b667a4b74cce6641ea5af5ee506f3b9c434996ba /mlir | |
| parent | d43f630de85d0d65f6141885cf06a59d82113d8e (diff) | |
| download | bcm5719-llvm-4fc9b51727b331933158ef2122d0d04909202562.tar.gz bcm5719-llvm-4fc9b51727b331933158ef2122d0d04909202562.zip | |
[TableGen] Emit verification code for op results
They can be verified using the same logic as operands.
PiperOrigin-RevId: 237101461
Diffstat (limited to 'mlir')
| -rw-r--r-- | mlir/include/mlir/TableGen/Operator.h | 4 | ||||
| -rw-r--r-- | mlir/test/mlir-tblgen/op-operand.td | 12 | ||||
| -rw-r--r-- | mlir/test/mlir-tblgen/op-result.td | 10 | ||||
| -rw-r--r-- | mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp | 36 |
4 files changed, 49 insertions, 13 deletions
diff --git a/mlir/include/mlir/TableGen/Operator.h b/mlir/include/mlir/TableGen/Operator.h index 366e83d6a8c..6c66b7618e4 100644 --- a/mlir/include/mlir/TableGen/Operator.h +++ b/mlir/include/mlir/TableGen/Operator.h @@ -68,6 +68,10 @@ public: // Returns the number of results this op produces. int getNumResults() const; + // Returns the op result at the given `index`. + Value &getResult(int index) { return results[index]; } + const Value &getResult(int index) const { return results[index]; } + // Returns the `index`-th result's type. Type getResultType(int index) const; // Returns the `index`-th result's name. diff --git a/mlir/test/mlir-tblgen/op-operand.td b/mlir/test/mlir-tblgen/op-operand.td new file mode 100644 index 00000000000..e3709a86a00 --- /dev/null +++ b/mlir/test/mlir-tblgen/op-operand.td @@ -0,0 +1,12 @@ +// RUN: mlir-tblgen -gen-op-definitions -I %S/../../include %s | FileCheck %s + +include "mlir/IR/OpBase.td" + +def OneOperandOp : Op<"one_operand_op", []> { + let arguments = (ins I32:$input); +} + +// CHECK-LABEL: class OneOperandOp +// CHECK: bool verify() const { +// CHECK: if (!((this->getInstruction()->getOperand(0)->getType().isInteger(32)))) { +// CHECK-NEXT: return emitOpError("operand #0 must be 32-bit integer"); diff --git a/mlir/test/mlir-tblgen/op-result.td b/mlir/test/mlir-tblgen/op-result.td index 22363202096..8ca903c9e36 100644 --- a/mlir/test/mlir-tblgen/op-result.td +++ b/mlir/test/mlir-tblgen/op-result.td @@ -2,6 +2,16 @@ include "mlir/IR/OpBase.td" +def OneResultOp : Op<"one_result_op", []> { + let results = (outs I32:$result); +} + +// CHECK-LABEL: class OneResultOp +// CHECK: bool verify() const { +// CHECK: if (!((this->getInstruction()->getResult(0)->getType().isInteger(32)))) { +// CHECK-NEXT: return emitOpError("result #0 must be 32-bit integer"); + + def SameTypeOp : Op<"same_type_op", [SameValueType]> { let arguments = (ins I32:$x); let results = (outs I32:$y); diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp index 30773dc687b..fa7dc96fc7c 100644 --- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp +++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp @@ -470,7 +470,7 @@ void OpEmitter::emitVerifier() { auto valueInit = def.getValueInit("verifier"); CodeInit *codeInit = dyn_cast<CodeInit>(valueInit); bool hasCustomVerify = codeInit && !codeInit->getValue().empty(); - if (!hasCustomVerify && op.getNumArgs() == 0) + if (!hasCustomVerify && op.getNumArgs() == 0 && op.getNumResults() == 0) return; OUT(2) << "bool verify() const {\n"; @@ -515,28 +515,38 @@ void OpEmitter::emitVerifier() { OUT(4) << "}\n"; } - int opIndex = 0; - for (const auto &operand : op.getOperands()) { - // TODO: Handle variadic operand verification. - if (operand.type.isVariadic()) - continue; + // Emits verification code for an operand or result. + auto verifyValue = [this](const tblgen::Value &value, int index, + bool isOperand) -> void { + // TODO: Handle variadic operand/result verification. + if (value.type.isVariadic()) + return; // TODO: Commonality between matchers could be extracted to have a more // concise code. - if (operand.hasPredicate()) { - auto description = operand.type.getDescription(); + if (value.hasPredicate()) { + auto description = value.type.getDescription(); OUT(4) << "if (!(" - << formatv(operand.type.getConditionTemplate(), - "this->getInstruction()->getOperand(" + Twine(opIndex) + - ")->getType()") + << formatv(value.type.getConditionTemplate(), + "this->getInstruction()->get" + + Twine(isOperand ? "Operand" : "Result") + "(" + + Twine(index) + ")->getType()") << ")) {\n"; - OUT(6) << "return emitOpError(\"operand #" + Twine(opIndex) + OUT(6) << "return emitOpError(\"" << (isOperand ? "operand" : "result") + << " #" << index << (description.empty() ? " type precondition failed" : " must be " + Twine(description)) << "\");"; OUT(4) << "}\n"; } - ++opIndex; + }; + + for (unsigned i = 0, e = op.getNumOperands(); i < e; ++i) { + verifyValue(op.getOperand(i), i, /*isOperand=*/true); + } + + for (unsigned i = 0, e = op.getNumResults(); i < e; ++i) { + verifyValue(op.getResult(i), i, /*isOperand=*/false); } for (auto &trait : op.getTraits()) { |

