8000 Get rid of dynamic lookup · python/cpython@94448ed · GitHub
[go: up one dir, main page]

Skip to content

Commit 94448ed

Browse files
committed
Get rid of dynamic lookup
1 parent 32b2708 commit 94448ed

File tree

2 files changed

+22
-42
lines changed

2 files changed

+22
-42
lines changed

Python/jit.c

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#include "Python.h"
22
#include "pycore_abstract.h"
33
#include "pycore_ceval.h"
4+
#include "pycore_dict.h"
5+
#include "pycore_intrinsics.h"
6+
#include "pycore_long.h"
47
#include "pycore_opcode_metadata.h"
58
#include "pycore_opcode_utils.h"
9+
#include "pycore_pyerrors.h"
10+
#include "pycore_setobject.h"
11+
#include "pycore_sliceobject.h"
612
#include "pycore_uops.h"
713
#include "pycore_jit.h"
814

@@ -12,25 +18,8 @@
1218
#ifdef MS_WINDOWS
1319
#include <psapi.h>
1420
#include <windows.h>
15-
FARPROC
16-
LOOKUP(LPCSTR symbol)
17-
{
18-
DWORD cbNeeded;
19-
HMODULE hMods[1024];
20-
HANDLE hProcess = GetCurrentProcess();
21-
if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded)) {
22-
for (unsigned int i = 0; i < (cbNeeded / sizeof(HMODULE)); i++) {
23-
FARPROC value = GetProcAddress(hMods[i], symbol);
24-
if (value) {
25-
return value;
26-
}
27-
}
28-
}
29-
return NULL;
30-
}
3121
#else
3222
#include <sys/mman.h>
33-
#define LOOKUP(SYMBOL) dlsym(RTLD_DEFAULT, (SYMBOL))
3423
#endif
3524

3625
#define MB (1 << 20)
@@ -193,7 +182,7 @@ copy_and_patch(unsigned char *memory, const Stencil *stencil, uint64_t patches[]
193182
}
194183
for (size_t i = 0; i < stencil->nloads; i++) {
195184
const SymbolLoad *load = &stencil->loads[i];
196-
uint64_t value = symbol_addresses[load->symbol];
185+
uint64_t value = load->symbol;
197186
patch_one(memory + load->offset, load->kind, value, load->addend);
198187
}
199188
}
@@ -207,14 +196,6 @@ _PyJIT_CompileTrace(_PyUOpInstruction *trace, int size)
207196
}
208197
if (initialized == 0) {
209198
initialized = -1;
210-
for (size_t i = 0; i < Py_ARRAY_LENGTH(symbols); i++) {
211-
symbol_addresses[i] = (uintptr_t)LOOKUP(symbols[i]);
212-
if (symbol_addresses[i] == 0) {
213-
const char *w = "JIT initialization failed (can't find symbol \"%s\")";
214-
PyErr_WarnFormat(PyExc_RuntimeWarning, 0, w, symbols[i]);
215-
return NULL;
216-
}
217-
}
218199
#ifdef MS_WINDOWS
219200
SYSTEM_INFO si;
220201
GetSystemInfo(&si);
@@ -281,10 +262,10 @@ _PyJIT_CompileTrace(_PyUOpInstruction *trace, int size)
281262
return NULL;
282263
}
283264
unsigned char *head = memory;
284-
uint64_t patches[] = GET_PATCHES();
285265
// First, the trampoline:
286266
_PyUOpInstruction *instruction_continue = &trace[0];
287267
const Stencil *stencil = &trampoline_stencil;
268+
uint64_t patches[] = GET_PATCHES();
288269
patches[_JIT_BASE] = (uintptr_t)head;
289270
patches[_JIT_CONTINUE] = (uintptr_t)head + offsets[0];
290271
patches[_JIT_CONTINUE_OPARG] = instruction_continue->oparg;
@@ -297,6 +278,7 @@ _PyJIT_CompileTrace(_PyUOpInstruction *trace, int size)
297278
_PyUOpInstruction *instruction_continue = &trace[(i + 1) % size];
298279
_PyUOpInstruction *instruction_jump = &trace[instruction->oparg % size];
299280
const Stencil *stencil = &stencils[instruction->opcode];
281+
uint64_t patches[] = GET_PATCHES();
300282
patches[_JIT_BASE] = (uintptr_t)memory + offsets[i];
301283
patches[_JIT_CONTINUE] = (uintptr_t)memory + offsets[(i + 1) % size];
302284
patches[_JIT_CONTINUE_OPARG] = instruction_continue->oparg;

