For each TFLite op, emits a builder function that packs the TFLite op into the corresponding FlatBuffer object. TODO(hinsu): Revisit if only builtin_options and mutating_variable_inputs arguments that depend on op definitions should be auto-generated and then operator should be built by the caller because it does not require auto-generation.
| 158 | // operator should be built by the caller because it does not require |
| 159 | // auto-generation. |
| 160 | static void EmitOperatorBuilders(const std::vector<Record *> &defs, |
| 161 | raw_ostream *ostream) { |
| 162 | raw_ostream &os = *ostream; |
| 163 | |
| 164 | for (const auto *def : defs) { |
| 165 | StringRef op_name = def->getName().drop_front(4); |
| 166 | |
| 167 | // Signature |
| 168 | os << "static flatbuffers::Offset<tflite::Operator> " |
| 169 | << GetOperatorBuilderName(def->getName()) << "(mlir::TFL::" << op_name |
| 170 | << " tflOp, uint32_t opcode_index, " |
| 171 | << "const std::vector<int32_t>& operands," |
| 172 | << "const std::vector<int32_t>& results," |
| 173 | << "flatbuffers::FlatBufferBuilder *fbb) {\n"; |
| 174 | |
| 175 | // Inputs & outputs |
| 176 | os << " auto inputs = fbb->CreateVector(operands);\n" |
| 177 | " auto outputs = fbb->CreateVector(results);\n\n"; |
| 178 | |
| 179 | // Build the FlatBuffer operator |
| 180 | os << " return tflite::CreateOperator(\n" |
| 181 | " *fbb, opcode_index, inputs, outputs,\n"; |
| 182 | if (def->getValueAsBit("hasOptions")) { |
| 183 | auto option_name = GetOperatorOptionName(*def); |
| 184 | std::string tflite_option_name = |
| 185 | option_name == "BasicLSTMOptions" ? "LSTMOptions" : option_name; |
| 186 | os << " tflite::BuiltinOptions_" << tflite_option_name << ", " |
| 187 | << "Create" << option_name << "(tflOp, fbb).Union(),\n"; |
| 188 | } else { |
| 189 | os << " tflite::BuiltinOptions_NONE, /*builtin_options=*/0,\n"; |
| 190 | } |
| 191 | // Only builtin ops' builders are auto-generated. custom_options are only |
| 192 | // used by custom or flex ops and those ops are handled manually. |
| 193 | os << " /*custom_options=*/0, " |
| 194 | "tflite::CustomOptionsFormat_FLEXBUFFERS,\n" |
| 195 | " /*mutating_variable_inputs=*/0);\n" |
| 196 | "}\n\n"; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static inline std::string GetOperatorName(const Record &def) { |
| 201 | auto name = def.getValueAsString("opName"); |
no test coverage detected