-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
[CMake] Support per-target linker flags #68393
Conversation
I have wanted this for a long time. Thank you for the patch. (I am not familiar with CMake.) Does this variable need to be cached? New CMake variables can be documented at llvm/docs/CMake.rst
1.4x as fast as the baseline or the new speed is 240% of the baseline? |
Looks useful :) Do we need the granularity of the exe vs. module vs. shared custom flags, or could we just have a single set of custom flags per target? (That'd also allow you to have a common function that you just call from both |
Yes, otherwise LLVM will not pick up these values. I'll add it to the documentation. Thanks for the pointer.
1.4x as fast as the baseline. So, as an example, if an overall build took 1 hour to build everything with LTO, it would've taken us around ~43 minutes to build our toolchains with LTO because we selectively only chose what we wanted to optimize. YMMV depending on the machine instance and what you decide to not optimize. |
Initially, I separated because I thought the flags would've been duplicated, but I now realize that the two methods are mutually exclusive (i.e. |
@petrhosek Does this change look reasonable to you? I know this particular issue could be solved with |
What is the purpose of |
Hmm, you're right. It isn't providing any use here. I had originally thought of having it so we can could write specific logic against targets that have been added with custom flags, but I realize that might not be a real use case after all. |
@@ -1524,6 +1528,13 @@ macro(add_llvm_tool_subdirectory name) | |||
add_llvm_external_project(${name}) | |||
endmacro(add_llvm_tool_subdirectory) | |||
|
|||
macro(add_custom_linker_flags name) | |||
if (LLVM_${name}_LINKER_FLAGS) | |||
message(STATUS "Applying ${LLVM_${name}_LINKER_FLAGS} to ${name}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd omit this to avoid the extra build spam (or make it optional).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What did you mean by optional here? Like a CMake flag that enables the message? In general, I find this message to be quite helpful when debug whether targets were applied with custom flags without needing to go into the CMakeCache.txt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to land this as is, and if this becomes very build spammy, we can figure out other approaches to disable it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative to an option would be to use the DEBUG
mode instead of STATUS
so developers can use CMAKE_MESSAGE_LOG_LEVEL
to suppress the output if desired.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reducing the overhead of LTO was one of the motivations for `-ffat-lto-objects, which let you use LTO for distribution components while disabling LTO for everything else; @ilovepi is working on a patch for that which should be ready for review soon. This change might be still useful if you want to enable/disable LTO only for certain targets. My concern is the granularity, specifically how practical is controlling linker flags at the per-target level, especially if you build a large number of targets? The alternatives I could think of is setting the linker flags at the per-distribution granularity. |
For us, we define a set of distribution as well for other teams to use. But even with our filtered down distribution, we'd still like to only apply |
This is primarily only useful when debugging. It's generally assumed that users will have their custom flags applied if it's specified in their CMake cache files. Addresses #68393 (comment)
CMAKE_{C/CXX}_FLAGS
affects all targets in LLVM. This canbe undesirable in situations, like the case of enabling thinLTO,
where
-flto
is added to every source file. In reality, we onlycare about optimizing a select few of binaries, such as clang or lld,
that dominate the compilation pipeline. Auxiliary binaries in a
distribution and not on the critical path can be kept non-optimized.
This PR adds support of per-target linker flags, which can solve the
thinLTO problem by negating the effects of LTO via targeted linker
flags on the targets. The example of negating thinLTO
above can be done by doing the following:
There's other applications where this could be used (e.g. avoid
optimizing host tools for build speed improvement etc.).
I've generalized this so that users can apply their desired flags to
targets that are generated by
llvm_add_library
oradd_llvm_executable
.Internally, our toolchain builds were on average 1.4x faster when
selectively choosing the binaries that we want optimized.