Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to force to build in a separate thread #696

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,11 @@ using the name of the corresponding environment variable.
* `CLVK_DEVICE_EXTENSIONS` specifies extensions to be added to the list of
exposed extensions. It expects a whitespace separated list of extensions.

* `CLVK_BUILD_IN_SEPARATE_THREAD` force to build kernels in a separate thread
(default: false). It brings a slight overhead when creating the thread, but
can be a work-around when having issues with clang compiling in the
application thread.

# Limitations

* Only one device per CL context
Expand Down
2 changes: 2 additions & 0 deletions src/config.def
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ OPTION(uint32_t, printf_buffer_size, 1024*1024u)

OPTION(uint32_t, opencl_version, (uint32_t)CL_MAKE_VERSION(3, 0, 0))

OPTION(bool, build_in_separate_thread, false)

#if COMPILER_AVAILABLE
OPTION(std::string, clspv_options, "")
#if !CLSPV_ONLINE_COMPILER
Expand Down
20 changes: 15 additions & 5 deletions src/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1515,8 +1515,6 @@ bool cvk_program::check_capabilities(const cvk_device* device) const {
}

void cvk_program::do_build() {
cvk_set_current_thread_name_if_supported("clvk-build");

// Destroy entry points from previous build
m_entry_points.clear();

Expand Down Expand Up @@ -1682,12 +1680,24 @@ cl_int cvk_program::build(build_operation operation, cl_uint num_devices,
m_operation_callback_data = data;

cl_int ret = CL_SUCCESS;
if (cb) {
bool build_in_separate_thread = config.build_in_separate_thread() || cb;
bool wait_for_completion = !cb;
if (build_in_separate_thread) {
// Kick off build
m_thread = std::make_unique<std::thread>(&cvk_program::do_build, this);
m_thread->detach();
m_thread = std::make_unique<std::thread>(
&cvk_program::do_build_in_separate_thread, this);
if (!wait_for_completion) {
m_thread->detach();
}
} else {
do_build();
}

if (wait_for_completion) {
if (build_in_separate_thread) {
CVK_ASSERT(m_thread->joinable());
m_thread->join();
}
if (build_status() != CL_BUILD_SUCCESS) {
switch (operation) {
case build_operation::link:
Expand Down
5 changes: 5 additions & 0 deletions src/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "memory.hpp"
#include "objects.hpp"
#include "printf.hpp"
#include "utils.hpp"

const int SPIR_WORD_SIZE = 4;

Expand Down Expand Up @@ -855,6 +856,10 @@ struct cvk_program : public _cl_program, api_object<object_magic::program> {

private:
void do_build();
void do_build_in_separate_thread() {
cvk_set_current_thread_name_if_supported("clvk-build");
do_build();
};
std::string prepare_build_options(const cvk_device* device) const;
CHECK_RETURN cl_build_status do_build_inner(const cvk_device* device);

Expand Down