Tools/jit/build.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,12 @@ async def build(self) -> None:
583583
task = self._compile(opname, TOOLS_JIT_TEMPLATE, tempdir)
584584
group.create_task(task)
585585

586+
def as_i64(value: int, width: int = 0) -> str:
587+
value %= 1 << 64
588+
width = max(0, width - 3)
589+
if value & (1 << 63):
590+
return f"-0x{(1 << 64) - value:0{width}x}"
591+
return f"+0x{value:0{width}x}"
586592

587593
def dump(stencils: dict[str, Stencil]) -> typing.Generator[str, None, None]:
588594
yield f"// $ {sys.executable} {' '.join(sys.argv)}" # XXX
@@ -608,7 +614,7 @@ def dump(stencils: dict[str, Stencil]) -> typing.Generator[str, None, None]:
608614
yield f" const HoleKind kind;"
609615
yield f" const uint64_t offset;"
610616
yield f" const uint64_t addend;"
611-
yield f" const int symbol;"
617+
yield f" const uint64_t symbol;"
612618
yield f"}} SymbolLoad;"
613619
yield f""
614620
yield f"typedef struct {{"
@@ -642,7 +648,7 @@ def dump(stencils: dict[str, Stencil]) -> typing.Generator[str, None, None]:
642648
(len(f"{hole.offset:x}") for hole in stencil.holes), default=0
643649
)
644650
addend_width = max(
645-
(len(f"{hole.addend % (1 << 64):x}") for hole in stencil.holes), default=0
651+
(len(f"{as_i64(hole.addend)}") for hole in stencil.holes), default=0
646652
)
647653
value_width = max(
648654
(
@@ -654,7 +660,7 @@ def dump(stencils: dict[str, Stencil]) -> typing.Generator[str, None, None]:
654660
)
655661
symbol_width = max(
656662
(
657-
len(f"{symbols.index(hole.symbol)}")
663+
len(f"{hole.symbol}")
658664
for hole in stencil.holes
659665
if hole.symbol not in HoleValue.__members__
660666
),
@@ -667,7 +673,7 @@ def dump(stencils: dict[str, Stencil]) -> typing.Generator[str, None, None]:
667673
f" {{"
668674
f".kind={hole.kind:{kind_width}}, "
669675
f".offset=0x{hole.offset:0{offset_width}x}, "
670-
f".addend=0x{hole.addend % (1 << 64):0{addend_width}x}, "
676+
f".addend={as_i64(hole.addend, addend_width)}, "
671677
f".value={value.name:{value_width}}"
672678
f"}},"
673679
)
@@ -676,9 +682,9 @@ def dump(stencils: dict[str, Stencil]) -> typing.Generator[str, None, None]:
676682
f" {{"
677683
f".kind={hole.kind:{kind_width}}, "
678684
f".offset=0x{hole.offset:0{offset_width}x}, "
679-
f".addend=0x{hole.addend % (1 << 64):0{addend_width}x}, "
680-
f".symbol={symbols.index(hole.symbol):{symbol_width}}"
681-
f"}}, // {hole.symbol}"
685+
f".addend={as_i64(hole.addend, addend_width)}, "
686+
f".symbol=(uintptr_t)&{hole.symbol:{symbol_width}}"
687+
f"}},"
682688
)
683689
if holes:
684690
yield f"static const Hole {opname}_stencil_holes[{len(holes) + 1}] = {{"
@@ -695,14 +701,6 @@ def dump(stencils: dict[str, Stencil]) -> typing.Generator[str, None, None]:
695701
else:
696702
yield f"static const SymbolLoad {opname}_stencil_loads[{len(loads) + 1}];"
697703
yield f""
698-
yield f""
699-
yield f"static const char *const symbols[{len(symbols)}] = {{"
700-
for symbol in symbols:
701-
yield f' "{symbol}",'
702-
yield f"}};"
703-
yield f""
704-
yield f"static uint64_t symbol_addresses[{len(symbols)}];"
705-
yield f""
706704
yield f"#define INIT_STENCIL(OP) {{ \\"
707705
yield f" .nbytes = Py_ARRAY_LENGTH(OP##_stencil_bytes), \\"
708706
yield f" .bytes = OP##_stencil_bytes, \\"

0 commit comments

Comments
 (0)
0