From a9eb2e8ffc7c17c268e8bccdbe5af484a282921f Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Tue, 11 Dec 2018 13:59:29 -0800 Subject: [PATCH] Generate another op builder with aggregated parameters For each op, generate another builder with the following signature: static void build(Builder* builder, OperationState* result, ArrayRef resultTypes, ArrayRef args, ArrayRef attributes); PiperOrigin-RevId: 225066007 --- mlir/include/mlir/IR/op_base.td | 13 +++++++---- mlir/tools/mlir-op-gen/mlir-op-gen.cpp | 31 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/mlir/include/mlir/IR/op_base.td b/mlir/include/mlir/IR/op_base.td index eb8a4cc8b4c6..69626a5b9827 100644 --- a/mlir/include/mlir/IR/op_base.td +++ b/mlir/include/mlir/IR/op_base.td @@ -144,15 +144,20 @@ class Op props = []> { // Define the hooks used for building, parsing, printing, verification. // Custom builder. - // If a derived class/def does not override this, then a default builder - // is generated, with the following signature: + // If a derived class/def does not override this, then two default builders + // are generated, with the following signatures: // // static void build(Builder* builder, OperationState* result, // Type resultType0, Type resultType1, ..., // SSAValue* arg0, SSAValue* arg1, ..., - // Attribute attr0, Attribute attr1, ...); + // Attribute , Attribute , ...); // - // Where the attributes follow the same declaration order as in the op. + // * where the attributes follow the same declaration order as in the op. + // + // static void build(Builder* builder, OperationState* result, + // ArrayRef resultTypes, + // ArrayRef args, + // ArrayRef attributes); code builder = ?; // Custom parser. diff --git a/mlir/tools/mlir-op-gen/mlir-op-gen.cpp b/mlir/tools/mlir-op-gen/mlir-op-gen.cpp index 623abb2b0502..72e49bd7df58 100644 --- a/mlir/tools/mlir-op-gen/mlir-op-gen.cpp +++ b/mlir/tools/mlir-op-gen/mlir-op-gen.cpp @@ -231,6 +231,13 @@ void OpEmitter::emitBuilder() { // Otherwise, generate a default builder that requires all result type, // operands, and attributes as parameters. + // We generate two builders here, one having a stand-alone parameter for + // each result type / operand / attribute, the other having an aggregated + // parameter for all result types / operands / attributes, to facilitate + // different call patterns. + + // 1. Stand-alone parameters + std::vector returnTypes = def.getValueAsListOfDefs("returnTypes"); std::vector operandTypes = def.getValueAsListOfDefs("operandTypes"); @@ -277,6 +284,30 @@ void OpEmitter::emitBuilder() { } os << " }\n"; + + // 2. Aggregated parameters + + // Signature + os << " static void build(Builder* builder, OperationState* result, " + << "ArrayRef resultTypes, ArrayRef args, " + "ArrayRef attributes) {\n"; + + // Result types + os << " assert(resultTypes.size() == " << returnTypes.size() + << "u && \"mismatched number of return types\");\n" + << " result->addTypes(resultTypes);\n"; + + // Operands + os << " assert(args.size() == " << operandTypes.size() + << "u && \"mismatched number of parameters\");\n" + << " result->addOperands(args);\n\n"; + + // Attributes + os << " assert(attributes.size() >= " << attrs.size() + << "u && \"not enough attributes\");\n" + << " for (const auto& pair : attributes)\n" + << " result->addAttribute(pair.first, pair.second);\n" + << " }\n"; } void OpEmitter::emitCanonicalizationPatterns() {