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

Emit table of return sorts #911

Merged
merged 4 commits into from
Dec 1, 2023
Merged

Emit table of return sorts #911

merged 4 commits into from
Dec 1, 2023

Conversation

Baltoli
Copy link
Contributor

@Baltoli Baltoli commented Dec 1, 2023

Part of: #905

In #905, we are implementing a Python binding for the backend's function evaluator: given a function label and list of argument Patterns, construct runtime terms for the arguments, evaluate the function with the given label, and return the result as an AST pattern.

To safely reify the runtime term produced by the function call to an AST pattern, we need to know its sort (so that the machinery in #907, #908 can be used correctly). In some places in the bindings, we have to require that callers provide a sort when reifying terms back to patterns. However, when calling a function, the label of the function determines precisely the correct sort to use.

This PR emits a new table of global data into compiled interpreters that maps tags to declared return sorts, along with a function that abstracts away indexing into this table. This change is similar to (but simpler than) an existing table of argument sorts for each symbol that we already emit.

Testing is handled by binding the new function to Python.

@Baltoli Baltoli marked this pull request as ready for review December 1, 2023 11:55
@Baltoli Baltoli changed the title Implement emitted return sort table Emit table of return sorts Dec 1, 2023
Copy link
Collaborator

@Robertorosmaninho Robertorosmaninho left a comment

Choose a reason for hiding this comment

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

LGTM! One question, though, as we're emitting more code, how does this modification affect the performance of the uses of our generated runtime lib?

@Baltoli
Copy link
Contributor Author

Baltoli commented Dec 1, 2023

LGTM! One question, though, as we're emitting more code, how does this modification affect the performance of the uses of our generated runtime lib?

I'd be very surprised if this caused any kind of meaningful performance change; the new code-generation step is one new O(symbols) step among a set of similar ones, and the data getting emitted is only a single pointer for each symbol (the sort names are already present in the binary).

@rv-jenkins rv-jenkins merged commit 44f29b2 into master Dec 1, 2023
5 checks passed
@rv-jenkins rv-jenkins deleted the return-sort-table branch December 1, 2023 12:48
rv-jenkins pushed a commit that referenced this pull request Dec 1, 2023
A common pattern in the backend's code is to do the following:
```c++
std::ostringstream out;
pattern->print(out);
do_something(out.str());
```

In #911, we introduced a helper function `ast_to_string` that wraps this
common pattern up; this PR follows up that change by mechanically
refactoring the remainder of the codebase to use the helper. After the
change, the above code would be:
```c++
do_something(ast_to_string(*pattern));
```

Each commit in the PR (bar the first) applies the same conceptual
refactoring to a single file; no actual behavioural changes other than
in the first commit are implemented, and so this should be an easy PR to
review.

In future work, I'd like to further clean up this code and make better
use of `fmt` across the codebase, but for now this is a useful
self-contained change to make.
rv-jenkins pushed a commit that referenced this pull request Dec 18, 2023
This PR adds a new feature to the backend's binding API that allows for
function symbols to be evaluated on a set of AST pattern arguments, with
the result returned as a pattern also. This is motivated by the proof
generation project; the execution trace format generated by the backend
to summarise a concrete execution can contain the labels and arguments
for function call events, but not the functions' return values without
breaking tail-call optimisation.

Most of the prerequisite work for this PR was done in #908 and #911; the
actual changes here are quite small:
* Adds a sort-aware term-to-pattern reification method that uses the new
`rawTerm{}` wrapper.
* Marshals arguments and returns between pattern and term level in the
core bindings API.
* Adds a test case demonstrating a few simple cases for this feature; as
ever the bulk of the testing ought to be performed in Pyk.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants