Skip to content

Commit 4dd472a

Browse files
feat: store metadata in array rather than hacky thing we did (python#58)
1 parent 2acbe41 commit 4dd472a

1 file changed

Lines changed: 17 additions & 22 deletions

File tree

Python/tier2.c

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "opcode.h"
1111

12+
1213
#define BB_DEBUG 0
1314
#define TYPEPROP_DEBUG 0
1415
// Max typed version basic blocks per basic block
@@ -1835,9 +1836,8 @@ _PyTier2_Code_DetectAndEmitBB(
18351836
// 2. If there's a type guard.
18361837
bool needs_guard = 0;
18371838

1838-
_PyTier2BBMetadata *meta = NULL;
1839-
_PyTier2BBMetadata *temp_meta = NULL;
1840-
_PyTier2BBMetadata *jump_end_meta = NULL;
1839+
static _PyTier2BBMetadata *metas[256] = {NULL};
1840+
int metas_size = -1;
18411841

18421842
_PyTier2Info *t2_info = co->_tier2_info;
18431843
PyObject *consts = co->co_consts;
@@ -2138,13 +2138,10 @@ _PyTier2_Code_DetectAndEmitBB(
21382138
// Add the basic block to the jump ids
21392139
assert(start_type_context_copy != NULL);
21402140
assert(virtual_tier1_start != NULL);
2141-
if (add_metadata_to_jump_2d_array(t2_info, meta,
2141+
assert(metas_size >= 0);
2142+
if (add_metadata_to_jump_2d_array(t2_info, metas[metas_size],
21422143
backwards_jump_target_offset, start_type_context_copy,
21432144
virtual_tier1_start) < 0) {
2144-
PyMem_Free(meta);
2145-
if (meta != temp_meta) {
2146-
PyMem_Free(temp_meta);
2147-
}
21482145
_PyTier2TypeContext_Free(starting_type_context);
21492146
return NULL;
21502147
}
@@ -2169,11 +2166,11 @@ _PyTier2_Code_DetectAndEmitBB(
21692166
if (type_context_copy == NULL) {
21702167
return NULL;
21712168
}
2172-
// We can't unconditionally overwrite the first bb
2173-
// because we might have multiple jump targets in a single BB.
2174-
meta = meta != NULL ? meta : _PyTier2_AllocateBBMetaData(co,
2169+
metas_size++;
2170+
assert(metas_size <= 256);
2171+
metas[metas_size] = _PyTier2_AllocateBBMetaData(co,
21752172
t2_start, _PyCode_CODE(co) + i, type_context_copy);
2176-
if (meta == NULL) {
2173+
if (metas[metas_size] == NULL) {
21772174
_PyTier2TypeContext_Free(type_context_copy);
21782175
return NULL;
21792176
}
@@ -2248,28 +2245,26 @@ _PyTier2_Code_DetectAndEmitBB(
22482245

22492246
}
22502247
end:
2251-
// Create the tier 2 BB
2252-
2253-
temp_meta = _PyTier2_AllocateBBMetaData(co, t2_start,
2248+
// Create the final tier 2 BB
2249+
metas_size++;
2250+
assert(metas_size <= 256);
2251+
metas[metas_size] = _PyTier2_AllocateBBMetaData(co, t2_start,
22542252
// + 1 because we want to start with the NEXT instruction for the scan
22552253
_PyCode_CODE(co) + i + 1, starting_type_context);
2256-
if (temp_meta == NULL) {
2254+
if (metas[metas_size] == NULL) {
22572255
_PyTier2TypeContext_Free(starting_type_context);
22582256
return NULL;
22592257
}
2260-
// We need to return the first block to enter into. If there is already a block generated
2261-
// before us, then we use that instead of the most recent block.
2262-
if (meta == NULL) {
2263-
meta = temp_meta;
2264-
}
22652258
// Tell BB space the number of bytes we wrote.
22662259
// -1 becaues write_i points to the instruction AFTER the end
22672260
bb_space->water_level += (write_i - t2_start) * sizeof(_Py_CODEUNIT);
22682261
#if BB_DEBUG
22692262
fprintf(stderr, "Generated BB T2 Start: %p, T1 offset: %zu\n", meta->tier2_start,
22702263
meta->tier1_end - _PyCode_CODE(co));
22712264
#endif
2272-
return meta;
2265+
// Return the first BB
2266+
assert(metas_size >= 0);
2267+
return metas[0];
22732268

22742269
}
22752270

0 commit comments

Comments
 (0)