GH-126910: Build/link the JIT shim in the Python interpreter (#148872) · python/cpython@9633c52

GitHub

@@ -60,8 +60,6 @@ jit_error(const char *message)

6060PyErr_Format(PyExc_RuntimeWarning, "JIT %s (%d)", message, hint);

6161}

626263-staticsize_t_Py_jit_shim_size=0;

64-6563staticint

6664address_in_executor_array(_PyExecutorObject**ptrs, size_tcount, uintptr_taddr)

6765{

@@ -104,13 +102,6 @@ _PyJIT_AddressInJitCode(PyInterpreterState *interp, uintptr_t addr)

104102if (interp==NULL) {

105103return0;

106104 }

107-if (_Py_jit_entry!=_Py_LazyJitShim&&_Py_jit_shim_size!=0) {

108-uintptr_tstart= (uintptr_t)_Py_jit_entry;

109-uintptr_tend=start+_Py_jit_shim_size;

110-if (addr >= start&&addr<end) {

111-return1;

112- }

113- }

114105if (address_in_executor_array(interp->executor_ptrs, interp->executor_count, addr)) {

115106return1;

116107 }

@@ -727,75 +718,6 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz

727718return0;

728719}

729720730-/* One-off compilation of the jit entry shim

731- * We compile this once only as it effectively a normal

732- * function, but we need to use the JIT because it needs

733- * to understand the jit-specific calling convention.

734- * Don't forget to call _PyJIT_Fini later!

735- */

736-static_PyJitEntryFuncPtr

737-compile_shim(void)

738-{

739-_PyExecutorObjectdummy;

740-constStencilGroup*group;

741-size_tcode_size=0;

742-size_tdata_size=0;

743-jit_statestate= {0};

744-group=&shim;

745-code_size+=group->code_size;

746-data_size+=group->data_size;

747-combine_symbol_mask(group->trampoline_mask, state.trampolines.mask);

748-combine_symbol_mask(group->got_mask, state.got_symbols.mask);

749-// Round up to the nearest page:

750-size_tpage_size=get_page_size();

751-assert((page_size& (page_size-1)) ==0);

752-size_tcode_padding=DATA_ALIGN- ((code_size+state.trampolines.size) & (DATA_ALIGN-1));

753-size_tpadding=page_size- ((code_size+state.trampolines.size+code_padding+data_size+state.got_symbols.size) & (page_size-1));

754-size_ttotal_size=code_size+state.trampolines.size+code_padding+data_size+state.got_symbols.size+padding;

755-unsigned char*memory=jit_alloc(total_size);

756-if (memory==NULL) {

757-returnNULL;

758- }

759-unsigned char*code=memory;

760-state.trampolines.mem=memory+code_size;

761-unsigned char*data=memory+code_size+state.trampolines.size+code_padding;

762-state.got_symbols.mem=data+data_size;

763-// Compile the shim, which handles converting between the native

764-// calling convention and the calling convention used by jitted code

765-// (which may be different for efficiency reasons).

766-group=&shim;

767-group->emit(code, data, &dummy, NULL, &state);

768-code+=group->code_size;

769-data+=group->data_size;

770-assert(code==memory+code_size);

771-assert(data==memory+code_size+state.trampolines.size+code_padding+data_size);

772-if (mark_executable(memory, total_size)) {

773-jit_free(memory, total_size);

774-returnNULL;

775- }

776-_Py_jit_shim_size=total_size;

777-return (_PyJitEntryFuncPtr)memory;

778-}

779-780-staticPyMutexlazy_jit_mutex= { 0 };

781-782-_Py_CODEUNIT*

783-_Py_LazyJitShim(

784-_PyExecutorObject*executor, _PyInterpreterFrame*frame, _PyStackRef*stack_pointer, PyThreadState*tstate

785-) {

786-PyMutex_Lock(&lazy_jit_mutex);

787-if (_Py_jit_entry==_Py_LazyJitShim) {

788-_PyJitEntryFuncPtrshim=compile_shim();

789-if (shim==NULL) {

790-PyMutex_Unlock(&lazy_jit_mutex);

791-Py_FatalError("Cannot allocate core JIT code");

792- }

793-_Py_jit_entry=shim;

794- }

795-PyMutex_Unlock(&lazy_jit_mutex);

796-return_Py_jit_entry(executor, frame, stack_pointer, tstate);

797-}

798-799721// Free executor's memory allocated with _PyJIT_Compile

800722void

801723_PyJIT_Free(_PyExecutorObject*executor)

@@ -812,22 +734,4 @@ _PyJIT_Free(_PyExecutorObject *executor)

812734 }

813735}

814736815-// Free shim memory allocated with compile_shim

816-void

817-_PyJIT_Fini(void)

818-{

819-PyMutex_Lock(&lazy_jit_mutex);

820-unsigned char*memory= (unsigned char*)_Py_jit_entry;

821-size_tsize=_Py_jit_shim_size;

822-if (size) {

823-_Py_jit_entry=_Py_LazyJitShim;

824-_Py_jit_shim_size=0;

825-if (jit_free(memory, size)) {

826-PyErr_FormatUnraisable("Exception ignored while "

827-"freeing JIT entry code");

828- }

829- }

830-PyMutex_Unlock(&lazy_jit_mutex);

831-}

832-833737#endif// _Py_JIT