[ORC] Add a runAsMain utility function to ExecutionUtils.
The runAsMain function takes a pointer to a function with a standard C main
signature, int(*)(int, char*[]), and invokes it using the given arguments and
program name. The arguments are copied into writable temporary storage as
required by the C and C++ specifications, so runAsMain safe to use when calling
main functions that modify their arguments in-place.
This patch also uses the new runAsMain function to replace hand-rolled versions
in lli, llvm-jitlink, and the SpeculativeJIT example.
diff --git a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
index 003a333..0e0953d 100644
--- a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
+++ b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
@@ -769,25 +769,6 @@
return S.ES.lookup(S.JDSearchOrder, EntryPointName);
}
-Expected<int> runEntryPoint(Session &S, JITEvaluatedSymbol EntryPoint) {
- assert(EntryPoint.getAddress() && "Entry point address should not be null");
-
- constexpr const char *JITProgramName = "<llvm-jitlink jit'd code>";
- auto PNStorage = std::make_unique<char[]>(strlen(JITProgramName) + 1);
- strcpy(PNStorage.get(), JITProgramName);
-
- std::vector<const char *> EntryPointArgs;
- EntryPointArgs.push_back(PNStorage.get());
- for (auto &InputArg : InputArgv)
- EntryPointArgs.push_back(InputArg.data());
- EntryPointArgs.push_back(nullptr);
-
- using MainTy = int (*)(int, const char *[]);
- MainTy EntryPointPtr = reinterpret_cast<MainTy>(EntryPoint.getAddress());
-
- return EntryPointPtr(EntryPointArgs.size() - 1, EntryPointArgs.data());
-}
-
struct JITLinkTimers {
TimerGroup JITLinkTG{"llvm-jitlink timers", "timers for llvm-jitlink phases"};
Timer LoadObjectsTimer{"load", "time to load/add object files", JITLinkTG};
@@ -841,8 +822,10 @@
int Result = 0;
{
+ using MainTy = int (*)(int, char *[]);
+ auto EntryFn = jitTargetAddressToFunction<MainTy>(EntryPoint.getAddress());
TimeRegion TR(Timers ? &Timers->RunTimer : nullptr);
- Result = ExitOnErr(runEntryPoint(S, EntryPoint));
+ Result = runAsMain(EntryFn, InputArgv, StringRef(InputFiles.front()));
}
return Result;