7 ms·
This is the RFC with more technical details https://wiki.php.net/rfc/jit https://wiki.php.net/rfc/jit. I believe their plan is to emit low-level code directly
by chrisseaton 8y ago
This is the RFC with more technical details https://wiki.php.net/rfc/jit https://wiki.php.net/rfc/jit.
I believe their plan is to emit low-level code directly via DynAsm, without their own intermediate representation.
This kind of approach has been tried again and again and again and every project doing this either not got the results they wanted and given up or has had to go back and add a proper intermediate representation.
Examples include Rust adding MIR before emitting LLVM, Rubinius trying to emit LLVM directly from Ruby and not getting good results so giving up, JRuby adding an IR before emitting Java byte code, Dropbox's Pyston implementation of Python that emitted LLVM directly where they gave up entirely, etc, etc, etc.
Low level backends are not going to do the kind of key optimisations such as scalar replacement of aggregates that are table stakes for making a dynamic language perform well - they just don't have the high-level semantic model of the language needed to do it.
Maybe they've got some new ideas, or maybe simplicity is a key constraint for them, but I predict from experience that they will need an intermediate representation to get the results that they want.
- dman 8y agoChris - any recommendations on a good simple IR to study?
- chrisseaton 8y agoMIR is probably a good example as it solves the problem I'm suggesting they'll encounter https://blog.rust-lang.org/2016/04/19/MIR.html https://blog.rust-lang.org/2016/04/19/MIR.html. Ideal is also a classic https://www.oracle.com/technetwork/java/javase/tech/c2-ir95-150110.pdf https://www.oracle.com/technetwork/java/javase/tech/c2-ir95-....
- dman 8y agoThanks!
- beberlei 8y agoNot sure if this qualifies as intermediate representation in the LLVM sense, but PHP has opcodes already, it is not fully interpreted anymore (like it was in PHP 3). These opcodes are used for optimizations and cached for the lifetime of the PHP process (across multiple requests).
- pas 8y agoThat's probably too low-level. Usually an AST is needed that is void of all syntactic sugar, yet can express the required data flows and dependencies so the trivial substitutions/replacements/caching and refactorings can be done at this level. Lower level bytecode lacks these higher level references (it usually has a lot of indices that index into raw lookup tables, but no real symbols that refer to results of computations and other variables).
- tyingq 8y agoThere does appear to be an internal AST in php7: https://github.com/nikic/php-ast https://github.com/nikic/php-ast
- ryanmccullagh 8y agoPHP has had an AST for years now.
- pas 8y agoOf course, but the opcode is lower level than that. And that was what the parent comment mentioned.
- jashmatthews 8y agoLuaJIT and WebKit both produce their IR from the bytecode, IIUC.
- readittwice 8y agoNot sure why you think this is the case: there are numerous JITs/VMs that optimize starting from bytecode e.g. all JVMs, WebAssembly, V8, JSC,... Can you point me to some paper/reference about this? AFAIK it doesn't matter whether you have an AST or bytecode. Optimizations are done on an IR anyways.
- wvenable 8y ago> making a dynamic language perform well Perhaps the advantage here is that PHP really all that dynamic. PHP is much closer in design to taking Java and compiling it on every request than it is like Python or Ruby.
- skrebbel 8y agoWell, you can do $moo = "hello"; $zork = "moo"; echo $$zork; // hello It doesn't get much more dynamic than that.
- wvenable 8y agoIt can. Python, for example, builds up classes at runtime. PHP builds up classes at compile time. There are plenty of other examples. PHP is really dynamic through eval() but that's more rare. Although PHP does support the above syntax, it's actually pretty rare in production code. PHP can be optimized like JavaScript is: perform direct variable/member access but provide a slow path for these kinds of dynamic lookups.
- viraptor 8y agoYou can't just wave away the impact of having eval though. Just the fact that it exists means you need to keep around some supporting state like variable names. You can't do full constant folding and eliminate all intermediate steps for example if there a chance a runtime-created code will reach into your frame and want them back.
- rujuladanh 8y agoSimilar things are done by JS engines and others nowadays. They optimize and keep the original stuff in case assumptions stop holding.
- wvenable 8y agoeval() is scoped to the current block. If you even just ignored JITing any function with eval() in it then you'd still get quite far.
- steveklabnik 8y ago(Rust had an AST and HIR before MIR happened, incidentally.)
- farlee 8y agoI respect that the PHP maintainers recognize that it is not enough to accept a working JIT implementation - they have to understand and maintain it. Speed is one thing, but long term maintenance is more important. > use DynAsm (developed for LuaJIT project) for generation of native code But at the very least, this exercise will ensure that DynAsm will be actively used by a few people. So even if this PHP JIT effort fails, the underlying JIT technology will be more widely understood and hopefully better documented for other projects.
- readittwice 8y agoTrue, however I would argue that having an IR in general makes the code easier to understand and maintain. Especially if you add more optimizations.
- yazr 8y ago> key optimisations such as scalar replacement of aggregates Can u give an example or link to a relevant article. Context: my area is more of high-level program (e.g. shape) analysis
- ufo 8y agoScalar replacement of aggregates means replacing an object allocated on the heap (for example, Point p = new Point) by local variables corresponding to its fields (double x, double y) Removing the object allocation saves time and puts less pressure on the GC. Replacing the instance variables with local variables may also allow the compiler to optimize some things that it would not have attempted to optimize before. For example, storing them on machine registers.
- chrisseaton 8y agoRight, and in a language where you are predominantly using strings, arrays, maps, unless you can remove and optimise through these data structures you spend your whole time allocating them, collecting them, doing lookup in them and you won't have any opportunity to apply other optimisations. You need to be able to understand these data structures at the level of an IR - DynASM or LLVM aren't going to remove a call to a lookup routine for you.
- yazr 8y agoI understand that a JIT engine cant do this, due to speed. But would be surprised that a LLVM compiler with a custom-designed heuristic for specific optimizations cant manage this. These problems may be polynomial etc etc, but MOST of the code generated should follow common patterns I will have to give this some thought. Thanks for the reply (also GP)
- chrisseaton 8y ago> I understand that a JIT engine cant do this, due to speed. Most serious JIT engines do! For example V8, LuaJIT, Graal, C2. > But would be surprised that a LLVM compiler with a custom-designed heuristic for specific optimizations cant manage this. Rubinius for example tried writing custom optimisations like you're describing and didn't get very far. LLVM is not designed for these kind of optimisations - it understands memory reads and writes, not high-level semantic information about arrays and objects. In practice, it just doesn't seem to work. As I say I think it's a bare minimum for making a dynamic language genuinely fast. I gave a lecture about this https://www.youtube.com/watch?v=b1NTaVQPt1E https://www.youtube.com/watch?v=b1NTaVQPt1E.