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"
|
2023-10-12 01:21:35 +02:00
|
|
|
#include "Crypto/unzip.h"
|
2023-11-27 14:31:33 +01:00
|
|
|
|
2021-01-19 18:40:15 +01:00
|
|
|
#include <charconv>
|
2018-05-14 22:06:17 +02:00
|
|
|
|
2020-02-01 08:43:43 +01:00
|
|
|
LOG_CHANNEL(jit_log, "JIT");
|
2020-02-01 05:15:50 +01:00
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
#ifdef LLVM_AVAILABLE
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#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
|
2024-03-10 12:56:16 +01:00
|
|
|
#include <llvm/Support/CodeGen.h>
|
2016-06-22 15:37:51 +02:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2023-09-25 17:04:48 +02:00
|
|
|
#include "llvm/TargetParser/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 data = c.newLabel();
|
2024-09-25 04:31:48 +02:00
|
|
|
Label jump_address = c.newLabel();
|
|
|
|
c.ldr(args[0], arm::ptr(data, 0));
|
|
|
|
c.ldr(a64::x14, arm::ptr(jump_address, 0));
|
|
|
|
c.br(a64::x14);
|
|
|
|
|
|
|
|
// Data frame
|
2022-06-14 14:28:38 +02:00
|
|
|
c.align(AlignMode::kCode, 16);
|
2024-09-25 04:31:48 +02:00
|
|
|
c.bind(jump_address);
|
|
|
|
c.embedUInt64(reinterpret_cast<u64>(&null));
|
2022-06-14 14:28:38 +02:00
|
|
|
|
2024-09-25 04:31:48 +02:00
|
|
|
c.align(AlignMode::kData, 16);
|
2022-06-14 14:28:38 +02:00
|
|
|
c.bind(data);
|
|
|
|
c.embed(name.c_str(), name.size());
|
|
|
|
c.embedUInt8(0U);
|
|
|
|
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;
|
2024-05-29 17:16:50 +02:00
|
|
|
const std::add_pointer_t<jit_compiler> m_compiler = nullptr;
|
2017-06-22 23:52:09 +02:00
|
|
|
|
|
|
|
public:
|
2024-05-29 17:16:50 +02:00
|
|
|
ObjectCache(const std::string& path, jit_compiler* compiler = nullptr)
|
2017-06-22 23:52:09 +02:00
|
|
|
: m_path(path)
|
2024-05-29 17:16:50 +02:00
|
|
|
, m_compiler(compiler)
|
2017-06-22 23:52:09 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~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;
|
2024-05-29 17:16:50 +02:00
|
|
|
|
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");
|
|
|
|
|
2024-05-29 17:16:50 +02:00
|
|
|
if (!obj.getBufferSize())
|
|
|
|
{
|
|
|
|
jit_log.error("LLVM: Nothing to write: %s", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ensure(m_compiler);
|
|
|
|
|
2023-11-27 14:31:33 +01:00
|
|
|
fs::file module_file(name, fs::rewrite);
|
2019-12-27 20:50:14 +01:00
|
|
|
|
2023-11-27 14:31:33 +01:00
|
|
|
if (!module_file)
|
2019-12-27 20:50:14 +01:00
|
|
|
{
|
2023-11-27 14:31:33 +01:00
|
|
|
jit_log.error("LLVM: Failed to create module file: %s (%s)", name, fs::g_tls_error);
|
2019-12-27 20:50:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-29 17:16:50 +02:00
|
|
|
// Bold assumption about upper limit of space consumption
|
|
|
|
const usz max_size = obj.getBufferSize() * 4;
|
|
|
|
|
|
|
|
if (!m_compiler->add_sub_disk_space(0 - max_size))
|
|
|
|
{
|
|
|
|
jit_log.error("LLVM: Failed to create module file: %s (not enough disk space left)", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-27 14:31:33 +01:00
|
|
|
if (!zip(obj.getBufferStart(), obj.getBufferSize(), module_file))
|
2021-03-01 14:11:01 +01:00
|
|
|
{
|
2023-11-27 14:31:33 +01:00
|
|
|
jit_log.error("LLVM: Failed to compress module: %s", _module->getName().data());
|
|
|
|
module_file.close();
|
|
|
|
fs::remove_file(name);
|
2023-10-12 01:21:35 +02:00
|
|
|
return;
|
2021-03-01 14:11:01 +01:00
|
|
|
}
|
|
|
|
|
2024-07-27 11:20:49 +02:00
|
|
|
jit_log.trace("LLVM: Created module: %s", _module->getName().data());
|
2024-05-29 17:16:50 +02:00
|
|
|
|
|
|
|
// Restore space that was overestimated
|
|
|
|
ensure(m_compiler->add_sub_disk_space(max_size - module_file.size()));
|
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})
|
|
|
|
{
|
2023-10-12 01:21:35 +02:00
|
|
|
const std::vector<u8> cached_data = cached.to_vector<u8>();
|
2020-04-07 15:26:22 +02:00
|
|
|
|
2023-10-12 01:21:35 +02:00
|
|
|
if (cached_data.empty()) [[unlikely]]
|
2020-04-07 15:26:22 +02:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-12-27 20:50:14 +01:00
|
|
|
|
2023-10-12 01:21:35 +02:00
|
|
|
const std::vector<u8> out = unzip(cached_data);
|
2019-12-27 20:50:14 +01:00
|
|
|
|
2023-10-12 01:21:35 +02:00
|
|
|
if (out.empty())
|
|
|
|
{
|
|
|
|
jit_log.error("LLVM: Failed to unzip module: '%s'", path);
|
|
|
|
return nullptr;
|
2019-12-27 20:50:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-08-03 03:54:37 +02:00
|
|
|
if (m_cpu == "generic")
|
|
|
|
{
|
|
|
|
// Try to detect a best match based on other criteria
|
|
|
|
m_cpu = fallback_cpu_detection();
|
|
|
|
}
|
|
|
|
|
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)
|
2024-08-25 19:38:02 +02:00
|
|
|
return llvm::Triple::normalize("aarch64-unknown-linux-android"); // Set environment to android to reserve x18
|
2023-04-08 14:21:22 +02:00
|
|
|
#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)
|
2024-08-25 19:38:02 +02:00
|
|
|
return llvm::Triple::normalize("aarch64-unknown-linux-android"); // Set environment to android to reserve x18
|
2023-04-08 14:21:22 +02:00
|
|
|
#else
|
|
|
|
return llvm::Triple::normalize(llvm::sys::getProcessTriple());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-05-29 17:16:50 +02:00
|
|
|
bool jit_compiler::add_sub_disk_space(ssz space)
|
|
|
|
{
|
|
|
|
if (space >= 0)
|
|
|
|
{
|
|
|
|
ensure(m_disk_space.fetch_add(space) < ~static_cast<usz>(space));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_disk_space.fetch_op([sub_size = static_cast<usz>(0 - space)](usz& val)
|
|
|
|
{
|
|
|
|
if (val >= sub_size)
|
|
|
|
{
|
|
|
|
val -= sub_size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}).second;
|
|
|
|
}
|
|
|
|
|
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))
|
2024-03-10 12:56:16 +01:00
|
|
|
#if LLVM_VERSION_MAJOR < 18
|
2017-06-24 17:36:49 +02:00
|
|
|
.setOptLevel(llvm::CodeGenOpt::Aggressive)
|
2024-03-10 12:56:16 +01:00
|
|
|
#else
|
|
|
|
.setOptLevel(llvm::CodeGenOptLevel::Aggressive)
|
|
|
|
#endif
|
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
|
|
|
}
|
2024-05-29 17:16:50 +02:00
|
|
|
|
|
|
|
fs::device_stat stats{};
|
|
|
|
|
|
|
|
if (fs::statfs(fs::get_cache_dir(), stats))
|
|
|
|
{
|
|
|
|
m_disk_space = stats.avail_free / 4;
|
|
|
|
}
|
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
|
|
|
{
|
2024-05-29 17:16:50 +02:00
|
|
|
ObjectCache cache{path, this};
|
2017-06-22 23:52:09 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 17:16:50 +02:00
|
|
|
bool jit_compiler::add(const std::string& path)
|
2017-07-15 11:20:40 +02:00
|
|
|
{
|
2019-10-20 21:42:59 +02:00
|
|
|
auto cache = ObjectCache::load(path);
|
|
|
|
|
2024-05-29 16:43:23 +02:00
|
|
|
if (!cache)
|
|
|
|
{
|
|
|
|
jit_log.error("ObjectCache: Failed to read file. (path='%s', error=%s)", path, fs::g_tls_error);
|
2024-05-29 17:16:50 +02:00
|
|
|
return false;
|
2024-05-29 16:43:23 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 21:42:59 +02:00
|
|
|
if (auto object_file = llvm::object::ObjectFile::createObjectFile(*cache))
|
|
|
|
{
|
2023-05-15 01:12:14 +02:00
|
|
|
m_engine->addObjectFile(llvm::object::OwningBinary<llvm::object::ObjectFile>(std::move(*object_file), std::move(cache)));
|
2024-05-29 17:16:50 +02:00
|
|
|
jit_log.trace("ObjectCache: Successfully added %s", path);
|
|
|
|
return true;
|
2019-10-20 21:42:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-01 05:15:50 +01:00
|
|
|
jit_log.error("ObjectCache: Adding failed: %s", path);
|
2024-05-29 17:16:50 +02:00
|
|
|
return false;
|
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
|
|
|
}
|
|
|
|
|
2024-08-03 03:54:37 +02:00
|
|
|
llvm::StringRef fallback_cpu_detection()
|
|
|
|
{
|
|
|
|
#if defined (ARCH_X64)
|
|
|
|
// If we got here we either have a very old and outdated CPU or a new CPU that has not been seen by LLVM yet.
|
2024-08-08 21:40:52 +02:00
|
|
|
const std::string brand = utils::get_cpu_brand();
|
2024-08-03 03:54:37 +02:00
|
|
|
const auto family = utils::get_cpu_family();
|
|
|
|
const auto model = utils::get_cpu_model();
|
|
|
|
|
2024-08-08 21:40:52 +02:00
|
|
|
if (brand.starts_with("AMD"))
|
2024-08-03 03:54:37 +02:00
|
|
|
{
|
|
|
|
switch (family)
|
|
|
|
{
|
2024-08-03 15:27:33 +02:00
|
|
|
case 0x10:
|
|
|
|
return "amdfam10";
|
|
|
|
case 0x15:
|
|
|
|
// Bulldozer class, includes piledriver, excavator, steamroller, etc
|
|
|
|
return utils::has_avx2() ? "bdver4" : "bdver1";
|
2024-08-03 03:54:37 +02:00
|
|
|
case 0x17:
|
|
|
|
case 0x18:
|
|
|
|
// No major differences between znver1 and znver2, return the lesser
|
|
|
|
return "znver1";
|
|
|
|
case 0x19:
|
|
|
|
// Models 0-Fh are zen3 as are 20h-60h. The rest we can assume are zen4
|
|
|
|
return ((model >= 0x20 && model <= 0x60) || model < 0x10)
|
|
|
|
? "znver3"
|
|
|
|
: "znver4";
|
|
|
|
case 0x1a:
|
2024-08-03 04:27:58 +02:00
|
|
|
// Only one generation in family 1a so far, zen5, which we do not support yet.
|
|
|
|
// Return zen4 as a workaround until the next LLVM upgrade.
|
|
|
|
return "znver4";
|
2024-08-03 03:54:37 +02:00
|
|
|
default:
|
2024-08-03 15:27:33 +02:00
|
|
|
return utils::has_avx512()
|
|
|
|
? "znver4"
|
|
|
|
: "znver3";
|
2024-08-03 03:54:37 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-08 21:40:52 +02:00
|
|
|
else if (brand.find("Intel") != std::string::npos)
|
2024-08-03 04:09:10 +02:00
|
|
|
{
|
|
|
|
if (!utils::has_avx())
|
|
|
|
{
|
|
|
|
return "nehalem";
|
|
|
|
}
|
|
|
|
if (!utils::has_avx2())
|
|
|
|
{
|
|
|
|
return "ivybridge";
|
|
|
|
}
|
|
|
|
if (!utils::has_avx512())
|
|
|
|
{
|
|
|
|
return "skylake";
|
|
|
|
}
|
|
|
|
if (utils::has_avx512_icl())
|
|
|
|
{
|
|
|
|
return "cannonlake";
|
|
|
|
}
|
|
|
|
return "icelake-client";
|
|
|
|
}
|
2024-08-08 21:40:52 +02:00
|
|
|
else if (brand.starts_with("VirtualApple"))
|
2024-08-03 03:54:37 +02:00
|
|
|
{
|
|
|
|
// No AVX. This will change in MacOS 15+, at which point we may revise this.
|
2024-08-03 04:09:10 +02:00
|
|
|
return utils::has_avx() ? "haswell" : "nehalem";
|
2024-08-03 03:54:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(ARCH_ARM64)
|
|
|
|
// TODO: Read the data from /proc/cpuinfo. ARM CPU registers are not accessible from usermode.
|
|
|
|
// This will be a pain when supporting snapdragon on windows but we'll cross that bridge when we get there.
|
|
|
|
// Require at least armv8-2a. Older chips are going to be useless anyway.
|
|
|
|
return "cortex-a78";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Failed to guess, use generic fallback
|
|
|
|
return "generic";
|
|
|
|
}
|
|
|
|
|
2023-12-29 15:14:22 +01:00
|
|
|
#endif // LLVM_AVAILABLE
|