1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_LLVM_COMMAND_LINE_OPTIONS_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_LLVM_COMMAND_LINE_OPTIONS_H_ 18 19 #include <string> 20 #include <vector> 21 22 #include "absl/strings/match.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "tensorflow/core/platform/logging.h" 25 26 namespace xla { 27 namespace llvm_ir { 28 29 // Given a map with options (e.g. originating from xla_backend_extra_options()) 30 // pass those that don't start with xla_ to LLVM. 31 template <typename T> InitializeLLVMCommandLineOptions(const T & options)32void InitializeLLVMCommandLineOptions(const T& options) { 33 if (!options.empty()) { 34 std::vector<std::string> fake_argv_storage; 35 fake_argv_storage.push_back(""); 36 for (const auto& it : options) { 37 // Skip options the XLA backend itself consumes. 38 if (!absl::StartsWith(it.first, "xla_")) { 39 if (it.second.empty()) { 40 fake_argv_storage.push_back(it.first); 41 } else { 42 fake_argv_storage.push_back(it.first + "=" + it.second); 43 } 44 } 45 } 46 47 VLOG(2) << "Passing argv to LLVM:"; 48 std::vector<const char*> fake_argv; 49 for (const auto& s : fake_argv_storage) { 50 fake_argv.push_back(s.c_str()); 51 VLOG(2) << s; 52 } 53 llvm::cl::ParseCommandLineOptions(static_cast<int>(fake_argv.size()), 54 &fake_argv[0]); 55 } 56 } 57 58 } // namespace llvm_ir 59 } // namespace xla 60 61 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_LLVM_COMMAND_LINE_OPTIONS_H_ 62