Skip to content

Conversation

@wenju-he
Copy link
Contributor

@wenju-he wenju-he commented Mar 11, 2025

In libclc, we observe that compiling OpenCL source files to bitcode is
executed sequentially on Windows, which increases debug build time by
about an hour.
add_custom_command may introduce additional implicit dependencies, see
https://gitlab.kitware.com/cmake/cmake/-/issues/17097
This PR adds a target for each command, enabling parallel builds of
OpenCL source files.
CMake 3.27 has fixed above issue with DEPENDS_EXPLICIT_ONLY. When LLVM
upgrades cmake vertion to 3.7, we can switch to DEPENDS_EXPLICIT_ONLY.

In libclc, we observe that compiling OpenCL source files to bitcode is
executed sequentially on Windows, which increases debug build time by
about an hour.
add_custom_command may introduce additional implicit dependencies, see
https://gitlab.kitware.com/cmake/cmake/-/issues/17097
This PR adds a target for each command and enables parallel builds of
OpenCL source files.
@wenju-he
Copy link
Contributor Author

@frasercrmck could you please review? thanks

@jsji jsji requested a review from frasercrmck March 17, 2025 00:17
@frasercrmck
Copy link
Contributor

Am I right in thinking that CMake 3.27's DEPENDS_EXPLICIT_ONLY would also fix this? If so it might be worth documenting this explicitly, both in the PR/commit and in the code? We might be able to refactor this in the future, when LLVM updates its minimum version to 3.27.

@wenju-he
Copy link
Contributor Author

wenju-he commented Mar 18, 2025

Am I right in thinking that CMake 3.27's DEPENDS_EXPLICIT_ONLY would also fix this? If so it might be worth documenting this explicitly, both in the PR/commit and in the code? We might be able to refactor this in the future, when LLVM updates its minimum version to 3.27.

done.
I noticed DEPENDS_EXPLICIT_ONLY in the cmake issue link above, but I didn't test it.
I have just tested DEPENDS_EXPLICIT_ONLY on windows, it can indeed fix the sequential build issue.

Copy link
Contributor

@frasercrmck frasercrmck left a comment

Choose a reason for hiding this comment

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

Sorry, I didn't realise I hadn't published this review from last week!

${ARG_DEPENDENCIES}
DEPFILE ${ARG_OUTPUT}.d
)
add_custom_target( ${ARG_TARGET} DEPENDS ${ARG_OUTPUT}${TMP_SUFFIX} )
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the (possibly) "temp" output (in the case of .ll files) should be given the honour of the ARG_TARGET target. The final output of this function should unconditionally have ARG_TARGET. That saves doing -as workarounds in other parts of the code. The TARGET name the user passes should be definitive, in other words.

Do we strictly need a target for the intermediate step too? If we do, we could have ${ARG_TARGET}${TMP_SUFFIX} here. Then non-IR files get ARG_TARGET target, and then in the IR block below we create ARG_TARGET for that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have simplified the code by removing ${ARG_TARGET}-as
The issue we observing is that .cl files are compiled sequentially. We don't see problem with .ll file, so we can leave it as-is.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes that's much simpler.

I'm half wondering if this isn't only for enabling parallel execution but is technically required for correct behaviour. I know CMake can behave strangely when only depending upon files and not targets, which is that we're doing before this PR. Though even with this PR, we're still implicitly depending on files coming out of the llvm-as step. That's what makes me a bit nervous. This is still better than what we've got so maybe we can keep an eye on it and see if we have any parallel build issues stemming from .ll files, and we can adjust as needed.

I think I'll also post a follow-up which handles empty arguments. I was going to ask about handling an empty TARGET but noticed there's nothing for empty INPUT or OUTPUT so maybe that's best left handled later in one go.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is still better than what we've got so maybe we can keep an eye on it and see if we have any parallel build issues stemming from .ll files, and we can adjust as needed.

yeah, I agree

get_filename_component( file_ext ${file} EXT )
if( ${file_ext} STREQUAL ".ll" )
list( APPEND bytecode_ir_files ${output_file} )
list( APPEND compile_tgts ${tgt}-as )
Copy link
Contributor

Choose a reason for hiding this comment

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

See above - we wouldn't need this case if we could rely on a consistent target name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, removed

set( builtins_comp_lib_tgt builtins.comp.${ARG_ARCH_SUFFIX} )
add_custom_target( ${builtins_comp_lib_tgt}
DEPENDS ${bytecode_files}
DEPENDS ${compile_tgts}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we not need to depend on the targets and the files? CMake dependencies are strange.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, I'll add ${bytecode_files} depends back

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, added back ${bytecode_files}

@wenju-he
Copy link
Contributor Author

@frasercrmck should we check CMAKE_VERSION and use DEPENDS_EXPLICIT_ONLY if the version is 3.27 or higher? I suppose DEPENDS_EXPLICIT_ONLY would be more light-weighted. And we need to keep both DEPENDS_EXPLICIT_ONLY and adding new target path, until llvm uplifts cmake version to 3.27.

@wenju-he wenju-he requested a review from frasercrmck April 10, 2025 08:10
Copy link
Contributor

@frasercrmck frasercrmck left a comment

Choose a reason for hiding this comment

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

On my build this increases the number of targets ninja -t targets from 19K to 28K. That's building all targets. I'm not sure what else we could do about that and whether it's a problem.

${ARG_DEPENDENCIES}
DEPFILE ${ARG_OUTPUT}.d
)
add_custom_target( ${ARG_TARGET} DEPENDS ${ARG_OUTPUT}${TMP_SUFFIX} )
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes that's much simpler.

I'm half wondering if this isn't only for enabling parallel execution but is technically required for correct behaviour. I know CMake can behave strangely when only depending upon files and not targets, which is that we're doing before this PR. Though even with this PR, we're still implicitly depending on files coming out of the llvm-as step. That's what makes me a bit nervous. This is still better than what we've got so maybe we can keep an eye on it and see if we have any parallel build issues stemming from .ll files, and we can adjust as needed.

I think I'll also post a follow-up which handles empty arguments. I was going to ask about handling an empty TARGET but noticed there's nothing for empty INPUT or OUTPUT so maybe that's best left handled later in one go.

@wenju-he
Copy link
Contributor Author

On my build this increases the number of targets ninja -t targets from 19K to 28K. That's building all targets. I'm not sure what else we could do about that and whether it's a problem.

yes, the number of targets increase significantly with this approach. But as far as I know, we don't have compilation correctness issue regarding the increase of the number of targets.
The only alternative approach is DEPENDS_EXPLICIT_ONLY. I can add DEPENDS_EXPLICIT_ONLY support to this PR if cmake version is 3.27 or higher.

@wenju-he wenju-he requested a review from frasercrmck April 11, 2025 00:08
@frasercrmck frasercrmck merged commit 0c21d6b into llvm:main Apr 14, 2025
9 checks passed
@wenju-he wenju-he deleted the libclc-compile-target branch April 14, 2025 13:19
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.

2 participants