Skip to content

[autolink-extract] Support (and ignore) LLVM IR files. #71142

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

In Linux, the current implementation of swift-autolink-extract does not support LLVM IR files resulting from using LTO.

If one tries to build LLVM using LTO and then try to link one of the targets that use swiftc to link, but link against LLVM object files (like swift-plugin-server), swift-autolink-extract will fail saying that some object files are not valid.

To deal with LLVM IR files correctly, create and pass a llvm::LLVMContext around, which allows the APIs in llvm::object to read LLVM IR files. Additionally, handle the case of IRObjectFile when extracting, but perform no action.

In Linux, the current implementation of swift-autolink-extract does not
support LLVM IR files resulting from using LTO.

If one tries to build LLVM using LTO and then try to link one of the
targets that use `swiftc` to link, but link against LLVM object files
(like `swift-plugin-server`), `swift-autolink-extract` will fail saying
that some object files are not valid.

To deal with LLVM IR files correctly, create and pass
a `llvm::LLVMContext` around, which allows the APIs in `llvm::object` to
read LLVM IR files. Additionally, handle the case of `IRObjectFile` when
extracting, but perform no action.
@drodriguez drodriguez requested review from artemcm and MaxDesiatov and removed request for MaxDesiatov and etcwilde January 25, 2024 03:50
@drodriguez
Copy link
Contributor Author

@swift-ci please test

@MaxDesiatov MaxDesiatov requested a review from etcwilde January 25, 2024 08:36
@MaxDesiatov MaxDesiatov added the swift-autolink-extract Area → compiler → legacy driver: the 'swift-autolink-extract' mode label Jan 25, 2024
@drodriguez
Copy link
Contributor Author

@swift-ci please test Linux platform

@kateinoigakukun
Copy link
Member

kateinoigakukun commented Jan 25, 2024

@drodriguez For bitcode linker input files, we already emit llvm.linker.options metadata and lld understands the given options, so we don't need swift-autolink-extract when performing LTO. Therefore, the driver usually does not run swift-autolink-extract with LTO option.

So I'm curious about what invokes swift-autolink-extract in your scenario.

https://github.com/apple/swift-driver/blob/49daa7ef0a0e662a95ec022869409d0ed9fb502f/Sources/SwiftDriver/Jobs/AutolinkExtractJob.swift#L20-L23

@drodriguez
Copy link
Contributor Author

The setup is complicated. We always build Swift not standalone, but using LLVM_EXTERNAL_PROJECTS (unified). We want to enable LTO for LLVM. I think the same will happen if one builds standalone and tries to LTO the LLVM sources, generates .a files with embedded LLVM IR files, and then uses that build directory, but I haven't gone that path.

swift-plugin-server (https://github.com/apple/swift/blob/main/tools/swift-plugin-server/CMakeLists.txt) depends on something that brings some LLVM libraries into the linker step. We do not enable LTO for the Swift pieces, only for the LLVM pieces. Swift is being used as a linker driver only, I am afraid. Some objects are IR objects (the LLVM ones and C/C++ ones), but some are not (the Swift ones). I think the code you linked does not check for each of the files (and static archives) to be passed to the swift-autolink-extract step, but checks if the driver is doing LTO (which is not, we are just linking at that point).

The provided test jumps over the usage of the Swift driver, but more or less reproduces what's going on: some step compiles a C++ files with LTO, the Swift driver is invoked and given those bitcode objects (both in the form of single objects and also in the form of archives of bitcode objects), the driver passes all the paths with extensions .a and .o, and autolink-extract tries to read those objects.

@kateinoigakukun
Copy link
Member

@drodriguez Thank you for your elaboration! Given that situation, accepting and ignoring object files from foreign sources makes sense to me. I'd like to hear @compnerd opinion just in case.

@drodriguez drodriguez requested a review from compnerd January 30, 2024 18:22
@drodriguez
Copy link
Contributor Author

@compnerd: added you as a reviewer. @kateinoigakukun above wanted to know your opinion.

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

Yeah, this makes sense - the autolink-extract is meant to serve as a workaround for non-lld linker compatibility. The IR files will be emitted to objects and passed to the linker subsequently. We still need to use swift-autolink-extract on it at some point, but the IR files themselves would not contribute. I think that this change is insufficient for enabling thin lto, but is improving the state of the world.

Copy link
Contributor

@artemcm artemcm left a comment

Choose a reason for hiding this comment

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

This change makes sense to me, thank you.

@drodriguez drodriguez merged commit b43d01f into swiftlang:main Jan 31, 2024
@drodriguez drodriguez deleted the autolink-extract-ir branch January 31, 2024 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
swift-autolink-extract Area → compiler → legacy driver: the 'swift-autolink-extract' mode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants