Skip to content

[IRGen] Emit mod summary for full LTO to enable hermetic seal with lld #42366

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

Conversation

kateinoigakukun
Copy link
Member

@kateinoigakukun kateinoigakukun commented Apr 14, 2022

LTO pipeline requires consistent EnableSplitLTOUnit and module summary
in regular full LTO bitcode, and clang enables EnableSplitLTOUnit and
emit regular lto module summary on non-ld64 platforms.
Therefore, swiftc has to emit them for the consistency with clang.

This patch unlocks stdlib to be linked with a simple code with --hermetic-seal-at-link and lld on Linux


There are still some remaining issues:

  1. autolinking somehow does not work
  2. It seems conditional-dead-stripping doesn't work now when targeting ELF. test/IRGen/conditional-dead-strip-exec.swift doesn't pass yet.
  3. test/IRGen/virtual-function-elimination-exec.swift and test/IRGen/witness-method-elimination-exec.swift are failing with the following preemption error.
ld.lld: error: cannot preempt symbol: $sBoWV
>>> defined in /home/katei/.ghq/work.katei.dev/swift-source/build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/linux/libswiftCore.so
>>> referenced by ld-temp.o
>>>               lto.tmp:($s4main7MyClassCMf)

It would be appreciated if you have any ideas.

Depends on swiftlang/llvm-project#4228

LTO pipeline requires consistent `EnableSplitLTOUnit` and module summary
in regular full LTO bitcode, and clang enables `EnableSplitLTOUnit` and
emit regular lto module summary on non-ld64 platforms.
Therefore, swiftc has to emit them for the consistency with clang.
@kateinoigakukun
Copy link
Member Author

@swift-ci Please test

@kateinoigakukun
Copy link
Member Author

Please test with following PR:
swiftlang/llvm-project#4228

@swift-ci Please test Linux platform

@kateinoigakukun
Copy link
Member Author

@kubamracek Could you take a look if you have time?

@kubamracek
Copy link
Contributor

I'm afraid I don't understand LTO enough to review this. @compnerd?

@MaxDesiatov
Copy link
Contributor

MaxDesiatov commented May 30, 2022

Hi @compnerd, you've also reviewed the cherry-picked swiftlang/llvm-project#4228 LLVM patch on Apple's fork. Would you be available to review this PR as well? Thank you!

@zoecarver
Copy link
Contributor

Ping @compnerd.

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

I'm not super familiar with this part of the compiler, but the change seems good to me :)

@zoecarver zoecarver requested a review from aschwaighofer June 10, 2022 17:30
@zoecarver
Copy link
Contributor

Ping @gottesmm

// Emit a module summary by default for Regular LTO except ld64-based ones
// (which use the legacy LTO API).
bool EmitRegularLTOSummary =
targetMachine->getTargetTriple().getVendor() != llvm::Triple::Apple;
Copy link
Member

Choose a reason for hiding this comment

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

Why the vendor check rather than !targetMachine->getTargetTriple().isOSDarwin()?

Copy link
Contributor

Choose a reason for hiding this comment

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

@kubamracek Which form do we prefer here? != Apple or isOSDarwin?

Copy link
Member Author

Choose a reason for hiding this comment

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

EmitPasses.add(createBitcodeWriterPass(out));
if (EmitRegularLTOSummary) {
module->addModuleFlag(llvm::Module::Error, "ThinLTO", uint32_t(0));
// Assume other sources are compiled with -fsplit-lto-unit (it's enabled
Copy link
Member

Choose a reason for hiding this comment

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

Can we avoid this assumption? Why do we need this assumption in the first place? Perhaps we should serialize and reference this from the Swift module?

Copy link
Member Author

Choose a reason for hiding this comment

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

Other sources mean not only Swiftc-oriented LTO summary but also including Clang-oriented summary. So we can't propagate that information through swiftmodule.
Another option to remove that assumption is adding -split-lto-unit in swiftc also. What do you think?

@tbkka
Copy link
Contributor

tbkka commented Jun 17, 2022

@eeckstein -- Any thoughts on the proposed changes to the LTO settings here?

@eeckstein
Copy link
Contributor

I know too little about LTO. That would be a question for @gottesmm or @aschwaighofer

@MaxDesiatov
Copy link
Contributor

Hi @gottesmm @aschwaighofer, would you have a moment to take a look at this PR? Thanks!

@aschwaighofer
Copy link
Contributor

aschwaighofer commented Jul 18, 2022

I am not an LTO expert either. The changes seem reasonable to me but I can't predict the consequences of unconditionally (on linux) adding the module flags:

EnableSplitLTOUnit=1 and ThinLTO=0

The code seems to indeed follow what clang does.

Can we take this change and deal with any fallout (which I expect to be only on linux) or do you have concerns with doing so @kubamracek ?

@kubamracek
Copy link
Contributor

No concerns with that.

@MaxDesiatov
Copy link
Contributor

Thanks for the reviews! Can we merge then?

@aschwaighofer
Copy link
Contributor

re-trigger testing because it looks like the last test is quite a bit in the past

@aschwaighofer
Copy link
Contributor

@swift-ci test

@fjtrujy
Copy link

fjtrujy commented Aug 11, 2022

Could be this merged now?
Cheers

@aschwaighofer
Copy link
Contributor

@swift-ci Please test and merge

@swift-ci swift-ci merged commit 9c4972f into swiftlang:main Aug 11, 2022
@kateinoigakukun kateinoigakukun deleted the katei/fix-hermetic-seal-with-lld branch August 12, 2022 02:40
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.

10 participants