Skip to content

Commit f2d1bbd

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Improve JIT behavior on unsupported platforms
Summary: If trying to run on an unsupported CPU architecture, actually emit a debug print that says what the problem is. Currently the DLOG is always silenced. Also catch exceptions from asmjit if we do ever get that far. Otherwise the entire Python runtime will crash. This way importing CinderX will raise a RuntimeError instead. Reviewed By: DinoV Differential Revision: D84076101 fbshipit-source-id: 4114ce5e038fad22ca0740c693fb6bb8cb72410f
1 parent 0638e47 commit f2d1bbd

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

cinderx/Jit/pyjit.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2789,6 +2789,22 @@ void finalizeInternedStrings() {
27892789
}
27902790
}
27912791

2792+
constexpr std::string_view getCpuArchName() {
2793+
#if defined(__x86_64__)
2794+
return "x86-64";
2795+
#elif defined(__i386__)
2796+
return "x86 (32-bit)";
2797+
#elif defined(__aarch64__)
2798+
return "arm64";
2799+
#elif defined(__arm__)
2800+
return "arm";
2801+
#elif defined(__riscv)
2802+
return "riscv";
2803+
#else
2804+
return "unknown";
2805+
#endif
2806+
}
2807+
27922808
} // namespace
27932809

27942810
#if PY_VERSION_HEX < 0x030C0000
@@ -2964,6 +2980,15 @@ int initialize() {
29642980
return 0;
29652981
}
29662982

2983+
// Do this check after config is initialized, so we can use JIT_DLOG().
2984+
#ifndef __x86_64__
2985+
JIT_DLOG(
2986+
"JIT only supported x86-64 platforms, detected current architecture as "
2987+
"'{}'. Disabling the JIT.",
2988+
getCpuArchName());
2989+
return 0;
2990+
#endif
2991+
29672992
std::unique_ptr<JITList> jit_list;
29682993
if (!getConfig().jit_list.filename.empty()) {
29692994
if (getConfig().allow_jit_list_wildcards) {
@@ -2992,7 +3017,14 @@ int initialize() {
29923017
cinderx::ModuleState* mod_state = cinderx::getModuleState();
29933018
mod_state->setCodeAllocator(CodeAllocator::make());
29943019

2995-
cinderx::getModuleState()->setJitContext(new CompilerContext<Compiler>());
3020+
// Initialize the main compiler object and its context. This will throw if
3021+
// asmjit cannot initialize.
3022+
try {
3023+
cinderx::getModuleState()->setJitContext(new CompilerContext<Compiler>());
3024+
} catch (const std::exception& exn) {
3025+
PyErr_SetString(PyExc_RuntimeError, exn.what());
3026+
return -1;
3027+
}
29963028

29973029
PyObject* mod = _Ci_CreateBuiltinModule(&jit_module, "cinderjit");
29983030
if (mod == nullptr) {

0 commit comments

Comments
 (0)