|
8 | 8 | // |
9 | 9 | // Unless required by applicable law or agreed to in writing, software |
10 | 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expruns or implied. |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
|
24 | 24 |
|
25 | 25 | #include "absl/flags/flag.h" // from @com_google_absl |
26 | 26 | #include "absl/flags/parse.h" // from @com_google_absl |
| 27 | +#include "absl/functional/function_ref.h" // from @com_google_absl |
27 | 28 | #include "absl/strings/str_format.h" // from @com_google_absl |
| 29 | +#include "litert/cc/litert_expected.h" |
| 30 | +#include "litert/cc/litert_opaque_options.h" |
28 | 31 | #include "litert/cc/litert_options.h" |
| 32 | +#include "litert/cc/options/compiler_options.h" |
| 33 | +#include "litert/cc/options/litert_google_tensor_options.h" |
| 34 | +#include "litert/cc/options/litert_intel_openvino_options.h" |
| 35 | +#include "litert/cc/options/litert_mediatek_options.h" |
| 36 | +#include "litert/cc/options/litert_qualcomm_options.h" |
29 | 37 | #include "litert/tools/apply_plugin.h" |
30 | 38 | #include "litert/tools/flags/apply_plugin_flags.h" |
31 | 39 | #include "litert/tools/flags/common_flags.h" |
|
36 | 44 | #include "litert/tools/flags/vendors/qualcomm_flags.h" // IWYU pragma: keep |
37 | 45 | #include "litert/tools/outstream.h" |
38 | 46 |
|
| 47 | +namespace { |
| 48 | + |
39 | 49 | using ::litert::tools::ApplyPlugin; |
40 | 50 | using ::litert::tools::ApplyPluginRun; |
41 | 51 | using ::litert::tools::IntList; |
@@ -80,6 +90,28 @@ ApplyPluginRun::Ptr ParseFlags() { |
80 | 90 | return res; |
81 | 91 | } |
82 | 92 |
|
| 93 | +template <typename T> |
| 94 | +bool ParseAndAddOptions( |
| 95 | + litert::Options& opts, ApplyPluginRun::Ptr& run, |
| 96 | + absl::FunctionRef<litert::Expected<T>()> options_from_flags) { |
| 97 | + static_assert(std::is_base_of_v<litert::OpaqueOptions, T>); |
| 98 | + litert::Expected<T> options = options_from_flags(); |
| 99 | + if (!options) { |
| 100 | + run->dump_out.Get().get() |
| 101 | + << "Failed to create " << T::Discriminator() |
| 102 | + << " options, Error:" << options.Error().Message() << "\n"; |
| 103 | + return false; |
| 104 | + } |
| 105 | + if (!opts.AddOpaqueOptions(std::move(*options))) { |
| 106 | + run->dump_out.Get().get() |
| 107 | + << "Failed to add " << T::Discriminator() << " options to list\n"; |
| 108 | + return false; |
| 109 | + } |
| 110 | + return true; |
| 111 | +} |
| 112 | + |
| 113 | +} // namespace |
| 114 | + |
83 | 115 | int main(int argc, char* argv[]) { |
84 | 116 | absl::ParseCommandLine(argc, argv); |
85 | 117 |
|
@@ -110,62 +142,20 @@ int main(int argc, char* argv[]) { |
110 | 142 | return 1; |
111 | 143 | } |
112 | 144 |
|
113 | | - { |
114 | | - auto qnn_opts = litert::qualcomm::QualcommOptionsFromFlags(); |
115 | | - if (!qnn_opts) { |
116 | | - run->dump_out.Get().get() << "Failed to create Qualcomm options\n"; |
117 | | - return 1; |
118 | | - } |
119 | | - |
120 | | - if (!opts->AddOpaqueOptions(std::move(*qnn_opts))) { |
121 | | - run->dump_out.Get().get() << "Failed to add Qualcomm options to list\n"; |
122 | | - return 1; |
123 | | - } |
124 | | - } |
125 | | - |
126 | | - { |
127 | | - auto google_tensor_opts = |
128 | | - litert::google_tensor::GoogleTensorOptionsFromFlags(); |
129 | | - if (!google_tensor_opts) { |
130 | | - run->dump_out.Get().get() << "Failed to create Google Tensor options\n"; |
131 | | - return 1; |
132 | | - } |
133 | | - |
134 | | - if (!opts->AddOpaqueOptions(std::move(*google_tensor_opts))) { |
135 | | - run->dump_out.Get().get() |
136 | | - << "Failed to add google tensor options to list\n"; |
137 | | - return 1; |
138 | | - } |
139 | | - } |
140 | | - |
141 | | - { |
142 | | - auto intel_openvino_opts = |
143 | | - litert::intel_openvino::IntelOpenVinoOptionsFromFlags(); |
144 | | - if (!intel_openvino_opts) { |
145 | | - run->dump_out.Get().get() << "Failed to create Intel OpenVINO options\n"; |
146 | | - return 1; |
147 | | - } |
148 | | - |
149 | | - if (!opts->AddOpaqueOptions(std::move(*intel_openvino_opts))) { |
150 | | - run->dump_out.Get().get() |
151 | | - << "Failed to add Intel OpenVINO options to list\n"; |
152 | | - return 1; |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - { |
157 | | - auto mediatek_opts = |
158 | | - litert::mediatek::MediatekOptionsFromFlags(); |
159 | | - if (!mediatek_opts) { |
160 | | - run->dump_out.Get().get() << "Failed to create Mediatek options\n"; |
161 | | - return 1; |
162 | | - } |
163 | | - |
164 | | - if (!opts->AddOpaqueOptions(std::move(*mediatek_opts))) { |
165 | | - run->dump_out.Get().get() |
166 | | - << "Failed to add Mediatek options to list\n"; |
167 | | - return 1; |
168 | | - } |
| 145 | + bool all_flags_parsed = |
| 146 | + (ParseAndAddOptions<litert::CompilerOptions>( |
| 147 | + *opts, run, litert::CompilerOptionsFromFlags) || |
| 148 | + ParseAndAddOptions<litert::qualcomm::QualcommOptions>( |
| 149 | + *opts, run, litert::qualcomm::QualcommOptionsFromFlags) || |
| 150 | + ParseAndAddOptions<litert::google_tensor::GoogleTensorOptions>( |
| 151 | + *opts, run, litert::google_tensor::GoogleTensorOptionsFromFlags) || |
| 152 | + ParseAndAddOptions<litert::intel_openvino::IntelOpenVinoOptions>( |
| 153 | + *opts, run, litert::intel_openvino::IntelOpenVinoOptionsFromFlags) || |
| 154 | + ParseAndAddOptions<litert::mediatek::MediatekOptions>( |
| 155 | + *opts, run, litert::mediatek::MediatekOptionsFromFlags)); |
| 156 | + |
| 157 | + if (!all_flags_parsed) { |
| 158 | + return 1; |
169 | 159 | } |
170 | 160 |
|
171 | 161 | run->options = std::move(*opts); |
|
0 commit comments