This is an archive of the discontinued LLVM Phabricator instance.

[ORC][COFF] Introduce COFFVCRuntimeBootstrapper.
ClosedPublic

Authored by sunho on Jul 24 2022, 8:12 PM.

Details

Summary

Introduces COFFVCRuntimeBootstrapper that loads/initialize vc runtime libraries. In COFF, we *must* jit-link vc runtime libraries as COFF relocation types have no proper way to deal with out-of-reach data symbols ragardless of linking mode. (even dynamic version msvcrt.lib have tons of static data symbols that must be jit-linked) This class tries to load vc runtime library files from msvc installations with an option to override the path.

There are some complications when dealing with static version of vc runtimes. First, they need static initializers to be ran that requires COFFPlatform support but orc runtime will not be usable before vc runtimes are fully initialized. (as orc runtime will use msvc stl libraries) COFFPlatform that will be introduced in a following up patch will collect static initializers and run them manually in host before boostrapping itself. So, the user will have to do the following.

  1. Create COFFPlatform that addes static initializer collecting passes.
  2. LoadVCRuntime
  3. InitializeVCRuntime
  4. COFFPlatform.bootstrap()

Second, the internal crt initialization function had to be reimplemented in orc side. There are other ways of doing this, but this is the simplest implementation that makes platform fully responsible for static initializer. The complication comes from the fact that crt initialization functions (such as acrt_initialize or dllmain_crt_process_attach) actually run all static initializers by traversing from __xi_a symbol to __xi_z. This requires symbols to be contiguously allocated in sections alphabetically sorted in memory, which is not possible right now and not practical in jit setting. We might ignore emission of __xi_a and __xi_z symbol and allocate them ourselves, but we have to take extra care after orc runtime boostrap has been done -- as that point orc runtime should be the one running the static initializers.

Diff Detail

Event Timeline

sunho created this revision.Jul 24 2022, 8:12 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 24 2022, 8:12 PM
sunho requested review of this revision.Jul 24 2022, 8:12 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 24 2022, 8:12 PM
sunho edited the summary of this revision. (Show Details)
sunho edited the summary of this revision. (Show Details)
sunho updated this revision to Diff 449213.Aug 2 2022, 1:17 AM

Add license header.

sunho updated this revision to Diff 449503.Aug 2 2022, 5:58 PM

Change API

lhames accepted this revision.Aug 7 2022, 2:13 PM

Otherwise LGTM!

llvm/include/llvm/ExecutionEngine/Orc/COFFVCRuntimeSupport.h
53

ImportedLibraries looks like an out-parameter. Could this method return an Expected<std::vector<std::string>> instead?

(Similarly for loadDynamicVCRuntime below)

llvm/include/llvm/ExecutionEngine/Orc/ExecutorProcessControl.h
262–267

We should move these into the ORC runtime, but we can do that in a follow-up patch.

Could you amend the comment to mention that these methods are temporary, and not to be used?

This revision is now accepted and ready to land.Aug 7 2022, 2:13 PM
sunho updated this revision to Diff 451734.Aug 10 2022, 11:10 PM
sunho updated this revision to Diff 451737.Aug 10 2022, 11:20 PM
This revision was landed with ongoing or failed builds.Aug 10 2022, 11:28 PM
This revision was automatically updated to reflect the committed changes.