Is it possible to use autodiff on an external program?

I have a c++ program I am running from julia that acts as the inner loop of a simulation (actually its a game engine). I’ve been playing with lots of derivative free optimizations, and tried the FiniteDiff package which kind of works but very slowly. Then I had the (possibly crazy) idea compile it to LLVM IR then pass that to Julia.

  1. Could I run that IR code from Julia?
  2. Could I use Enzyme to do autodiff on the results?

Yeah!

You could llvmcall the IR directly if you compiled it elsewhere, or alternatively you could compile your simulation with embedded bitcode and Enzyme would be able to differentiate through the ccall.

Wow, that seems immensely convenient. I will definitely figure this out then.