Table of Contents

NervLand: LLVM JIT - Back to basics

So in our previous post we were playing a bit with push constants, and came to the conclusion that using luajit over C++ to define dynamic behavior only introduced an about 5% performance penalty. Still, now I really feel like diving back into the LLVM JIT compiler implementation to check what has changed there since the last time I tried that 😁. And who knows, maybe I can easily reclaim that 5% hit in the end ? 😏

Refreshing our memory

Initial coding steps

To get this to link we need the libraries: LLVMSupport LLVMOrcJIT LLVMMC

Emulated TLS desactivation

We need to link to LLVMTarget LLVMCore to use the TargetMachine and DataLayout classes
To find which library is actually providing a given LLVM class we simply search the text content of the lib for for the expected implementation file, for instance searching for “DataLayout.cpp” will give us only “LLVMCore.lib” as result ;-)

Handling LLVM init/uninit

Taking a step back: analysis of llvm/tools/lli/lli.cpp

Trying to generate bytecode

I'm not quite sure we need the “frontEndOptions.ProgramAction” setup above ? Maybe we could simply bypass that.

Setting up compilation context

Note the SHA256 hashing mechanism used to check if a bytecode for a given source buffer should be recompiled or not: this is needed as the actual compilation itself is still taking a fair bit of time in fact.

Updating the LLJIT object

Initial module loading tests

  - name: LLVM
    version: 15.0.4
    extracted_dir: llvm-project-llvmorg-15.0.4
    windows_url: https://github.com/llvm/llvm-project/archive/refs/tags/llvmorg-15.0.4.zip
    linux_url: https://github.com/llvm/llvm-project/archive/refs/tags/llvmorg-15.0.4.tar.gz
    # version: 14.0.6
    # extracted_dir: llvm-project-llvmorg-14.0.6
    # windows_url: https://github.com/llvm/llvm-project/archive/refs/tags/llvmorg-14.0.6.zip
    # linux_url: https://github.com/llvm/llvm-project/archive/refs/tags/llvmorg-14.0.6.tar.gz

To keep compatibility between ragnarok/saturn builds I also had to upgrade to VS2022 version 17.4.1 on both sides

Relocation issue with LLVM 15.0.4

First execution of the JIT method

Update to our helloWorld test function

Testing creation of a custom MyCmdBuffersProvider class

Exporting required LLVM symbols

Testing injection inside lua state

Conclusion