Skip to content

Commit

Permalink
rafactor platform api
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Leo-Smith committed Feb 12, 2025
1 parent 60c2522 commit f637a7e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 31 deletions.
39 changes: 18 additions & 21 deletions include/luisa/core/dynamic_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable {
DynamicModule &operator=(DynamicModule &&rhs) noexcept;
~DynamicModule() noexcept;
void dispose() noexcept;
/**
* @brief Return function pointer of given name
*
* @tparam F function type
* @param name name of function
* @return pointer to function
*/
template<concepts::function F>
[[nodiscard]] auto function(std::string_view name) const noexcept {
return reinterpret_cast<std::add_pointer_t<F>>(
dynamic_module_find_symbol(_handle, name));
}

/**
* @brief Return address of given name
*
* @param name
* @return void*
*/
[[nodiscard]] void *address(std::string_view name) const noexcept {
[[nodiscard]] void *address(const char *name) const noexcept {
return dynamic_module_find_symbol(_handle, name);
}

/**
* @brief Return function pointer of given name
*
* @tparam F function type
* @param name name of function
* @return pointer to function
*/
template<concepts::function F>
[[nodiscard]] auto function(const char *name) const noexcept {
return reinterpret_cast<std::add_pointer_t<F>>(address(name));
}

/**
* @brief Invoke function
*
Expand All @@ -61,7 +61,7 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable {
*/
template<concepts::function F, typename... Args>
requires(std::is_invocable_v<F, Args && ...>)
decltype(auto) invoke(std::string_view name, Args &&...args) const noexcept {
decltype(auto) invoke(const char *name, Args &&...args) const noexcept {
return luisa::invoke(function<F>(name), std::forward<Args>(args)...);
}

Expand All @@ -75,7 +75,7 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable {
* @return decltype(auto)
*/
template<concepts::function F, typename Tuple>
decltype(auto) apply(std::string_view name, Tuple &&t) const noexcept {
decltype(auto) apply(const char *name, Tuple &&t) const noexcept {
return std::apply(function<F>(name), std::forward<Tuple>(t));
}

Expand All @@ -98,19 +98,16 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable {
* @param name Name of the module
* @return The module if successfully loaded, otherwise a nullopt
*/
[[nodiscard]] static DynamicModule load(
std::string_view name) noexcept;
[[nodiscard]] static DynamicModule load(std::string_view name) noexcept;

/**
* @brief Load module with the specified name in a folder
* @param folder The folder when the module is expected to exist
* @param name Name of the module
* @return The module if successfully loaded, otherwise a nullopt
*/
[[nodiscard]] static DynamicModule load(
const luisa::filesystem::path &folder,
std::string_view name
) noexcept;
[[nodiscard]] static DynamicModule load(const luisa::filesystem::path &folder,
std::string_view name) noexcept;
};

}// namespace luisa
2 changes: 1 addition & 1 deletion include/luisa/core/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ LC_CORE_API void aligned_free(void *p) noexcept;
[[nodiscard]] LC_CORE_API luisa::string_view dynamic_module_extension() noexcept;
[[nodiscard]] LC_CORE_API void *dynamic_module_load(const luisa::filesystem::path &path) noexcept;
LC_CORE_API void dynamic_module_destroy(void *handle) noexcept;
[[nodiscard]] LC_CORE_API void *dynamic_module_find_symbol(void *handle, luisa::string_view name) noexcept;
[[nodiscard]] LC_CORE_API void *dynamic_module_find_symbol(void *handle, const char *name) noexcept;
[[nodiscard]] LC_CORE_API luisa::string dynamic_module_name(luisa::string_view name) noexcept;
// [[nodiscard]] LC_CORE_API luisa::string demangle(const char *name) noexcept;

Expand Down
12 changes: 4 additions & 8 deletions src/core/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,8 @@ void dynamic_module_destroy(void *handle) noexcept {
if (handle != nullptr) { FreeLibrary(reinterpret_cast<HMODULE>(handle)); }
}

void *dynamic_module_find_symbol(void *handle, luisa::string_view name_view) noexcept {
static thread_local luisa::string name;
name = name_view;
auto symbol = GetProcAddress(reinterpret_cast<HMODULE>(handle), name.c_str());
void *dynamic_module_find_symbol(void *handle, const char *name) noexcept {
auto symbol = GetProcAddress(reinterpret_cast<HMODULE>(handle), name);
if (symbol == nullptr) [[unlikely]] {
LUISA_WARNING("Failed to load symbol '{}', reason: {}.",
name, detail::win32_last_error_message());
Expand Down Expand Up @@ -249,11 +247,9 @@ void dynamic_module_destroy(void *handle) noexcept {
if (handle != nullptr) { dlclose(handle); }
}

void *dynamic_module_find_symbol(void *handle, luisa::string_view name_view) noexcept {
static thread_local luisa::string name;
name = name_view;
void *dynamic_module_find_symbol(void *handle, const char *name) noexcept {
Clock clock;
auto symbol = dlsym(handle, name.c_str());
auto symbol = dlsym(handle, name);
if (symbol == nullptr) [[unlikely]] {
LUISA_WARNING("Failed to load symbol '{}', reason: {}.",
name, dlerror());
Expand Down
2 changes: 1 addition & 1 deletion src/xir/passes/promote_ref_arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static void traverse_call_graph_post_order(Function *f, const CallGraph &call_gr
traverse_call_graph_post_order(call->callee(), call_graph, bitmap, visited, post_order);
}
if (def->isa<CallableFunction>() && bitmap.callable_bit_offsets.contains(static_cast<CallableFunction *>(def))) {
post_order.emplace_back(f);
post_order.emplace_back(static_cast<CallableFunction *>(def));
}
}
}
Expand Down

0 comments on commit f637a7e

Please sign in to comment.