Skip to content

[CMake] Respect LLVM instrumentation variables in AddPureSwift.cmake #71135

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

Merged
merged 1 commit into from
Jan 31, 2024

Conversation

drodriguez
Copy link
Contributor

If one is building Swift against an instrumented LLVM build, when the functions in AddPureSwift.cmake tries to link Swift code that also links LLVM instrumented code, the linking will fail because the linker will not use the profiling runtimes.

The modifications replicate the existing code in LLVM HandleLLVMOptions.cmake, but adapted to Swift variables where possible.

The necessary arguments are forwarded through -Xclang-linker because the spelling of the Swift driver does not give access to all the options that Clang provides.

This has been tested with an unified build, but a standalone build could still pass the LLVM_* variables to the Swift build system to make use of this code.

If one is building Swift against an instrumented LLVM build, when the
functions in AddPureSwift.cmake tries to link Swift code that also links
LLVM instrumented code, the linking will fail because the linker will
not use the profiling runtimes.

The modifications replicate the existing code in LLVM
HandleLLVMOptions.cmake, but adapted to Swift variables where possible.

The necessary arguments are forwarded through `-Xclang-linker` because
the spelling of the Swift driver does not give access to all the options
that Clang provides.

This has been tested with an unified build, but a standalone build could
still pass the `LLVM_*` variables to the Swift build system to make use
of this code.
@drodriguez drodriguez requested a review from compnerd January 25, 2024 00:55
@drodriguez
Copy link
Contributor Author

@swift-ci please test

# This replicates he code existing in LLVM llvm/cmake/modules/HandleLLVMOptions.cmake
# The second part of the clause replicates the LINKER_IS_LLD_LINK of the
# original.
if(LLVM_BUILD_INSTRUMENTED AND NOT (SWIFT_COMPILER_IS_MSVC_LIKE AND SWIFT_USE_LINKER STREQUAL "lld"))
Copy link
Member

Choose a reason for hiding this comment

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

Why is LLD a requirement?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original code does not explain why those conditions are needed. The original commit that introduced the conditions doesn't explain it either: swiftlang/llvm-project@a84b200

The stable/20230725 code is in https://github.com/apple/llvm-project/blob/stable/20230725/llvm/cmake/modules/HandleLLVMOptions.cmake#L1095-L1133

The linker options are only added for cases that are NOT LINKER_IS_LLD_LINK

The values of LINKER_IS_LLD_LINK comes from https://github.com/apple/llvm-project/blob/stable/20230725/llvm/cmake/modules/HandleLLVMOptions.cmake#L20-L30

If I had read the things correctly, the requirements for LINKER_IS_LLD_LINK is that we are using MSVC (either clang-cl or the actual cl) and using LLD to link, which if I had translated correctly should happen when SWIFT_COMPILER_IS_MSVC_LIKE and when SWIFT_USE_LINKER is lld.

Copy link
Member

Choose a reason for hiding this comment

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

Am I just failing to apply DeMorgan's Law? NOT (MSVC AND LLD) -> NOT MSVC OR NOT LLD. It would make sense as AND (NOT MSVC OR LLD).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's see if I remember doing these things:

MSVC_LIKE = SWIFT_COMPILER_IS_MSVC_LIKE
LLD = SWIFT_USE_LINKER STREQUAL "lld"
| MSVC_LIKE | LLD | NOT (MSVC AND LLD) | NOT MSVC OR LLD |
| T         | T   | F                  | T               |
| T         | F   | T                  | F               |
| F         | T   | T                  | T               |
| F         | F   | T                  | T               |

For the version that I think I implemented, the only F is when MSVC and LLD are both T, which I think is the case in the original code. For the proposed version the case when MSVC and LLD is T, and the case of using MSVC but not LLD is F, which I think is the opposite of what we want.

PD: logic is hard, let's go shopping, and all the normal disclosures when doing booleans.

@drodriguez
Copy link
Contributor Author

Thinking in merging this later today, if nobody else have any objections.

@drodriguez drodriguez merged commit cc25000 into swiftlang:main Jan 31, 2024
@drodriguez drodriguez deleted the llvm-build-instrumented branch January 31, 2024 16:47
drodriguez added a commit that referenced this pull request May 15, 2024
…tables (#73629)

In #71135 I added this code to `add_pure_swift_host_library`
but failed to realize that it was also needed in
`add_pure_swift_host_tool`. Extract the code into a helper function and
invoke it from both functions in order to be able to instrument the
`swift-plugin-server` and other pure Swift tools.
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