![Page 1: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/1.jpg)
Easy::JitJust-In-Time compilation for C++ codes
Serge GueltonJuan Manuel Martinez Caamaño (me) from Quarkslab
![Page 2: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/2.jpg)
Introduction
✔ Compiler-assisted library for runtime code generation
● Easy to understand C++ wrapper around the LLVM
✘ An omniscient virtual machine
✘ Read-Eval-Print Loop
✘ Building blocks for a Just-in-Time compiler
![Page 3: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/3.jpg)
Easy::Jit: by example
static void apply_filter(const char *mask, unsigned mask_size, unsigned mask_area,
cv::Mat &image, cv::Mat *&out) {
kernel(mask, mask_size, mask_area,
image.ptr(0,0), out->ptr(0,0),
image.rows, image.cols, image.channels());
}
![Page 4: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/4.jpg)
Easy::Jit: by example
static void apply_filter(const char *mask, unsigned mask_size, unsigned mask_area,
cv::Mat &image, cv::Mat *&out) {
kernel(mask, mask_size, mask_area,
image.ptr(0,0), out->ptr(0,0),
image.rows, image.cols, image.channels());
}
![Page 5: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/5.jpg)
Easy::Jit: by example
static void apply_filter(const char *mask, unsigned mask_size, unsigned mask_area,
cv::Mat &image, cv::Mat *&out) {
kernel(mask, mask_size, mask_area,
image.ptr(0,0), out->ptr(0,0),
image.rows, image.cols, image.channels());
}
![Page 6: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/6.jpg)
#include <easy/jit.h>
static void apply_filter(const char *mask, unsigned mask_size, unsigned mask_area,
cv::Mat &image, cv::Mat *&out) {
using namespace std::placeholder;
auto callme = easy::jit(kernel, mask, mask_size, mask_area,
_1, _2, image.rows, image.cols, image.channels());
callme(image.ptr(0,0), out->ptr(0,0));
}
Easy::Jit: by example
![Page 7: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/7.jpg)
#include <easy/code_cache.h>
static void apply_filter(const char *mask, unsigned mask_size, unsigned mask_area,
cv::Mat &image, cv::Mat *&out) {
using namespace std::placeholder;
static easy::Cache<> cache;
auto const& callme = cache.jit(kernel, mask, mask_size, mask_area,
_1, _2, image.rows, image.cols, image.channels());
callme(image.ptr(0,0), out->ptr(0,0));
}
Easy::Jit: by example
![Page 8: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/8.jpg)
How?
![Page 9: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/9.jpg)
Easy::Jit: Internals
1. Parse calls to easy::jit and embed bitcode
![Page 10: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/10.jpg)
Easy::Jit: Internals
2. Associate function pointers with bitcode
![Page 11: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/11.jpg)
Easy::Jit: Internals
3. Recover the bitcode using the function pointer, specialize, apply classical optimizations
![Page 12: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/12.jpg)
Easy::Jit: Internals
4. Generate code
![Page 13: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/13.jpg)
Easy::Jit: Internals
5. Wrap in an opaque object
![Page 14: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/14.jpg)
Easy::Jit: The numbers
![Page 15: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/15.jpg)
Final words
![Page 16: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/16.jpg)
Easy::Jit: Stuff not mentioned
● Serialization / Deserialization on standard streams
● Inlining of function
● Composition of generated code
● Devirtualization of virtual method calls
![Page 17: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/17.jpg)
Easy::Jit: Contribute!
● C API (work started)
● Cache: Threading + Persistency
● Member functions and function objects
● Partial Evaluationvoid eval(AST* ast, int variables[]);
...
auto program = easy::jit(eval, my_ast, _1);
program(var_values)
![Page 18: Easy::Jit - llvm.org · Introduction Compiler-assisted library for runtime code generation Easy to understand C++ wrapper around the LLVM An omniscient virtual machine](https://reader031.vdocument.in/reader031/viewer/2022040301/5e79cf8a3277a678513d0996/html5/thumbnails/18.jpg)
Contribute!github.com/jmmartinez/easy-just-in-time
Merci Quarkslab :)