• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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_GPU_TESTS_MLIR_GPU_TEST_BASE_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_TESTS_MLIR_GPU_TEST_BASE_H_
18 
19 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
20 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
21 
22 namespace xla {
23 namespace gpu {
24 
25 class MlirGpuTestBase : public HloTestBase {
26  public:
27   MlirGpuTestBase();
28 
29   StatusOr<std::vector<std::vector<uint8>>> RunMlirTextWithHostBuffers(
30       absl::string_view module_text, std::vector<absl::Span<uint8>> arguments);
31 
32   StatusOr<std::unique_ptr<Executable>> CompileMlirText(
33       absl::string_view module_text);
34 
35   template <typename T>
ToUint8Span(std::vector<T> * v)36   static absl::Span<uint8> ToUint8Span(std::vector<T>* v) {
37     return absl::Span<uint8>(reinterpret_cast<uint8*>(v->data()),
38                              v->size() * sizeof(T));
39   }
40 
41   template <typename T>
FromUint8Span(absl::Span<const uint8> span)42   static absl::Span<const T> FromUint8Span(absl::Span<const uint8> span) {
43     CHECK_EQ(0, span.size() % sizeof(T));
44     return absl::Span<const T>(reinterpret_cast<const T*>(span.data()),
45                                span.size() / sizeof(T));
46   }
47 
48  private:
49   StatusOr<std::vector<std::vector<uint8>>> RunMlirModuleWithHostBuffers(
50       mlir::ModuleOp module, std::vector<absl::Span<uint8>> arguments);
51 
52   StatusOr<std::unique_ptr<Executable>> CompileMlirModule(mlir::ModuleOp module,
53                                                           se::Stream* stream);
54 
55   StatusOr<ExecutionOutput> RunMlirModule(
56       mlir::ModuleOp module, se::Stream* stream,
57       absl::Span<const se::DeviceMemoryBase> arguments);
58 
59   mlir::OwningModuleRef ParseMlirModule(absl::string_view module_text,
60                                         mlir::MLIRContext& context);
61 
62   std::unique_ptr<xla::Backend> backend_;
63 };
64 
65 }  // namespace gpu
66 }  // namespace xla
67 
68 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_TESTS_MLIR_GPU_TEST_BASE_H_
69