diff options
| author | Lei Zhang <antiagainst@google.com> | 2019-02-27 08:40:07 -0800 |
|---|---|---|
| committer | jpienaar <jpienaar@google.com> | 2019-03-29 16:46:41 -0700 |
| commit | 493d46067b371c87f00aa8e03dfc31df63a719ef (patch) | |
| tree | d03eaf80f494716286bf6e6aa019c227fecb91f6 | |
| parent | 9e18783e41297452f27b2491befc0424f0217638 (diff) | |
| download | bcm5719-llvm-493d46067b371c87f00aa8e03dfc31df63a719ef.tar.gz bcm5719-llvm-493d46067b371c87f00aa8e03dfc31df63a719ef.zip | |
[TableGen] Use result names in build() methods if possible
This will make it clear which result's type we are expecting in the build() signature.
PiperOrigin-RevId: 235925706
| -rw-r--r-- | mlir/test/mlir-tblgen/op-result.td | 32 | ||||
| -rw-r--r-- | mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp | 20 |
2 files changed, 47 insertions, 5 deletions
diff --git a/mlir/test/mlir-tblgen/op-result.td b/mlir/test/mlir-tblgen/op-result.td new file mode 100644 index 00000000000..d9484fd4b7c --- /dev/null +++ b/mlir/test/mlir-tblgen/op-result.td @@ -0,0 +1,32 @@ +// RUN: mlir-tblgen -gen-op-definitions -I %S/../../include %s | FileCheck %s + +include "mlir/IR/op_base.td" + +def SameTypeOp : Op<"same_type_op", [SameValueType]> { + let arguments = (ins I32:$x); + let results = (outs I32:$y); +} + +// CHECK-LABEL: class SameTypeOp +// CHECK: void build(Builder *builder, OperationState *result, Type y, Value *x) +// CHECK: result->addTypes({y}); +// CHECK: void build(Builder *builder, OperationState *result, Value *x) +// CHECK: result->addTypes({x->getType()}); + +def ThreeResultOp : Op<"three_result_op", []> { + let results = (outs I32:$x, /*unnamed*/I32, I32:$z); +} + +// CHECK-LABEL: class ThreeResultOp +// CHECK: void build(Builder *builder, OperationState *result, Type x, Type resultType1, Type z) +// CHECK: result->addTypes({x, resultType1, z}); + +def VariadicResultOp : Op<"variadic_op", []> { + let results = (outs I32:$x, Variadic<I32>:$y); +} + +// CHECK-LABEL: VariadicResultOp +// CHECK: void build(Builder *builder, OperationState *result, Type x, ArrayRef<Type> y) +// CHECK: result->addTypes({x}); +// CHECK: result->addTypes(y); + diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp index a8275eee146..35840dade9e 100644 --- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp +++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp @@ -246,11 +246,21 @@ void OpEmitter::emitStandaloneParamBuilder(bool isAllSameType) { auto numResults = op.getNumResults(); auto numOperands = op.getNumOperands(); + llvm::SmallVector<std::string, 4> resultNames; + resultNames.reserve(numResults); + // Emit parameters for all return types if (!isAllSameType) { - for (unsigned i = 0; i != numResults; ++i) + for (unsigned i = 0; i != numResults; ++i) { + std::string resultName = op.getResultName(i); + if (resultName.empty()) + resultName = formatv("resultType{0}", i); + os << (op.getResultType(i).isVariadic() ? ", ArrayRef<Type> " : ", Type ") - << "returnType" << i; + << resultName; + + resultNames.emplace_back(std::move(resultName)); + } } // Emit parameters for all operands @@ -280,15 +290,15 @@ void OpEmitter::emitStandaloneParamBuilder(bool isAllSameType) { numResults - static_cast<int>(hasVariadicResult); if (numNonVariadicResults > 0) { - OUT(4) << "result->addTypes({returnType0"; + OUT(4) << "result->addTypes({" << resultNames.front(); for (int i = 1; i < numNonVariadicResults; ++i) { - os << ", resultType" << i; + os << ", " << resultNames[i]; } os << "});\n"; } if (hasVariadicResult) { - OUT(4) << formatv("result->addTypes(returnType{0});\n", numResults - 1); + OUT(4) << "result->addTypes(" << resultNames.back() << ");\n"; } } else { OUT(4) << "result->addTypes({"; |

