The Magic of Partial Evaluation
60 points by aray07 3 hours ago | 7 comments

apignotti 55 minutes ago
We implemented a similar concept in our C++ -> JavaScript/WebAssembly compiler (Cheerp) several years ago. It was a nice challenge.

https://labs.leaningtech.com/blog/reducing-webassembly-size-...

reply
owlstuffing 2 hours ago
This type of optimization is also known as compile-time constant expression folding.

The problem with expanding it to include function calls is that it compromises binary compatibility. A code change on a function that results in a different range or has different side effects, will not be realized unless the caller is recompiled.

reply
cogman10 7 minutes ago
Which is why the second most important optimization of compilers is inlining. If the callee is small enough then hoisting it up into the caller can expose additional optimizations, including noticing that parameters in the caller to the callee are in fact constant.
reply
infogulch 42 minutes ago
Doesn't Zig have a pretty nice implementation of compile-time evaluation built in?
reply
jimmyhmiller 4 minutes ago
Comptime in zig is definitely quite cool, but not quite the same.

Consider this example. https://jimmyhmiller.com/split-shot/#/vm

Make the code a comptime argument in zig and you get "store to comptime variable depends on runtime condition".

reply
measurablefunc 2 hours ago
Combining this w/ a JIT gets you truffle/graal.
reply
grashalm 2 hours ago
If you are curious how this works in Truffle. Here are some docs:

https://www.graalvm.org/latest/graalvm-as-a-platform/languag...

reply