gh-115480: Type and constant propagation for int BINARY_OPs (GH-115478)

This commit is contained in:
Ken Jin 2024-02-15 14:02:18 +08:00 committed by GitHub
parent ed23839dc5
commit 4ebf8fbdab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 126 additions and 16 deletions

View file

@ -81,12 +81,62 @@ dummy_func(void) {
op(_BINARY_OP_ADD_INT, (left, right -- res)) {
// TODO constant propagation
(void)left;
(void)right;
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
if (is_const(left) && is_const(right)) {
assert(PyLong_CheckExact(get_const(left)));
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Add((PyLongObject *)get_const(left),
(PyLongObject *)get_const(right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
// TODO replace opcode with constant propagated one and add tests!
}
else {
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
}
}
}
op(_BINARY_OP_SUBTRACT_INT, (left, right -- res)) {
if (is_const(left) && is_const(right)) {
assert(PyLong_CheckExact(get_const(left)));
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Subtract((PyLongObject *)get_const(left),
(PyLongObject *)get_const(right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
// TODO replace opcode with constant propagated one and add tests!
}
else {
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
}
}
}
op(_BINARY_OP_MULTIPLY_INT, (left, right -- res)) {
if (is_const(left) && is_const(right)) {
assert(PyLong_CheckExact(get_const(left)));
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Multiply((PyLongObject *)get_const(left),
(PyLongObject *)get_const(right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
// TODO replace opcode with constant propagated one and add tests!
}
else {
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
}
}
}