Skip to content
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

[mypyc] Inline math literals #15324

Merged
merged 3 commits into from Jun 2, 2023
Merged

Conversation

dosisod
Copy link
Contributor

@dosisod dosisod commented May 30, 2023

This PR inlines math literals such as math.pi and math.e. Previously Using math.pi would emit a getattr, unbox, error check, then box op code, but now it just boxes the value directly.

This PR is an improvement, but there are a few things that could be improved further:

  • math is still being imported even if you only imported literal values such as pi (which is now inlined and doesn't need to be imported).

  • We are essentially boxing a literal which should be a CPyLit_Float value instead. I looked into this, but AFAICT there is no "float object" primitve, meaning Mypyc will emit the same amount of code as before (minus the getattr) which is not ideal. Is there any way to indicate in the IR that a CPyLit_Float value is a boxed float as opposed to an unboxed float (double)?

Also, I added a test to ensure that the math literals are inlined properly and have the same value, but the test is failing saying that pi does not exist on module math. There are no errors when running the same file through mypy, so I don't know why it is failing. I checked, and the issue still seems to be present on master.

Closes mypyc/mypyc#985

This PR inlines math literals such as `math.pi` and `math.e`. Previously
Using `math.pi` would emit a getattr, unbox, error check, then box op code,
but now it just boxes the value directly.

This PR is an improvement, but there are a few things that could be improved
further:

* `math` is still being imported even if you only imported literal values such
  as `pi` (which is now inlined and doesn't need to be imported).

* We are essentially boxing a literal which should be a `CPyLit_Float` value
  instead. I looked into this, but AFAICT there is no "float object" primitve,
  meaning Mypyc will emit the same amount of code as before (minus the getattr)
  which is not ideal. Is there any way to indicate in the IR that a
  `CPyLit_Float` value is a boxed float as opposed to an unboxed float
  (`double`)?

Also, I added a test to ensure that the math literals are inlined properly and
have the same value, but the test is failing saying that `pi` does not exist
on module `math`. There are no errors when running the same file through mypy,
so I don't know why it is failing. I checked, and the issue still seems to be
present on `master`.

Closes mypyc/mypyc#985
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

A few comments (not a full review).

mypyc/irbuild/expression.py Show resolved Hide resolved
mypyc/test-data/run-math.test Show resolved Hide resolved
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks, looks good! I don't think there is any extra boxing compared to using a float literal directly. Avoiding boxing when we need a boxed literal is a separate issue, but still worth fixing. I'll create an issue about it if one doesn't exist yet.

@JukkaL JukkaL merged commit c4f55ae into python:master Jun 2, 2023
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add primitives for math constants
2 participants