# Is it possible to use autodiff on an external program?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-use-autodiff-on-an-external-program/108475>\
**Category:** General Usage\
**Tags:** question, llvm, enzyme\
**Created:** [January 7, 2024, 6:27pm UTC](https://discourse.julialang.org/t/is-it-possible-to-use-autodiff-on-an-external-program/108475 "2024-01-07T18:27:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tetrakai](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)\
**Post date:** [January 7, 2024, 6:27pm UTC](https://discourse.julialang.org/t/is-it-possible-to-use-autodiff-on-an-external-program/108475/1 "2024-01-07T18:27:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)\
**Post date:** [January 8, 2024, 12:12am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-autodiff-on-an-external-program/108475/2 "2024-01-08T00:12:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tetrakai](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)\
**Post date:** [January 8, 2024, 2:50am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-autodiff-on-an-external-program/108475/3 "2024-01-08T02:50:24Z")

</div>

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