This is an archive of the discontinued LLVM Phabricator instance.

[ORC][LLJIT] Define atexit symbol in GenericLLVMIRPlatformSupport.
ClosedPublic

Authored by sunho on Jun 17 2022, 2:22 AM.

Details

Summary

Define atexit symbol in GenericLLVMIRPlatformSupport so that it doesn't need to be defined by user.

On windows, llvm codegen emits atexit runtime calls to support global deinitializers as there is no lower function like cxa_atexit as in Itanium C++ ABI. ORC JIT user had to define custom atexit symbol manually. This was a hassle as it has to deal with dso_handle and cxa_atexit internals of LLJIT. If client didn't provide atexit definition, the default behaviour is just linking with host atexit function which is destined to fail as it calls dtors when the host program exits. This is after jit instances and buffers are freed, so users would see weird access violation exception from the uknown location. (in console application, the debugger thinks exception happened in scrt_common_main_seh)

This is a hack that has some caveats. (e.g. memory address is not identical) But, it's better than the situation described in the above. Ultimately, we will move on to ORC runtime that is able to solve the memory address issue properly.

Diff Detail

Event Timeline

sunho created this revision.Jun 17 2022, 2:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 17 2022, 2:22 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
sunho requested review of this revision.Jun 17 2022, 2:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 17 2022, 2:22 AM
sunho updated this revision to Diff 437831.Jun 17 2022, 2:47 AM
sunho retitled this revision from [ORC][LLJIT] Add atexit support. to [ORC][LLJIT] Define atexit symbol in GenericLLVMIRPlatformSupport..Jun 17 2022, 2:50 AM
sunho edited the summary of this revision. (Show Details)
sunho edited the summary of this revision. (Show Details)Jun 17 2022, 2:54 AM
sunho edited the summary of this revision. (Show Details)Jun 17 2022, 3:41 AM
sunho added reviewers: lhames, sgraenitz.
sunho edited the summary of this revision. (Show Details)Jun 17 2022, 3:43 AM
sunho edited the summary of this revision. (Show Details)Jun 17 2022, 3:55 AM

Thanks, that sounds reasonable. Maybe we can provide some more context on atexit vs. __cxa_atexit in the commit message?

It appears that there is a test for __cxa_atexit here -- can we extend it to cover atexit?
https://github.com/llvm/llvm-project/blob/main/llvm/test/ExecutionEngine/OrcLazy/global-ctors-and-dtors.ll

sunho updated this revision to Diff 439706.Jun 24 2022, 5:17 AM

Add testcases

sunho edited the summary of this revision. (Show Details)Jun 24 2022, 5:27 AM

@sgraenitz I added a testcase and editted the description of the revision to explain the situation more thoroughly which I will use as a commit message. Does it look good to you?

sunho edited the summary of this revision. (Show Details)Jun 24 2022, 5:29 AM
sgraenitz accepted this revision.Jun 25 2022, 9:00 AM

Thanks, yes looks good to me!

This revision is now accepted and ready to land.Jun 25 2022, 9:00 AM
This revision was landed with ongoing or failed builds.Jun 25 2022, 11:50 AM
This revision was automatically updated to reflect the committed changes.