1 /* Copyright 2022 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 XLA_RUNTIME_MEMORY_MAPPER_H_ 17 #define XLA_RUNTIME_MEMORY_MAPPER_H_ 18 19 #include <memory> 20 #include <string> 21 #include <system_error> // NOLINT 22 23 #include "tensorflow/core/platform/platform.h" 24 25 #if defined(PLATFORM_GOOGLE) 26 #include "tensorflow/compiler/xla/runtime/google/memory_mapper.h" 27 #else 28 #include "tensorflow/compiler/xla/runtime/default/memory_mapper.h" 29 #endif 30 31 #include "llvm/ExecutionEngine/SectionMemoryManager.h" 32 33 namespace xla { 34 namespace runtime { 35 36 // XLA runtime memory mapper allocates memory for XLA executables (object files) 37 // using `memfd_create` system call, and gives the user-friendly name to the 38 // file descriptor, so that for example in `perf` it should be possible to 39 // identify input XLA programs by name. 40 class XlaRuntimeMemoryMapper final 41 : public llvm::SectionMemoryManager::MemoryMapper { 42 public: 43 static std::unique_ptr<XlaRuntimeMemoryMapper> Create(llvm::StringRef name); 44 45 llvm::sys::MemoryBlock allocateMappedMemory( 46 llvm::SectionMemoryManager::AllocationPurpose purpose, size_t len, 47 const llvm::sys::MemoryBlock* const near_block, unsigned prot_flags, 48 std::error_code& error_code) final; 49 50 std::error_code protectMappedMemory(const llvm::sys::MemoryBlock& block, 51 unsigned prot_flags) final; 52 53 std::error_code releaseMappedMemory(llvm::sys::MemoryBlock& block) final; 54 55 private: XlaRuntimeMemoryMapper(llvm::StringRef name)56 explicit XlaRuntimeMemoryMapper(llvm::StringRef name) : name_(name.str()) {} 57 58 std::string name_; 59 }; 60 61 } // namespace runtime 62 } // namespace xla 63 64 #endif // XLA_RUNTIME_MEMORY_MAPPER_H_ 65