Constant folding of Int, Float, String, and Bool#1896
Conversation
|
Please provide times at make -j1 for tests/regression-new, pl-tutorial |
radumereuta
left a comment
There was a problem hiding this comment.
- Can you also add some integration tests?
One that passes, and has simple (ex: 1+2) and complex rules (1+2*5 + "abc").
One definition that goes intochecksand outputs some errors thrown by the new functions. - Is there an issue you should reference in this PR?
|
I addressed Radu's comments. I will get the performance numbers as soon as I can, but I wanted to take care of the requested code changes first. |
ttuegel
left a comment
There was a problem hiding this comment.
The implementation of the hooks appears the same as the Haskell backend, where the backend implements the hook.
radumereuta
left a comment
There was a problem hiding this comment.
Not really happy about the look of the double location error message, but I agree this can be addressed in another PR.
|
This is a placeholder post where I will add statistics relating to the performance details Everett has asked for as I compute them. Don't assume that what you see here is everything I am collecting until I explicitly let you know that I've finished computing everything. On master branch: On folding branch, same methodology: |
|
Looks good to me, only concerns are:
|
|
Someone had a good idea. If we add an option to enable/disable this step during kompile then we offer developers a way of avoiding possible bugs. |
|
I think the main argument at this time in favor of merging this PR is that it will improve the readability of macros in our code. Instead of writing something like: We can instead write: And it will be functionally identical, because constant folding will expand the latter to the former automatically. That being said, just because it doesn't substantially improve performance currently doesn't mean it's not still a good optimization to have. Consider for example EVM. Previously, we introduced macros like pow256 because they were quantities being used by rules in the semantics where |
This reverts commit 42dbd6a.
* initial constant folding of string, int, boolean * make rand and srand impure * remove MINT from constant folding * don't fold impure functions * constant folding of tokens * add helper function to FloatBuiltin * float hooks * fix whitespace * make field static * make hooks package-private * add unit tests for STRING, BOOL, INT * add setLoc function * fix STRING.length * fix STRING.ord * some fixes where nonnegative integers are expected * fix find, rfind, findChar, rfindChar * fix float2string * fix ediv and emod * fix bitRange * add hashCode and equals for FloatBuiltin * fix bugs in indexOfAny and lastIndexOfAny * fix whitespace * fix float2int * add some useful functions to FloatBuiltin * add tests for FLOAT hooks * add comment * add integration tests Co-authored-by: rv-jenkins <admin@runtimeverification.com>
* Constant folding of Int, Float, String, and Bool (#1896) * initial constant folding of string, int, boolean * make rand and srand impure * remove MINT from constant folding * don't fold impure functions * constant folding of tokens * add helper function to FloatBuiltin * float hooks * fix whitespace * make field static * make hooks package-private * add unit tests for STRING, BOOL, INT * add setLoc function * fix STRING.length * fix STRING.ord * some fixes where nonnegative integers are expected * fix find, rfind, findChar, rfindChar * fix float2string * fix ediv and emod * fix bitRange * add hashCode and equals for FloatBuiltin * fix bugs in indexOfAny and lastIndexOfAny * fix whitespace * fix float2int * add some useful functions to FloatBuiltin * add tests for FLOAT hooks * add comment * add integration tests Co-authored-by: rv-jenkins <admin@runtimeverification.com> * fix quadratic performance Co-authored-by: rv-jenkins <admin@runtimeverification.com>
This PR adds a compilation phase (enabled only on the llvm and haskell backend for simplicity's sake) which will evaluate at compile time hooked expressions over integers, floats, strings, bools, and identifiers to their ultimate value if they contain no variables or side effects.
This is done by means of a simple set of hook implementations provided as part of ConstantFolding.java which desugar each string token to its underlying domain-specific implementation, then reflectively invoke the right hook based on the hook attribute, then reconstruct the resulting token. It does this recursively, evaluating arguments, and then evaluating the function itself if the arguments have evaluated to a token.
Formally speaking, it will evaluate an expression if all of the following statements are true:
impureattribute.STRING,BOOL,INT, orFLOAT.If this is the case, the compiler will attempt to fold the expression and if it fails, either because no constant-folding hook implementation was provided, or because the function is not defined on those inputs, it will report an error.
This is not ready yet, but the final draft of the PR will contain Java unit tests for each of the hook implementations, as well as several integration tests testing the functionality end to end. Right now I'm just trying to get it to a point where it passes CI.