After quite some time stewing in a PR, the first JIT just landed in nlvm: https://github.com/arnetheduck/nlvm/pull/45 .
The normal Nim pipeline goes something like this:
nim -> C -> IR -> object files -> linker -> executable -> run
nlvm shortens the pipeline to take out the C step:
nlvm -> IR -> object files -> linker -> executable -> run
The naive way to run code with a JIT is to simply:
nlvm -> IR -> JIT run
...and that's exactly what nlvm r yourfile.nim will do - it will compile the code to IR, then within the same process, run the code it just compiled. Of course, because we took out some steps, this will start execution faster - good for iterating on tests for example.
However, the above implementation, while simple, has a significant drawback: it must compile the full program before it starts running it - thus we come to the next part: obvious improvements that perhaps one fine day will make it to the codebase:
All of the above are fun PR:s to make if getting into the weeds of compilers is something you've wanted to do but didn't know where to start :) They're also ordered roughly in implementation dependency and difficulty order.
Of course, the first cut JIT implementation has a few gaps - linking is not done yet, meaning external dependencies might not work - the GC also needs polish though I expect ARC will be more simple to support - otherwise, the easiest thing to do might be to share GC between nlvm itself and the program that's running.
Sorry if this a stupid question. Can this be used as a sort of Nim script? And if so, I understand that it wouldn't have limitations in terms of using wrapped libraries, would it?
Or is Nim script significally faster when started?
Can this be used as a sort of Nim script? And if so, I understand that it wouldn't have limitations in terms of using wrapped libraries, would it?
yep - it can run anything that can be compiled
Or is Nim script significally faster when started?
I haven't timed it, but I expect nimscript to the faster for small scripts (due to startup time) while nlvm would be faster for longer-running things - that said, the optimization pipeline is not yet enabled for this first JIT commit, so ymmv and eventually nlvm startup times can likely match those of nimscript more or less
Does it only compiles C? Before compiling, does it also stores the C files inside the cache?
nim does this, yes - nlvm does not, see https://github.com/arnetheduck/nlvm#introduction for more info.