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 relay/backend/name_transforms.h |
22 | * \brief Transformations which are applied on names to generate appropriately named compiler |
23 | * artifacts |
24 | * |
25 | * Example: |
26 | * ToCFunctionStyle(PrefixName(CombineNames({"Device", "target", "Invoke"}))) |
27 | * // TVMDeviceTargetInvoke |
28 | * |
29 | * ToCFunctionStyle(PrefixGeneratedName(CombineNames({"model", "Run"}))) |
30 | * // TVMGenModelRun |
31 | * |
32 | * ToCVariableStyle(PrefixName(CombineNames({"Device", "target", "t"}))) |
33 | * // tvm_device_target_t |
34 | * |
35 | * ToCVariableStyle(PrefixGeneratedName(CombineNames({"model", "Devices"}))) |
36 | * // tvmgen_model_devices |
37 | * |
38 | * ToCConstantStyle(PrefixGeneratedName(CombineNames({"model", "Devices"}))) |
39 | * // TVMGEN_MODEL_DEVICES |
40 | * |
41 | */ |
42 | |
43 | #include <tvm/runtime/container/array.h> |
44 | #include <tvm/runtime/container/string.h> |
45 | #include <tvm/runtime/logging.h> |
46 | |
47 | #include <algorithm> |
48 | #include <iostream> |
49 | #include <string> |
50 | |
51 | #ifndef TVM_RELAY_BACKEND_NAME_TRANSFORMS_H_ |
52 | #define TVM_RELAY_BACKEND_NAME_TRANSFORMS_H_ |
53 | |
54 | namespace tvm { |
55 | namespace relay { |
56 | namespace backend { |
57 | |
58 | /*! |
59 | * \brief Transform a name to the C variable style assuming it is |
60 | * appropriately constructed using the prefixing functions |
61 | * \param original_name Original name |
62 | * \return Transformed function in the C function style |
63 | */ |
64 | std::string ToCFunctionStyle(const std::string& original_name); |
65 | |
66 | /*! |
67 | * \brief Transform a name to the C variable style assuming it is |
68 | * appropriately constructed using the prefixing functions |
69 | * \param name Original name |
70 | * \return Transformed function in the C variable style |
71 | */ |
72 | std::string ToCVariableStyle(const std::string& original_name); |
73 | |
74 | /*! |
75 | * \brief Transform a name to the C constant style assuming it is |
76 | * appropriately constructed using the prefixing functions |
77 | * \param name Original name |
78 | * \return Transformed function in the C constant style |
79 | */ |
80 | std::string ToCConstantStyle(const std::string& original_name); |
81 | |
82 | /*! |
83 | * \brief Transform a name to the Rust struct style assuming it is |
84 | * appropriately constructed using the combining functions |
85 | * \param name Original name |
86 | * \return Transformed function in the Rust struct style |
87 | */ |
88 | std::string ToRustStructStyle(const std::string& original_name); |
89 | |
90 | /*! |
91 | * \brief Transform a name to the Rust macro style assuming it is |
92 | * appropriately constructed using the combining functions |
93 | * \param name Original name |
94 | * \return Transformed function in the Rust macro style |
95 | */ |
96 | std::string ToRustMacroStyle(const std::string& original_name); |
97 | |
98 | /*! |
99 | * \brief Transform a name to the Rust constant style assuming it is |
100 | * appropriately constructed using the combining functions |
101 | * \param name Original name |
102 | * \return Transformed function in the Rust constant style |
103 | */ |
104 | std::string ToRustConstantStyle(const std::string& original_name); |
105 | |
106 | /*! |
107 | * \brief Combine names together for use as a generated name |
108 | * \param names Vector of strings to combine |
109 | * \return Combined together names |
110 | */ |
111 | std::string CombineNames(const Array<String>& names); |
112 | |
113 | /*! |
114 | * \brief Apply TVM-specific prefix to a name |
115 | * \param names Vector of names to combine to form a combined name |
116 | * \return Name with prefix applied or prefix-only if no name passed |
117 | */ |
118 | inline std::string PrefixName(const Array<String>& names) { return "TVM_" + CombineNames(names); } |
119 | |
120 | /*! |
121 | * \brief Apply generated TVM-specific prefix to a name |
122 | * \param names Vector of names to combine to form a combined name |
123 | * \return Name with prefix applied or prefix-only if no name passed |
124 | */ |
125 | inline std::string PrefixGeneratedName(const Array<String>& names) { |
126 | return "TVMGen_" + CombineNames(names); |
127 | } |
128 | |
129 | } // namespace backend |
130 | } // namespace relay |
131 | } // namespace tvm |
132 | |
133 | #endif // TVM_RELAY_BACKEND_NAME_TRANSFORMS_H_ |
134 | |