[HN Gopher] How to JIT - An Introduction
       ___________________________________________________________________
        
       How to JIT - An Introduction
        
       Author : signa11
       Score  : 86 points
       Date   : 2022-12-02 13:34 UTC (9 hours ago)
        
 (HTM) web link (eli.thegreenplace.net)
 (TXT) w3m dump (eli.thegreenplace.net)
        
       | chrisseaton wrote:
       | How to install some machine code bytes. What machine code bytes
       | do you want to install though? That's the interesting bit!
        
       | pizlonator wrote:
       | Great to see these kinds of intro articles!
       | 
       | Worth noting that the 2-phase split isn't really there in JITs
       | that have been optimized to the limit because they tend to do
       | some amount of self-modifying code and to make that great, they
       | interleave the compiler IR with runtime data structures. Not
       | saying it has to be that way, only that it often is, for good
       | reasons.
        
       | samsquire wrote:
       | My dream is to create an efficient multithreaded runtime that
       | JITs. I came across this article for creating an AST in C and
       | simple machine code generation.
       | 
       | https://keleshev.com/abstract-syntax-tree-an-example-in-c/
       | 
       | I am working on codegen for my imaginary assembler and I'm trying
       | to work out how to set compound expressions such as
       | struct[key][key2]["balance"] += 100
       | 
       | In assembly.
        
         | JonChesterfield wrote:
         | split the compound expression into multiple simpler ones until
         | the pieces are small enough to translate directly to asm and
         | you're done
        
       | mkeeter wrote:
       | On Apple silicon, there's a fun new twist: the instruction (I)
       | and data (D) caches are separate, so you have to remember to call
       | sys_icache_flush [1] to keep them in sync!
       | 
       | If you don't handle this correctly, then you'll have a Very Fun
       | time [2].
       | 
       | What's especially Fun is that if you halt your program in a
       | debugger and read back the contents of the JIT buffer, it will
       | look perfectly correct - because those reads are coming through
       | RAM / DCache, not ICache!
       | 
       | [1]
       | https://developer.apple.com/library/archive/documentation/Sy...
       | [2] https://twitter.com/impraxical/status/1579586601117548544
        
         | moonchild wrote:
         | Arm, rather, and many other architectures besides. Even x86
         | does not guarantee complete I/D cache coherency (although its
         | guarantees are much stronger than arm's).
        
       | einpoklum wrote:
       | So, the article is actually about running the code you've JIT-
       | compiled and have in memory - not about the actual JIT-
       | compilation. But ok, that (easier) part is useful to go over too.
        
       | mcculley wrote:
       | It is a silly term. It is "on-demand compilation", with the
       | latency that entails. "Just-in-time compilation" would be
       | finished right when you need to execute the code.
        
       | sesuximo wrote:
       | One underrated way to jit is to write some code, spawn a
       | compiler, then link to a shared object. Takes 2 mins to write and
       | sidesteps a lot of complexity
        
       | UncleEntity wrote:
       | Libjit is fun to play around with, I wrote a python wrapper
       | around it, but the more you play the more you realize how much
       | you need to know to truly use it in anger.
       | 
       | Those were the days before wasm and I didn't really have any
       | concrete plans (some cockamamie blender related idea I'm sure) so
       | never did anything with it. Probably be fairly trivial to make a
       | wasm front end for it though.
        
       | KyleSanderson wrote:
       | A JIT what?
        
       | mshockwave wrote:
       | maybe put 2013 in the title?
        
       | celeritascelery wrote:
       | > Whenever a program, while running, creates and runs some new
       | executable code which was not part of the program when it was
       | stored on disk, it's a JIT.
       | 
       | By this definition, the Emacs native compiler would be a jit,
       | even though it is considered an AoT compiler.
       | 
       | I think there needs to be another caveat, where a jit uses some
       | run time information to generate the machine code.
       | 
       | It makes me wonder if there are any JIT compilers that generate
       | code based on runtime data, but not at runtime? Similar to
       | profile guided optimizations in static languages. Basically
       | collect your traces and runtime info, then generate the code
       | offline. This would let you take more time to optimize, and you
       | could save the new code to disk and reuse it.
        
         | IainIreland wrote:
         | By your definition, webassembly doesn't use a JIT, because it
         | doesn't use any runtime information.
         | 
         | In practice, the JIT/AOT distinction boils down to whether
         | compilation time counts as part of execution time, or not. If
         | cycles you spend optimizing your code are cycles you don't
         | spend running it, you make different design decisions.
         | 
         | (Note also that the Emacs native compiler is built using
         | libgccjit.)
        
       | uvdn7 wrote:
       | A bit off topic, Eli Bendersky's entire blog is absolutely
       | amazing. I started writing a technical blog inspired by his. Most
       | of his recent blogs seem to be about Go, which I don't know much
       | about. But I found myself going back to some of his older blog
       | posts many times.
        
       ___________________________________________________________________
       (page generated 2022-12-02 23:01 UTC)