Skip to content

Commit 9b59996

Browse files
authored
Merge pull request swiftlang#28691 from nkcsgexi/tbdgen-ld-hide
TBDGen/IRGen: generate $ld$hide$os symbols for decls marked with @_originallyDefinedIn
2 parents bac5a64 + 624c2fc commit 9b59996

31 files changed

+507
-57
lines changed

include/swift/AST/Attr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,8 @@ class OriginallyDefinedInAttr: public DeclAttribute {
16251625
/// Indicates when the symbol was moved here.
16261626
const llvm::VersionTuple MovedVersion;
16271627

1628+
/// Returns true if this attribute is active given the current platform.
1629+
bool isActivePlatform(const ASTContext &ctx) const;
16281630
static bool classof(const DeclAttribute *DA) {
16291631
return DA->getKind() == DAK_OriginallyDefinedIn;
16301632
}

include/swift/AST/DiagnosticsSema.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,15 @@ NOTE(option_set_empty_set_init,none,
14671467
ERROR(originally_defined_in_dupe_platform,none,
14681468
"duplicate version number for platform %0", (StringRef))
14691469

1470+
ERROR(originally_definedin_topleve_decl,none,
1471+
"@%0 is only applicable to top-level decl", (StringRef))
1472+
1473+
ERROR(originally_definedin_need_available,none,
1474+
"need @available attribute for @%0", (StringRef))
1475+
1476+
ERROR(originally_definedin_must_after_available_version,none,
1477+
"moved version from @%0 must after introduced OS version", (StringRef))
1478+
14701479
// Alignment attribute
14711480
ERROR(alignment_not_power_of_two,none,
14721481
"alignment value must be a power of two", ())

include/swift/Basic/SupplementaryOutputPaths.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ struct SupplementaryOutputPaths {
149149
/// \sa swift::emitSwiftInterface
150150
std::string ModuleInterfaceOutputPath;
151151

152+
/// The path to a .c file where we should declare $ld$add symbols for those
153+
/// symbols moved to the current module.
154+
/// When symbols are moved to this module, this module declares them as HIDE
155+
/// for the OS versions prior to when the move happened. On the other hand, the
156+
/// original module should ADD them for these OS versions. An executable
157+
/// can choose the right library to link against depending on the deployment target.
158+
/// This is a walk-around that linker directives cannot specify other install
159+
/// name per symbol, we should eventually remove this.
160+
std::string LdAddCFilePath;
161+
152162
SupplementaryOutputPaths() = default;
153163
SupplementaryOutputPaths(const SupplementaryOutputPaths &) = default;
154164

@@ -158,7 +168,7 @@ struct SupplementaryOutputPaths {
158168
ReferenceDependenciesFilePath.empty() &&
159169
SerializedDiagnosticsPath.empty() && LoadedModuleTracePath.empty() &&
160170
TBDPath.empty() && ModuleInterfaceOutputPath.empty() &&
161-
ModuleSourceInfoOutputPath.empty();
171+
ModuleSourceInfoOutputPath.empty() && LdAddCFilePath.empty();
162172
}
163173
};
164174
} // namespace swift

include/swift/Frontend/Frontend.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ class CompilerInvocation {
385385
/// fail an assert if not in that mode.
386386
std::string getModuleInterfaceOutputPathForWholeModule() const;
387387

388+
std::string getLdAddCFileOutputPathForWholeModule() const;
389+
388390
SerializationOptions
389391
computeSerializationOptions(const SupplementaryOutputPaths &outs,
390392
bool moduleIsPublic);

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,4 +644,7 @@ def type_info_dump_filter_EQ : Joined<["-"], "type-info-dump-filter=">,
644644
Flags<[FrontendOption]>,
645645
HelpText<"One of 'all', 'resilient' or 'fragile'">;
646646

647+
def emit_ldadd_cfile_path
648+
: Separate<["-"], "emit-ldadd-cfile-path">, MetaVarName<"<path>">,
649+
HelpText<"Generate .c file defining symbols to add back">;
647650
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

include/swift/Subsystems.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/ADT/ArrayRef.h"
2626
#include "llvm/ADT/Optional.h"
2727
#include "llvm/ADT/StringRef.h"
28+
#include "llvm/ADT/StringSet.h"
2829
#include "llvm/Support/Mutex.h"
2930

3031
#include <memory>
@@ -277,7 +278,8 @@ namespace swift {
277278
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
278279
llvm::LLVMContext &LLVMContext,
279280
ArrayRef<std::string> parallelOutputFilenames,
280-
llvm::GlobalVariable **outModuleHash = nullptr);
281+
llvm::GlobalVariable **outModuleHash = nullptr,
282+
llvm::StringSet<> *LinkerDirectives = nullptr);
281283

282284
/// Turn the given Swift module into either LLVM IR or native code
283285
/// and return the generated LLVM IR module.
@@ -287,7 +289,8 @@ namespace swift {
287289
std::unique_ptr<SILModule> SILMod,
288290
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
289291
llvm::LLVMContext &LLVMContext,
290-
llvm::GlobalVariable **outModuleHash = nullptr);
292+
llvm::GlobalVariable **outModuleHash = nullptr,
293+
llvm::StringSet<> *LinkerDirectives = nullptr);
291294

