|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * \file interface_c.cc |
| 22 | + * \brief Generates a C interface header for a given modules inputs and outputs |
| 23 | + */ |
| 24 | + |
| 25 | +#include <tvm/runtime/container/array.h> |
| 26 | +#include <tvm/runtime/container/string.h> |
| 27 | +#include <tvm/runtime/module.h> |
| 28 | +#include <tvm/runtime/packed_func.h> |
| 29 | +#include <tvm/runtime/registry.h> |
| 30 | + |
| 31 | +#include <string> |
| 32 | + |
| 33 | +#include "../../relay/backend/name_transforms.h" |
| 34 | + |
| 35 | +namespace tvm { |
| 36 | +namespace codegen { |
| 37 | + |
| 38 | +using runtime::PackedFunc; |
| 39 | +using namespace tvm::relay::backend; |
| 40 | + |
| 41 | +class InterfaceCNode : public runtime::ModuleNode { |
| 42 | + public: |
| 43 | + InterfaceCNode(std::string module_name, Array<String> inputs, Array<String> outputs) |
| 44 | + : module_name_(module_name), inputs_(inputs), outputs_(outputs) {} |
| 45 | + const char* type_key() const { return "h"; } |
| 46 | + |
| 47 | + std::string GetSource(const std::string& format) final { |
| 48 | + std::stringstream code; |
| 49 | + std::string mangled_module_name = ToCVariableStyle(PrefixGeneratedName({module_name_})); |
| 50 | + std::string header_guard_name = ToCConstantStyle(PrefixGeneratedName({module_name_})); |
| 51 | + |
| 52 | + EmitUpperHeaderGuard(code, header_guard_name); |
| 53 | + EmitBrief(code, "Input tensor pointers"); |
| 54 | + EmitStruct(code, mangled_module_name, "inputs", inputs_); |
| 55 | + EmitBrief(code, "Output tensor pointers"); |
| 56 | + EmitStruct(code, mangled_module_name, "outputs", outputs_); |
| 57 | + EmitRunFunction(code, mangled_module_name); |
| 58 | + EmitLowerHeaderGuard(code, header_guard_name); |
| 59 | + |
| 60 | + return code.str(); |
| 61 | + } |
| 62 | + |
| 63 | + PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) final { |
| 64 | + return PackedFunc(nullptr); |
| 65 | + } |
| 66 | + |
| 67 | + private: |
| 68 | + void EmitUpperHeaderGuard(std::stringstream& code_stream, const std::string& header_guard_name) { |
| 69 | + code_stream << "#ifndef " << header_guard_name << "_H_\n" |
| 70 | + << "#define " << header_guard_name << "_H_\n" |
| 71 | + << "#include <stdint.h>\n\n" |
| 72 | + << "#ifdef __cplusplus\n" |
| 73 | + << "extern \"C\" {\n" |
| 74 | + << "#endif\n\n"; |
| 75 | + } |
| 76 | + |
| 77 | + void EmitLowerHeaderGuard(std::stringstream& code_stream, const std::string& header_guard_name) { |
| 78 | + code_stream << "\n#ifdef __cplusplus\n" |
| 79 | + << "}\n" |
| 80 | + << "#endif\n\n" |
| 81 | + << "#endif // " << header_guard_name << "_H_\n"; |
| 82 | + } |
| 83 | + |
| 84 | + void EmitBrief(std::stringstream& code_stream, const std::string& description) { |
| 85 | + code_stream << "/*!\n" |
| 86 | + << " * \\brief " << description << " for TVM module \"" << module_name_ << "\" \n" |
| 87 | + << " */\n"; |
| 88 | + } |
| 89 | + |
| 90 | + void EmitStruct(std::stringstream& code_stream, const std::string& mangled_module_name, |
| 91 | + const std::string& suffix, Array<String> properties) { |
| 92 | + code_stream << "struct " << mangled_module_name << "_" << suffix << " {\n"; |
| 93 | + |
| 94 | + std::vector<std::string> sanitised_properties; |
| 95 | + for (const String& property : properties) { |
| 96 | + std::string sanitised_property = SanitiseName(property); |
| 97 | + ICHECK(std::find(sanitised_properties.begin(), sanitised_properties.end(), |
| 98 | + sanitised_property) == sanitised_properties.end()) |
| 99 | + << "Sanitized input tensor name clash" << sanitised_property; |
| 100 | + code_stream << " void* " << sanitised_property << ";\n"; |
| 101 | + sanitised_properties.push_back(sanitised_property); |
| 102 | + } |
| 103 | + code_stream << "};\n\n"; |
| 104 | + } |
| 105 | + |
| 106 | + void EmitRunFunction(std::stringstream& code_stream, const std::string& mangled_module_name) { |
| 107 | + code_stream << "/*!\n" |
| 108 | + << " * \\brief entrypoint function for TVM module \"" << module_name_ << "\"\n" |
| 109 | + << " * \\param inputs Input tensors for the module \n" |
| 110 | + << " * \\param outputs Output tensors for the module \n" |
| 111 | + << " */\n" |
| 112 | + << "int32_t " << mangled_module_name << "_run(\n" |
| 113 | + << " struct " << mangled_module_name << "_inputs* inputs,\n" |
| 114 | + << " struct " << mangled_module_name << "_outputs* outputs\n" |
| 115 | + << ");\n"; |
| 116 | + } |
| 117 | + |
| 118 | + std::string module_name_; |
| 119 | + Array<String> inputs_; |
| 120 | + Array<String> outputs_; |
| 121 | +}; |
| 122 | + |
| 123 | +runtime::Module InterfaceCCreate(std::string module_name, Array<String> inputs, |
| 124 | + Array<String> outputs) { |
| 125 | + auto n = make_object<InterfaceCNode>(module_name, inputs, outputs); |
| 126 | + return runtime::Module(n); |
| 127 | +} |
| 128 | + |
| 129 | +TVM_REGISTER_GLOBAL("runtime.InterfaceCCreate").set_body_typed(InterfaceCCreate); |
| 130 | + |
| 131 | +} // namespace codegen |
| 132 | +} // namespace tvm |
0 commit comments