xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/llvm_ir/llvm_type_conversion_util.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_TYPE_CONVERSION_UTIL_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_LLVM_TYPE_CONVERSION_UTIL_H_
18 
19 #include <string>
20 #include <vector>
21 
22 #include "absl/strings/string_view.h"
23 #include "absl/types/span.h"
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/ADT/StringRef.h"
26 
27 namespace xla {
28 namespace llvm_ir {
29 
30 // Convert a absl::string_view to a llvm::StringRef. Note: both
31 // absl::string_view and llvm::StringRef are non-owning pointers into a
32 // string in memory. This method is used to feed strings to LLVM
33 // & Clang APIs that expect llvm::StringRef.
AsStringRef(absl::string_view str)34 inline llvm::StringRef AsStringRef(absl::string_view str) {
35   return llvm::StringRef(str.data(), str.size());
36 }
37 
AsStringView(llvm::StringRef str)38 inline absl::string_view AsStringView(llvm::StringRef str) {
39   return absl::string_view(str.data(), str.size());
40 }
41 
42 template <typename T>
AsArrayRef(const std::vector<T> & vec)43 llvm::ArrayRef<T> AsArrayRef(const std::vector<T>& vec) {
44   return llvm::ArrayRef<T>(vec.data(), vec.size());
45 }
46 
47 template <typename T>
AsArrayRef(const absl::Span<const T> slice)48 llvm::ArrayRef<T> AsArrayRef(const absl::Span<const T> slice) {
49   return llvm::ArrayRef<T>(slice.data(), slice.size());
50 }
51 
52 }  // namespace llvm_ir
53 }  // namespace xla
54 
55 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_LLVM_TYPE_CONVERSION_UTIL_H_
56