<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/156052>156052</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [ORC][C-API] Crashes during exit when using LLJIT on Windows
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          njroussel
      </td>
    </tr>
</table>

<pre>
    Hi ðŸ‘‹ 

On recent LLVM versions (19+), I've observed unusual crashes during program exit when using the ORCv2 C-API on Windows.

# Details 
I have built a reproducer that does what I believe to be the bare minimum steps to build a LLJIT instance and dispose of it. Once my program exits, it crashes. More specifically, my programs exit with code: -1073741819 (0xC0000005 - Access Violation). I load the C-API dynamically in my programs and free it before exiting.

I can only reproduce this issue on Windows. In addition, it seems to only happen when using the pre-built binaries provided on the [releases page](https://github.com/llvm/llvm-project/releases). I have built the LLVM C-API locally in both debug and release modes and could not reproduce the issue.


# Reproducer
Platform information:
OS: Windows
Architecture: x86
LLVM versions: 19.1.7, 20.1.7

Minimal reproducer: 
[reproducer.cpp](https://github.com/user-attachments/files/22049555/reproducer.cpp)

This does not seem to be an isolated issue to my setup, we've seen [other users run into it](https://github.com/mitsuba-renderer/drjit/issues/356). In addition, we're able to observe this behavior using github's action runners (`windows-latest`).

# Notes
Not creating the LLJIT instance (i.e not calling `LLVMOrcCreateLLJIT`)  or not disposing of it (with `LLVMOrcDisposeLLJIT`) does not produce the crash, hence why I believe this issue to be linked to LLJIT specifically.

I am unfamiliar with how Windows dynamically loads and frees libraries - I'm assuming that the cleanup is not complete after the `FreeLibrary` call as the program only crashes when exiting.

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVk-P4rgT_TTmUgIFQwI5cKBpoR8_9UyPZkezZyeuEM86duQ_MHz7VdnQQF9G2xfoxK569erVK4T36mgQN6x8YeXrRMTQW7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