LLVM addrsig support

Hi folks,

Earlier I got to know a special LLVM code generation flag named -addrsig that tells the backend to emit a special __DATA,__llvm_addrsig section which helps some linkers (LLD for example) perform ICF without creating a thunk, which in turn results in a smaller binary.

I was wondering how much it will help to reduce binary size for Swift code, so I tried to enable it via swiftc with -Xllvm -addrsig. Surprisingly, the compiler (well, actually the LLVM option parsing) complains immediately that it does not recognize -addrsig.

swift (LLVM option parsing): Unknown command line argument '-addrsig'.  Try: 'swift (LLVM option parsing) --help'
swift (LLVM option parsing): Did you mean '--version'?
error: fatalError

It took me some time to realize that it is impossible to pass LLVM code generation options in command line. Asking the same question on LLVM discourse, it seems that this is a design decision.

So here are my questions for Swift community:

  1. Do we have a plan to add the addrsig support in Swift compiler like what Clang does (a driver flag-faddrsig)?
  2. Is the generic implementation good enough to be used as-is for Swift code?

If Clang already supports -faddrsig, there's no need for the Swift compiler to explicitly add support for it, you can already pass it directly: swiftc -Xcc -faddrsig.

IIUC it will not affect the codegen for Swift code, but only for the Clang modules whose compilation is triggered by the same swiftc invocation.

No, that's incorrect, Clang flags that affect LLVM codegen features do affect Swift code when passed to swiftc. See previous posts on this topic: Targeting specific microarchitectures - #9 by Max_Desiatov.

Just gave swiftc -Xcc -faddrsig a try for a single .swift file. I passed -v to see the actual frontend invocation and inspected the intermediate object file, I don't see __DATA,__llvm_addrsig section there.

I noticed that a pull request was merged last week, which manually bridges a Clang CodeGen option -femulate-tls to LLVM target options. You can view the details here: PR #78591.

Does this mean that we need to follow a similar approach to enable addrsig for Swift?

I initially tried simply passing in -Xcc -femulated-tls as Max says, but it did nothing. It appears some clang flags passed to the Swift compiler are used for Swift codegen and others are not. I did not dig into the reasons and simply manually set it in that pull, as you note. It's possible a compiler change like that would be needed for your addrsig feature also.