Files
swift-mirror/test/IRGen/autolink-force-link.swift
Robert Widmann f6b2294d5a Undo Force Load + Incremental Ban on Darwin Platforms
Gather 'round to hear tell of the saga of autolinking in incremental
mode.

In the beginning, there was Swift code, and there was Objective-C code.
To make one import bind two languages, a twinned Swift module named the same as an
Objective-C module could be imported as an overlay. But all was not
well, for an overlay could be created which had no Swift content, yet
required Swift symbols. And there was much wailing and gnashing of teeth
as loaders everywhere disregarded loading these content-less Swift
libraries.

So, a solution was found - a magical symbol _swift_FORCE_LOAD_$_<MODULE>
that forced the loaders to heed the dependency on a Swift library
regardless of its content. It was a constant with common linkage, and it
was good. But, along came COFF which needed to support autolinking but
had no support for such matters. It did, however, have support for
COMDAT sections into which we placed the symbol. Immediately, a darkness
fell across the land as the windows linker loudly proclaimed it had
discovered a contradiction: "_swift_FORCE_LOAD_$_<MODULE> cannot be
a constant!", it said, gratingly, "for this value requires rebasing."
Undeterred, we switched to a function instead, and the windows linker
happily added a level of indirection to its symbol resolution procedure
and all was right with the world.

But this definition was not all right. In order to support multiple
translation units emitting it, and to prevent the linker from dead
stripping it, Weak ODR linkage was used. Weak ODR linkage has the nasty
side effect of pessimizing load times since the dynamic linker must
assume that loading a later library could produce a more definitive
definition for the symbol.

A compromise was drawn up: To keep load times low, external linkage was
used. To keep the linker from complaining about multiple strong
definitions for the same symbol, the first translation unit in the
module was nominated to recieve the magic symbol. But one final problem
remained:

Incremental builds allow for files to be added or removed during the
build procedure. The placement of the symbol was therefore dependent
entirely upon the order of files passed at the command line. This was no
good, so a decree was set forth that using -autolink-force-load and
-incremental together was a criminal offense.

So we must compromise once more: Return to a symbol with common linkage,
but only on Mach-O targets. Preserve the existing COMDAT-friendly
approach everywhere else.

This concludes our tale.

rdar://77803299
2021-05-24 15:53:13 -07:00

28 lines
1.7 KiB
Swift

// RUN: %empty-directory(%t)
// RUN: %swift -disable-legacy-type-info -target x86_64-apple-macosx10.9 -parse-stdlib -autolink-force-load -module-name TEST -module-link-name TEST -emit-ir %s %S/../Inputs/empty.swift | %FileCheck -check-prefix=CHECK-WMO %s
// CHECK-WMO: source_filename = "<swift-imported-modules>"
// CHECK-WMO: @"_swift_FORCE_LOAD_$_TEST"
// CHECK-WMO-NOT: source_filename
// RUN: %swift -disable-legacy-type-info -target x86_64-apple-macosx10.9 -parse-stdlib -autolink-force-load -module-name TEST -module-link-name TEST -emit-ir -num-threads 1 %s %S/../Inputs/empty.swift | %FileCheck -check-prefix=CHECK-WMO-THREADED %s
// CHECK-WMO-THREADED: source_filename = "<swift-imported-modules>"
// CHECK-WMO-THREADED: @"_swift_FORCE_LOAD_$_TEST"
// CHECK-WMO-THREADED: source_filename = "<swift-imported-modules>"
// CHECK-WMO-THREADED: @"_swift_FORCE_LOAD_$_TEST"
// RUN: %swift -disable-legacy-type-info -target x86_64-apple-macosx10.9 -parse-stdlib -autolink-force-load -module-name TEST -module-link-name TEST -emit-ir -primary-file %s %S/../Inputs/empty.swift | %FileCheck -check-prefix=CHECK-SINGLE-FILE-FIRST %s
// RUN: %swift -disable-legacy-type-info -target x86_64-apple-macosx10.9 -parse-stdlib -autolink-force-load -module-name TEST -module-link-name TEST -emit-ir %S/../Inputs/empty.swift -primary-file %s | %FileCheck -check-prefix=CHECK-SINGLE-FILE-SECOND %s
// CHECK-SINGLE-FILE-FIRST: source_filename = "<swift-imported-modules>"
// CHECK-SINGLE-FILE-FIRST: @"_swift_FORCE_LOAD_$_TEST"
// CHECK-SINGLE-FILE-FIRST-NOT: source_filename
// CHECK-SINGLE-FILE-SECOND: source_filename = "<swift-imported-modules>"
// CHECK-SINGLE-FILE-SECOND: @"_swift_FORCE_LOAD_$_TEST"
// CHECK-SINGLE-FILE-SECOND-NOT: source_filename