Run-time code generation

I was thinking about this again. I believe it is possible in principle to use LLVM directly, but that would mean if a system is using a different code generator to LLVM, it might not work. I also suspect it would be hard, perhaps involving knowing a lot about the compiler internals?

It seems like maybe there should be a Rust-specific interface for generating code at run-time? Are there any such crates? Or quite possibly I am confused and not thinking this through correctly, as I have not even tried it yet.

I'm honestly not sure what you are getting at. Processors don't care what particular piece of software generates the executable. They chew through LLVM-generated opcodes just as well as those emitted by GCC or written by hand by HackermanTM.

LLVM has several Rust bindings at various levels of abstraction (eg. Inkwell), you can use them, but expect to read a lot of LLVM documentation.

I've used cranelift previously to do some basic runtime code generation.

But if you're talking about compiling Rust code on the fly... you'd need to ship the whole toolchain to your users.

What are you trying to do with the runtime codegen? Do you need to make things that can be used as rust fns? Or are you trying to do something more like plugins? Because using a WASM runtime to do the codegen for you, after pre-compiling whatever to WASM, might be an easier option that doing the LLVM/cranelift directly.


As long as you are willing to accept the 32-bit 4GB limit, I strongly second @scottmcm 's wasm suggestion.

I previously looked at dynasm, cranelift, wasm for runtime codegen. If I had to pick something right now, I'd try very hard to fit it in 32-big 4GB and go with wasm + wasmtime.

1 Like

I have an SQL-like language which is currently interpreted, which really works fine, but I was thinking about the possibility of compiling rather than interpreting. Inkwell looks promising.

Pre-compiling to WASM looks easy. Do you have an example Rust program that executes some WASM?

Edit: I think I found that here, am I right:

Edit2: It seems like wasmtime does what I want, am starting to experiment with it.

1 Like

Was there a reason you prefer wasm to cranelift? After some more thought, it appears that wasm is not able to directly access "Rust memory", which may be a issue for me, as it means accessing database data will involve a host function call, if I am not confused ( which is very possible ).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.