This is an archive of the discontinued LLVM Phabricator instance.

[CMake] Respect variables for specifying host tools even without LLVM_USE_HOST_TOOLS set
ClosedPublic

Authored by mstorsjo on Mar 22 2023, 3:56 PM.

Details

Summary

When LLVM_NATIVE_TOOL_DIR was introduced in
d3da9067d143f3d4ce59b6d9ab4606a8ef1dc937 / D131052, it consisted
of refactoring a couple cases of manual logic for tools in
clang-tools-extra/clang-tidy, clang-tools-extra/pseudo/include
and mlir/tools/mlir-linalg-ods-gen. The former two had the same
consistent behaviour while the latter was slightly different, so
the refactoring would end up slightly adjusting one or the other.

The difference was that the clang-tools-extra tools respected the
external variable for setting the tool name, regardless of the
LLVM_USE_HOST_TOOLS variable, while mlir-linalg-ods-gen tool
only checked its external variable if LLVM_USE_HOST_TOOLS was set.

LLVM_USE_HOST_TOOLS is supposed to be enabled automatically whenever
cross compiling, so this shouldn't have been an issue.

In https://github.com/llvm/llvm-project/issues/60784, it seems like
some users do cross compile LLVM, without CMake knowing about it
(without CMAKE_CROSSCOMPILING being set). In these cases, their
build broke, as the variables for pointing to external host tools
no longer were being respected.

Skip the checks for LLVM_USE_HOST_TOOLS and always respect the
variables for pointing to external tools (both the old tool specific
variables, and the new LLVM_NATIVE_TOOL_DIR), if they're set. This
makes the logic within setup_host_tool more exactly match the
logic for the clang-tools-extra tools from before the refactoring
in d3da9067d143f3d4ce59b6d9ab4606a8ef1dc937.

This should hopefully fix
https://github.com/llvm/llvm-project/issues/60784 (and should
probably be backported to the 16.x release branch in the end).

Diff Detail

Event Timeline

mstorsjo created this revision.Mar 22 2023, 3:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 22 2023, 3:56 PM
mstorsjo requested review of this revision.Mar 22 2023, 3:56 PM

I'd need to learn more about the use case (I see you're awaiting a response on the issue), but I'm not a huge fan personally; it seems really weird to not be going through the standard CMake mechanisms for cross-compilation (setting a CMAKE_SYSTEM_NAME), and if you do that then this should work as expected. We should understand why LLVM_USE_HOST_TOOLS isn't being set automatically.

I'd need to learn more about the use case (I see you're awaiting a response on the issue), but I'm not a huge fan personally; it seems really weird to not be going through the standard CMake mechanisms for cross-compilation (setting a CMAKE_SYSTEM_NAME), and if you do that then this should work as expected. We should understand why LLVM_USE_HOST_TOOLS isn't being set automatically.

Yeah, in one sense, that’s the correct thing to do. But on the other hand, even if I’m doing a simple native build, if I’m going out of my way to specify the path to these tools, perhaps we should respect it. We already do it somewhat inconsistently - AFAIK the variables do take effect for the tablegen family of tools without checking the LLVM_USE_HOST_TOOLS variables.

I'd need to learn more about the use case (I see you're awaiting a response on the issue), but I'm not a huge fan personally; it seems really weird to not be going through the standard CMake mechanisms for cross-compilation (setting a CMAKE_SYSTEM_NAME), and if you do that then this should work as expected. We should understand why LLVM_USE_HOST_TOOLS isn't being set automatically.

Yeah, in one sense, that’s the correct thing to do. But on the other hand, even if I’m doing a simple native build, if I’m going out of my way to specify the path to these tools, perhaps we should respect it. We already do it somewhat inconsistently - AFAIK the variables do take effect for the tablegen family of tools without checking the LLVM_USE_HOST_TOOLS variables.

The flip side could be that you have a shared configuration for all your builds that just sets that flag in case it's needed, and that shared config is used by both native and cross-compilation. Not sure how realistic that is either, but I can see someone being surprised the other way as well.

I'd need to learn more about the use case (I see you're awaiting a response on the issue), but I'm not a huge fan personally; it seems really weird to not be going through the standard CMake mechanisms for cross-compilation (setting a CMAKE_SYSTEM_NAME), and if you do that then this should work as expected. We should understand why LLVM_USE_HOST_TOOLS isn't being set automatically.

Yeah, in one sense, that’s the correct thing to do. But on the other hand, even if I’m doing a simple native build, if I’m going out of my way to specify the path to these tools, perhaps we should respect it. We already do it somewhat inconsistently - AFAIK the variables do take effect for the tablegen family of tools without checking the LLVM_USE_HOST_TOOLS variables.

The flip side could be that you have a shared configuration for all your builds that just sets that flag in case it's needed, and that shared config is used by both native and cross-compilation. Not sure how realistic that is either, but I can see someone being surprised the other way as well.

That's true - although if the provided tools are valid (up to date), it should be harmless to use them. It could possibly even speed up the build, if it could use the preexisting tools for generating headers without first needing to build the new tool (but it's possible that there are dependencies in place that force the new tools to be built first anyway).

In any case, if looking at historical behaviour, the tablegen tools have respected the setting without looking at LLVM_USE_HOST_TOOLS since forever - and still do, that part wasn't refactored in my commit, and the clang-tools-extra tools also used their individual settings without looking at LLVM_USE_HOST_TOOLS in 15.x. Previously it was only mlir-linalg-ods-gen which required LLVM_USE_HOST_TOOLS to be set.

So with long term behaviour vs surprises, my refactoring was the one that brought in the surprise change.

It's still of course important to use cmake correctly and make sure it's aware of the fact that the user is cross compiling - but I think I needlessly caused surprises/breakage for users here.

smeenai accepted this revision.Mar 23 2023, 10:25 AM

Fair enough; I'm convinced :) LGTM

This revision is now accepted and ready to land.Mar 23 2023, 10:25 AM