292295
/// Given an already created LLVM module, construct a pass pipeline and run
293296
/// the Swift LLVM Pipeline upon it. This does not cause the module to be

include/swift/TBDGen/TBDGen.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ struct TBDGenOptions {
3333
/// Whether this compilation is producing a TBD for InstallAPI.
3434
bool IsInstallAPI;
3535

36+
/// Only collect linker directive symbols.
37+
bool LinkerDirectivesOnly = false;
38+
3639
/// The install_name to use in the TBD file.
3740
std::string InstallName;
3841

lib/AST/Attr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,10 @@ bool AvailableAttr::isActivePlatform(const ASTContext &ctx) const {
12441244
return isPlatformActive(Platform, ctx.LangOpts);
12451245
}
12461246

1247+
bool OriginallyDefinedInAttr::isActivePlatform(const ASTContext &ctx) const {
1248+
return isPlatformActive(Platform, ctx.LangOpts);
1249+
}
1250+
12471251
bool AvailableAttr::isLanguageVersionSpecific() const {
12481252
if (PlatformAgnostic ==
12491253
PlatformAgnosticAvailabilityKind::SwiftVersionSpecific)

lib/AST/Decl.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,12 @@ Result->X = SM.getLocFromExternalSource(Locs->SourceFilePath, Locs->X.Line, \
577577
}
578578

579579
StringRef Decl::getAlternateModuleName() const {
580-
if (auto *OD = Attrs.getAttribute(DeclAttrKind::DAK_OriginallyDefinedIn)) {
581-
return static_cast<const OriginallyDefinedInAttr*>(OD)->OriginalModuleName;
580+
for (auto *Att: Attrs) {
581+
if (auto *OD = dyn_cast<OriginallyDefinedInAttr>(Att)) {
582+
if (OD->isActivePlatform(getASTContext())) {
583+
return OD->OriginalModuleName;
584+
}
585+
}
582586
}
583587
for (auto *DC = getDeclContext(); DC; DC = DC->getParent()) {
584588
if (auto decl = DC->getAsDecl()) {
@@ -3535,7 +3539,11 @@ bool NominalTypeDecl::isResilient(ModuleDecl *M,
35353539
case ResilienceExpansion::Minimal:
35363540
return isResilient();
35373541
case ResilienceExpansion::Maximal:
3538-
return M != getModuleContext() && isResilient();
3542+
// We consider this decl belongs to the module either it's currently
3543+
// defined in this module or it's originally defined in this module, which
3544+
// is specified by @_originallyDefinedIn
3545+
return M != getModuleContext() &&
3546+
M->getName().str() != getAlternateModuleName() && isResilient();
35393547
}
35403548
llvm_unreachable("bad resilience expansion");
35413549
}

lib/Frontend/ArgsToFrontendOutputsConverter.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,12 @@ SupplementaryOutputPathsComputer::getSupplementaryOutputPathsFromArguments()
302302
options::OPT_emit_module_interface_path);
303303
auto moduleSourceInfoOutput = getSupplementaryFilenamesFromArguments(
304304
options::OPT_emit_module_source_info_path);
305-
305+
auto ldAddCFileOutput = getSupplementaryFilenamesFromArguments(
306+
options::OPT_emit_ldadd_cfile_path);
306307
if (!objCHeaderOutput || !moduleOutput || !moduleDocOutput ||
307308
!dependenciesFile || !referenceDependenciesFile ||
308309
!serializedDiagnostics || !fixItsOutput || !loadedModuleTrace || !TBD ||
309-
!moduleInterfaceOutput || !moduleSourceInfoOutput) {
310+
!moduleInterfaceOutput || !moduleSourceInfoOutput || !ldAddCFileOutput) {
310311
return None;
311312
}
312313
std::vector<SupplementaryOutputPaths> result;
@@ -328,6 +329,7 @@ SupplementaryOutputPathsComputer::getSupplementaryOutputPathsFromArguments()
328329
sop.TBDPath = (*TBD)[i];
329330
sop.ModuleInterfaceOutputPath = (*moduleInterfaceOutput)[i];
330331
sop.ModuleSourceInfoOutputPath = (*moduleSourceInfoOutput)[i];
332+
sop.LdAddCFilePath = (*ldAddCFileOutput)[i];
331333
result.push_back(sop);
332334
}
333335
return result;
@@ -446,6 +448,7 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(
446448
sop.TBDPath = tbdPath;
447449
sop.ModuleInterfaceOutputPath = ModuleInterfaceOutputPath;
448450
sop.ModuleSourceInfoOutputPath = moduleSourceInfoOutputPath;
451+
sop.LdAddCFilePath = pathsFromArguments.LdAddCFilePath;
449452
return sop;
450453
}
451454

@@ -546,7 +549,8 @@ SupplementaryOutputPathsComputer::readSupplementaryOutputFileMap() const {
546549
options::OPT_emit_loaded_module_trace_path,
547550
options::OPT_emit_module_interface_path,
548551
options::OPT_emit_module_source_info_path,
549-
options::OPT_emit_tbd_path)) {
552+
options::OPT_emit_tbd_path,
553+
options::OPT_emit_ldadd_cfile_path)) {
550554
Diags.diagnose(SourceLoc(),
551555
diag::error_cannot_have_supplementary_outputs,
552556
A->getSpelling(), "-supplementary-output-file-map");

0 commit comments

Comments
 (0)