[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
This commit is contained in:
Lei Zhang 2019-02-27 08:40:07 -08:00 committed by jpienaar
parent 9e18783e41
commit 493d46067b
2 changed files with 47 additions and 5 deletions

View File

@ -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);

View File

@ -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({";