Skip to content

Fix+Refactor: Handling of root nodes in special-cased type prop #40

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

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 26 additions & 36 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ partitionnode_make_root(uint8_t static_or_dynamic, PyObject *const_val)
}
root->static_or_dyanmic = static_or_dynamic;
root->const_val = Py_NewRef(const_val);
return (_Py_PARTITIONNODE_t)root;
return (_Py_PARTITIONNODE_t)root | TYPE_ROOT;
}

static inline _Py_PARTITIONNODE_t
Expand Down Expand Up @@ -179,27 +179,7 @@ partitionnode_get_rootptr(_Py_PARTITIONNODE_t *ref)
static bool
partitionnode_is_same_partition(_Py_PARTITIONNODE_t *x, _Py_PARTITIONNODE_t *y)
{
_Py_PARTITIONNODE_t *x_rootref = x;
_Py_PARTITIONNODE_t *y_rootref = y;
uintptr_t x_tag = partitionnode_get_tag(*x);
uintptr_t y_tag = partitionnode_get_tag(*y);
switch (y_tag) {
case TYPE_REF:
y_rootref = partitionnode_get_rootptr(y);
case TYPE_ROOT:
break;
default:
Py_UNREACHABLE();
}
switch (x_tag) {
case TYPE_REF:
x_rootref = partitionnode_get_rootptr(x);
case TYPE_ROOT:
break;
default:
Py_UNREACHABLE();
}
return x_rootref == y_rootref;
return partitionnode_get_rootptr(x) == partitionnode_get_rootptr(y);
}

/**
Expand Down Expand Up @@ -307,7 +287,6 @@ partitionnode_overwrite(_Py_UOpsAbstractInterpContext *ctx,
if (!src_is_new) {
// Make dst a reference to src
*dst = partitionnode_make_ref(src);
assert(partitionnode_get_tag(*dst) == TYPE_REF);
assert(partitionnode_clear_tag(*dst) != (_Py_PARTITIONNODE_t)_Py_NULL);
}
else {
Expand Down Expand Up @@ -444,19 +423,21 @@ print_ctx(_Py_UOpsAbstractInterpContext *ctx)

if (tag == TYPE_REF) {
_Py_PARTITIONNODE_t *parent = (_Py_PARTITIONNODE_t *)(partitionnode_clear_tag(*node));
is_local = parent >= ctx->locals && parent < ctx->stack;
is_stack = parent >= ctx->stack && parent < (ctx->stack + nstack);
int local_index = (int)(parent - ctx->locals);
int stack_index = (int)(parent - ctx->stack);
is_local = local_index >= 0 && local_index < ctx->locals_len;
is_stack = stack_index >= 0 && stack_index < nstack;
parent_idx = is_local
? (int)(parent - ctx->locals)
? local_index
: is_stack
? (int)(parent - ctx->locals)
? stack_index
: -1;
}


_Py_PartitionRootNode *ptr = (_Py_PartitionRootNode *)partitionnode_clear_tag(*root);
fprintf(stderr, "%s",
ptr == NULL ? "?" : (ptr->static_or_dyanmic ? "dynamic" : "static"));
ptr == NULL ? "?" : (ptr->static_or_dyanmic == 0 ? "static" : "dynamic"));

if (tag == TYPE_REF) {
const char *wher = is_local
Expand All @@ -479,18 +460,20 @@ print_ctx(_Py_UOpsAbstractInterpContext *ctx)

if (tag == TYPE_REF) {
_Py_PARTITIONNODE_t *parent = (_Py_PARTITIONNODE_t *)(partitionnode_clear_tag(*node));
is_local = parent >= ctx->locals && parent < ctx->stack;
is_stack = parent >= ctx->stack && parent < (ctx->stack + nstack);
int local_index = (int)(parent - ctx->locals);
int stack_index = (int)(parent - ctx->stack);
is_local = local_index >= 0 && local_index < ctx->locals_len;
is_stack = stack_index >= 0 && stack_index < nstack;
parent_idx = is_local
? (int)(parent - ctx->locals)
? local_index
: is_stack
? (int)(parent - ctx->locals)
? stack_index
: -1;
}

_Py_PartitionRootNode *ptr = (_Py_PartitionRootNode *)partitionnode_clear_tag(*root);
fprintf(stderr, "%s",
ptr == NULL ? "?" : (ptr->static_or_dyanmic ? "dynamic" : "static"));
ptr == NULL ? "?" : (ptr->static_or_dyanmic == 0 ? "static" : "dynamic"));

if (tag == TYPE_REF) {
const char *wher = is_local
Expand Down Expand Up @@ -570,6 +553,13 @@ _Py_uop_analyze_and_optimize(
for (int i = 0; i < trace_len; i++) {
oparg = trace[i].oparg;
opcode = trace[i].opcode;
#ifdef PARTITION_DEBUG
#ifdef Py_DEBUG
fprintf(stderr, " [-] Type propagating across: %s{%d} : %d\n",
(opcode >= 300 ? _PyOpcode_uop_name : _PyOpcode_OpName)[opcode],
opcode, oparg);
#endif
#endif
/*
* The following are special cased:
"LOAD_FAST",
Expand All @@ -591,13 +581,13 @@ _Py_uop_analyze_and_optimize(
case LOAD_FAST_AND_CLEAR: {
STACK_GROW(1);
PARTITIONNODE_OVERWRITE(GETLOCAL(oparg), PEEK(1), false);
PARTITIONNODE_OVERWRITE(&PARTITIONNODE_NULLROOT, GETLOCAL(oparg), false);
PARTITIONNODE_OVERWRITE((_Py_PARTITIONNODE_t *)PARTITIONNODE_NULLROOT, GETLOCAL(oparg), true);
break;
}
case LOAD_CONST: {
_Py_PARTITIONNODE_t value = MAKE_STATIC_ROOT(GETITEM(co->co_consts, oparg));
_Py_PARTITIONNODE_t* value = (_Py_PARTITIONNODE_t *)MAKE_STATIC_ROOT(GETITEM(co->co_consts, oparg));
STACK_GROW(1);
PARTITIONNODE_OVERWRITE((_Py_PARTITIONNODE_t *)value, PEEK(1), false);
PARTITIONNODE_OVERWRITE(value, PEEK(1), true);
#if PARTITION_DEBUG
print_ctx(ctx);
#endif
Expand Down