From f637a7e06b8ea5f008fe2a9aab722774ab27c76f Mon Sep 17 00:00:00 2001 From: Mike Smith Date: Wed, 12 Feb 2025 23:21:53 +0800 Subject: [PATCH] rafactor platform api --- include/luisa/core/dynamic_module.h | 39 +++++++++++++---------------- include/luisa/core/platform.h | 2 +- src/core/platform.cpp | 12 +++------ src/xir/passes/promote_ref_arg.cpp | 2 +- 4 files changed, 24 insertions(+), 31 deletions(-) diff --git a/include/luisa/core/dynamic_module.h b/include/luisa/core/dynamic_module.h index 559810883..d579337a4 100644 --- a/include/luisa/core/dynamic_module.h +++ b/include/luisa/core/dynamic_module.h @@ -27,18 +27,6 @@ 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 - [[nodiscard]] auto function(std::string_view name) const noexcept { - return reinterpret_cast>( - dynamic_module_find_symbol(_handle, name)); - } /** * @brief Return address of given name @@ -46,10 +34,22 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable { * @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 + [[nodiscard]] auto function(const char *name) const noexcept { + return reinterpret_cast>(address(name)); + } + /** * @brief Invoke function * @@ -61,7 +61,7 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable { */ template requires(std::is_invocable_v) - 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(name), std::forward(args)...); } @@ -75,7 +75,7 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable { * @return decltype(auto) */ template - 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(name), std::forward(t)); } @@ -98,8 +98,7 @@ 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 @@ -107,10 +106,8 @@ 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( - 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 diff --git a/include/luisa/core/platform.h b/include/luisa/core/platform.h index 47ba885e6..2a8bdc80d 100644 --- a/include/luisa/core/platform.h +++ b/include/luisa/core/platform.h @@ -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; diff --git a/src/core/platform.cpp b/src/core/platform.cpp index 18d3d2d59..088e4fbbb 100644 --- a/src/core/platform.cpp +++ b/src/core/platform.cpp @@ -106,10 +106,8 @@ void dynamic_module_destroy(void *handle) noexcept { if (handle != nullptr) { FreeLibrary(reinterpret_cast(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(handle), name.c_str()); +void *dynamic_module_find_symbol(void *handle, const char *name) noexcept { + auto symbol = GetProcAddress(reinterpret_cast(handle), name); if (symbol == nullptr) [[unlikely]] { LUISA_WARNING("Failed to load symbol '{}', reason: {}.", name, detail::win32_last_error_message()); @@ -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()); diff --git a/src/xir/passes/promote_ref_arg.cpp b/src/xir/passes/promote_ref_arg.cpp index 68763ea99..5d963ea5e 100644 --- a/src/xir/passes/promote_ref_arg.cpp +++ b/src/xir/passes/promote_ref_arg.cpp @@ -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() && bitmap.callable_bit_offsets.contains(static_cast(def))) { - post_order.emplace_back(f); + post_order.emplace_back(static_cast(def)); } } }