Add util for converting between llvm and absl view container objects (StringRef <-> string_view, {Mutable}ArrayRef <-> Span).

This keeps all bijections in one location to be imported and reused, and will reduce copying strings and arrays when passing between TensorFlow and MLIR.

PiperOrigin-RevId: 328768904
Change-Id: I9dd9a24906df7587b199f9bae4f2242f8f343623
This commit is contained in:
Andy Ly 2020-08-27 10:47:13 -07:00 committed by TensorFlower Gardener
parent 331e92dc50
commit d4617757bb
3 changed files with 103 additions and 0 deletions

View File

@ -24,6 +24,24 @@ filegroup(
srcs = glob(["**/*.td"]),
)
cc_library(
name = "string_container_utils",
hdrs = ["utils/string_container_utils.h"],
deps = [
"@com_google_absl//absl/strings",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "array_container_utils",
hdrs = ["utils/array_container_utils.h"],
deps = [
"@com_google_absl//absl/types:span",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "name_utils",
srcs = ["utils/name_utils.cc"],

View File

@ -0,0 +1,51 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_COMPILER_MLIR_UTILS_ARRAY_CONTAINER_UTILS_H_
#define TENSORFLOW_COMPILER_MLIR_UTILS_ARRAY_CONTAINER_UTILS_H_
#include "absl/types/span.h"
#include "llvm/ADT/ArrayRef.h"
namespace mlir {
template <typename T>
inline llvm::ArrayRef<T> SpanToArrayRef(absl::Span<const T> span) {
return llvm::ArrayRef<T>(span.data(), span.size());
}
template <typename T>
inline llvm::ArrayRef<T> SpanToArrayRef(absl::Span<T> span) {
return llvm::ArrayRef<T>(span.data(), span.size());
}
template <typename T>
inline llvm::MutableArrayRef<T> SpanToMutableArrayRef(absl::Span<T> span) {
return llvm::MutableArrayRef<T>(span.data(), span.size());
}
template <typename T>
inline absl::Span<const T> ArrayRefToSpan(llvm::ArrayRef<T> ref) {
return absl::Span<const T>(ref.data(), ref.size());
}
template <typename T>
inline absl::Span<T> MutableArrayRefToSpan(llvm::MutableArrayRef<T> ref) {
return absl::Span<T>(ref.data(), ref.size());
}
} // namespace mlir
#endif // TENSORFLOW_COMPILER_MLIR_UTILS_ARRAY_CONTAINER_UTILS_H_

View File

@ -0,0 +1,34 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_COMPILER_MLIR_UTILS_STRING_CONTAINER_UTILS_H_
#define TENSORFLOW_COMPILER_MLIR_UTILS_STRING_CONTAINER_UTILS_H_
#include "absl/strings/string_view.h"
#include "llvm/ADT/StringRef.h"
namespace mlir {
inline absl::string_view StringRefToView(llvm::StringRef ref) {
return absl::string_view(ref.data(), ref.size());
}
inline llvm::StringRef StringViewToRef(absl::string_view view) {
return llvm::StringRef(view.data(), view.size());
}
} // namespace mlir
#endif // TENSORFLOW_COMPILER_MLIR_UTILS_STRING_CONTAINER_UTILS_H_