Skip to content

[mypyc] Add LoadLiteral and use tables to construct and store literals #10040

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 30 commits into from
Feb 6, 2021

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Feb 6, 2021

Redesign the implementation of constant PyObject * values, such as string and integer
literals.

There are several related changes:

  • Add LoadLiteral op for loading literal values instead of using LoadStatic.
  • No longer store all literals in a table while we construct IR to make it easier to remove
    literals no longer needed after optimizations in the future.
  • Simplify the pretty-printed IR for loading literals.
  • Rename the helpers used to construct literal references (e.g. load_static_unicode ->
    load_str).
  • Collect all used literals values after we have final IR.
  • Store literal value initialization information in C arrays instead of generating code for
    each value.
  • Store constructed literal objects in a single CPyStatics array.

The goal is to speed up compiles (slightly) and to make it easy to support tuple literals
and efficient table-driven import statements. Both of these can use the CPyStatics array.

Closes mypyc/mypyc#794. Closes mypyc/mypyc#797.

@JukkaL JukkaL merged commit 7ec1455 into master Feb 6, 2021
@JukkaL JukkaL deleted the lazy-literals branch February 6, 2021 13:24
@JukkaL
Copy link
Collaborator Author

JukkaL commented Feb 6, 2021

I'm not waiting for code review since I have a bunch of other changes that depend on this. If somebody would like to give feedback, I will respond to it in a separate PR.

JukkaL added a commit that referenced this pull request Feb 6, 2021
…0041)

Add support for loading tuple literals using `LoadLiteral`. The literal tuple objects will
be constructed during module import time, similar to other literals. Only tuples containing
items of supported literal types can be represented (this includes other tuples).

Add `None`, `True` and `False` to the literals array so that they can be used as tuple
literal items.

Currently tuple literals aren't used for anything. I added some unit tests to check parts
of the implementation.

The primary use case I have in mind is supporting vectorcall APIs which expect a tuple
of keyword argument names. I will implemented this in a separate PR. This will also add 
some end-to-end testing for tuple literals.

These could be used to avoid repeatedly constructing tuples with literal values in other
contexts as well.

Use array-based encoding for tuple literal values. We use the literal object array introduced 
in #10040 to allow a simple integer-based encoding of heterogeneous tuples. For example, 
tuple `('x', 5)` could be encoded like this as three integers:

* 2 (length of tuple)
* 123 (index of literal `'x'`)
* 345 (index of literal `5`)
JukkaL added a commit that referenced this pull request Feb 19, 2021
JukkaL added a commit that referenced this pull request Feb 19, 2021
JukkaL added a commit that referenced this pull request Feb 19, 2021
… literals (#10040)" (#10113)

This reverts commit 7ec1455.

Looks like this broke Windows wheel builds somehow.
JukkaL added a commit that referenced this pull request Feb 27, 2021
#10040)

Redesign the implementation of constant `PyObject *` values, such as string and integer 
literals.

There are several related changes:
* Add `LoadLiteral` op for loading literal values instead of using `LoadStatic`.
* No longer store all literals in a table while we construct IR to make it easier to remove
  literals no longer needed after optimizations in the future.
* Simplify the pretty-printed IR for loading literals.
* Rename the helpers used to construct literal references (e.g. `load_static_unicode` -> 
  `load_str`).
* Collect all used literals values after we have final IR.
* Store literal value initialization information in C arrays instead of generating code for 
  each value.
* Store constructed literal objects in a single `CPyStatics` array.

The goal is to speed up compiles (slightly) and to make it easy to support tuple literals
and efficient table-driven import statements. Both of these can use the `CPyStatics` array.

Closes mypyc/mypyc#794. Closes mypyc/mypyc#797.
JukkaL added a commit that referenced this pull request Feb 27, 2021
…s, try 2 (#10147)

This is my second attempt to land this. See the first attempt (#10040) for the details.

Previously this broke Windows wheel builds. The change from the first attempt is that
instead of generating long static C strings, we generate arrays of shorter strings.
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.

Efficient construction of strings and other literals Generate code for literals lazily
1 participant