2020-12-12 13:01:29 +01:00
|
|
|
#include "util/types.hpp"
|
2020-12-18 10:55:54 +01:00
|
|
|
#include "util/sysinfo.hpp"
|
2018-05-14 22:06:17 +02:00
|
|
|
#include "JIT.h"
|
2019-01-26 21:15:45 +01:00
|
|
|
#include "StrFmt.h"
|
|
|
|
#include "File.h"
|
2020-03-07 10:29:23 +01:00
|
|
|
#include "util/logs.hpp"
|
2019-01-26 21:15:45 +01:00
|
|
|
#include "mutex.h"
|
2020-11-07 23:56:35 +01:00
|
|
|
#include "util/vm.hpp"
|
2020-12-18 15:43:34 +01:00
|
|
|
#include "util/asm.hpp"
|
2022-08-24 18:36:37 +02:00
|
|
|
#include "util/v128.hpp"
|
2022-08-29 02:55:59 +02:00
|
|
|
#include "util/simd.hpp"
|
2021-01-19 18:40:15 +01:00
|
|
|
#include <charconv>
|
2019-12-27 20:50:14 +01:00
|
|
|
#include <zlib.h>
|
2018-05-14 22:06:17 +02:00
|
|
|
|
2019-01-26 21:15:45 +01:00
|
|
|
#ifdef __linux__
|
|
|
|
#define CAN_OVERCOMMIT
|
|
|
|
#endif
|
|
|
|
|
2020-02-01 08:43:43 +01:00
|
|
|
LOG_CHANNEL(jit_log, "JIT");
|
2020-02-01 05:15:50 +01:00
|
|
|
|
2021-12-24 18:33:32 +01:00
|
|
|
void jit_announce(uptr func, usz size, std::string_view name)
|
|
|
|
{
|
2022-09-08 13:34:17 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
static const struct tmp_perf_map
|
|
|
|
{
|
|
|
|
std::string name{fmt::format("/tmp/perf-%d.map", getpid())};
|
|
|
|
fs::file data{name, fs::rewrite + fs::append};
|
|
|
|
|
|
|
|
tmp_perf_map() = default;
|
|
|
|
tmp_perf_map(const tmp_perf_map&) = delete;
|
|
|
|
tmp_perf_map& operator=(const tmp_perf_map&) = delete;
|
|
|
|
|
|
|
|
~tmp_perf_map()
|
|
|
|
{
|
|
|
|
fs::remove_file(name);
|
|
|
|
}
|
|
|
|
} s_map;
|
|
|
|
|
|
|
|
if (size && name.size())
|
|
|
|
{
|
|
|
|
s_map.data.write(fmt::format("%x %x %s\n", func, size, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!func && !size && !name.size())
|
|
|
|
{
|
|
|
|
fs::remove_file(s_map.name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-30 17:39:18 +01:00
|
|
|
if (!size)
|
|
|
|
{
|
|
|
|
jit_log.error("Empty function announced: %s (%p)", name, func);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-26 02:48:20 +01:00
|
|
|
// If directory ASMJIT doesn't exist, nothing will be written
|
|
|
|
static constexpr u64 c_dump_size = 0x1'0000'0000;
|
|
|
|
static constexpr u64 c_index_size = c_dump_size / 16;
|
|
|
|
static atomic_t<u64> g_index_off = 0;
|
|
|
|
static atomic_t<u64> g_data_off = c_index_size;
|
|
|
|
|
|
|
|
static void* g_asm = []() -> void*
|
2022-01-23 13:22:00 +01:00
|
|
|
{
|
2022-01-26 02:48:20 +01:00
|
|
|
fs::remove_all(fs::get_cache_dir() + "/ASMJIT/", false);
|
2022-01-23 13:22:00 +01:00
|
|
|
|
2022-01-26 02:48:20 +01:00
|
|
|
fs::file objs(fmt::format("%s/ASMJIT/.objects", fs::get_cache_dir()), fs::read + fs::rewrite);
|
2022-01-23 13:22:00 +01:00
|
|
|
|
2022-01-26 02:48:20 +01:00
|
|
|
if (!objs || !objs.trunc(c_dump_size))
|
2022-01-23 13:22:00 +01:00
|
|
|
{
|
2022-01-26 02:48:20 +01:00
|
|
|
return nullptr;
|
2022-01-23 13:22:00 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 02:48:20 +01:00
|
|
|
return utils::memory_map_fd(objs.get_handle(), c_dump_size, utils::protection::rw);
|
|
|
|
}();
|
|
|
|
|
|
|
|
if (g_asm && size < c_index_size)
|
|
|
|
{
|
|
|
|
struct entry
|
|
|
|
{
|
|
|
|
u64 addr; // RPCS3 process address
|
|
|
|
u32 size; // Function size
|
|
|
|
u32 off; // Function offset
|
|
|
|
};
|
|
|
|
|
|
|
|
// Write index entry at the beginning of file, and data + NTS name at fixed offset
|
|
|
|
const u64 index_off = g_index_off.fetch_add(1);
|
|
|
|
const u64 size_all = size + name.size() + 1;
|
|
|
|
const u64 data_off = g_data_off.fetch_add(size_all);
|
|
|
|
|
|
|
|
// If either index or data area is exhausted, nothing will be written
|
|
|
|
if (index_off < c_index_size / sizeof(entry) && data_off + size_all < c_dump_size)
|
2022-01-23 13:22:00 +01:00
|
|
|
{
|
2022-01-26 02:48:20 +01:00
|
|
|
entry& index = static_cast<entry*>(g_asm)[index_off];
|
2022-01-23 13:22:00 +01:00
|
|
|
|
2022-01-26 02:48:20 +01:00
|
|
|
std::memcpy(static_cast<char*>(g_asm) + data_off, reinterpret_cast<char*>(func), size);
|
|
|
|
std::memcpy(static_cast<char*>(g_asm) + data_off + size, name.data(), name.size());
|
|
|
|
index.size = static_cast<u32>(size);
|
|
|
|
index.off = static_cast<u32>(data_off);
|
|
|
|
atomic_storage<u64>::store(index.addr, func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_asm && !name.empty() && name[0] != '_')
|
|
|
|
{
|
|
|
|
// Save some objects separately
|
|
|
|
fs::file dump(fmt::format("%s/ASMJIT/%s", fs::get_cache_dir(), name), fs::rewrite);
|
|
|
|
|
|
|
|
if (dump)
|
|
|
|
{
|
|
|
|
dump.write(reinterpret_cast<uchar*>(func), size);
|
2022-01-23 13:22:00 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-24 18:33:32 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 21:15:45 +01:00
|
|
|
static u8* get_jit_memory()
|
|
|
|
{
|
|
|
|
// Reserve 2G memory (magic static)
|
|
|
|
static void* const s_memory2 = []() -> void*
|
|
|
|
{
|
|
|
|
void* ptr = utils::memory_reserve(0x80000000);
|
2021-02-22 11:32:35 +01:00
|
|
|
#ifdef CAN_OVERCOMMIT
|
2019-01-26 21:15:45 +01:00
|
|
|
utils::memory_commit(ptr, 0x80000000);
|
|
|
|
utils::memory_protect(ptr, 0x40000000, utils::protection::wx);
|
2021-02-22 11:32:35 +01:00
|
|
|
#endif
|
2019-01-26 21:15:45 +01:00
|
|
|
return ptr;
|
|
|
|
}();
|
|
|
|
|
|
|
|
return static_cast<u8*>(s_memory2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocation counters (1G code, 1G data subranges)
|
|
|
|
static atomic_t<u64> s_code_pos{0}, s_data_pos{0};
|
|
|
|
|
|
|
|
// Snapshot of code generated before main()
|
|
|
|
static std::vector<u8> s_code_init, s_data_init;
|
|
|
|
|
|
|
|
template <atomic_t<u64>& Ctr, uint Off, utils::protection Prot>
|
2020-12-18 08:39:54 +01:00
|
|
|
static u8* add_jit_memory(usz size, uint align)
|
2019-01-26 21:15:45 +01:00
|
|
|
{
|
|
|
|
// Select subrange
|
|
|
|
u8* pointer = get_jit_memory() + Off;
|
|
|
|
|
2020-02-05 08:00:08 +01:00
|
|
|
if (!size && !align) [[unlikely]]
|
2019-01-26 21:15:45 +01:00
|
|
|
{
|
|
|
|
// Return subrange info
|
|
|
|
return pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 olda, newa;
|
|
|
|
|
|
|
|
// Simple allocation by incrementing pointer to the next free data
|
|
|
|
const u64 pos = Ctr.atomic_op([&](u64& ctr) -> u64
|
|
|
|
{
|
2020-12-18 15:43:34 +01:00
|
|
|
const u64 _pos = utils::align(ctr & 0xffff'ffff, align);
|
|
|
|
const u64 _new = utils::align(_pos + size, align);
|
2019-01-26 21:15:45 +01:00
|
|
|
|
2020-02-05 08:00:08 +01:00
|
|
|
if (_new > 0x40000000) [[unlikely]]
|
2019-01-26 21:15:45 +01:00
|
|
|
{
|
2019-03-18 21:01:16 +01:00
|
|
|
// Sorry, we failed, and further attempts should fail too.
|
2019-10-25 13:33:23 +02:00
|
|
|
ctr |= 0x40000000;
|
2019-01-26 21:15:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-10-25 13:33:23 +02:00
|
|
|
// Last allocation is stored in highest bits
|
|
|
|
olda = ctr >> 32;
|
|
|
|
newa = olda;
|
|
|
|
|
2019-01-26 21:15:45 +01:00
|
|
|
// Check the necessity to commit more memory
|
2020-02-05 08:00:08 +01:00
|
|
|
if (_new > olda) [[unlikely]]
|
2019-10-25 13:33:23 +02:00
|
|
|
{
|
2020-12-18 15:43:34 +01:00
|
|
|
newa = utils::align(_new, 0x200000);
|
2019-10-25 13:33:23 +02:00
|
|
|
}
|
2019-01-26 21:15:45 +01:00
|
|
|
|
2019-10-25 13:33:23 +02:00
|
|
|
ctr += _new - (ctr & 0xffff'ffff);
|
2019-01-26 21:15:45 +01:00
|
|
|
return _pos;
|
|
|
|
});
|
|
|
|
|
2020-02-19 18:03:59 +01:00
|
|
|
if (pos == umax) [[unlikely]]
|
2019-01-26 21:15:45 +01:00
|
|
|
{
|
2021-01-12 13:06:51 +01:00
|
|
|
jit_log.error("Out of memory (size=0x%x, align=0x%x, off=0x%x)", size, align, Off);
|
2019-01-26 21:15:45 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-05 08:00:08 +01:00
|
|
|
if (olda != newa) [[unlikely]]
|
2019-01-26 21:15:45 +01:00
|
|
|
{
|
2020-11-04 12:56:40 +01:00
|
|
|
#ifndef CAN_OVERCOMMIT
|
2019-01-26 21:15:45 +01:00
|
|
|
// Commit more memory
|
|
|
|
utils::memory_commit(pointer + olda, newa - olda, Prot);
|
|
|
|
#endif
|
2019-10-25 13:33:23 +02:00
|
|
|
// Acknowledge committed memory
|
|
|
|
Ctr.atomic_op([&](u64& ctr)
|
|
|
|
{
|
|
|
|
if ((ctr >> 32) < newa)
|
|
|
|
{
|
|
|
|
ctr += (newa - (ctr >> 32)) << 32;
|
|
|
|
}
|
|
|
|
});
|
2019-01-26 21:15:45 +01:00
|
|
|
}
|
|
|
|
|
2023-03-11 20:08:27 +01:00
|
|
|
ensure(pointer + pos >= get_jit_memory() + Off);
|
|
|
|
ensure(pointer + pos < get_jit_memory() + Off + 0x40000000);
|
|
|
|
|
2019-01-26 21:15:45 +01:00
|
|
|
return pointer + pos;
|
|
|
|
}
|
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
const asmjit::Environment& jit_runtime_base::environment() const noexcept
|
2019-01-26 21:15:45 +01:00
|
|
|
{
|
2021-12-28 20:25:36 +01:00
|
|
|
static const asmjit::Environment g_env = asmjit::Environment::host();
|
2019-01-26 21:15:45 +01:00
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
return g_env;
|
2019-01-26 21:15:45 +01:00
|
|
|
}
|
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
void* jit_runtime_base::_add(asmjit::CodeHolder* code) noexcept
|
2019-01-26 21:15:45 +01:00
|
|
|
{
|
2021-12-28 20:25:36 +01:00
|
|
|
ensure(!code->flatten());
|
|
|
|
ensure(!code->resolveUnresolvedLinks());
|
2021-12-30 17:39:18 +01:00
|
|
|
usz codeSize = code->codeSize();
|
|
|
|
if (!codeSize)
|
|
|
|
return nullptr;
|
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
auto p = ensure(this->_alloc(codeSize, 64));
|
|
|
|
ensure(!code->relocateToBase(uptr(p)));
|
2019-01-26 21:15:45 +01:00
|
|
|
|
2021-12-30 17:39:18 +01:00
|
|
|
{
|
2022-06-14 14:28:38 +02:00
|
|
|
// We manage rw <-> rx transitions manually on Apple
|
|
|
|
// because it's easier to keep track of when and where we need to toggle W^X
|
|
|
|
#if !(defined(ARCH_ARM64) && defined(__APPLE__))
|
2021-12-30 17:39:18 +01:00
|
|
|
asmjit::VirtMem::ProtectJitReadWriteScope rwScope(p, codeSize);
|
2022-06-14 14:28:38 +02:00
|
|
|
#endif
|
2019-01-26 21:15:45 +01:00
|
|
|
|
2021-12-30 17:39:18 +01:00
|
|
|
for (asmjit::Section* section : code->_sections)
|
|
|
|
{
|
|
|
|
std::memcpy(p + section->offset(), section->data(), section->bufferSize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
jit_runtime::jit_runtime()
|
|
|
|
{
|
|
|
|
}
|
2019-01-26 21:15:45 +01:00
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
jit_runtime::~jit_runtime()
|
|
|
|
{
|
2019-01-26 21:15:45 +01:00
|
|
|
}
|
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
uchar* jit_runtime::_alloc(usz size, usz align) noexcept
|
2019-01-26 21:15:45 +01:00
|
|
|
{
|
2021-12-28 20:25:36 +01:00
|
|
|
return jit_runtime::alloc(size, align, true);
|
2019-01-26 21:15:45 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:39:54 +01:00
|
|
|
u8* jit_runtime::alloc(usz size, uint align, bool exec) noexcept
|
2019-01-26 21:15:45 +01:00
|
|
|
{
|
|
|
|
if (exec)
|
|
|
|
{
|
|
|
|
return add_jit_memory<s_code_pos, 0x0, utils::protection::wx>(size, align);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return add_jit_memory<s_data_pos, 0x40000000, utils::protection::rw>(size, align);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void jit_runtime::initialize()
|
|
|
|
{
|
|
|
|
if (!s_code_init.empty() || !s_data_init.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create code/data snapshot
|
2019-10-25 13:33:23 +02:00
|
|
|
s_code_init.resize(s_code_pos & 0xffff'ffff);
|
|
|
|
std::memcpy(s_code_init.data(), alloc(0, 0, true), s_code_init.size());
|
|
|
|
s_data_init.resize(s_data_pos & 0xffff'ffff);
|
|
|
|
std::memcpy(s_data_init.data(), alloc(0, 0, false), s_data_init.size());
|
2019-01-26 21:15:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void jit_runtime::finalize() noexcept
|
|
|
|
{
|
2022-06-14 14:28:38 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
pthread_jit_write_protect_np(false);
|
|
|
|
#endif
|
2019-01-26 21:15:45 +01:00
|
|
|
// Reset JIT memory
|
|
|
|
#ifdef CAN_OVERCOMMIT
|
|
|
|
utils::memory_reset(get_jit_memory(), 0x80000000);
|
|
|
|
utils::memory_protect(get_jit_memory(), 0x40000000, utils::protection::wx);
|
|
|
|
#else
|
|
|
|
utils::memory_decommit(get_jit_memory(), 0x80000000);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
s_code_pos = 0;
|
|
|
|
s_data_pos = 0;
|
|
|
|
|
|
|
|
// Restore code/data snapshot
|
|
|
|
std::memcpy(alloc(s_code_init.size(), 1, true), s_code_init.data(), s_code_init.size());
|
|
|
|
std::memcpy(alloc(s_data_init.size(), 1, false), s_data_init.data(), s_data_init.size());
|
2022-06-14 14:28:38 +02:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
pthread_jit_write_protect_np(true);
|
|
|
|
#endif
|
|
|
|
#ifdef ARCH_ARM64
|
|
|
|
// Flush all cache lines after potentially writing executable code
|
|
|
|
asm("ISB");
|
|
|
|
asm("DSB ISH");
|
|
|
|
#endif
|
2019-01-26 21:15:45 +01:00
|
|
|
}
|
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
jit_runtime_base& asmjit::get_global_runtime()
|
2018-05-14 22:06:17 +02:00
|
|
|
{
|
2020-10-17 18:28:21 +02:00
|
|
|
// 16 MiB for internal needs
|
|
|
|
static constexpr u64 size = 1024 * 1024 * 16;
|
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
struct custom_runtime final : jit_runtime_base
|
2020-10-17 18:28:21 +02:00
|
|
|
{
|
|
|
|
custom_runtime() noexcept
|
|
|
|
{
|
|
|
|
// Search starting in first 2 GiB of memory
|
|
|
|
for (u64 addr = size;; addr += size)
|
|
|
|
{
|
|
|
|
if (auto ptr = utils::memory_reserve(size, reinterpret_cast<void*>(addr)))
|
|
|
|
{
|
2021-12-28 20:25:36 +01:00
|
|
|
m_pos.raw() = static_cast<uchar*>(ptr);
|
2020-10-17 18:28:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize "end" pointer
|
|
|
|
m_max = m_pos + size;
|
|
|
|
|
|
|
|
// Make memory writable + executable
|
|
|
|
utils::memory_commit(m_pos, size, utils::protection::wx);
|
|
|
|
}
|
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
uchar* _alloc(usz size, usz align) noexcept override
|
2020-10-17 18:28:21 +02:00
|
|
|
{
|
2021-12-28 20:25:36 +01:00
|
|
|
return m_pos.atomic_op([&](uchar*& pos) -> uchar*
|
2020-10-17 18:28:21 +02:00
|
|
|
{
|
2021-12-28 20:25:36 +01:00
|
|
|
const auto r = reinterpret_cast<uchar*>(utils::align(uptr(pos), align));
|
2020-10-17 18:28:21 +02:00
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
if (r >= pos && r + size > pos && r + size <= m_max)
|
|
|
|
{
|
|
|
|
pos = r + size;
|
|
|
|
return r;
|
|
|
|
}
|
2020-10-17 18:28:21 +02:00
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
return nullptr;
|
|
|
|
});
|
2020-10-17 18:28:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-12-28 20:25:36 +01:00
|
|
|
atomic_t<uchar*> m_pos{};
|
2020-10-17 18:28:21 +02:00
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
uchar* m_max{};
|
2020-10-17 18:28:21 +02:00
|
|
|
};
|
|
|
|
|
2018-05-14 22:06:17 +02:00
|
|
|
// Magic static
|
2020-10-17 18:28:21 +02:00
|
|
|
static custom_runtime g_rt;
|
2018-05-14 22:06:17 +02:00
|
|
|
return g_rt;
|
|
|
|
}
|
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
asmjit::inline_runtime::inline_runtime(uchar* data, usz size)
|
|
|
|
: m_data(data)
|
|
|
|
, m_size(size)
|
2021-12-22 17:27:20 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-28 20:25:36 +01:00
|
|
|
uchar* asmjit::inline_runtime::_alloc(usz size, usz align) noexcept
|
2021-12-22 17:27:20 +01:00
|
|
|
{
|
2021-12-28 20:25:36 +01:00
|
|
|
ensure(align <= 4096);
|
|
|
|
|
|
|
|
return size <= m_size ? m_data : nullptr;
|
2021-12-22 17:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
asmjit::inline_runtime::~inline_runtime()
|
|
|
|
{
|
|
|
|
utils::memory_protect(m_data, m_size, utils::protection::rx);
|
|
|
|
}
|
|
|
|
|
2022-08-24 18:36:37 +02:00
|
|
|
#if defined(ARCH_X64)
|
|
|
|
asmjit::simd_builder::simd_builder(CodeHolder* ch) noexcept
|
|
|
|
: native_asm(ch)
|
|
|
|
{
|
2022-09-03 14:51:37 +02:00
|
|
|
_init(0);
|
2022-08-29 02:55:59 +02:00
|
|
|
consts[~v128()] = this->newLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
asmjit::simd_builder::~simd_builder()
|
|
|
|
{
|
2022-08-24 18:36:37 +02:00
|
|
|
}
|
|
|
|
|
2022-09-03 14:51:37 +02:00
|
|
|
void asmjit::simd_builder::_init(uint new_vsize)
|
2022-08-24 18:36:37 +02:00
|
|
|
{
|
2022-09-03 14:51:37 +02:00
|
|
|
if ((!new_vsize && utils::has_avx512_icl()) || new_vsize == 64)
|
2022-08-24 18:36:37 +02:00
|
|
|
{
|
|
|
|
v0 = x86::zmm0;
|
|
|
|
v1 = x86::zmm1;
|
|
|
|
v2 = x86::zmm2;
|
|
|
|
v3 = x86::zmm3;
|
|
|
|
v4 = x86::zmm4;
|
|
|
|
v5 = x86::zmm5;
|
|
|
|
vsize = 64;
|
|
|
|
}
|
2022-09-03 14:51:37 +02:00
|
|
|
else if ((!new_vsize && utils::has_avx2()) || new_vsize == 32)
|
2022-08-24 18:36:37 +02:00
|
|
|
{
|
|
|
|
v0 = x86::ymm0;
|
|
|
|
v1 = x86::ymm1;
|
|
|
|
v2 = x86::ymm2;
|
|
|
|
v3 = x86::ymm3;
|
|
|
|
v4 = x86::ymm4;
|
|
|
|
v5 = x86::ymm5;
|
|
|
|
vsize = 32;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v0 = x86::xmm0;
|
|
|
|
v1 = x86::xmm1;
|
|
|
|
v2 = x86::xmm2;
|
|
|
|
v3 = x86::xmm3;
|
|
|
|
v4 = x86::xmm4;
|
|
|
|
v5 = x86::xmm5;
|
2022-09-03 14:51:37 +02:00
|
|
|
vsize = new_vsize ? new_vsize : 16;
|
2022-08-24 18:36:37 +02:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:06:09 +02:00
|
|
|
if (utils::has_avx512())
|
2022-08-24 18:36:37 +02:00
|
|
|
{
|
2022-09-08 16:06:09 +02:00
|
|
|
if (!new_vsize)
|
|
|
|
vmask = -1;
|
2022-08-24 18:36:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vmask = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 02:55:59 +02:00
|
|
|
void asmjit::simd_builder::operator()() noexcept
|
|
|
|
{
|
|
|
|
for (auto&& [x, y] : consts)
|
|
|
|
{
|
|
|
|
this->align(AlignMode::kData, 16);
|
|
|
|
this->bind(y);
|
|
|
|
this->embed(&x, 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 18:36:37 +02:00
|
|
|
void asmjit::simd_builder::vec_cleanup_ret()
|
|
|
|
{
|
|
|
|
if (utils::has_avx() && vsize > 16)
|
|
|
|
this->vzeroupper();
|
|
|
|
this->ret();
|
|
|
|
}
|
|
|
|
|
|
|
|
void asmjit::simd_builder::vec_set_all_zeros(const Operand& v)
|
|
|
|
{
|
|
|
|
x86::Xmm reg(v.id());
|
|
|
|
if (utils::has_avx())
|
|
|
|
this->vpxor(reg, reg, reg);
|
|
|
|
else
|
|
|
|
this->xorps(reg, reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void asmjit::simd_builder::vec_set_all_ones(const Operand& v)
|
|
|
|
{
|
|
|
|
x86::Xmm reg(v.id());
|
|
|
|
if (x86::Zmm zr(v.id()); zr == v)
|
|
|
|
this->vpternlogd(zr, zr, zr, 0xff);
|
|
|
|
else if (x86::Ymm yr(v.id()); yr == v)
|
|
|
|
this->vpcmpeqd(yr, yr, yr);
|
|
|
|
else if (utils::has_avx())
|
|
|
|
this->vpcmpeqd(reg, reg, reg);
|
|
|
|
else
|
|
|
|
this->pcmpeqd(reg, reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void asmjit::simd_builder::vec_set_const(const Operand& v, const v128& val)
|
|
|
|
{
|
|
|
|
if (!val._u)
|
|
|
|
return vec_set_all_zeros(v);
|
|
|
|
if (!~val._u)
|
|
|
|
return vec_set_all_ones(v);
|
2022-08-29 02:55:59 +02:00
|
|
|
else
|
2022-08-24 18:36:37 +02:00
|
|
|
{
|
2022-08-29 02:55:59 +02:00
|
|
|
Label co = consts[val];
|
|
|
|
if (!co.isValid())
|
|
|
|
co = consts[val] = this->newLabel();
|
2022-08-24 18:36:37 +02:00
|
|
|
if (x86::Zmm zr(v.id()); zr == v)
|
2022-08-29 02:55:59 +02:00
|
|
|
this->vbroadcasti32x4(zr, x86::oword_ptr(co));
|
2022-08-24 18:36:37 +02:00
|
|
|
else if (x86::Ymm yr(v.id()); yr == v)
|
2022-08-29 02:55:59 +02:00
|
|
|
this->vbroadcasti128(yr, x86::oword_ptr(co));
|
2022-08-24 18:36:37 +02:00
|
|
|
else if (utils::has_avx())
|
2022-08-29 02:55:59 +02:00
|
|
|
this->vmovaps(x86::Xmm(v.id()), x86::oword_ptr(co));
|
2022-08-24 18:36:37 +02:00
|
|
|
else
|
2022-08-29 02:55:59 +02:00
|
|
|
this->movaps(x86::Xmm(v.id()), x86::oword_ptr(co));
|
2022-08-24 18:36:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asmjit::simd_builder::vec_clobbering_test(u32 esize, const Operand& v, const Operand& rhs)
|
|
|
|
{
|
|
|
|
if (esize == 64)
|
|
|
|
{
|
|
|
|
this->emit(x86::Inst::kIdVptestmd, x86::k0, v, rhs);
|
|
|
|
this->ktestw(x86::k0, x86::k0);
|
|
|
|
}
|
|
|
|
else if (esize == 32)
|
|
|
|
{
|
|
|
|
this->emit(x86::Inst::kIdVptest, v, rhs);
|
|
|
|
}
|
2022-09-03 14:51:37 +02:00
|
|
|
else if (esize == 16 && utils::has_avx())
|
|
|
|
{
|
|
|
|
this->emit(x86::Inst::kIdVptest, v, rhs);
|
|
|
|
}
|
2022-08-24 18:36:37 +02:00
|
|
|
else if (esize == 16 && utils::has_sse41())
|
|
|
|
{
|
|
|
|
this->emit(x86::Inst::kIdPtest, v, rhs);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (v != rhs)
|
|
|
|
this->emit(x86::Inst::kIdPand, v, rhs);
|
|
|
|
if (esize == 16)
|
|
|
|
this->emit(x86::Inst::kIdPacksswb, v, v);
|
|
|
|
this->emit(x86::Inst::kIdMovq, x86::rax, v);
|
|
|
|
if (esize == 16 || esize == 8)
|
|
|
|
this->test(x86::rax, x86::rax);
|
|
|
|
else if (esize == 4)
|
|
|
|
this->test(x86::eax, x86::eax);
|
|
|
|
else if (esize == 2)
|
|
|
|
this->test(x86::ax, x86::ax);
|
|
|
|
else if (esize == 1)
|
|
|
|
this->test(x86::al, x86::al);
|
|
|
|
else
|
|
|
|
fmt::throw_exception("Unimplemented");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 13:23:14 +02:00
|
|
|
void asmjit::simd_builder::vec_broadcast_gpr(u32 esize, const Operand& v, const x86::Gp& r)
|
|
|
|
{
|
|
|
|
if (esize == 2)
|
|
|
|
{
|
|
|
|
if (utils::has_avx512())
|
|
|
|
this->emit(x86::Inst::kIdVpbroadcastw, v, r.r32());
|
|
|
|
else if (utils::has_avx())
|
|
|
|
{
|
|
|
|
this->emit(x86::Inst::kIdVmovd, v, r.r32());
|
|
|
|
if (utils::has_avx2())
|
|
|
|
this->emit(x86::Inst::kIdVpbroadcastw, v, v);
|
|
|
|
else
|
|
|
|
{
|
2022-09-24 15:07:56 +02:00
|
|
|
this->emit(x86::Inst::kIdVpunpcklwd, v, v, v);
|
2022-09-04 13:23:14 +02:00
|
|
|
this->emit(x86::Inst::kIdVpshufd, v, v, Imm(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->emit(x86::Inst::kIdMovd, v, r.r32());
|
|
|
|
this->emit(x86::Inst::kIdPunpcklwd, v, v);
|
|
|
|
this->emit(x86::Inst::kIdPshufd, v, v, Imm(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (esize == 4)
|
|
|
|
{
|
|
|
|
if (utils::has_avx512())
|
|
|
|
this->emit(x86::Inst::kIdVpbroadcastd, v, r.r32());
|
|
|
|
else if (utils::has_avx())
|
|
|
|
{
|
|
|
|
this->emit(x86::Inst::kIdVmovd, v, r.r32());
|
|
|
|
if (utils::has_avx2())
|
|
|
|
this->emit(x86::Inst::kIdVpbroadcastd, v, v);
|
|
|
|
else
|
|
|
|
this->emit(x86::Inst::kIdVpshufd, v, v, Imm(0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->emit(x86::Inst::kIdMovd, v, r.r32());
|
|
|
|
this->emit(x86::Inst::kIdPshufd, v, v, Imm(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fmt::throw_exception("Unimplemented");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 18:36:37 +02:00
|
|
|
asmjit::x86::Mem asmjit::simd_builder::ptr_scale_for_vec(u32 esize, const x86::Gp& base, const x86::Gp& index)
|
|
|
|
{
|
|
|
|
switch (ensure(esize))
|
|
|
|
{
|
|
|
|
case 1: return x86::ptr(base, index, 0, 0);
|
|
|
|
case 2: return x86::ptr(base, index, 1, 0);
|
|
|
|
case 4: return x86::ptr(base, index, 2, 0);
|
|
|
|
case 8: return x86::ptr(base, index, 3, 0);
|
|
|
|
default: fmt::throw_exception("Bad esize");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asmjit::simd_builder::vec_load_unaligned(u32 esize, const Operand& v, const x86::Mem& src)
|
|
|
|
{
|
|
|
|
ensure(std::has_single_bit(esize));
|
|
|
|
ensure(std::has_single_bit(vsize));
|
|
|
|
|
|
|
|
if (esize == 2)
|
|
|
|
{
|
|
|
|
ensure(vsize >= 2);
|
|
|
|
if (vsize == 2)
|
|
|
|
vec_set_all_zeros(v);
|
|
|
|
if (vsize == 2 && utils::has_avx())
|
|
|
|
this->emit(x86::Inst::kIdVpinsrw, x86::Xmm(v.id()), x86::Xmm(v.id()), src, Imm(0));
|
|
|
|
else if (vsize == 2)
|
|
|
|
this->emit(x86::Inst::kIdPinsrw, v, src, Imm(0));
|
2022-09-08 16:06:09 +02:00
|
|
|
else if ((vmask && vmask < 8) || vsize >= 64)
|
2022-08-24 18:36:37 +02:00
|
|
|
this->emit(x86::Inst::kIdVmovdqu16, v, src);
|
|
|
|
else
|
|
|
|
return vec_load_unaligned(vsize, v, src);
|
|
|
|
}
|
|
|
|
else if (esize == 4)
|
|
|
|
{
|
|
|
|
ensure(vsize >= 4);
|
|
|
|
if (vsize == 4 && utils::has_avx())
|
|
|
|
this->emit(x86::Inst::kIdVmovd, x86::Xmm(v.id()), src);
|
|
|
|
else if (vsize == 4)
|
|
|
|
this->emit(x86::Inst::kIdMovd, v, src);
|
2022-09-08 16:06:09 +02:00
|
|
|
else if ((vmask && vmask < 8) || vsize >= 64)
|
2022-08-24 18:36:37 +02:00
|
|
|
this->emit(x86::Inst::kIdVmovdqu32, v, src);
|
|
|
|
else
|
|
|
|
return vec_load_unaligned(vsize, v, src);
|
|
|
|
}
|
|
|
|
else if (esize == 8)
|
|
|
|
{
|
|
|
|
ensure(vsize >= 8);
|
|
|
|
if (vsize == 8 && utils::has_avx())
|
|
|
|
this->emit(x86::Inst::kIdVmovq, x86::Xmm(v.id()), src);
|
|
|
|
else if (vsize == 8)
|
|
|
|
this->emit(x86::Inst::kIdMovq, v, src);
|
2022-09-08 16:06:09 +02:00
|
|
|
else if ((vmask && vmask < 8) || vsize >= 64)
|
2022-08-24 18:36:37 +02:00
|
|
|
this->emit(x86::Inst::kIdVmovdqu64, v, src);
|
|
|
|
else
|
|
|
|
return vec_load_unaligned(vsize, v, src);
|
|
|
|
}
|
|
|
|
else if (esize >= 16)
|
|
|
|
{
|
|
|
|
ensure(vsize >= 16);
|
2022-09-08 16:06:09 +02:00
|
|
|
if ((vmask && vmask < 8) || vsize >= 64)
|
|
|
|
this->emit(x86::Inst::kIdVmovdqu64, v, src); // Not really needed
|
|
|
|
else if (utils::has_avx())
|
2022-08-24 18:36:37 +02:00
|
|
|
this->emit(x86::Inst::kIdVmovdqu, v, src);
|
|
|
|
else
|
|
|
|
this->emit(x86::Inst::kIdMovups, v, src);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fmt::throw_exception("Unimplemented");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asmjit::simd_builder::vec_store_unaligned(u32 esize, const Operand& v, const x86::Mem& dst)
|
|
|
|
{
|
|
|
|
ensure(std::has_single_bit(esize));
|
|
|
|
ensure(std::has_single_bit(vsize));
|
|
|
|
|
|
|
|
if (esize == 2)
|
|
|
|
{
|
|
|
|
ensure(vsize >= 2);
|
|
|
|
if (vsize == 2 && utils::has_avx())
|
|
|
|
this->emit(x86::Inst::kIdVpextrw, dst, x86::Xmm(v.id()), Imm(0));
|
|
|
|
else if (vsize == 2 && utils::has_sse41())
|
|
|
|
this->emit(x86::Inst::kIdPextrw, dst, v, Imm(0));
|
|
|
|
else if (vsize == 2)
|
|
|
|
this->push(x86::rax), this->pextrw(x86::eax, x86::Xmm(v.id()), 0), this->mov(dst, x86::ax), this->pop(x86::rax);
|
|
|
|
else if ((vmask && vmask < 8) || vsize >= 64)
|
|
|
|
this->emit(x86::Inst::kIdVmovdqu16, dst, v);
|
|
|
|
else
|
|
|
|
return vec_store_unaligned(vsize, v, dst);
|
|
|
|
}
|
|
|
|
else if (esize == 4)
|
|
|
|
{
|
|
|
|
ensure(vsize >= 4);
|
|
|
|
if (vsize == 4 && utils::has_avx())
|
|
|
|
this->emit(x86::Inst::kIdVmovd, dst, x86::Xmm(v.id()));
|
|
|
|
else if (vsize == 4)
|
|
|
|
this->emit(x86::Inst::kIdMovd, dst, v);
|
|
|
|
else if ((vmask && vmask < 8) || vsize >= 64)
|
|
|
|
this->emit(x86::Inst::kIdVmovdqu32, dst, v);
|
|
|
|
else
|
|
|
|
return vec_store_unaligned(vsize, v, dst);
|
|
|
|
}
|
|
|
|
else if (esize == 8)
|
|
|
|
{
|
|
|
|
ensure(vsize >= 8);
|
|
|
|
if (vsize == 8 && utils::has_avx())
|
|
|
|
this->emit(x86::Inst::kIdVmovq, dst, x86::Xmm(v.id()));
|
|
|
|
else if (vsize == 8)
|
|
|
|
this->emit(x86::Inst::kIdMovq, dst, v);
|
|
|
|
else if ((vmask && vmask < 8) || vsize >= 64)
|
|
|
|
this->emit(x86::Inst::kIdVmovdqu64, dst, v);
|
|
|
|
else
|
|
|
|
return vec_store_unaligned(vsize, v, dst);
|
|
|
|
}
|
|
|
|
else if (esize >= 16)
|
|
|
|
{
|
|
|
|
ensure(vsize >= 16);
|
|
|
|
if ((vmask && vmask < 8) || vsize >= 64)
|
|
|
|
this->emit(x86::Inst::kIdVmovdqu64, dst, v); // Not really needed
|
|
|
|
else if (utils::has_avx())
|
|
|
|
this->emit(x86::Inst::kIdVmovdqu, dst, v);
|
|
|
|
else
|
|
|
|
this->emit(x86::Inst::kIdMovups, dst, v);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fmt::throw_exception("Unimplemented");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asmjit::simd_builder::_vec_binary_op(x86::Inst::Id sse_op, x86::Inst::Id vex_op, x86::Inst::Id evex_op, const Operand& dst, const Operand& lhs, const Operand& rhs)
|
|
|
|
{
|
|
|
|
if (utils::has_avx())
|
|
|
|
{
|
2022-09-03 14:51:37 +02:00
|
|
|
if (evex_op != x86::Inst::kIdNone && (vex_op == x86::Inst::kIdNone || this->_extraReg.isReg() || vsize >= 64))
|
2022-08-24 18:36:37 +02:00
|
|
|
{
|
|
|
|
this->evex().emit(evex_op, dst, lhs, rhs);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->emit(vex_op, dst, lhs, rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (dst == lhs)
|
|
|
|
{
|
|
|
|
this->emit(sse_op, dst, rhs);
|
|
|
|
}
|
|
|
|
else if (dst == rhs)
|
|
|
|
{
|
|
|
|
fmt::throw_exception("Unimplemented");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->emit(x86::Inst::kIdMovaps, dst, lhs);
|
|
|
|
this->emit(sse_op, dst, rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asmjit::simd_builder::vec_umin(u32 esize, const Operand& dst, const Operand& lhs, const Operand& rhs)
|
|
|
|
{
|
|
|
|
using enum x86::Inst::Id;
|
|
|
|
if (esize == 2)
|
|
|
|
{
|
|
|
|
if (utils::has_sse41())
|
|
|
|
return _vec_binary_op(kIdPminuw, kIdVpminuw, kIdVpminuw, dst, lhs, rhs);
|
|
|
|
}
|
|
|
|
else if (esize == 4)
|
|
|
|
{
|
|
|
|
if (utils::has_sse41())
|
|
|
|
return _vec_binary_op(kIdPminud, kIdVpminud, kIdVpminud, dst, lhs, rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt::throw_exception("Unimplemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
void asmjit::simd_builder::vec_umax(u32 esize, const Operand& dst, const Operand& lhs, const Operand& rhs)
|
|
|
|
{
|
|
|
|
using enum x86::Inst::Id;
|
|
|
|
if (esize == 2)
|
|
|
|
{
|
|
|
|
if (utils::has_sse41())
|
|
|
|
return _vec_binary_op(kIdPmaxuw, kIdVpmaxuw, kIdVpmaxuw, dst, lhs, rhs);
|
|
|
|
}
|
|
|
|
else if (esize == 4)
|
|
|
|
{
|
|
|
|
if (utils::has_sse41())
|
|
|
|
return _vec_binary_op(kIdPmaxud, kIdVpmaxud, kIdVpmaxud, dst, lhs, rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt::throw_exception("Unimplemented");
|
|
|
|
}
|
|
|
|
|
2022-09-04 13:23:14 +02:00
|
|
|
void asmjit::simd_builder::vec_cmp_eq(u32 esize, const Operand& dst, const Operand& lhs, const Operand& rhs)
|
|
|
|
{
|
|
|
|
using enum x86::Inst::Id;
|
|
|
|
if (esize == 2)
|
|
|
|
{
|
|
|
|
if (vsize == 64)
|
|
|
|
{
|
|
|
|
this->evex().emit(kIdVpcmpeqw, x86::k0, lhs, rhs);
|
|
|
|
this->evex().emit(kIdVpmovm2w, dst, x86::k0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_vec_binary_op(kIdPcmpeqw, kIdVpcmpeqw, kIdNone, dst, lhs, rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (esize == 4)
|
|
|
|
{
|
|
|
|
if (vsize == 64)
|
|
|
|
{
|
|
|
|
this->evex().emit(kIdVpcmpeqd, x86::k0, lhs, rhs);
|
|
|
|
this->evex().emit(kIdVpmovm2d, dst, x86::k0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-15 02:55:13 +02:00
|
|
|
_vec_binary_op(kIdPcmpeqd, kIdVpcmpeqd, kIdNone, dst, lhs, rhs);
|
2022-09-04 13:23:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fmt::throw_exception("Unimplemented");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-03 14:51:37 +02:00
|
|
|
void asmjit::simd_builder::vec_extract_high(u32, const Operand& dst, const Operand& src)
|
2022-08-24 18:36:37 +02:00
|
|
|
{
|
2022-09-03 14:51:37 +02:00
|
|
|
if (vsize == 32)
|
|
|
|
this->vextracti32x8(x86::Ymm(dst.id()), x86::Zmm(src.id()), 1);
|
|
|
|
else if (vsize == 16)
|
|
|
|
this->vextracti128(x86::Xmm(dst.id()), x86::Ymm(src.id()), 1);
|
|
|
|
else
|
2022-08-24 18:36:37 +02:00
|
|
|
{
|
|
|
|
if (utils::has_avx())
|
2022-09-03 14:51:37 +02:00
|
|
|
this->vpsrldq(x86::Xmm(dst.id()), x86::Xmm(src.id()), vsize);
|
2022-08-24 18:36:37 +02:00
|
|
|
else
|
|
|
|
{
|
2022-09-03 14:51:37 +02:00
|
|
|
this->movdqa(x86::Xmm(dst.id()), x86::Xmm(src.id()));
|
|
|
|
this->psrldq(x86::Xmm(dst.id()), vsize);
|
2022-08-24 18:36:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-03 14:51:37 +02:00
|
|
|
void asmjit::simd_builder::vec_extract_gpr(u32 esize, const x86::Gp& dst, const Operand& src)
|
2022-08-24 18:36:37 +02:00
|
|
|
{
|
2022-09-03 14:51:37 +02:00
|
|
|
if (esize == 8 && utils::has_avx())
|
|
|
|
this->vmovq(dst.r64(), x86::Xmm(src.id()));
|
|
|
|
else if (esize == 8)
|
|
|
|
this->movq(dst.r64(), x86::Xmm(src.id()));
|
|
|
|
else if (esize == 4 && utils::has_avx())
|
|
|
|
this->vmovd(dst.r32(), x86::Xmm(src.id()));
|
2022-08-24 18:36:37 +02:00
|
|
|
else if (esize == 4)
|
2022-09-03 14:51:37 +02:00
|
|
|
this->movd(dst.r32(), x86::Xmm(src.id()));
|
|
|
|
else if (esize == 2 && utils::has_avx())
|
|
|
|
this->vpextrw(dst.r32(), x86::Xmm(src.id()), 0);
|
|
|
|
else if (esize == 2)
|
|
|
|
this->pextrw(dst.r32(), x86::Xmm(src.id()), 0);
|
2022-08-24 18:36:37 +02:00
|
|
|
else
|
|
|
|
fmt::throw_exception("Unimplemented");
|
|
|
|
}
|
2022-09-03 14:51:37 +02:00
|
|
|
|
2022-08-24 18:36:37 +02:00
|
|
|
#endif /* X86 */
|
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
#ifdef LLVM_AVAILABLE
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2017-06-22 23:52:09 +02:00
|
|
|
#include <deque>
|
2016-06-22 15:37:51 +02:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 0)
|
2019-11-30 00:11:28 +01:00
|
|
|
#else
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wall"
|
|
|
|
#pragma GCC diagnostic ignored "-Wextra"
|
|
|
|
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
2021-03-05 20:05:37 +01:00
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
2021-03-08 21:41:23 +01:00
|
|
|
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
2021-03-23 20:32:50 +01:00
|
|
|
#pragma GCC diagnostic ignored "-Wredundant-decls"
|
2021-03-30 17:31:46 +02:00
|
|
|
#pragma GCC diagnostic ignored "-Weffc++"
|
2021-04-07 23:52:18 +02:00
|
|
|
#pragma GCC diagnostic ignored "-Wmissing-noreturn"
|
2016-06-22 15:37:51 +02:00
|
|
|
#endif
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2020-11-02 04:07:58 +01:00
|
|
|
#include "llvm/Support/Host.h"
|
2016-06-22 15:37:51 +02:00
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
|
|
|
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
|
2017-06-22 23:52:09 +02:00
|
|
|
#include "llvm/ExecutionEngine/ObjectCache.h"
|
2021-12-24 18:33:32 +01:00
|
|
|
#include "llvm/ExecutionEngine/JITEventListener.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/Object/SymbolSize.h"
|
2016-06-22 15:37:51 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
2019-11-30 00:11:28 +01:00
|
|
|
#else
|
|
|
|
#pragma GCC diagnostic pop
|
2016-06-22 15:37:51 +02:00
|
|
|
#endif
|
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
const bool jit_initialize = []() -> bool
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2020-05-19 18:09:27 +02:00
|
|
|
llvm::InitializeNativeTarget();
|
|
|
|
llvm::InitializeNativeTargetAsmPrinter();
|
|
|
|
llvm::InitializeNativeTargetAsmParser();
|
|
|
|
LLVMLinkInMCJIT();
|
|
|
|
return true;
|
|
|
|
}();
|
2019-10-28 23:01:07 +01:00
|
|
|
|
2021-01-15 18:54:00 +01:00
|
|
|
[[noreturn]] static void null(const char* name)
|
|
|
|
{
|
|
|
|
fmt::throw_exception("Null function: %s", name);
|
|
|
|
}
|
|
|
|
|
2021-01-19 18:40:15 +01:00
|
|
|
namespace vm
|
|
|
|
{
|
|
|
|
extern u8* const g_sudo_addr;
|
|
|
|
}
|
|
|
|
|
2021-01-15 18:54:00 +01:00
|
|
|
static shared_mutex null_mtx;
|
|
|
|
|
|
|
|
static std::unordered_map<std::string, u64> null_funcs;
|
|
|
|
|
|
|
|
static u64 make_null_function(const std::string& name)
|
|
|
|
{
|
2021-01-19 18:40:15 +01:00
|
|
|
if (name.starts_with("__0x"))
|
|
|
|
{
|
|
|
|
u32 addr = -1;
|
|
|
|
auto res = std::from_chars(name.c_str() + 4, name.c_str() + name.size(), addr, 16);
|
|
|
|
|
|
|
|
if (res.ec == std::errc() && res.ptr == name.c_str() + name.size() && addr < 0x8000'0000)
|
|
|
|
{
|
|
|
|
// Point the garbage to reserved, non-executable memory
|
|
|
|
return reinterpret_cast<u64>(vm::g_sudo_addr + addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 18:54:00 +01:00
|
|
|
std::lock_guard lock(null_mtx);
|
|
|
|
|
|
|
|
if (u64& func_ptr = null_funcs[name]) [[likely]]
|
|
|
|
{
|
|
|
|
// Already exists
|
|
|
|
return func_ptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
using namespace asmjit;
|
|
|
|
|
|
|
|
// Build a "null" function that contains its name
|
2021-12-30 17:39:18 +01:00
|
|
|
const auto func = build_function_asm<void (*)()>("NULL", [&](native_asm& c, auto& args)
|
2021-01-15 18:54:00 +01:00
|
|
|
{
|
2021-12-30 17:39:18 +01:00
|
|
|
#if defined(ARCH_X64)
|
2021-01-15 18:54:00 +01:00
|
|
|
Label data = c.newLabel();
|
|
|
|
c.lea(args[0], x86::qword_ptr(data, 0));
|
2021-12-28 20:25:36 +01:00
|
|
|
c.jmp(Imm(&null));
|
|
|
|
c.align(AlignMode::kCode, 16);
|
2021-01-15 18:54:00 +01:00
|
|
|
c.bind(data);
|
|
|
|
|
|
|
|
// Copy function name bytes
|
|
|
|
for (char ch : name)
|
|
|
|
c.db(ch);
|
|
|
|
c.db(0);
|
2021-12-28 20:25:36 +01:00
|
|
|
c.align(AlignMode::kData, 16);
|
2022-06-14 14:28:38 +02:00
|
|
|
#else
|
|
|
|
// AArch64 implementation
|
|
|
|
Label jmp_address = c.newLabel();
|
|
|
|
Label data = c.newLabel();
|
|
|
|
// Force absolute jump to prevent out of bounds PC-rel jmp
|
|
|
|
c.ldr(args[0], arm::ptr(jmp_address));
|
|
|
|
c.br(args[0]);
|
|
|
|
c.align(AlignMode::kCode, 16);
|
|
|
|
|
|
|
|
c.bind(data);
|
|
|
|
c.embed(name.c_str(), name.size());
|
|
|
|
c.embedUInt8(0U);
|
|
|
|
c.bind(jmp_address);
|
|
|
|
c.embedUInt64(reinterpret_cast<u64>(&null));
|
|
|
|
c.align(AlignMode::kData, 16);
|
2021-12-30 17:39:18 +01:00
|
|
|
#endif
|
2021-01-15 18:54:00 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
func_ptr = reinterpret_cast<u64>(func);
|
|
|
|
return func_ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:33:32 +01:00
|
|
|
struct JITAnnouncer : llvm::JITEventListener
|
|
|
|
{
|
|
|
|
void notifyObjectLoaded(u64, const llvm::object::ObjectFile& obj, const llvm::RuntimeDyld::LoadedObjectInfo& info) override
|
|
|
|
{
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
object::OwningBinary<object::ObjectFile> debug_obj_ = info.getObjectForDebug(obj);
|
2021-12-26 17:21:50 +01:00
|
|
|
if (!debug_obj_.getBinary())
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
jit_log.error("LLVM: Failed to announce JIT events (no debug object)");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:33:32 +01:00
|
|
|
const object::ObjectFile& debug_obj = *debug_obj_.getBinary();
|
|
|
|
|
|
|
|
for (const auto& [sym, size] : computeSymbolSizes(debug_obj))
|
|
|
|
{
|
|
|
|
Expected<object::SymbolRef::Type> type_ = sym.getType();
|
|
|
|
if (!type_ || *type_ != object::SymbolRef::ST_Function)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Expected<StringRef> name = sym.getName();
|
|
|
|
if (!name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Expected<u64> addr = sym.getAddress();
|
|
|
|
if (!addr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
jit_announce(*addr, size, {name->data(), name->size()});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
// Simple memory manager
|
|
|
|
struct MemoryManager1 : llvm::RTDyldMemoryManager
|
2017-06-24 17:36:49 +02:00
|
|
|
{
|
2020-05-19 18:09:27 +02:00
|
|
|
// 256 MiB for code or data
|
|
|
|
static constexpr u64 c_max_size = 0x20000000 / 2;
|
2018-01-01 08:40:57 +01:00
|
|
|
|
2020-11-25 05:58:37 +01:00
|
|
|
// Allocation unit (2M)
|
|
|
|
static constexpr u64 c_page_size = 2 * 1024 * 1024;
|
2017-06-24 17:36:49 +02:00
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
// Reserve 512 MiB
|
|
|
|
u8* const ptr = static_cast<u8*>(utils::memory_reserve(c_max_size * 2));
|
2017-06-24 17:36:49 +02:00
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
u64 code_ptr = 0;
|
|
|
|
u64 data_ptr = c_max_size;
|
2017-06-24 17:36:49 +02:00
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
MemoryManager1() = default;
|
2017-03-19 13:53:48 +01:00
|
|
|
|
2021-04-03 18:38:02 +02:00
|
|
|
MemoryManager1(const MemoryManager1&) = delete;
|
|
|
|
|
|
|
|
MemoryManager1& operator=(const MemoryManager1&) = delete;
|
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
~MemoryManager1() override
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2021-12-24 18:33:32 +01:00
|
|
|
// Hack: don't release to prevent reuse of address space, see jit_announce
|
|
|
|
utils::memory_decommit(ptr, c_max_size * 2);
|
2016-06-22 15:37:51 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
llvm::JITSymbol findSymbol(const std::string& name) override
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2020-05-19 18:09:27 +02:00
|
|
|
u64 addr = RTDyldMemoryManager::getSymbolAddress(name);
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2017-03-19 13:53:48 +01:00
|
|
|
if (!addr)
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2021-01-15 18:54:00 +01:00
|
|
|
addr = make_null_function(name);
|
2021-01-19 18:40:15 +01:00
|
|
|
|
|
|
|
if (!addr)
|
|
|
|
{
|
|
|
|
fmt::throw_exception("Failed to link '%s'", name);
|
|
|
|
}
|
2017-03-11 17:49:32 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
return {addr, llvm::JITSymbolFlags::Exported};
|
2016-06-22 15:37:51 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 14:29:55 +01:00
|
|
|
u8* allocate(u64& oldp, uptr size, uint align, utils::protection prot)
|
2019-10-28 23:01:07 +01:00
|
|
|
{
|
2020-05-19 18:09:27 +02:00
|
|
|
if (align > c_page_size)
|
2019-10-28 23:01:07 +01:00
|
|
|
{
|
2021-01-12 13:06:51 +01:00
|
|
|
jit_log.fatal("Unsupported alignment (size=0x%x, align=0x%x)", size, align);
|
2016-06-22 15:37:51 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:43:34 +01:00
|
|
|
const u64 olda = utils::align(oldp, align);
|
|
|
|
const u64 newp = utils::align(olda + size, align);
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
if ((newp - 1) / c_max_size != oldp / c_max_size)
|
2019-10-28 23:01:07 +01:00
|
|
|
{
|
2021-01-12 13:06:51 +01:00
|
|
|
jit_log.fatal("Out of memory (size=0x%x, align=0x%x)", size, align);
|
2016-06-22 15:37:51 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
if ((oldp - 1) / c_page_size != (newp - 1) / c_page_size)
|
2017-03-20 14:23:50 +01:00
|
|
|
{
|
2020-05-19 18:09:27 +02:00
|
|
|
// Allocate pages on demand
|
2020-12-18 15:43:34 +01:00
|
|
|
const u64 pagea = utils::align(oldp, c_page_size);
|
|
|
|
const u64 psize = utils::align(newp - pagea, c_page_size);
|
2020-05-19 18:09:27 +02:00
|
|
|
utils::memory_commit(this->ptr + pagea, psize, prot);
|
2017-03-20 14:23:50 +01:00
|
|
|
}
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
// Update allocation counter
|
|
|
|
oldp = newp;
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
return this->ptr + olda;
|
2018-06-10 14:46:01 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 20:05:37 +01:00
|
|
|
u8* allocateCodeSection(uptr size, uint align, uint /*sec_id*/, llvm::StringRef /*sec_name*/) override
|
2018-06-10 14:46:01 +02:00
|
|
|
{
|
2020-05-19 18:09:27 +02:00
|
|
|
return allocate(code_ptr, size, align, utils::protection::wx);
|
2018-06-10 14:46:01 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 20:05:37 +01:00
|
|
|
u8* allocateDataSection(uptr size, uint align, uint /*sec_id*/, llvm::StringRef /*sec_name*/, bool /*is_ro*/) override
|
2018-06-10 14:46:01 +02:00
|
|
|
{
|
2020-05-19 18:09:27 +02:00
|
|
|
return allocate(data_ptr, size, align, utils::protection::rw);
|
2018-06-10 14:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool finalizeMemory(std::string* = nullptr) override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-18 17:24:55 +01:00
|
|
|
|
2021-03-05 20:05:37 +01:00
|
|
|
void registerEHFrames(u8*, u64, usz) override
|
2019-03-18 17:24:55 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void deregisterEHFrames() override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
// Simple memory manager
|
|
|
|
struct MemoryManager2 : llvm::RTDyldMemoryManager
|
2019-03-18 17:24:55 +01:00
|
|
|
{
|
2020-05-19 18:09:27 +02:00
|
|
|
MemoryManager2() = default;
|
2019-03-18 17:24:55 +01:00
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
~MemoryManager2() override
|
2019-03-18 17:24:55 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-15 18:54:00 +01:00
|
|
|
llvm::JITSymbol findSymbol(const std::string& name) override
|
|
|
|
{
|
|
|
|
u64 addr = RTDyldMemoryManager::getSymbolAddress(name);
|
|
|
|
|
|
|
|
if (!addr)
|
|
|
|
{
|
|
|
|
addr = make_null_function(name);
|
2021-01-19 18:40:15 +01:00
|
|
|
|
|
|
|
if (!addr)
|
|
|
|
{
|
|
|
|
fmt::throw_exception("Failed to link '%s' (MM2)", name);
|
|
|
|
}
|
2021-01-15 18:54:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return {addr, llvm::JITSymbolFlags::Exported};
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:05:37 +01:00
|
|
|
u8* allocateCodeSection(uptr size, uint align, uint /*sec_id*/, llvm::StringRef /*sec_name*/) override
|
2019-03-18 17:24:55 +01:00
|
|
|
{
|
2020-05-19 18:09:27 +02:00
|
|
|
return jit_runtime::alloc(size, align, true);
|
2019-03-18 17:24:55 +01:00
|
|
|
}
|
|
|
|
|
2021-03-05 20:05:37 +01:00
|
|
|
u8* allocateDataSection(uptr size, uint align, uint /*sec_id*/, llvm::StringRef /*sec_name*/, bool /*is_ro*/) override
|
2019-03-18 17:24:55 +01:00
|
|
|
{
|
2020-05-19 18:09:27 +02:00
|
|
|
return jit_runtime::alloc(size, align, false);
|
2019-03-18 17:24:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool finalizeMemory(std::string* = nullptr) override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:05:37 +01:00
|
|
|
void registerEHFrames(u8*, u64, usz) override
|
2019-03-18 17:24:55 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void deregisterEHFrames() override
|
|
|
|
{
|
|
|
|
}
|
2018-06-10 14:46:01 +02:00
|
|
|
};
|
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
// Helper class
|
|
|
|
class ObjectCache final : public llvm::ObjectCache
|
|
|
|
{
|
|
|
|
const std::string& m_path;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ObjectCache(const std::string& path)
|
|
|
|
: m_path(path)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ObjectCache() override = default;
|
|
|
|
|
2020-05-06 17:18:30 +02:00
|
|
|
void notifyObjectCompiled(const llvm::Module* _module, llvm::MemoryBufferRef obj) override
|
2017-06-22 23:52:09 +02:00
|
|
|
{
|
|
|
|
std::string name = m_path;
|
2020-05-06 17:18:30 +02:00
|
|
|
name.append(_module->getName().data());
|
2019-12-27 20:50:14 +01:00
|
|
|
//fs::file(name, fs::rewrite).write(obj.getBufferStart(), obj.getBufferSize());
|
|
|
|
name.append(".gz");
|
|
|
|
|
|
|
|
z_stream zs{};
|
2020-12-09 14:03:15 +01:00
|
|
|
uLong zsz = compressBound(::narrow<u32>(obj.getBufferSize())) + 256;
|
2019-12-27 20:50:14 +01:00
|
|
|
auto zbuf = std::make_unique<uchar[]>(zsz);
|
2020-02-23 08:29:42 +01:00
|
|
|
#ifndef _MSC_VER
|
2019-12-27 20:50:14 +01:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
2020-02-23 08:29:42 +01:00
|
|
|
#endif
|
2019-12-27 20:50:14 +01:00
|
|
|
deflateInit2(&zs, 9, Z_DEFLATED, 16 + 15, 9, Z_DEFAULT_STRATEGY);
|
2020-02-23 08:29:42 +01:00
|
|
|
#ifndef _MSC_VER
|
2019-12-27 20:50:14 +01:00
|
|
|
#pragma GCC diagnostic pop
|
2020-02-23 08:29:42 +01:00
|
|
|
#endif
|
2019-12-27 20:50:14 +01:00
|
|
|
zs.avail_in = static_cast<uInt>(obj.getBufferSize());
|
|
|
|
zs.next_in = reinterpret_cast<uchar*>(const_cast<char*>(obj.getBufferStart()));
|
|
|
|
zs.avail_out = static_cast<uInt>(zsz);
|
|
|
|
zs.next_out = zbuf.get();
|
|
|
|
|
|
|
|
switch (deflate(&zs, Z_FINISH))
|
|
|
|
{
|
|
|
|
case Z_OK:
|
|
|
|
case Z_STREAM_END:
|
|
|
|
{
|
|
|
|
deflateEnd(&zs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2020-05-06 17:18:30 +02:00
|
|
|
jit_log.error("LLVM: Failed to compress module: %s", _module->getName().data());
|
2019-12-27 20:50:14 +01:00
|
|
|
deflateEnd(&zs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 14:11:01 +01:00
|
|
|
if (!fs::write_file(name, fs::rewrite, zbuf.get(), zsz - zs.avail_out))
|
|
|
|
{
|
|
|
|
jit_log.error("LLVM: Failed to create module file: %s (%s)", name, fs::g_tls_error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-06 17:18:30 +02:00
|
|
|
jit_log.notice("LLVM: Created module: %s", _module->getName().data());
|
2017-06-22 23:52:09 +02:00
|
|
|
}
|
|
|
|
|
2017-07-15 11:20:40 +02:00
|
|
|
static std::unique_ptr<llvm::MemoryBuffer> load(const std::string& path)
|
2017-06-22 23:52:09 +02:00
|
|
|
{
|
2019-12-27 20:50:14 +01:00
|
|
|
if (fs::file cached{path + ".gz", fs::read})
|
|
|
|
{
|
|
|
|
std::vector<uchar> gz = cached.to_vector<uchar>();
|
|
|
|
std::vector<uchar> out;
|
|
|
|
z_stream zs{};
|
2020-04-07 15:26:22 +02:00
|
|
|
|
|
|
|
if (gz.empty()) [[unlikely]]
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-02-23 08:29:42 +01:00
|
|
|
#ifndef _MSC_VER
|
2019-12-27 20:50:14 +01:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
2020-02-23 08:29:42 +01:00
|
|
|
#endif
|
2019-12-27 20:50:14 +01:00
|
|
|
inflateInit2(&zs, 16 + 15);
|
2020-02-23 08:29:42 +01:00
|
|
|
#ifndef _MSC_VER
|
2019-12-27 20:50:14 +01:00
|
|
|
#pragma GCC diagnostic pop
|
2020-02-23 08:29:42 +01:00
|
|
|
#endif
|
2019-12-27 20:50:14 +01:00
|
|
|
zs.avail_in = static_cast<uInt>(gz.size());
|
|
|
|
zs.next_in = gz.data();
|
|
|
|
out.resize(gz.size() * 6);
|
|
|
|
zs.avail_out = static_cast<uInt>(out.size());
|
|
|
|
zs.next_out = out.data();
|
|
|
|
|
|
|
|
while (zs.avail_in)
|
|
|
|
{
|
|
|
|
switch (inflate(&zs, Z_FINISH))
|
|
|
|
{
|
|
|
|
case Z_OK: break;
|
|
|
|
case Z_STREAM_END: break;
|
|
|
|
case Z_BUF_ERROR:
|
|
|
|
{
|
|
|
|
if (zs.avail_in)
|
|
|
|
break;
|
|
|
|
[[fallthrough]];
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
inflateEnd(&zs);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zs.avail_in)
|
|
|
|
{
|
|
|
|
auto cur_size = zs.next_out - out.data();
|
|
|
|
out.resize(out.size() + 65536);
|
|
|
|
zs.avail_out = static_cast<uInt>(out.size() - cur_size);
|
|
|
|
zs.next_out = out.data() + cur_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out.resize(zs.next_out - out.data());
|
|
|
|
inflateEnd(&zs);
|
|
|
|
|
|
|
|
auto buf = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(out.size());
|
|
|
|
std::memcpy(buf->getBufferStart(), out.data(), out.size());
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-07-15 11:20:40 +02:00
|
|
|
if (fs::file cached{path, fs::read})
|
2017-06-22 23:52:09 +02:00
|
|
|
{
|
2020-04-07 15:26:22 +02:00
|
|
|
if (cached.size() == 0) [[unlikely]]
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-01 12:20:36 +02:00
|
|
|
auto buf = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(cached.size());
|
|
|
|
cached.read(buf->getBufferStart(), buf->getBufferSize());
|
2018-09-05 22:52:34 +02:00
|
|
|
return buf;
|
2017-06-22 23:52:09 +02:00
|
|
|
}
|
2017-07-15 11:20:40 +02:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-06 17:18:30 +02:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> getObject(const llvm::Module* _module) override
|
2017-07-15 11:20:40 +02:00
|
|
|
{
|
|
|
|
std::string path = m_path;
|
2020-05-06 17:18:30 +02:00
|
|
|
path.append(_module->getName().data());
|
2017-07-15 11:20:40 +02:00
|
|
|
|
|
|
|
if (auto buf = load(path))
|
2017-06-22 23:52:09 +02:00
|
|
|
{
|
2020-05-06 17:18:30 +02:00
|
|
|
jit_log.notice("LLVM: Loaded module: %s", _module->getName().data());
|
2017-07-15 11:20:40 +02:00
|
|
|
return buf;
|
2017-06-22 23:52:09 +02:00
|
|
|
}
|
2017-07-15 11:20:40 +02:00
|
|
|
|
|
|
|
return nullptr;
|
2017-06-22 23:52:09 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-17 18:41:35 +01:00
|
|
|
std::string jit_compiler::cpu(const std::string& _cpu)
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2018-03-17 18:41:35 +01:00
|
|
|
std::string m_cpu = _cpu;
|
|
|
|
|
2017-03-14 13:23:07 +01:00
|
|
|
if (m_cpu.empty())
|
|
|
|
{
|
2023-03-10 23:57:21 +01:00
|
|
|
m_cpu = llvm::sys::getHostCPUName().str();
|
2017-07-18 14:21:29 +02:00
|
|
|
|
|
|
|
if (m_cpu == "sandybridge" ||
|
|
|
|
m_cpu == "ivybridge" ||
|
|
|
|
m_cpu == "haswell" ||
|
|
|
|
m_cpu == "broadwell" ||
|
|
|
|
m_cpu == "skylake" ||
|
|
|
|
m_cpu == "skylake-avx512" ||
|
2019-03-05 19:46:58 +01:00
|
|
|
m_cpu == "cascadelake" ||
|
2019-12-20 19:11:07 +01:00
|
|
|
m_cpu == "cooperlake" ||
|
2018-05-01 12:20:36 +02:00
|
|
|
m_cpu == "cannonlake" ||
|
2019-02-28 22:20:04 +01:00
|
|
|
m_cpu == "icelake" ||
|
|
|
|
m_cpu == "icelake-client" ||
|
2019-12-20 19:11:07 +01:00
|
|
|
m_cpu == "icelake-server" ||
|
2021-05-13 10:34:37 +02:00
|
|
|
m_cpu == "tigerlake" ||
|
2023-03-11 20:08:27 +01:00
|
|
|
m_cpu == "rocketlake" ||
|
|
|
|
m_cpu == "alderlake" ||
|
|
|
|
m_cpu == "raptorlake" ||
|
|
|
|
m_cpu == "meteorlake")
|
2017-07-18 14:21:29 +02:00
|
|
|
{
|
2018-05-01 12:20:36 +02:00
|
|
|
// Downgrade if AVX is not supported by some chips
|
2017-07-18 14:21:29 +02:00
|
|
|
if (!utils::has_avx())
|
|
|
|
{
|
|
|
|
m_cpu = "nehalem";
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 12:20:36 +02:00
|
|
|
|
|
|
|
if (m_cpu == "skylake-avx512" ||
|
2019-03-05 19:46:58 +01:00
|
|
|
m_cpu == "cascadelake" ||
|
2019-12-20 19:11:07 +01:00
|
|
|
m_cpu == "cooperlake" ||
|
2018-05-01 12:20:36 +02:00
|
|
|
m_cpu == "cannonlake" ||
|
2019-02-28 22:20:04 +01:00
|
|
|
m_cpu == "icelake" ||
|
|
|
|
m_cpu == "icelake-client" ||
|
2019-12-20 19:11:07 +01:00
|
|
|
m_cpu == "icelake-server" ||
|
2021-05-13 10:34:37 +02:00
|
|
|
m_cpu == "tigerlake" ||
|
|
|
|
m_cpu == "rocketlake")
|
2018-05-01 12:20:36 +02:00
|
|
|
{
|
|
|
|
// Downgrade if AVX-512 is disabled or not supported
|
2020-03-10 17:21:00 +01:00
|
|
|
if (!utils::has_avx512())
|
2018-05-01 12:20:36 +02:00
|
|
|
{
|
|
|
|
m_cpu = "skylake";
|
|
|
|
}
|
|
|
|
}
|
2019-11-14 17:09:34 +01:00
|
|
|
|
|
|
|
if (m_cpu == "znver1" && utils::has_clwb())
|
|
|
|
{
|
|
|
|
// Upgrade
|
|
|
|
m_cpu = "znver2";
|
|
|
|
}
|
2023-03-11 20:08:27 +01:00
|
|
|
|
|
|
|
if ((m_cpu == "znver3" || m_cpu == "goldmont" || m_cpu == "alderlake" || m_cpu == "raptorlake" || m_cpu == "meteorlake") && utils::has_avx512_icl())
|
|
|
|
{
|
|
|
|
// Upgrade
|
|
|
|
m_cpu = "icelake-client";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_cpu == "goldmont" && utils::has_avx2())
|
|
|
|
{
|
|
|
|
// Upgrade
|
|
|
|
m_cpu = "alderlake";
|
|
|
|
}
|
2017-03-14 13:23:07 +01:00
|
|
|
}
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2018-03-17 18:41:35 +01:00
|
|
|
return m_cpu;
|
|
|
|
}
|
|
|
|
|
2023-04-08 14:21:22 +02:00
|
|
|
std::string jit_compiler::triple1()
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
return llvm::Triple::normalize(llvm::sys::getProcessTriple());
|
|
|
|
#elif defined(__APPLE__) && defined(ARCH_X64)
|
|
|
|
return llvm::Triple::normalize("x86_64-unknown-linux-gnu");
|
|
|
|
#elif defined(__APPLE__) && defined(ARCH_ARM64)
|
|
|
|
return llvm::Triple::normalize("aarch64-unknown-linux-gnu");
|
|
|
|
#else
|
|
|
|
return llvm::Triple::normalize(llvm::sys::getProcessTriple());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string jit_compiler::triple2()
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && defined(ARCH_X64)
|
|
|
|
return llvm::Triple::normalize("x86_64-unknown-linux-gnu");
|
|
|
|
#elif defined(_WIN32) && defined(ARCH_ARM64)
|
|
|
|
return llvm::Triple::normalize("aarch64-unknown-linux-gnu");
|
|
|
|
#elif defined(__APPLE__) && defined(ARCH_X64)
|
|
|
|
return llvm::Triple::normalize("x86_64-unknown-linux-gnu");
|
|
|
|
#elif defined(__APPLE__) && defined(ARCH_ARM64)
|
|
|
|
return llvm::Triple::normalize("aarch64-unknown-linux-gnu");
|
|
|
|
#else
|
|
|
|
return llvm::Triple::normalize(llvm::sys::getProcessTriple());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-03-18 17:24:55 +01:00
|
|
|
jit_compiler::jit_compiler(const std::unordered_map<std::string, u64>& _link, const std::string& _cpu, u32 flags)
|
2021-12-23 16:03:48 +01:00
|
|
|
: m_context(new llvm::LLVMContext)
|
|
|
|
, m_cpu(cpu(_cpu))
|
2018-03-17 18:41:35 +01:00
|
|
|
{
|
2017-02-26 16:56:31 +01:00
|
|
|
std::string result;
|
|
|
|
|
2021-12-23 16:03:48 +01:00
|
|
|
auto null_mod = std::make_unique<llvm::Module> ("null_", *m_context);
|
2023-04-08 14:21:22 +02:00
|
|
|
null_mod->setTargetTriple(jit_compiler::triple1());
|
2023-03-11 20:08:27 +01:00
|
|
|
|
|
|
|
std::unique_ptr<llvm::RTDyldMemoryManager> mem;
|
2019-10-15 16:43:33 +02:00
|
|
|
|
2020-05-19 18:09:27 +02:00
|
|
|
if (_link.empty())
|
2017-06-24 17:36:49 +02:00
|
|
|
{
|
2023-03-11 20:08:27 +01:00
|
|
|
// Auxiliary JIT (does not use custom memory manager, only writes the objects)
|
2019-03-18 17:24:55 +01:00
|
|
|
if (flags & 0x1)
|
|
|
|
{
|
2020-05-19 18:09:27 +02:00
|
|
|
mem = std::make_unique<MemoryManager1>();
|
2019-03-18 17:24:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mem = std::make_unique<MemoryManager2>();
|
2023-04-08 14:21:22 +02:00
|
|
|
null_mod->setTargetTriple(jit_compiler::triple2());
|
2019-03-18 17:24:55 +01:00
|
|
|
}
|
2017-06-24 17:36:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-11 20:08:27 +01:00
|
|
|
mem = std::make_unique<MemoryManager1>();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-10-15 16:43:33 +02:00
|
|
|
m_engine.reset(llvm::EngineBuilder(std::move(null_mod))
|
2017-06-24 17:36:49 +02:00
|
|
|
.setErrorStr(&result)
|
2018-05-12 21:56:18 +02:00
|
|
|
.setEngineKind(llvm::EngineKind::JIT)
|
2023-03-11 20:08:27 +01:00
|
|
|
.setMCJITMemoryManager(std::move(mem))
|
2017-06-24 17:36:49 +02:00
|
|
|
.setOptLevel(llvm::CodeGenOpt::Aggressive)
|
2019-03-18 17:24:55 +01:00
|
|
|
.setCodeModel(flags & 0x2 ? llvm::CodeModel::Large : llvm::CodeModel::Small)
|
2023-03-11 20:08:27 +01:00
|
|
|
#ifdef __APPLE__
|
2023-04-08 14:21:22 +02:00
|
|
|
//.setCodeModel(llvm::CodeModel::Large)
|
2023-03-11 20:08:27 +01:00
|
|
|
#endif
|
|
|
|
.setRelocationModel(llvm::Reloc::Model::PIC_)
|
2017-06-24 17:36:49 +02:00
|
|
|
.setMCPU(m_cpu)
|
|
|
|
.create());
|
2023-03-11 20:08:27 +01:00
|
|
|
}
|
2017-06-24 17:36:49 +02:00
|
|
|
|
2023-03-11 20:08:27 +01:00
|
|
|
if (!_link.empty())
|
|
|
|
{
|
2020-05-19 18:09:27 +02:00
|
|
|
for (auto&& [name, addr] : _link)
|
2017-06-24 17:36:49 +02:00
|
|
|
{
|
2021-01-19 18:40:15 +01:00
|
|
|
m_engine->updateGlobalMapping(name, addr);
|
2017-06-24 17:36:49 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2021-12-24 18:33:32 +01:00
|
|
|
if (!_link.empty() || !(flags & 0x1))
|
|
|
|
{
|
|
|
|
m_engine->RegisterJITEventListener(llvm::JITEventListener::createIntelJITEventListener());
|
|
|
|
m_engine->RegisterJITEventListener(new JITAnnouncer);
|
|
|
|
}
|
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
if (!m_engine)
|
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
fmt::throw_exception("LLVM: Failed to create ExecutionEngine: %s", result);
|
2016-06-22 15:37:51 +02:00
|
|
|
}
|
2017-06-24 17:36:49 +02:00
|
|
|
}
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2017-06-24 17:36:49 +02:00
|
|
|
jit_compiler::~jit_compiler()
|
|
|
|
{
|
2017-02-26 16:56:31 +01:00
|
|
|
}
|
|
|
|
|
2020-05-06 17:18:30 +02:00
|
|
|
void jit_compiler::add(std::unique_ptr<llvm::Module> _module, const std::string& path)
|
2017-02-26 16:56:31 +01:00
|
|
|
{
|
2017-06-22 23:52:09 +02:00
|
|
|
ObjectCache cache{path};
|
|
|
|
m_engine->setObjectCache(&cache);
|
2017-02-26 16:56:31 +01:00
|
|
|
|
2020-05-06 17:18:30 +02:00
|
|
|
const auto ptr = _module.get();
|
|
|
|
m_engine->addModule(std::move(_module));
|
2017-06-22 23:52:09 +02:00
|
|
|
m_engine->generateCodeForModule(ptr);
|
|
|
|
m_engine->setObjectCache(nullptr);
|
2017-02-26 16:56:31 +01:00
|
|
|
|
2018-05-01 12:20:36 +02:00
|
|
|
for (auto& func : ptr->functions())
|
|
|
|
{
|
|
|
|
// Delete IR to lower memory consumption
|
|
|
|
func.deleteBody();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 17:18:30 +02:00
|
|
|
void jit_compiler::add(std::unique_ptr<llvm::Module> _module)
|
2018-05-01 12:20:36 +02:00
|
|
|
{
|
2020-05-06 17:18:30 +02:00
|
|
|
const auto ptr = _module.get();
|
|
|
|
m_engine->addModule(std::move(_module));
|
2018-05-01 12:20:36 +02:00
|
|
|
m_engine->generateCodeForModule(ptr);
|
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
for (auto& func : ptr->functions())
|
2017-02-26 16:56:31 +01:00
|
|
|
{
|
2017-06-22 23:52:09 +02:00
|
|
|
// Delete IR to lower memory consumption
|
|
|
|
func.deleteBody();
|
2017-02-26 16:56:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 11:20:40 +02:00
|
|
|
void jit_compiler::add(const std::string& path)
|
|
|
|
{
|
2019-10-20 21:42:59 +02:00
|
|
|
auto cache = ObjectCache::load(path);
|
|
|
|
|
|
|
|
if (auto object_file = llvm::object::ObjectFile::createObjectFile(*cache))
|
|
|
|
{
|
|
|
|
m_engine->addObjectFile( std::move(*object_file) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-01 05:15:50 +01:00
|
|
|
jit_log.error("ObjectCache: Adding failed: %s", path);
|
2019-10-20 21:42:59 +02:00
|
|
|
}
|
2017-07-15 11:20:40 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 15:09:47 +02:00
|
|
|
bool jit_compiler::check(const std::string& path)
|
|
|
|
{
|
|
|
|
if (auto cache = ObjectCache::load(path))
|
|
|
|
{
|
|
|
|
if (auto object_file = llvm::object::ObjectFile::createObjectFile(*cache))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 15:26:22 +02:00
|
|
|
|
|
|
|
if (fs::remove_file(path))
|
|
|
|
{
|
|
|
|
jit_log.error("ObjectCache: Removed damaged file: %s", path);
|
|
|
|
}
|
2020-04-07 15:09:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:53:05 +02:00
|
|
|
void jit_compiler::update_global_mapping(const std::string& name, u64 addr)
|
|
|
|
{
|
|
|
|
m_engine->updateGlobalMapping(name, addr);
|
|
|
|
}
|
|
|
|
|
2017-06-24 17:36:49 +02:00
|
|
|
void jit_compiler::fin()
|
2017-02-26 16:56:31 +01:00
|
|
|
{
|
2016-06-22 15:37:51 +02:00
|
|
|
m_engine->finalizeObject();
|
2017-06-22 23:52:09 +02:00
|
|
|
}
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2017-06-29 16:25:39 +02:00
|
|
|
u64 jit_compiler::get(const std::string& name)
|
2017-06-22 23:52:09 +02:00
|
|
|
{
|
2017-06-29 16:25:39 +02:00
|
|
|
return m_engine->getGlobalValueAddress(name);
|
2017-06-24 17:36:49 +02:00
|
|
|
}
|
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
#endif
|