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_t>>> RunMlirTextWithHostBuffers( 30 absl::string_view module_text, 31 std::vector<absl::Span<uint8_t>> arguments); 32 33 StatusOr<std::unique_ptr<Executable>> CompileMlirText( 34 absl::string_view module_text); 35 36 template <typename T> ToUint8Span(std::vector<T> * v)37 static absl::Span<uint8_t> ToUint8Span(std::vector<T>* v) { 38 return absl::Span<uint8_t>(reinterpret_cast<uint8_t*>(v->data()), 39 v->size() * sizeof(T)); 40 } 41 42 template <typename T> FromUint8Span(absl::Span<const uint8_t> span)43 static absl::Span<const T> FromUint8Span(absl::Span<const uint8_t> span) { 44 CHECK_EQ(0, span.size() % sizeof(T)); 45 return absl::Span<const T>(reinterpret_cast<const T*>(span.data()), 46 span.size() / sizeof(T)); 47 } 48 49 StreamPool::Ptr BorrowStream(); 50 51 private: 52 StatusOr<std::vector<std::vector<uint8_t>>> RunMlirModuleWithHostBuffers( 53 mlir::ModuleOp module, std::vector<absl::Span<uint8_t>> arguments); 54 55 StatusOr<std::unique_ptr<Executable>> CompileMlirModule(mlir::ModuleOp module, 56 se::Stream* stream); 57 58 StatusOr<ExecutionOutput> RunMlirModule( 59 mlir::ModuleOp module, se::Stream* stream, 60 absl::Span<const se::DeviceMemoryBase> arguments); 61 62 StatusOr<mlir::OwningOpRef<mlir::ModuleOp>> ParseMlirModule( 63 absl::string_view module_text, mlir::MLIRContext& context); 64 65 std::unique_ptr<xla::Backend> backend_; 66 }; 67 68 } // namespace gpu 69 } // namespace xla 70 71 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_TESTS_MLIR_GPU_TEST_BASE_H_ 72