<commit_before>/*
 * Copyright 2022 The TensorFlow Runtime Authors
 *
 * 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.
 */

//===- memory_mapper.cc - -------------------------------------------------===//
// Support for managing memory from a SectionMemoryManager via a memfd.
// This allows profilers to correctly label JIT-executed code.
//===----------------------------------------------------------------------===//

#include "tfrt/jitrt/memory_mapper.h"

#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>

#include <system_error>

#include "llvm/ExecutionEngine/SectionMemoryManager.h"

// Support for memfd_create(2) was added in glibc v2.27.
#if defined(__linux__) && defined(__GLIBC__) && defined(__GLIBC_PREREQ)
#if __GLIBC_PREREQ(2, 27)
#define ENABLE_JITRT_MEMORY_MAPPER
#endif  // __GLIBC_PREREQ(2, 27)
#endif  // __linux__ and __GLIBC__ and __GLIBC_PREREQ

#ifndef ENABLE_JITRT_MEMORY_MAPPER
namespace tfrt {
namespace jitrt {

std::unique_ptr<JitRtMemoryMapper> JitRtMemoryMapper::Create(
    llvm::StringRef name) {
  return nullptr;
}

}  // namespace jitrt
}  // namespace tfrt

#else  // ENABLE_JITRT_MEMORY_MAPPER

namespace {
using MemoryMapper = llvm::SectionMemoryManager::MemoryMapper;
using AllocationPurpose = llvm::SectionMemoryManager::AllocationPurpose;

// Some syscalls can be interrupted by a signal handler; retry if that happens.
template <typename FunctionType>
static auto RetryOnEINTR(FunctionType func, decltype(func()) failure_value)
    -> decltype(func()) {
  using ReturnType = decltype(func());

  ReturnType ret;<commit_msg>[tfrt:jitrt] Implement fake MemoryMapper virtual methods<commit_after>/*
 * Copyright 2022 The TensorFlow Runtime Authors
 *
 * 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.
 */

//===- memory_mapper.cc - -------------------------------------------------===//
// Support for managing memory from a SectionMemoryManager via a memfd.
// This allows profilers to correctly label JIT-executed code.
//===----------------------------------------------------------------------===//

#include "tfrt/jitrt/memory_mapper.h"

#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>

#include "llvm/ExecutionEngine/SectionMemoryManager.h"

// Support for memfd_create(2) was added in glibc v2.27.
#if defined(__linux__) && defined(__GLIBC__) && defined(__GLIBC_PREREQ)
#if __GLIBC_PREREQ(2, 27)
#define ENABLE_JITRT_MEMORY_MAPPER
#endif  // __GLIBC_PREREQ(2, 27)
#endif  // __linux__ and __GLIBC__ and __GLIBC_PREREQ

#ifndef ENABLE_JITRT_MEMORY_MAPPER
namespace tfrt {
namespace jitrt {

std::unique_ptr<JitRtMemoryMapper> JitRtMemoryMapper::Create(
    llvm::StringRef name) {
  return nullptr;
}

llvm::sys::MemoryBlock JitRtMemoryMapper::allocateMappedMemory(
    llvm::SectionMemoryManager::AllocationPurpose purpose, size_t len,
    const llvm::sys::MemoryBlock* const near_block, unsigned prot_flags,
    std::error_code& error_code) {
  llvm_unreachable("JitRtMemoryMapper is not implemented");
}

std::error_code JitRtMemoryMapper::protectMappedMemory(
    const llvm::sys::MemoryBlock& block, unsigned prot_flags) {
  llvm_unreachable("JitRtMemoryMapper is not implemented");
}

std::error_code JitRtMemoryMapper::releaseMappedMemory(
    llvm::sys::MemoryBlock& block) {
  llvm_unreachable("JitRtMemoryMapper is not implemented");
}

}  // namespace jitrt
}  // namespace tfrt

#else  // ENABLE_JITRT_MEMORY_MAPPER

namespace {
using MemoryMapper = llvm::SectionMemoryManager::MemoryMapper;
using AllocationPurpose = llvm::SectionMemoryManager::AllocationPurpose;

// Some syscalls can be interrupted by a signal handler; retry if that happens.
template <typename FunctionType>
static auto RetryOnEINTR(FunctionType func, decltype(func()) failure_value)
    -> decltype(func()) {
  using ReturnType = decltype(func());

  ReturnType ret;
{'fraction_non_alphanumeric': 0.14536444352673963, 'fraction_numerical': 0.007433409043981004, 'mean_word_length': 3.810327706057597, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2745619', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1446, 'n_words': 429}