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#include "name_transforms.h"
21
22#include <tvm/runtime/name_transforms.h>
23#include <tvm/runtime/registry.h>
24
25#include <cctype>
26#include <string>
27
28namespace tvm {
29namespace relay {
30namespace backend {
31
32std::string ToCamel(const std::string& original_name) {
33 std::string camel_name;
34 camel_name.reserve(original_name.size());
35
36 bool new_block = true;
37 for (const char& symbol : original_name) {
38 if (std::isalpha(symbol)) {
39 if (new_block) {
40 camel_name.push_back(std::toupper(symbol));
41 new_block = false;
42 } else {
43 camel_name.push_back(std::tolower(symbol));
44 }
45 } else if (symbol == '_') {
46 new_block = true;
47 }
48 }
49 return camel_name;
50}
51
52std::string ToCFunctionStyle(const std::string& original_name) {
53 ICHECK(!original_name.empty()) << "Function name is empty";
54 ICHECK_EQ(original_name.find("TVM"), 0) << "Function not TVM prefixed";
55
56 int tvm_prefix_length = 3;
57 std::string function_prefix("TVM");
58
59 return function_prefix + ToCamel(original_name.substr(tvm_prefix_length));
60}
61
62std::string ToCVariableStyle(const std::string& original_name) {
63 ICHECK(!original_name.empty()) << "Variable name is empty";
64 ICHECK_EQ(original_name.find("TVM"), 0) << "Variable not TVM prefixed";
65
66 std::string variable_name;
67 variable_name.resize(original_name.size());
68
69 std::transform(original_name.begin(), original_name.end(), variable_name.begin(), ::tolower);
70 return variable_name;
71}
72
73std::string ToCConstantStyle(const std::string& original_name) {
74 ICHECK_EQ(original_name.find("TVM"), 0) << "Constant not TVM prefixed";
75 std::string constant_name = ToCVariableStyle(original_name);
76
77 std::transform(constant_name.begin(), constant_name.end(), constant_name.begin(), ::toupper);
78 return constant_name;
79}
80
81std::string ToRustStructStyle(const std::string& original_name) {
82 ICHECK(!original_name.empty()) << "Struct name is empty";
83 return ToCamel(original_name);
84}
85
86std::string ToRustMacroStyle(const std::string& original_name) {
87 ICHECK(!original_name.empty()) << "Macro name is empty";
88
89 std::string macro_name;
90 macro_name.resize(original_name.size());
91
92 std::transform(original_name.begin(), original_name.end(), macro_name.begin(), ::tolower);
93 return macro_name;
94}
95
96std::string ToRustConstantStyle(const std::string& original_name) {
97 ICHECK(!original_name.empty()) << "Constant name is empty";
98 std::string constant_name;
99 constant_name.resize(original_name.size());
100
101 std::transform(original_name.begin(), original_name.end(), constant_name.begin(), ::toupper);
102 return constant_name;
103}
104
105std::string CombineNames(const Array<String>& names) {
106 std::stringstream combine_stream;
107 ICHECK(!names.empty()) << "Name segments empty";
108
109 for (const String& name : names) {
110 ICHECK(!name.empty()) << "Name segment is empty";
111 combine_stream << name << "_";
112 }
113
114 std::string combined_name = combine_stream.str();
115 combined_name.pop_back();
116 return combined_name;
117}
118
119TVM_REGISTER_GLOBAL("relay.backend.ToCFunctionStyle").set_body_typed(ToCFunctionStyle);
120TVM_REGISTER_GLOBAL("relay.backend.ToCVariableStyle").set_body_typed(ToCVariableStyle);
121TVM_REGISTER_GLOBAL("relay.backend.ToCConstantStyle").set_body_typed(ToCConstantStyle);
122TVM_REGISTER_GLOBAL("relay.backend.PrefixName").set_body_typed(PrefixName);
123TVM_REGISTER_GLOBAL("relay.backend.PrefixGeneratedName").set_body_typed(PrefixGeneratedName);
124
125} // namespace backend
126} // namespace relay
127} // namespace tvm
128