Skip to content

Commit 2e9ba92

Browse files
Address comments about get_externals and trampoline helper
1 parent 618a4a6 commit 2e9ba92

File tree

3 files changed

+32
-45
lines changed

3 files changed

+32
-45
lines changed

PCbuild/get_external.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,22 @@
1111
import zipfile
1212

1313

14-
def request_with_retry(request_func, *args, max_retries=7,
15-
err_msg='Request failed.', **kwargs):
16-
"""Make a request using request_func with exponential backoff"""
14+
def retrieve_with_retries(download_location, output_path, reporthook, max_retries=7):
15+
"""Download a file with retries."""
1716
for attempt in range(max_retries + 1):
1817
try:
19-
resp = request_func(*args, **kwargs)
18+
resp = urllib.request.urlretrieve(
19+
download_location,
20+
output_path,
21+
reporthook,
22+
)
2023
except (urllib.error.URLError, ConnectionError) as ex:
2124
if attempt == max_retries:
22-
raise OSError(err_msg) from ex
25+
raise OSError(f'Download from {download_location} failed.') from ex
2326
time.sleep(2.25**attempt)
2427
else:
2528
return resp
2629

27-
28-
def retrieve_with_retries(download_location, output_path, reporthook):
29-
"""Download a file with retries."""
30-
return request_with_retry(
31-
urllib.request.urlretrieve,
32-
download_location,
33-
output_path,
34-
reporthook,
35-
err_msg=f'Download from {download_location} failed.',
36-
)
37-
38-
3930
def fetch_zip(commit_hash, zip_dir, *, org='python', binary=False, verbose=False):
4031
repo = 'cpython-bin-deps' if binary else 'cpython-source-deps'
4132
url = f'https://github.com/{org}/{repo}/archive/{commit_hash}.zip'

PCbuild/get_externals.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ for %%b in (%binaries%) do (
9393
) else (
9494
echo.Fetching %%b...
9595
if "%%b"=="llvm-20.1.8.0" (
96-
%PYTHON% -E "%PCBUILD%\get_external.py" -r -O %ORG% -e "%EXTERNALS_DIR%" %%b
96+
%PYTHON% -E "%PCBUILD%\get_external.py" --release --organization %ORG% --externals-dir "%EXTERNALS_DIR%" %%b
9797
) else (
98-
%PYTHON% -E "%PCBUILD%\get_external.py" -b -O %ORG% -e "%EXTERNALS_DIR%" %%b
98+
%PYTHON% -E "%PCBUILD%\get_external.py" --binary --organization %ORG% --externals-dir "%EXTERNALS_DIR%" %%b
9999
)
100100
)
101101
)

Python/jit.c

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,25 @@ void patch_x86_64_trampoline(unsigned char *location, int ordinal, jit_state *st
461461
#define DATA_ALIGN 1
462462
#endif
463463

464+
// Get the trampoline memory location for a given symbol ordinal.
465+
static unsigned char *
466+
get_trampoline_slot(int ordinal, jit_state *state)
467+
{
468+
const uint32_t symbol_mask = 1 << (ordinal % 32);
469+
const uint32_t trampoline_mask = state->trampolines.mask[ordinal / 32];
470+
assert(symbol_mask & trampoline_mask);
471+
472+
// Count the number of set bits in the trampoline mask lower than ordinal
473+
int index = _Py_popcount32(trampoline_mask & (symbol_mask - 1));
474+
for (int i = 0; i < ordinal / 32; i++) {
475+
index += _Py_popcount32(state->trampolines.mask[i]);
476+
}
477+
478+
unsigned char *trampoline = state->trampolines.mem + index * TRAMPOLINE_SIZE;
479+
assert((size_t)(index + 1) * TRAMPOLINE_SIZE <= state->trampolines.size);
480+
return trampoline;
481+
}
482+
464483
// Generate and patch AArch64 trampolines. The symbols to jump to are stored
465484
// in the jit_stencils.h in the symbols_map.
466485
void
@@ -477,20 +496,8 @@ patch_aarch64_trampoline(unsigned char *location, int ordinal, jit_state *state)
477496
return;
478497
}
479498

480-
// Masking is done modulo 32 as the mask is stored as an array of uint32_t
481-
const uint32_t symbol_mask = 1 << (ordinal % 32);
482-
const uint32_t trampoline_mask = state->trampolines.mask[ordinal / 32];
483-
assert(symbol_mask & trampoline_mask);
484-
485-
// Count the number of set bits in the trampoline mask lower than ordinal,
486-
// this gives the index into the array of trampolines.
487-
int index = _Py_popcount32(trampoline_mask & (symbol_mask - 1));
488-
for (int i = 0; i < ordinal / 32; i++) {
489-
index += _Py_popcount32(state->trampolines.mask[i]);
490-
}
491-
492-
uint32_t *p = (uint32_t*)(state->trampolines.mem + index * TRAMPOLINE_SIZE);
493-
assert((size_t)(index + 1) * TRAMPOLINE_SIZE <= state->trampolines.size);
499+
// Out of range - need a trampoline
500+
uint32_t *p = (uint32_t *)get_trampoline_slot(ordinal, state);
494501

495502

496503
/* Generate the trampoline
@@ -521,18 +528,7 @@ patch_x86_64_trampoline(unsigned char *location, int ordinal, jit_state *state)
521528
}
522529

523530
// Out of range - need a trampoline
524-
const uint32_t symbol_mask = 1 << (ordinal % 32);
525-
const uint32_t trampoline_mask = state->trampolines.mask[ordinal / 32];
526-
assert(symbol_mask & trampoline_mask);
527-
528-
// Count the number of set bits in the trampoline mask lower than ordinal
529-
int index = _Py_popcount32(trampoline_mask & (symbol_mask - 1));
530-
for (int i = 0; i < ordinal / 32; i++) {
531-
index += _Py_popcount32(state->trampolines.mask[i]);
532-
}
533-
534-
unsigned char *trampoline = state->trampolines.mem + index * TRAMPOLINE_SIZE;
535-
assert((size_t)(index + 1) * TRAMPOLINE_SIZE <= state->trampolines.size);
531+
unsigned char *trampoline = get_trampoline_slot(ordinal, state);
536532

537533
/* Generate the trampoline (14 bytes, padded to 16):
538534
0: ff 25 00 00 00 00 jmp *(%rip)

0 commit comments

Comments
 (0)