Skip to content

GH-118095: Handle RETURN_GENERATOR in tier 2 - #118180

Merged
markshannon merged 10 commits into
python:mainfrom
faster-cpython:return-gen-tier-2
Apr 25, 2024
Merged

GH-118095: Handle RETURN_GENERATOR in tier 2#118180
markshannon merged 10 commits into
python:mainfrom
faster-cpython:return-gen-tier-2

Conversation

@markshannon

@markshannonmarkshannon commented Apr 23, 2024

Copy link
Copy Markdown
Member

@markshannon
markshannon marked this pull request as ready for review April 24, 2024 11:43

@gvanrossumgvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add a test? E.g.

deftest_return_generator(self):
defgen():
yieldNonedeftestfunc(n):
foriinrange(n):
gen()
res, ex=self._run_with_optimizer(testfunc, 20)
self.assertIsNotNone(ex)
self.assertIn("_RETURN_GENERATOR", get_opnames(ex))

get_code(_PyUOpInstruction *op)
{
assert(op->opcode == _PUSH_FRAME || op->opcode == _POP_FRAME);
assert(op->opcode == _PUSH_FRAME || op->opcode == _POP_FRAME || op->opcode == _RETURN_GENERATOR);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit inelegant that all places that treat _PUSH_FRAME and _POP_FRAME special now also have to check for _RETURN_GENERATOR. Not sure what to do about it. :-(

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once #118095 is done we can add a flag for all uops that push or pop frames.

ctx->frame->stack_pointer = stack_pointer;
frame_pop(ctx);
stack_pointer = ctx->frame->stack_pointer;
OUT_OF_SPACE_IF_NULL(res = sym_new_unknown(ctx));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I correct that this is the only line that differs from _POP_FRAME? The duplication of so much code is unfortunate. At the same time I don't see a better solution. Maybe add a comment to both explaining they need to be kept in sync?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good candidate for refactoring, but I'd like to get #118095 done first so that we more easily see the common patterns.

def testfunc(n):
for i in range(n):
gen()
res, ex = self._run_with_optimizer(testfunc, 20)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd assert the value of res as well.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@markshannon

Copy link
Copy Markdown
MemberAuthor

The four failing tests are the usual suspects.

@markshannon
markshannon merged commit f180b31 into python:mainApr 25, 2024
@markshannon
markshannon deleted the return-gen-tier-2 branch April 25, 2024 10:34
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@markshannon@iritkatriel@gvanrossum