[compiler-rt] [llvm] [ORC][Runtime] Add dlupdate for coff (PR #115448)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 8 01:30:49 PST 2024
https://github.com/SahilPatidar created https://github.com/llvm/llvm-project/pull/115448
With the help of @lhames, This pull request introduces the dlupdate function in the ORC runtime. dlupdate enables incremental execution of new initializers introduced in the REPL environment. Unlike traditional dlopen, which manages initializers, code mapping, and library reference counts, dlupdate focuses exclusively on running new initializers.
>From 7c6486b1821aa5df256a2ab811e87bb7b27edb59 Mon Sep 17 00:00:00 2001
From: SahilPatidar <patidarsahil2001 at gmail.com>
Date: Sat, 6 Jul 2024 14:00:13 +0530
Subject: [PATCH 1/4] [ORC][Runtime] Add `dlupdate` for COFF
---
compiler-rt/lib/orc/coff_platform.cpp | 75 +++++++++++++++++++
compiler-rt/lib/orc/coff_platform.h | 1 +
llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp | 1 +
3 files changed, 77 insertions(+)
diff --git a/compiler-rt/lib/orc/coff_platform.cpp b/compiler-rt/lib/orc/coff_platform.cpp
index 49b805a0ec7d31..1d16c4d2b7b0a9 100644
--- a/compiler-rt/lib/orc/coff_platform.cpp
+++ b/compiler-rt/lib/orc/coff_platform.cpp
@@ -110,6 +110,7 @@ class COFFPlatformRuntimeState {
const char *dlerror();
void *dlopen(std::string_view Name, int Mode);
+ void *dlupdate(void *DSOHandle, int Mode);
int dlclose(void *Header);
void *dlsym(void *Header, std::string_view Symbol);
@@ -141,6 +142,10 @@ class COFFPlatformRuntimeState {
Error dlopenFull(JITDylibState &JDS);
Error dlopenInitialize(JITDylibState &JDS, COFFJITDylibDepInfoMap &DepInfo);
+ Expected<void *> dlupdateImpl(void *DSOHandle, int Mode);
+ Error dlupdateFull(JITDylibState &JDS);
+ Error dlupdateInitialize(JITDylibState &JDS);
+
Error dlcloseImpl(void *DSOHandle);
Error dlcloseDeinitialize(JITDylibState &JDS);
@@ -265,6 +270,21 @@ void *COFFPlatformRuntimeState::dlopen(std::string_view Path, int Mode) {
}
}
+void *COFFPlatformRuntimeState::dlupdate(void *DSOHandle, int Mode) {
+ ORC_RT_DEBUG({
+ std::string S;
+ printdbg("COFFPlatform::dlupdate(%p) (%s)\n", DSOHandle, S.c_str());
+ });
+ std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex);
+ if (auto H = dlupdateImpl(DSOHandle, Mode))
+ return *H;
+ else {
+ // FIXME: Make dlerror thread safe.
+ DLFcnError = toString(H.takeError());
+ return nullptr;
+ }
+}
+
int COFFPlatformRuntimeState::dlclose(void *DSOHandle) {
ORC_RT_DEBUG({
auto *JDS = getJITDylibStateByHeader(DSOHandle);
@@ -390,6 +410,57 @@ Error COFFPlatformRuntimeState::dlopenInitialize(
return Error::success();
}
+Expected<void *> COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle,
+ int Mode) {
+ // Try to find JITDylib state by name.
+ auto *JDS = getJITDylibStateByHeader(DSOHandle);
+
+ if (!JDS) {
+ std::ostringstream ErrStream;
+ ErrStream << "No registered JITDylib for " << DSOHandle;
+ return make_error<StringError>(ErrStream.str());
+ }
+
+ if (!JDS->referenced())
+ return make_error<StringError>("dlupdate failed, JITDylib must be open.");
+
+ if (auto Err = dlupdateFull(*JDS))
+ return std::move(Err);
+
+ // Return the header address.
+ return JDS->Header;
+}
+
+Error COFFPlatformRuntimeState::dlupdateFull(JITDylibState &JDS) {
+ // Call back to the JIT to push the initializers.
+ Expected<COFFJITDylibDepInfoMap> DepInfoMap((COFFJITDylibDepInfoMap()));
+ if (auto Err = WrapperFunction<SPSExpected<SPSCOFFJITDylibDepInfoMap>(
+ SPSExecutorAddr)>::call(&__orc_rt_coff_push_initializers_tag,
+ DepInfoMap,
+ ExecutorAddr::fromPtr(JDS.Header)))
+ return Err;
+ if (!DepInfoMap)
+ return DepInfoMap.takeError();
+
+ if (auto Err = dlupdateInitialize(JDS))
+ return Err;
+
+ return Error::success();
+}
+
+Error COFFPlatformRuntimeState::dlupdateInitialize(JITDylibState &JDS) {
+ ORC_RT_DEBUG({
+ printdbg("COFFPlatformRuntimeState::dlupdateInitialize(\"%s\")\n",
+ JDS.Name.c_str());
+ });
+
+ // Run static initializers.
+ JDS.CInitSection.RunAllNewAndFlush();
+ JDS.CXXInitSection.RunAllNewAndFlush();
+
+ return Error::success();
+}
+
Error COFFPlatformRuntimeState::dlcloseImpl(void *DSOHandle) {
// Try to find JITDylib state by header.
auto *JDS = getJITDylibStateByHeader(DSOHandle);
@@ -667,6 +738,10 @@ void *__orc_rt_coff_jit_dlopen(const char *path, int mode) {
return COFFPlatformRuntimeState::get().dlopen(path, mode);
}
+void *__orc_rt_coff_jit_dlupdate(void *dso_handle, int mode) {
+ return COFFPlatformRuntimeState::get().dlupdate(dso_handle, mode);
+}
+
int __orc_rt_coff_jit_dlclose(void *header) {
return COFFPlatformRuntimeState::get().dlclose(header);
}
diff --git a/compiler-rt/lib/orc/coff_platform.h b/compiler-rt/lib/orc/coff_platform.h
index aae57bc6100e53..a437e82fe4b478 100644
--- a/compiler-rt/lib/orc/coff_platform.h
+++ b/compiler-rt/lib/orc/coff_platform.h
@@ -19,6 +19,7 @@
// dlfcn functions.
ORC_RT_INTERFACE const char *__orc_rt_coff_jit_dlerror();
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlopen(const char *path, int mode);
+ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlupdate(void *dso_handle, int mode);
ORC_RT_INTERFACE int __orc_rt_coff_jit_dlclose(void *header);
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlsym(void *header,
const char *symbol);
diff --git a/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp b/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp
index f46cb906bb7556..0de2d0a28ed2c1 100644
--- a/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp
@@ -367,6 +367,7 @@ COFFPlatform::standardRuntimeUtilityAliases() {
{"__orc_rt_run_program", "__orc_rt_coff_run_program"},
{"__orc_rt_jit_dlerror", "__orc_rt_coff_jit_dlerror"},
{"__orc_rt_jit_dlopen", "__orc_rt_coff_jit_dlopen"},
+ {"__orc_rt_jit_dlupdate", "__orc_rt_coff_jit_dlupdate"},
{"__orc_rt_jit_dlclose", "__orc_rt_coff_jit_dlclose"},
{"__orc_rt_jit_dlsym", "__orc_rt_coff_jit_dlsym"},
{"__orc_rt_log_error", "__orc_rt_log_error_to_stderr"}};
>From 3d19cd8ba2937e2f37a48f76763e88d7993dd2f9 Mon Sep 17 00:00:00 2001
From: SahilPatidar <patidarsahil2001 at gmail.com>
Date: Sat, 27 Jul 2024 12:12:58 +0530
Subject: [PATCH 2/4] Modify dlupdate to return error code instead of
dso_handle
---
compiler-rt/lib/orc/coff_platform.cpp | 25 +++++++++++--------------
compiler-rt/lib/orc/coff_platform.h | 2 +-
2 files changed, 12 insertions(+), 15 deletions(-)
diff --git a/compiler-rt/lib/orc/coff_platform.cpp b/compiler-rt/lib/orc/coff_platform.cpp
index 1d16c4d2b7b0a9..285c1aaa49f293 100644
--- a/compiler-rt/lib/orc/coff_platform.cpp
+++ b/compiler-rt/lib/orc/coff_platform.cpp
@@ -110,7 +110,7 @@ class COFFPlatformRuntimeState {
const char *dlerror();
void *dlopen(std::string_view Name, int Mode);
- void *dlupdate(void *DSOHandle, int Mode);
+ int dlupdate(void *DSOHandle, int Mode);
int dlclose(void *Header);
void *dlsym(void *Header, std::string_view Symbol);
@@ -142,7 +142,7 @@ class COFFPlatformRuntimeState {
Error dlopenFull(JITDylibState &JDS);
Error dlopenInitialize(JITDylibState &JDS, COFFJITDylibDepInfoMap &DepInfo);
- Expected<void *> dlupdateImpl(void *DSOHandle, int Mode);
+ Error dlupdateImpl(void *DSOHandle, int Mode);
Error dlupdateFull(JITDylibState &JDS);
Error dlupdateInitialize(JITDylibState &JDS);
@@ -270,19 +270,18 @@ void *COFFPlatformRuntimeState::dlopen(std::string_view Path, int Mode) {
}
}
-void *COFFPlatformRuntimeState::dlupdate(void *DSOHandle, int Mode) {
+int COFFPlatformRuntimeState::dlupdate(void *DSOHandle, int Mode) {
ORC_RT_DEBUG({
std::string S;
printdbg("COFFPlatform::dlupdate(%p) (%s)\n", DSOHandle, S.c_str());
});
std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex);
- if (auto H = dlupdateImpl(DSOHandle, Mode))
- return *H;
- else {
+ if (auto Err = dlupdateImpl(DSOHandle, Mode)) {
// FIXME: Make dlerror thread safe.
- DLFcnError = toString(H.takeError());
- return nullptr;
+ DLFcnError = toString(std::move(Err));
+ return -1;
}
+ return 0;
}
int COFFPlatformRuntimeState::dlclose(void *DSOHandle) {
@@ -410,8 +409,7 @@ Error COFFPlatformRuntimeState::dlopenInitialize(
return Error::success();
}
-Expected<void *> COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle,
- int Mode) {
+Error COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle, int Mode) {
// Try to find JITDylib state by name.
auto *JDS = getJITDylibStateByHeader(DSOHandle);
@@ -425,10 +423,9 @@ Expected<void *> COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle,
return make_error<StringError>("dlupdate failed, JITDylib must be open.");
if (auto Err = dlupdateFull(*JDS))
- return std::move(Err);
+ return Err;
- // Return the header address.
- return JDS->Header;
+ return Error::success();
}
Error COFFPlatformRuntimeState::dlupdateFull(JITDylibState &JDS) {
@@ -738,7 +735,7 @@ void *__orc_rt_coff_jit_dlopen(const char *path, int mode) {
return COFFPlatformRuntimeState::get().dlopen(path, mode);
}
-void *__orc_rt_coff_jit_dlupdate(void *dso_handle, int mode) {
+int __orc_rt_coff_jit_dlupdate(void *dso_handle, int mode) {
return COFFPlatformRuntimeState::get().dlupdate(dso_handle, mode);
}
diff --git a/compiler-rt/lib/orc/coff_platform.h b/compiler-rt/lib/orc/coff_platform.h
index a437e82fe4b478..e9ee52cd1d61ab 100644
--- a/compiler-rt/lib/orc/coff_platform.h
+++ b/compiler-rt/lib/orc/coff_platform.h
@@ -19,7 +19,7 @@
// dlfcn functions.
ORC_RT_INTERFACE const char *__orc_rt_coff_jit_dlerror();
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlopen(const char *path, int mode);
-ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlupdate(void *dso_handle, int mode);
+ORC_RT_INTERFACE int __orc_rt_coff_jit_dlupdate(void *dso_handle, int mode);
ORC_RT_INTERFACE int __orc_rt_coff_jit_dlclose(void *header);
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlsym(void *header,
const char *symbol);
>From 99f14e424aaf29c7424fa9c7237620133e8ab26a Mon Sep 17 00:00:00 2001
From: SahilPatidar <patidarsahil2001 at gmail.com>
Date: Tue, 5 Nov 2024 11:29:16 +0530
Subject: [PATCH 3/4] Remove mode arg
---
compiler-rt/lib/orc/coff_platform.cpp | 14 +++++++-------
compiler-rt/lib/orc/coff_platform.h | 2 +-
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/compiler-rt/lib/orc/coff_platform.cpp b/compiler-rt/lib/orc/coff_platform.cpp
index 285c1aaa49f293..81ab5be58a2cc2 100644
--- a/compiler-rt/lib/orc/coff_platform.cpp
+++ b/compiler-rt/lib/orc/coff_platform.cpp
@@ -110,7 +110,7 @@ class COFFPlatformRuntimeState {
const char *dlerror();
void *dlopen(std::string_view Name, int Mode);
- int dlupdate(void *DSOHandle, int Mode);
+ int dlupdate(void *DSOHandle);
int dlclose(void *Header);
void *dlsym(void *Header, std::string_view Symbol);
@@ -142,7 +142,7 @@ class COFFPlatformRuntimeState {
Error dlopenFull(JITDylibState &JDS);
Error dlopenInitialize(JITDylibState &JDS, COFFJITDylibDepInfoMap &DepInfo);
- Error dlupdateImpl(void *DSOHandle, int Mode);
+ Error dlupdateImpl(void *DSOHandle);
Error dlupdateFull(JITDylibState &JDS);
Error dlupdateInitialize(JITDylibState &JDS);
@@ -270,13 +270,13 @@ void *COFFPlatformRuntimeState::dlopen(std::string_view Path, int Mode) {
}
}
-int COFFPlatformRuntimeState::dlupdate(void *DSOHandle, int Mode) {
+int COFFPlatformRuntimeState::dlupdate(void *DSOHandle) {
ORC_RT_DEBUG({
std::string S;
printdbg("COFFPlatform::dlupdate(%p) (%s)\n", DSOHandle, S.c_str());
});
std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex);
- if (auto Err = dlupdateImpl(DSOHandle, Mode)) {
+ if (auto Err = dlupdateImpl(DSOHandle)) {
// FIXME: Make dlerror thread safe.
DLFcnError = toString(std::move(Err));
return -1;
@@ -409,7 +409,7 @@ Error COFFPlatformRuntimeState::dlopenInitialize(
return Error::success();
}
-Error COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle, int Mode) {
+Error COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle) {
// Try to find JITDylib state by name.
auto *JDS = getJITDylibStateByHeader(DSOHandle);
@@ -735,8 +735,8 @@ void *__orc_rt_coff_jit_dlopen(const char *path, int mode) {
return COFFPlatformRuntimeState::get().dlopen(path, mode);
}
-int __orc_rt_coff_jit_dlupdate(void *dso_handle, int mode) {
- return COFFPlatformRuntimeState::get().dlupdate(dso_handle, mode);
+int __orc_rt_coff_jit_dlupdate(void *dso_handle) {
+ return COFFPlatformRuntimeState::get().dlupdate(dso_handle);
}
int __orc_rt_coff_jit_dlclose(void *header) {
diff --git a/compiler-rt/lib/orc/coff_platform.h b/compiler-rt/lib/orc/coff_platform.h
index e9ee52cd1d61ab..e9891f0b92a8a4 100644
--- a/compiler-rt/lib/orc/coff_platform.h
+++ b/compiler-rt/lib/orc/coff_platform.h
@@ -19,7 +19,7 @@
// dlfcn functions.
ORC_RT_INTERFACE const char *__orc_rt_coff_jit_dlerror();
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlopen(const char *path, int mode);
-ORC_RT_INTERFACE int __orc_rt_coff_jit_dlupdate(void *dso_handle, int mode);
+ORC_RT_INTERFACE int __orc_rt_coff_jit_dlupdate(void *dso_handle);
ORC_RT_INTERFACE int __orc_rt_coff_jit_dlclose(void *header);
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlsym(void *header,
const char *symbol);
>From 25b78830e178ade8c2c712a5d05b7263df09615d Mon Sep 17 00:00:00 2001
From: SahilPatidar <patidarsahil2001 at gmail.com>
Date: Fri, 8 Nov 2024 14:58:00 +0530
Subject: [PATCH 4/4] Update with changes from upstream
---
compiler-rt/lib/orc/coff_platform.cpp | 8 ++++----
compiler-rt/lib/orc/dlfcn_wrapper.cpp | 2 --
llvm/lib/ExecutionEngine/Orc/LLJIT.cpp | 13 +++++--------
3 files changed, 9 insertions(+), 14 deletions(-)
diff --git a/compiler-rt/lib/orc/coff_platform.cpp b/compiler-rt/lib/orc/coff_platform.cpp
index 81ab5be58a2cc2..d13ebe20088c10 100644
--- a/compiler-rt/lib/orc/coff_platform.cpp
+++ b/compiler-rt/lib/orc/coff_platform.cpp
@@ -410,7 +410,7 @@ Error COFFPlatformRuntimeState::dlopenInitialize(
}
Error COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle) {
- // Try to find JITDylib state by name.
+ // Try to find JITDylib state by header.
auto *JDS = getJITDylibStateByHeader(DSOHandle);
if (!JDS) {
@@ -432,9 +432,9 @@ Error COFFPlatformRuntimeState::dlupdateFull(JITDylibState &JDS) {
// Call back to the JIT to push the initializers.
Expected<COFFJITDylibDepInfoMap> DepInfoMap((COFFJITDylibDepInfoMap()));
if (auto Err = WrapperFunction<SPSExpected<SPSCOFFJITDylibDepInfoMap>(
- SPSExecutorAddr)>::call(&__orc_rt_coff_push_initializers_tag,
- DepInfoMap,
- ExecutorAddr::fromPtr(JDS.Header)))
+ SPSExecutorAddr)>::
+ call(JITDispatch(&__orc_rt_coff_push_initializers_tag), DepInfoMap,
+ ExecutorAddr::fromPtr(JDS.Header)))
return Err;
if (!DepInfoMap)
return DepInfoMap.takeError();
diff --git a/compiler-rt/lib/orc/dlfcn_wrapper.cpp b/compiler-rt/lib/orc/dlfcn_wrapper.cpp
index 3d7da18e3045b9..f0d6cc00ad4341 100644
--- a/compiler-rt/lib/orc/dlfcn_wrapper.cpp
+++ b/compiler-rt/lib/orc/dlfcn_wrapper.cpp
@@ -42,7 +42,6 @@ __orc_rt_jit_dlopen_wrapper(const char *ArgData, size_t ArgSize) {
.release();
}
-#ifndef _WIN32
ORC_RT_INTERFACE orc_rt_CWrapperFunctionResult
__orc_rt_jit_dlupdate_wrapper(const char *ArgData, size_t ArgSize) {
return WrapperFunction<int32_t(SPSExecutorAddr)>::handle(
@@ -52,7 +51,6 @@ __orc_rt_jit_dlupdate_wrapper(const char *ArgData, size_t ArgSize) {
})
.release();
}
-#endif
ORC_RT_INTERFACE orc_rt_CWrapperFunctionResult
__orc_rt_jit_dlclose_wrapper(const char *ArgData, size_t ArgSize) {
diff --git a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
index b31f4629565493..ed4c29a9c9ab91 100644
--- a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
@@ -625,14 +625,11 @@ Error ORCPlatformSupport::initialize(orc::JITDylib &JD) {
[](const JITDylibSearchOrder &SO) { return SO; });
StringRef WrapperToCall = "__orc_rt_jit_dlopen_wrapper";
bool dlupdate = false;
- const Triple &TT = ES.getTargetTriple();
- if (TT.isOSBinFormatMachO() || TT.isOSBinFormatELF()) {
- if (InitializedDylib.contains(&JD)) {
- WrapperToCall = "__orc_rt_jit_dlupdate_wrapper";
- dlupdate = true;
- } else
- InitializedDylib.insert(&JD);
- }
+ if (InitializedDylib.contains(&JD)) {
+ WrapperToCall = "__orc_rt_jit_dlupdate_wrapper";
+ dlupdate = true;
+ } else
+ InitializedDylib.insert(&JD);
if (auto WrapperAddr =
ES.lookup(MainSearchOrder, J.mangleAndIntern(WrapperToCall))) {
More information about the llvm-commits
mailing list