From 01e928ba369642cf7005e16e0016698ac4f54039 Mon Sep 17 00:00:00 2001 From: xiaomx32 <2116906041@qq.com> Date: Sat, 7 Dec 2024 18:32:35 +0800 Subject: [PATCH 1/3] prioritize using tools under the luisa namespace over those under std; use path (". so") to avoid implicit type conversion; reduce repetitive logic --- src/core/dynamic_module.cpp | 36 +++++---- src/core/platform.cpp | 4 +- src/runtime/context.cpp | 145 +++++++++++++++++++++++------------- 3 files changed, 120 insertions(+), 65 deletions(-) diff --git a/src/core/dynamic_module.cpp b/src/core/dynamic_module.cpp index af3bd5682..3a107d971 100644 --- a/src/core/dynamic_module.cpp +++ b/src/core/dynamic_module.cpp @@ -45,14 +45,20 @@ void DynamicModule::dispose() noexcept { } #endif -void DynamicModule::add_search_path(const std::filesystem::path &path) noexcept { - std::lock_guard lock{dynamic_module_search_path_mutex()}; - auto canonical_path = std::filesystem::canonical(path); +void DynamicModule::add_search_path(const luisa::filesystem::path &path) noexcept { + std::lock_guard lock { dynamic_module_search_path_mutex() }; + auto canonical_path = luisa::filesystem::canonical(path); auto &&paths = dynamic_module_search_paths(); - if (auto iter = std::find_if(paths.begin(), paths.end(), [&canonical_path](auto &&p) noexcept { - return p.first == canonical_path; - }); - iter != paths.end()) { + if ( + auto iter = std::find_if( + paths.begin(), + paths.end(), + [&canonical_path](auto &&p) noexcept { + return p.first == canonical_path; + } + ); + iter != paths.end() + ) { iter->second++; } else { #ifdef LUISA_PLATFORM_WINDOWS @@ -106,17 +112,21 @@ DynamicModule DynamicModule::load(std::string_view name) noexcept { return DynamicModule{nullptr}; } -DynamicModule DynamicModule::load(const std::filesystem::path &folder, std::string_view name) noexcept { +DynamicModule DynamicModule::load( + const luisa::filesystem::path &folder, + luisa::string_view name +) noexcept { Clock clock; auto p = folder / dynamic_module_name(name); if (auto handle = dynamic_module_load(p)) { LUISA_INFO( "Loaded dynamic module '{}' in {} ms.", - to_string(p), clock.toc()); - return DynamicModule{handle}; + to_string(p), clock.toc() + ); + return DynamicModule { handle }; } - return DynamicModule{nullptr}; -} -}// namespace luisa + return DynamicModule { nullptr }; +} +} // namespace luisa end diff --git a/src/core/platform.cpp b/src/core/platform.cpp index 3b3f4678b..fc8cff355 100644 --- a/src/core/platform.cpp +++ b/src/core/platform.cpp @@ -228,8 +228,10 @@ void *dynamic_module_load(const luisa::filesystem::path &path) noexcept { } LUISA_WARNING_WITH_LOCATION( "Failed to load dynamic module '{}', reason: {}.", - luisa::to_string(p), dlerror()); + luisa::to_string(p), dlerror() + ); } + return nullptr; } diff --git a/src/runtime/context.cpp b/src/runtime/context.cpp index 0858172b9..15bf93ba2 100644 --- a/src/runtime/context.cpp +++ b/src/runtime/context.cpp @@ -61,118 +61,161 @@ namespace detail { class ContextImpl { public: - std::filesystem::path runtime_directory; + luisa::filesystem::path runtime_directory; luisa::unordered_map> loaded_backends; luisa::vector installed_backends; ValidationLayer validation_layer; - luisa::unordered_map> runtime_subdir_paths; + luisa::unordered_map> runtime_subdir_paths; std::mutex runtime_subdir_mutex; std::mutex module_mutex; [[nodiscard]] const BackendModule &load_backend(const luisa::string &backend_name) noexcept { - if (std::find(installed_backends.cbegin(), - installed_backends.cend(), - backend_name) == installed_backends.cend()) { + if ( + std::find( + installed_backends.cbegin(), + installed_backends.cend(), + backend_name + ) == installed_backends.cend() + ) { LUISA_ERROR_WITH_LOCATION("Backend '{}' is not installed.", backend_name); } - std::scoped_lock lock{module_mutex}; - if (auto iter = loaded_backends.find(backend_name); - iter != loaded_backends.cend()) { return *iter->second; } - BackendModule m{.module = DynamicModule::load( - runtime_directory, - luisa::format("lc-backend-{}", backend_name))}; + + std::scoped_lock lock { module_mutex }; + if ( + auto iter = loaded_backends.find(backend_name); + iter != loaded_backends.cend() + ) { + return *iter->second; + } + BackendModule m { + .module = DynamicModule::load( + runtime_directory, + luisa::format("lc-backend-{}", backend_name) + ) + }; LUISA_ASSERT(m.module, "Failed to load backend '{}'.", backend_name); m.creator = m.module.function("create"); m.deleter = m.module.function("destroy"); m.backend_device_names = m.module.function("backend_device_names"); - auto pm = loaded_backends - .emplace(backend_name, luisa::make_unique(std::move(m))) - .first->second.get(); + auto pm = loaded_backends.emplace( + backend_name, + luisa::make_unique(std::move(m)) + ).first->second.get(); + return *pm; } [[nodiscard]] const ValidationLayer &load_validation_layer() noexcept { - std::scoped_lock lock{module_mutex}; + std::scoped_lock lock { module_mutex }; if (!validation_layer.module) { validation_layer.module = DynamicModule::load(runtime_directory, "lc-validation-layer"); validation_layer.creator = validation_layer.module.function("create"); validation_layer.deleter = validation_layer.module.function("destroy"); } + return validation_layer; } explicit ContextImpl(luisa::string_view program_path) noexcept { - std::filesystem::path program{program_path}; + luisa::filesystem::path program { program_path }; + LUISA_INFO( + "Created context for program '{}'.", + luisa::to_string(program.filename()) + ); + { - auto cp = std::filesystem::canonical(program); - if (std::filesystem::is_directory(cp)) { + auto cp = luisa::filesystem::canonical(program); + if (luisa::filesystem::is_directory(cp)) { runtime_directory = std::move(cp); } else { - runtime_directory = std::filesystem::canonical(cp.parent_path()); + runtime_directory = luisa::filesystem::canonical(cp.parent_path()); } + LUISA_INFO( + "Runtime directory: {}.", + luisa::to_string(runtime_directory) + ); + DynamicModule::add_search_path(runtime_directory); } - LUISA_INFO("Created context for program '{}'.", to_string(program.filename())); - LUISA_INFO("Runtime directory: {}.", to_string(runtime_directory)); - DynamicModule::add_search_path(runtime_directory); - for (auto &&p : std::filesystem::directory_iterator{runtime_directory}) { - if (auto &&path = p.path(); - p.is_regular_file() && - (path.extension() == ".so" || - path.extension() == ".dll" || - path.extension() == ".dylib")) { + + for (auto &&p : luisa::filesystem::directory_iterator{runtime_directory}) { + if ( + auto &&path = p.path(); + p.is_regular_file() && ( + path.extension() == luisa::filesystem::path(".so") || + path.extension() == luisa::filesystem::path(".dll") || + path.extension() == luisa::filesystem::path(".dylib") + ) + ) { using namespace std::string_view_literals; - constexpr std::array possible_prefixes{ + constexpr std::array possible_prefixes { "lc-backend-"sv, - // Make Mingw happy - "liblc-backend-"sv}; - auto filename = to_string(path.stem()); + "liblc-backend-"sv // Make Mingw happy + }; + auto filename = luisa::to_string(path.stem()); for (auto prefix : possible_prefixes) { if (filename.starts_with(prefix)) { - auto name = filename.substr(prefix.size()); - for (auto &c : name) { c = static_cast(std::tolower(c)); } - LUISA_VERBOSE_WITH_LOCATION("Found backend: {}.", name); - installed_backends.emplace_back(std::move(name)); + auto backend_name = filename.substr(prefix.size()); + for (auto &c : backend_name) { + c = static_cast(std::tolower(c)); + } + LUISA_VERBOSE_WITH_LOCATION("Found backend: {}.", backend_name); + installed_backends.emplace_back(std::move(backend_name)); break; } } } } - luisa::sort(installed_backends.begin(), installed_backends.end()); + luisa::sort( + installed_backends.begin(), + installed_backends.end() + ); installed_backends.erase( - std::unique(installed_backends.begin(), installed_backends.end()), - installed_backends.end()); + std::unique( + installed_backends.begin(), + installed_backends.end() + ), + installed_backends.end() + ); } + ~ContextImpl() noexcept { DynamicModule::remove_search_path(runtime_directory); } }; -}// namespace detail +} // namespace detail end + Context::Context(string_view program_path) noexcept - : _impl{luisa::make_shared(program_path)} {} + : _impl{luisa::make_shared(program_path)} +{} -Device Context::create_device(luisa::string_view backend_name_in, const DeviceConfig *settings, bool enable_validation) noexcept { - luisa::string backend_name{backend_name_in}; - for (auto &c : backend_name) { c = static_cast(std::tolower(c)); } +Device Context::create_device( + luisa::string_view backend_name_in, + const DeviceConfig *settings, + bool enable_validation +) noexcept { + luisa::string backend_name { backend_name_in }; auto &&m = _impl->load_backend(backend_name); auto interface = m.creator(Context{_impl}, settings); interface->_backend_name = std::move(backend_name); - auto handle = Device::Handle{ + auto handle = Device::Handle { interface, [impl = _impl, deleter = m.deleter](auto p) noexcept { deleter(p); - }}; + } + }; if (enable_validation) { auto &validation_layer = _impl->load_validation_layer(); - auto layer_handle = Device::Handle{ + auto layer_handle = Device::Handle { validation_layer.creator(Context{_impl}, std::move(handle)), [impl = _impl](auto layer) noexcept { impl->validation_layer.deleter(layer); - }}; - return Device{std::move(layer_handle)}; + } + }; + return Device { std::move(layer_handle) }; } else { - return Device{std::move(handle)}; + return Device { std::move(handle) }; } } @@ -216,7 +259,7 @@ const luisa::filesystem::path &Context::create_runtime_subdir(luisa::string_view "Failed to create runtime sub-directory '{}': {}.", to_string(dir), ec.message()); } - return luisa::make_unique(std::move(dir)); + return luisa::make_unique(std::move(dir)); })); return *iter.first->second; } From 31add72bb333602057030e7525e351b733c08323 Mon Sep 17 00:00:00 2001 From: xiaomx32 <2116906041@qq.com> Date: Sat, 7 Dec 2024 22:02:10 +0800 Subject: [PATCH 2/3] use the tools provided in the luisa name space --- include/luisa/core/dynamic_module.h | 26 ++++++++++++++------------ include/luisa/runtime/context.h | 2 +- src/core/dynamic_module.cpp | 4 ++-- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/include/luisa/core/dynamic_module.h b/include/luisa/core/dynamic_module.h index a8f5a1041..559810883 100644 --- a/include/luisa/core/dynamic_module.h +++ b/include/luisa/core/dynamic_module.h @@ -9,7 +9,7 @@ namespace luisa { /** * @brief Dynamic module loader - * + * */ class LC_CORE_API DynamicModule : concepts::Noncopyable { @@ -29,7 +29,7 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable { void dispose() noexcept; /** * @brief Return function pointer of given name - * + * * @tparam F function type * @param name name of function * @return pointer to function @@ -42,9 +42,9 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable { /** * @brief Return address of given name - * + * * @param name - * @return void* + * @return void* */ [[nodiscard]] void *address(std::string_view name) const noexcept { return dynamic_module_find_symbol(_handle, name); @@ -52,7 +52,7 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable { /** * @brief Invoke function - * + * * @tparam F function type * @tparam Args function args * @param name name of function @@ -67,12 +67,12 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable { /** * @brief Apply function to each element - * + * * @tparam F function type * @tparam Tuple tuple type * @param name name of function * @param t tuple to be applied - * @return decltype(auto) + * @return decltype(auto) */ template decltype(auto) apply(std::string_view name, Tuple &&t) const noexcept { @@ -81,15 +81,15 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable { /** * @brief Add dynamic module search path - * - * @param path + * + * @param path */ static void add_search_path(const std::filesystem::path &path) noexcept; /** * @brief Remove dynamic module search path - * - * @param path + * + * @param path */ static void remove_search_path(const std::filesystem::path &path) noexcept; @@ -108,7 +108,9 @@ class LC_CORE_API DynamicModule : concepts::Noncopyable { * @return The module if successfully loaded, otherwise a nullopt */ [[nodiscard]] static DynamicModule load( - const std::filesystem::path &folder, std::string_view name) noexcept; + const luisa::filesystem::path &folder, + std::string_view name + ) noexcept; }; }// namespace luisa diff --git a/include/luisa/runtime/context.h b/include/luisa/runtime/context.h index a4550feca..da2284863 100644 --- a/include/luisa/runtime/context.h +++ b/include/luisa/runtime/context.h @@ -26,7 +26,7 @@ class LC_RUNTIME_API Context { luisa::shared_ptr _impl; public: - explicit Context(luisa::shared_ptr impl) noexcept; + explicit Context(luisa::shared_ptr impl) noexcept; // program_path can be first arg from main entry explicit Context(luisa::string_view program_path) noexcept; explicit Context(const char *program_path) noexcept diff --git a/src/core/dynamic_module.cpp b/src/core/dynamic_module.cpp index 3a107d971..8d6a9347b 100644 --- a/src/core/dynamic_module.cpp +++ b/src/core/dynamic_module.cpp @@ -34,7 +34,7 @@ void DynamicModule::dispose() noexcept { } [[nodiscard]] static auto &dynamic_module_search_paths() noexcept { - static luisa::vector> paths; + static luisa::vector> paths; return paths; } @@ -65,7 +65,7 @@ void DynamicModule::add_search_path(const luisa::filesystem::path &path) noexcep auto &&cookies = dynamic_module_search_path_cookies(); cookies.emplace_back(AddDllDirectory(canonical_path.c_str())); #endif - paths.emplace_back(std::move(canonical_path), 0u); + paths.emplace_back(std::move(canonical_path), 0); } } From fd37ea03d176532fcb08b8e87fe79aa8d9d045c1 Mon Sep 17 00:00:00 2001 From: xiaomx32 <2116906041@qq.com> Date: Sun, 8 Dec 2024 12:26:52 +0800 Subject: [PATCH 3/3] add code format check --- .github/workflows/code-format-check.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .github/workflows/code-format-check.yml diff --git a/.github/workflows/code-format-check.yml b/.github/workflows/code-format-check.yml new file mode 100644 index 000000000..599f73297 --- /dev/null +++ b/.github/workflows/code-format-check.yml @@ -0,0 +1,21 @@ +name: C++ Code Check +on: [ push, pull_request ] +jobs: + formatting-check: + name: Check C++ Code Formatting + runs-on: ubuntu-24.04 + strategy: + matrix: + path: + - check: 'include' + - check: 'src' + exclude: 'ext' + steps: + - uses: actions/checkout@v4 + - name: Run clang-format style check for C/C++/Protobuf programs. + uses: jidicula/clang-format-action@v4.14.0 + with: + clang-format-version: '18' + check-path: ${{ matrix.path['check'] }} + exclude-regex: ${{ matrix.path['exclude'] }} + fallback-style: 'LLVM'