8000 gh-119258: Eliminate Type Guards in Tier 2 Optimizer by saulshanabrook · Pull Request #119259 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-119258: Eliminate Type Guards in Tier 2 Optimizer #119259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Prev Previous commit
Next Next commit
wooohoooo
  • Loading branch information
dpdani committed May 20, 2024
commit 77bc293e6a959f8fca87a7d65acb47c8c079b4dd
7 changes: 4 additions & 3 deletions Include/internal/pycore_optimizer.h
8000
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct _Py_UopsSymbol {
PyTypeObject *typ; // Borrowed reference
PyObject *const_val; // Owned reference (!)
int32_t typ_version; // currently stores type version
int32_t typ_version_offset; // bytecode offset where the last type version was set
int typ_version_offset; // bytecode offset where the last type version was set
};

#define UOP_FORMAT_TARGET 0
Expand Down Expand Up @@ -98,6 +98,7 @@ struct _Py_UOpsContext {
char done;
char out_of_space;
bool contradiction;
int64_t latest_escape_offset;
// The current "executing" frame.
_Py_UOpsAbstractFrame *frame;
_Py_UOpsAbstractFrame frames[MAX_ABSTRACT_FRAME_DEPTH];
Expand Down Expand Up @@ -125,11 +126,11 @@ extern _Py_UopsSymbol *_Py_uop_sym_new_const(_Py_UOpsContext *ctx, PyObject *con
extern _Py_UopsSymbol *_Py_uop_sym_new_null(_Py_UOpsContext *ctx);
extern bool _Py_uop_sym_has_type(_Py_UopsSymbol *sym);
extern bool _Py_uop_sym_matches_type(_Py_UopsSymbol *sym, PyTypeObject *typ);
extern bool _Py_uop_sym_matches_type_version(_Py_UopsSymbol *sym, int32_t version);
extern bool _Py_uop_sym_matches_type_version(_Py_UopsSymbol *sym, int32_t version, int offset);
extern void _Py_uop_sym_set_null(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym);
extern void _Py_uop_sym_set_non_null(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym);
extern void _Py_uop_sym_set_type(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, PyTypeObject *typ);
extern void _Py_uop_sym_set_type_version(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, int32_t version);
extern void _Py_uop_sym_set_type_version(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, int32_t version, int offset);
extern void _Py_uop_sym_set_const(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, PyObject *const_val);
extern bool _Py_uop_sym_is_bottom(_Py_UopsSymbol *sym);
extern int _Py_uop_sym_truthiness(_Py_UopsSymbol *sym);
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,33 @@ class Foo:
guard_type_version_count = opnames.count("_GUARD_TYPE_VERSION")
self.assertEqual(guard_type_version_count, 1)

def test_guard_type_version_not_removed(self):
def fn():
pass

def thing(a):
x = 0
for _ in range(100):
x += a.attr
fn()
x += a.attr
return x

class Foo:
attr = 1

breakpoint()

res, ex = self._run_with_optimizer(thing, Foo())
opnames = list(iter_opnames(ex))
for i in iter_opnames(ex):
print(i)

self.assertIsNotNone(ex)
self.assertEqual(res, 200)
guard_type_version_count = opnames.count("_GUARD_TYPE_VERSION")
self.assertEqual(guard_type_version_count, 2)


if __name__ == "__main__":
unittest.main()
8 changes: 7 additions & 1 deletion Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
#define sym_set_null(SYM) _Py_uop_sym_set_null(ctx, SYM)
#define sym_set_non_null(SYM) _Py_uop_sym_set_non_null(ctx, SYM)
#define sym_set_type(SYM, TYPE) _Py_uop_sym_set_type(ctx, SYM, TYPE)
#define sym_set_type_version(SYM, VERSION) _Py_uop_sym_set_type_version(ctx, SYM, VERSION)
#define sym_set_type_version(SYM, VERSION, OFFSET) _Py_uop_sym_set_type_version(ctx, SYM, VERSION, OFFSET)
#define sym_set_const(SYM, CNST) _Py_uop_sym_set_const(ctx, SYM, CNST)
#define sym_is_bottom _Py_uop_sym_is_bottom
#define sym_truthiness _Py_uop_sym_truthiness
Expand Down Expand Up @@ -406,6 +406,7 @@ optimize_uops(
ctx->done = false;
ctx->out_of_space = false;
ctx->contradiction = false;
ctx->latest_escape_offset = 0;

_PyUOpInstruction *this_instr = NULL;
for (int i = 0; !ctx->done; i++) {
Expand All @@ -416,6 +417,11 @@ optimize_uops(
opcode = this_instr->opcode;
_Py_UopsSymbol **stack_pointer = ctx->frame->stack_pointer;

if (_PyUop_Flags[opcode] & HAS_ESCAPES_FLAG) {
printf("opcode: %d ", opcode);
ctx->latest_escape_offset = i; // i is the offset we're looping on
}

#ifdef Py_DEBUG
if (get_lltrace() >= 3) {
printf("%4d abs: ", (int)(this_instr - trace));
Expand Down
6 changes: 3 additions & 3 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
#define sym_set_null(SYM) _Py_uop_sym_set_null(ctx, SYM)
#define sym_set_non_null(SYM) _Py_uop_sym_set_non_null(ctx, SYM)
#define sym_set_type(SYM, TYPE) _Py_uop_sym_set_type(ctx, SYM, TYPE)
#define sym_set_type_version(SYM, VERSION) _Py_uop_sym_set_type_version(ctx, SYM, VERSION)
#define sym_set_type_version(SYM, VERSION, OFFSET) _Py_uop_sym_set_type_version(ctx, SYM, VERSION, OFFSET)
#define sym_set_const(SYM, CNST) _Py_uop_sym_set_const(ctx, SYM, CNST)
#define sym_is_bottom _Py_uop_sym_is_bottom
#define frame_new _Py_uop_frame_new
Expand Down Expand Up @@ -117,10 +117,10 @@ dummy_func(void) {

op(_GUARD_TYPE_VERSION, (type_version/2, owner -- owner)) {
printf("%d %d %d\n", type_version, owner->typ_version, owner->typ_version_offset);
if (sym_matches_type_version(owner, type_version)) {
if (sym_matches_type_version(owner, type_version, ctx->latest_escape_offset)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
sym_set_type_version(owner, type_version);
sym_set_type_version(owner, type_version, i);
}

op(_GUARD_BOTH_FLOAT, (left, right -- left, right)) {
Expand Down
9 changes: 9 additions & 0 deletions Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions Python/optimizer_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ _Py_uop_sym_set_type(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, PyTypeObject *ty
}

void
_Py_uop_sym_set_type_version(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, int32_t version)
_Py_uop_sym_set_type_version(_Py_UOpsContext *ctx, _Py_UopsSymbol *sym, int32_t version, int offset)
{
sym->typ_version = version;
sym->typ_version_offset = offset;
}

void
Expand Down Expand Up @@ -272,6 +273,12 @@ _Py_uop_sym_get_type_version(_Py_UopsSymbol *sym)
return sym->typ_version;
}

int
_Py_uop_sym_get_type_version_offset(_Py_UopsSymbol *sym)
{
return sym->typ_version_offset;
}

bool
_Py_uop_sym_has_type(_Py_UopsSymbol *sym)
{
Expand All @@ -289,9 +296,10 @@ _Py_uop_sym_matches_type(_Py_UopsSymbol *sym, PyTypeObject *typ)
}

bool
_Py_uop_sym_matches_type_version(_Py_UopsSymbol *sym, int32_t version)
_Py_uop_sym_matches_type_version(_Py_UopsSymbol *sym, int32_t version, int offset)
{
return _Py_uop_sym_get_type_version(sym) == version;
return _Py_uop_sym_get_type_version(sym) == version
&& _Py_uop_sym_get_type_version_offset(sym) > offset;
}


Expand Down
0