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

Fix issue in find_package in cross-compilation for no OS #7282

Merged
merged 3 commits into from
Feb 10, 2023

Conversation

stevesuzuki-arm
Copy link
Contributor

@stevesuzuki-arm stevesuzuki-arm commented Jan 17, 2023

When using toolchain where Threads libs are not available, which is the case in baremetal target cross-compilation, we were not able to load even HalideHelpers pacakge.

Copy link
Member

@alexreinking alexreinking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comments are a bit mixed, but I wanted to keep my whole thought process around.

What I'm not sure about is whether the lack of threads must be fixed upon package export (i.e. does it constitute a package variant?) or if it can truly be set by the loader of the package.

In the latter case, we should move targets that depend on threads to a separate export set, so they aren't even created in the user's build when Halide_NO_THREADS is set.

packaging/common/HalideHelpersConfig.cmake Outdated Show resolved Hide resolved
cmake/HalideGeneratorHelpers.cmake Outdated Show resolved Hide resolved
README_cmake.md Outdated Show resolved Hide resolved
@alexreinking
Copy link
Member

In #7297 we removed the dependency on Threads::Threads for Halide::Halide, Halide::Generator, and Halide::RunGenMain. I wonder if this change can be improved in light of that.

@stevesuzuki-arm
Copy link
Contributor Author

In #7297 we removed the dependency on Threads::Threads for Halide::Halide, Halide::Generator, and Halide::RunGenMain. I wonder if this change can be improved in light of that.

The latest commit no longer uses Halide_NO_THREADS.

Alternative approach where user explicitly declares is to add the option argument NO_THREADS to add_halide_library and add_halide_runtime to skip linking Threads.

@steven-johnson
Copy link
Contributor

Where does this PR stand?

@steven-johnson
Copy link
Contributor

Ping for status

Copy link
Member

@alexreinking alexreinking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple change for less-noisy UX. Please see my comment below

cmake/HalideGeneratorHelpers.cmake Outdated Show resolved Hide resolved
@alexreinking
Copy link
Member

alexreinking commented Feb 7, 2023

Overall, I'm a little bit concerned about how this PR determines whether or not a runtime will get a dependency on Threads::Threads.

The "real" problem as I understand it is that, currently, pipeline libraries that don't actually use threads in their schedules get a spurious dependency on Threads::Threads. Most of the time, this is benign, but on target machines that don't support threads in the first place, this becomes an error. Right?

Thus, it seems like the most correct solution would be to only add Threads::Threads if any schedule in the library uses .parallel(). However, this would be extremely complex to implement without at least requiring all Halide generators to be in a separate package from the libraries they produce, which makes the common cases as hard as the cross-compiling cases. It would also require work on the generator side to quickly check that property (but not run the whole compiler, which would be slow). Not awesome.

So we're electing to use a proxy instead. In this PR, that proxy is "whether or not the Threads package can be found". I think that's too loose, especially for larger projects.

Better would be to take a two step approach:

  1. Add an argument to add_halide_library that passes through to add_halide_runtime (both functions have the argument) that optionally disables threads. Say, NO_THREADS. When NOT NO_THREADS and NOT TARGET Threads::Threads, then we find_package(Threads REQUIRED) inside add_halide_runtime.
  2. The default for NO_THREADS is NO unless the variable Halide_RUNTIME_NO_THREADS is defined, in which case it provides the default value.

This is pretty similar to your prior implementation, with the major difference being the function argument qualification so that invocations of add_halide_library that the user knows will not use threads can have this property specified locally/declaratively in the call.

@steven-johnson
Copy link
Contributor

only add Threads::Threads if any schedule in the library uses .parallel().

(1) .... or async()
(2) Doing so would also require upgrading LLVM_Runtime_Linker; it currently will unconditionally add threading-runtime-support on all platforms that can possibly support it, regardless of whether the thing being linked actually uses any of those features.

@alexreinking
Copy link
Member

alexreinking commented Feb 7, 2023

(1) .... or async()

D'oh. Yes.

(2) Doing so would also require upgrading LLVM_Runtime_Linker; it currently will unconditionally add threading-runtime-support on all platforms that can possibly support it, regardless of whether the thing being linked actually uses any of those features.

Maybe this means that whether or not threads are required can be accurately computed from the target string?

When using toolchain where Threads libs are not available,
which is the case in baremetal target cross-compilation,
we were not able to load even HalideHelpers pacakge.
@stevesuzuki-arm
Copy link
Contributor Author

  1. Add an argument to add_halide_library that passes through to add_halide_runtime (both functions have the argument) that optionally disables threads. Say, NO_THREADS. When NOT NO_THREADS and NOT TARGET Threads::Threads, then we find_package(Threads REQUIRED) inside add_halide_runtime.
  2. The default for NO_THREADS is NO unless the variable Halide_RUNTIME_NO_THREADS is defined, in which case it provides the default value.

I have updated the patch accordingly. I appreciate your efforts in your busy time!

@steven-johnson
Copy link
Contributor

Looks like some breakage from top-of-tree LLVM changes, fixes underway

README_cmake.md Show resolved Hide resolved
@steven-johnson
Copy link
Contributor

So is this ready to land?

@alexreinking alexreinking merged commit 88d40c2 into halide:main Feb 10, 2023
@alexreinking
Copy link
Member

So is this ready to land?

Yep, merged.

@stevesuzuki-arm stevesuzuki-arm deleted the fix_find_package branch February 13, 2023 10:39
ardier pushed a commit to ardier/Halide-mutation that referenced this pull request Mar 3, 2024
When using toolchain where Threads libs are not available,
which is the case in baremetal target cross-compilation,
we were not able to load even HalideHelpers pacakge.

Co-authored-by: Alex Reinking <alex.reinking@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants