Skip to content

Commit 831b098

Browse files
authored
Merge pull request #69779 from cyndyishida/Swift-tbdv5
[TBDGen] Capture segment symbols are defined in & emit tbd-v5 format
2 parents dcbfa98 + 3ccaaf5 commit 831b098

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+419
-228
lines changed

include/swift/IRGen/Linking.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,12 @@ class LinkEntity {
16121612
/// entity.
16131613
llvm::Type *getDefaultDeclarationType(IRGenModule &IGM) const;
16141614

1615+
/// Determine whether entity that represents a symbol is in TEXT segment.
1616+
bool isText() const;
1617+
1618+
/// Determine whether entity that represents a symbol is in DATA segment.
1619+
bool isData() const { return !isText(); }
1620+
16151621
bool isAlwaysSharedLinkage() const;
16161622
#undef LINKENTITY_GET_FIELD
16171623
#undef LINKENTITY_SET_FIELD

lib/IRGen/Linking.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,106 @@ Alignment LinkEntity::getAlignment(IRGenModule &IGM) const {
11841184
}
11851185
}
11861186

1187+
bool LinkEntity::isText() const {
1188+
switch (getKind()) {
1189+
case Kind::DispatchThunkAllocator:
1190+
case Kind::MethodDescriptor:
1191+
case Kind::MethodDescriptorDerivative:
1192+
case Kind::MethodDescriptorAllocator:
1193+
case Kind::MethodLookupFunction:
1194+
case Kind::EnumCase:
1195+
case Kind::FieldOffset:
1196+
case Kind::ClassMetadataBaseOffset:
1197+
case Kind::PropertyDescriptor:
1198+
case Kind::NominalTypeDescriptor:
1199+
case Kind::OpaqueTypeDescriptor:
1200+
case Kind::OpaqueTypeDescriptorAccessor:
1201+
case Kind::OpaqueTypeDescriptorAccessorImpl:
1202+
case Kind::OpaqueTypeDescriptorAccessorKey:
1203+
case Kind::AssociatedConformanceDescriptor:
1204+
case Kind::AssociatedTypeDescriptor:
1205+
case Kind::BaseConformanceDescriptor:
1206+
case Kind::DynamicallyReplaceableFunctionKeyAST:
1207+
case Kind::DynamicallyReplaceableFunctionImpl:
1208+
case Kind::DispatchThunk:
1209+
case Kind::ProtocolDescriptor:
1210+
case Kind::ProtocolRequirementsBaseDescriptor:
1211+
case Kind::ProtocolConformanceDescriptor:
1212+
case Kind::ProtocolConformanceDescriptorRecord:
1213+
case Kind::TypeMetadataAccessFunction:
1214+
return true;
1215+
case Kind::DispatchThunkAsyncFunctionPointer:
1216+
case Kind::DistributedThunkAsyncFunctionPointer:
1217+
case Kind::SwiftMetaclassStub:
1218+
case Kind::ObjCResilientClassStub:
1219+
case Kind::OpaqueTypeDescriptorAccessorVar:
1220+
case Kind::DynamicallyReplaceableFunctionVariableAST:
1221+
case Kind::AsyncFunctionPointerAST:
1222+
case Kind::ProtocolWitnessTable:
1223+
case Kind::TypeMetadata:
1224+
return false;
1225+
// The following cases do not currently generate linkable symbols
1226+
// through TBDGen. The full enumeration is captured to ensure
1227+
// that as more LinkEntity kind's are created their segement assignment
1228+
// will be known.
1229+
case Kind::ObjCMetadataUpdateFunction:
1230+
case Kind::NominalTypeDescriptorRecord:
1231+
case Kind::OpaqueTypeDescriptorRecord:
1232+
case Kind::SILFunction:
1233+
case Kind::PartialApplyForwarder:
1234+
case Kind::DistributedAccessor:
1235+
case Kind::DispatchThunkDerivative:
1236+
case Kind::DispatchThunkInitializer:
1237+
case Kind::MethodDescriptorInitializer:
1238+
case Kind::TypeMetadataPattern:
1239+
case Kind::TypeMetadataInstantiationCache:
1240+
case Kind::TypeMetadataInstantiationFunction:
1241+
case Kind::TypeMetadataSingletonInitializationCache:
1242+
case Kind::TypeMetadataCompletionFunction:
1243+
case Kind::ModuleDescriptor:
1244+
case Kind::DefaultAssociatedConformanceAccessor:
1245+
case Kind::CanonicalPrespecializedGenericTypeCachingOnceToken:
1246+
case Kind::DynamicallyReplaceableFunctionKey:
1247+
case Kind::ExtensionDescriptor:
1248+
case Kind::AnonymousDescriptor:
1249+
case Kind::SILGlobalVariable:
1250+
case Kind::ReadOnlyGlobalObject:
1251+
case Kind::ProtocolWitnessTablePattern:
1252+
case Kind::GenericProtocolWitnessTableInstantiationFunction:
1253+
case Kind::AssociatedTypeWitnessTableAccessFunction:
1254+
case Kind::ReflectionAssociatedTypeDescriptor:
1255+
case Kind::ProtocolWitnessTableLazyAccessFunction:
1256+
case Kind::DifferentiabilityWitness:
1257+
case Kind::ValueWitness:
1258+
case Kind::ValueWitnessTable:
1259+
case Kind::TypeMetadataLazyCacheVariable:
1260+
case Kind::TypeMetadataDemanglingCacheVariable:
1261+
case Kind::ReflectionBuiltinDescriptor:
1262+
case Kind::ReflectionFieldDescriptor:
1263+
case Kind::CoroutineContinuationPrototype:
1264+
case Kind::DynamicallyReplaceableFunctionVariable:
1265+
case Kind::CanonicalSpecializedGenericTypeMetadataAccessFunction:
1266+
case Kind::ExtendedExistentialTypeShape:
1267+
return true;
1268+
case Kind::ObjCClass:
1269+
case Kind::ObjCClassRef:
1270+
case Kind::ObjCMetaclass:
1271+
case Kind::AsyncFunctionPointer:
1272+
case Kind::PartialApplyForwarderAsyncFunctionPointer:
1273+
case Kind::KnownAsyncFunctionPointer:
1274+
case Kind::DispatchThunkInitializerAsyncFunctionPointer:
1275+
case Kind::DispatchThunkAllocatorAsyncFunctionPointer:
1276+
case Kind::NoncanonicalSpecializedGenericTypeMetadataCacheVariable:
1277+
case Kind::DistributedAccessorAsyncPointer:
1278+
case Kind::ProtocolWitnessTableLazyCacheVariable:
1279+
case Kind::ProtocolDescriptorRecord:
1280+
case Kind::CanonicalSpecializedGenericSwiftMetaclassStub:
1281+
case Kind::NoncanonicalSpecializedGenericTypeMetadata:
1282+
case Kind::AccessibleFunctionRecord:
1283+
return false;
1284+
}
1285+
}
1286+
11871287
bool LinkEntity::isWeakImported(ModuleDecl *module) const {
11881288
switch (getKind()) {
11891289
case Kind::SILGlobalVariable:

lib/IRGen/TBDGen.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ using namespace swift::tbdgen;
6262
using namespace llvm::yaml;
6363
using StringSet = llvm::StringSet<>;
6464
using SymbolKind = llvm::MachO::SymbolKind;
65+
using SymbolFlags = llvm::MachO::SymbolFlags;
6566

6667
TBDGenVisitor::TBDGenVisitor(const TBDGenDescriptor &desc,
6768
APIRecorder &recorder)
6869
: TBDGenVisitor(desc.getTarget(), desc.getDataLayoutString(),
6970
desc.getParentModule(), desc.getOptions(), recorder) {}
7071

7172
void TBDGenVisitor::addSymbolInternal(StringRef name, SymbolKind kind,
72-
SymbolSource source) {
73+
SymbolSource source, SymbolFlags flags) {
7374
if (!source.isLinkerDirective() && Opts.LinkerDirectivesOnly)
7475
return;
7576

@@ -82,7 +83,7 @@ void TBDGenVisitor::addSymbolInternal(StringRef name, SymbolKind kind,
8283
}
8384
#endif
8485
recorder.addSymbol(name, kind, source,
85-
DeclStack.empty() ? nullptr : DeclStack.back());
86+
DeclStack.empty() ? nullptr : DeclStack.back(), flags);
8687
}
8788

8889
static std::vector<OriginallyDefinedInAttr::ActiveVersion>
@@ -341,7 +342,7 @@ void TBDGenVisitor::addLinkerDirectiveSymbolsLdPrevious(StringRef name,
341342
OS << Ver.Version.getMajor() << "." << getMinor(Ver.Version.getMinor()) << "$";
342343
OS << name << "$";
343344
addSymbolInternal(OS.str(), SymbolKind::GlobalSymbol,
344-
SymbolSource::forLinkerDirective());
345+
SymbolSource::forLinkerDirective(), SymbolFlags::Data);
345346
}
346347
}
347348

@@ -387,13 +388,13 @@ void TBDGenVisitor::addLinkerDirectiveSymbolsLdHide(StringRef name,
387388
llvm::raw_svector_ostream OS(Buffer);
388389
OS << "$ld$hide$os" << CurMaj << "." << CurMin << "$" << name;
389390
addSymbolInternal(OS.str(), SymbolKind::GlobalSymbol,
390-
SymbolSource::forLinkerDirective());
391+
SymbolSource::forLinkerDirective(), SymbolFlags::Data);
391392
}
392393
}
393394
}
394395

395396
void TBDGenVisitor::addSymbol(StringRef name, SymbolSource source,
396-
SymbolKind kind) {
397+
SymbolFlags flags, SymbolKind kind) {
397398
// The linker expects to see mangled symbol names in TBD files,
398399
// except when being passed objective c classes,
399400
// so make sure to mangle before inserting the symbol.
@@ -406,7 +407,7 @@ void TBDGenVisitor::addSymbol(StringRef name, SymbolSource source,
406407
llvm::Mangler::getNameWithPrefix(mangled, name, *DataLayout);
407408
}
408409

409-
addSymbolInternal(mangled, kind, source);
410+
addSymbolInternal(mangled, kind, source, flags);
410411
if (previousInstallNameMap) {
411412
addLinkerDirectiveSymbolsLdPrevious(mangled, kind);
412413
} else {
@@ -434,30 +435,33 @@ void TBDGenVisitor::didVisitDecl(Decl *D) {
434435
}
435436

436437
void TBDGenVisitor::addFunction(SILDeclRef declRef) {
437-
addSymbol(declRef.mangle(), SymbolSource::forSILDeclRef(declRef));
438+
addSymbol(declRef.mangle(), SymbolSource::forSILDeclRef(declRef),
439+
SymbolFlags::Text);
438440
}
439441

440442
void TBDGenVisitor::addFunction(StringRef name, SILDeclRef declRef) {
441-
addSymbol(name, SymbolSource::forSILDeclRef(declRef));
443+
addSymbol(name, SymbolSource::forSILDeclRef(declRef), SymbolFlags::Text);
442444
}
443445

444446
void TBDGenVisitor::addGlobalVar(VarDecl *VD) {
445447
Mangle::ASTMangler mangler;
446-
addSymbol(mangler.mangleEntity(VD), SymbolSource::forGlobal(VD));
448+
addSymbol(mangler.mangleEntity(VD), SymbolSource::forGlobal(VD),
449+
SymbolFlags::Data);
447450
}
448451

449452
void TBDGenVisitor::addLinkEntity(LinkEntity entity) {
450453
auto linkage =
451454
LinkInfo::get(UniversalLinkInfo, SwiftModule, entity, ForDefinition);
452455

453-
addSymbol(linkage.getName(), SymbolSource::forIRLinkEntity(entity));
456+
SymbolFlags flags = entity.isData() ? SymbolFlags::Data : SymbolFlags::Text;
457+
addSymbol(linkage.getName(), SymbolSource::forIRLinkEntity(entity), flags);
454458
}
455459

456460
void TBDGenVisitor::addObjCInterface(ClassDecl *CD) {
457461
// FIXME: We ought to have a symbol source for this.
458462
SmallString<128> buffer;
459463
addSymbol(CD->getObjCRuntimeName(buffer), SymbolSource::forUnknown(),
460-
SymbolKind::ObjectiveCClass);
464+
SymbolFlags::Data, SymbolKind::ObjectiveCClass);
461465
recorder.addObjCInterface(CD);
462466
}
463467

@@ -474,13 +478,14 @@ void TBDGenVisitor::addProtocolWitnessThunk(RootProtocolConformance *C,
474478

475479
std::string decorated = Mangler.mangleWitnessThunk(C, requirementDecl);
476480
// FIXME: We should have a SILDeclRef SymbolSource for this.
477-
addSymbol(decorated, SymbolSource::forUnknown());
481+
addSymbol(decorated, SymbolSource::forUnknown(), SymbolFlags::Text);
478482

479483
if (requirementDecl->isProtocolRequirement()) {
480484
ValueDecl *PWT = C->getWitness(requirementDecl).getDecl();
481485
if (const auto *AFD = dyn_cast<AbstractFunctionDecl>(PWT))
482486
if (AFD->hasAsync())
483-
addSymbol(decorated + "Tu", SymbolSource::forUnknown());
487+
addSymbol(decorated + "Tu", SymbolSource::forUnknown(),
488+
SymbolFlags::Text);
484489
}
485490
}
486491

@@ -489,7 +494,7 @@ void TBDGenVisitor::addFirstFileSymbols() {
489494
// FIXME: We ought to have a symbol source for this.
490495
SmallString<32> buf;
491496
addSymbol(irgen::encodeForceLoadSymbolName(buf, Opts.ModuleLinkName),
492-
SymbolSource::forUnknown());
497+
SymbolSource::forUnknown(), SymbolFlags::Data);
493498
}
494499
}
495500

@@ -588,7 +593,7 @@ TBDFile GenerateTBDRequest::evaluate(Evaluator &evaluator,
588593
auto &ctx = M->getASTContext();
589594

590595
llvm::MachO::InterfaceFile file;
591-
file.setFileType(llvm::MachO::FileType::TBD_V4);
596+
file.setFileType(llvm::MachO::FileType::TBD_V5);
592597
file.setApplicationExtensionSafe(isApplicationExtensionSafe(ctx.LangOpts));
593598
file.setInstallName(opts.InstallName);
594599
file.setTwoLevelNamespace();
@@ -615,7 +620,9 @@ TBDFile GenerateTBDRequest::evaluate(Evaluator &evaluator,
615620
}
616621

617622
auto addSymbol = [&](StringRef symbol, SymbolKind kind, SymbolSource source,
618-
Decl *decl) { file.addSymbol(kind, symbol, targets); };
623+
Decl *decl, SymbolFlags flags) {
624+
file.addSymbol(kind, symbol, targets, flags);
625+
};
619626
SimpleAPIRecorder recorder(addSymbol);
620627
TBDGenVisitor visitor(desc, recorder);
621628
visitor.visit(desc);
@@ -627,7 +634,7 @@ PublicSymbolsRequest::evaluate(Evaluator &evaluator,
627634
TBDGenDescriptor desc) const {
628635
std::vector<std::string> symbols;
629636
auto addSymbol = [&](StringRef symbol, SymbolKind kind, SymbolSource source,
630-
Decl *decl) {
637+
Decl *decl, SymbolFlags flags) {
631638
if (kind == SymbolKind::GlobalSymbol)
632639
symbols.push_back(symbol.str());
633640
// TextAPI ObjC Class Kinds represents two symbols.
@@ -652,7 +659,7 @@ void swift::writeTBDFile(ModuleDecl *M, llvm::raw_ostream &os,
652659
auto desc = TBDGenDescriptor::forModule(M, opts);
653660
auto file = llvm::cantFail(evaluator(GenerateTBDRequest{desc}));
654661
llvm::cantFail(llvm::MachO::TextAPIWriter::writeToStream(os, file),
655-
"YAML writing should be error-free");
662+
"TBD writing should be error-free");
656663
}
657664

658665
class APIGenRecorder final : public APIRecorder {
@@ -675,7 +682,7 @@ class APIGenRecorder final : public APIRecorder {
675682
~APIGenRecorder() {}
676683

677684
void addSymbol(StringRef symbol, SymbolKind kind, SymbolSource source,
678-
Decl *decl) override {
685+
Decl *decl, SymbolFlags flags) override {
679686
if (kind != SymbolKind::GlobalSymbol)
680687
return;
681688

@@ -879,7 +886,7 @@ SymbolSourceMapRequest::evaluate(Evaluator &evaluator,
879886
auto *SymbolSources = Ctx.Allocate<SymbolSourceMap>();
880887

881888
auto addSymbol = [=](StringRef symbol, SymbolKind kind, SymbolSource source,
882-
Decl *decl) {
889+
Decl *decl, SymbolFlags flags) {
883890
SymbolSources->insert({symbol, source});
884891
};
885892

lib/IRGen/TBDGenVisitor.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,25 @@ class APIRecorder {
6262
virtual ~APIRecorder() {}
6363

6464
virtual void addSymbol(StringRef name, llvm::MachO::SymbolKind kind,
65-
SymbolSource source, Decl *decl) {}
65+
SymbolSource source, Decl *decl,
66+
llvm::MachO::SymbolFlags flags) {}
6667
virtual void addObjCInterface(const ClassDecl *decl) {}
6768
virtual void addObjCCategory(const ExtensionDecl *decl) {}
6869
virtual void addObjCMethod(const GenericContext *ctx, SILDeclRef method) {}
6970
};
7071

7172
class SimpleAPIRecorder final : public APIRecorder {
7273
public:
73-
using SymbolCallbackFn = llvm::function_ref<void(
74-
StringRef, llvm::MachO::SymbolKind, SymbolSource, Decl *)>;
74+
using SymbolCallbackFn =
75+
llvm::function_ref<void(StringRef, llvm::MachO::SymbolKind, SymbolSource,
76+
Decl *, llvm::MachO::SymbolFlags)>;
7577

7678
SimpleAPIRecorder(SymbolCallbackFn func) : func(func) {}
7779

7880
void addSymbol(StringRef symbol, llvm::MachO::SymbolKind kind,
79-
SymbolSource source, Decl *decl) override {
80-
func(symbol, kind, source, decl);
81+
SymbolSource source, Decl *decl,
82+
llvm::MachO::SymbolFlags flags) override {
83+
func(symbol, kind, source, decl, flags);
8184
}
8285

8386
private:
@@ -99,17 +102,18 @@ class TBDGenVisitor : public IRSymbolVisitor {
99102
APIRecorder &recorder;
100103

101104
using SymbolKind = llvm::MachO::SymbolKind;
105+
using SymbolFlags = llvm::MachO::SymbolFlags;
102106

103107
std::vector<Decl*> DeclStack;
104108
std::unique_ptr<std::map<std::string, InstallNameStore>>
105109
previousInstallNameMap;
106110
std::unique_ptr<std::map<std::string, InstallNameStore>>
107111
parsePreviousModuleInstallNameMap();
108-
void addSymbolInternal(StringRef name, llvm::MachO::SymbolKind kind,
109-
SymbolSource source);
110-
void addLinkerDirectiveSymbolsLdHide(StringRef name, llvm::MachO::SymbolKind kind);
111-
void addLinkerDirectiveSymbolsLdPrevious(StringRef name, llvm::MachO::SymbolKind kind);
112-
void addSymbol(StringRef name, SymbolSource source,
112+
void addSymbolInternal(StringRef name, SymbolKind kind, SymbolSource source,
113+
SymbolFlags);
114+
void addLinkerDirectiveSymbolsLdHide(StringRef name, SymbolKind kind);
115+
void addLinkerDirectiveSymbolsLdPrevious(StringRef name, SymbolKind kind);
116+
void addSymbol(StringRef name, SymbolSource source, SymbolFlags flags,
113117
SymbolKind kind = SymbolKind::GlobalSymbol);
114118

115119
void addSymbol(LinkEntity entity);

test/IRGen/virtual-function-elimination-two-modules.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME: Disabled due to https://github.com/apple/swift/issues/69485
2+
// REQUIRES: issue_69485
3+
14
// Tests that under -enable-llvm-vfe + -internalize-at-link, cross-module
25
// virtual calls are done via thunks and LLVM GlobalDCE is able to remove unused
36
// virtual methods from a library based on a list of used symbols by a client.

test/IRGen/witness-method-elimination-two-modules.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME: Disabled due to https://github.com/apple/swift/issues/69485
2+
// REQUIRES: issue_69485
3+
14
// Tests that under -enable-llvm-wme + -internalize-at-link, cross-module
25
// witness method calls are done via thunks and LLVM GlobalDCE is able to remove
36
// unused witness methods from a library based on a list of used symbols by a

test/SILOptimizer/default-cmo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// RUN: %target-build-swift -O -wmo -Xfrontend -enable-default-cmo -parse-as-library -emit-module -emit-module-path=%t/Submodule.swiftmodule -module-name=Submodule %S/Inputs/cross-module/default-submodule.swift -c -o %t/submodule.o
55
// RUN: %target-build-swift -O -wmo -Xfrontend -enable-default-cmo -parse-as-library -emit-module -emit-module-path=%t/Module.swiftmodule -module-name=Module -I%t -I%S/Inputs/cross-module %S/Inputs/cross-module/default-module.swift -c -o %t/module.o
6-
// RUN: %target-build-swift -O -wmo -Xfrontend -enable-default-cmo -parse-as-library -emit-tbd -emit-tbd-path %t/ModuleTBD.tbd -emit-module -emit-module-path=%t/ModuleTBD.swiftmodule -module-name=ModuleTBD -I%t -I%S/Inputs/cross-module %S/Inputs/cross-module/default-module.swift -c -o %t/moduletbd.o
6+
// RUN: %target-build-swift -O -wmo -Xfrontend -enable-default-cmo -parse-as-library -emit-tbd -emit-tbd-path %t/ModuleTBD.tbd -emit-module -emit-module-path=%t/ModuleTBD.swiftmodule -module-name=ModuleTBD -I%t -I%S/Inputs/cross-module %S/Inputs/cross-module/default-module.swift -c -o %t/moduletbd.o -Xfrontend -tbd-install_name -Xfrontend module
77

88
// RUN: %target-build-swift -O -wmo -module-name=Main -I%t -I%S/Inputs/cross-module %s -emit-sil | %FileCheck %s
99

test/SPI/spi_symbols.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// REQUIRES: VENDOR=apple
22
// RUN: %empty-directory(%t)
33

4-
// RUN: %target-swift-frontend %S/Inputs/spi_helper.swift -emit-ir -o %t/spi_helper.ll -emit-tbd-path %t/spi_helper.tbd -module-name spi_helper
4+
// RUN: %target-swift-frontend %S/Inputs/spi_helper.swift -emit-ir -o %t/spi_helper.ll -emit-tbd-path %t/spi_helper.tbd -module-name spi_helper -tbd-install_name spi_helper
55

66
// RUN: cat %t/spi_helper.ll | %FileCheck -check-prefix=CHECK-IR %s
7-
// RUN: cat %t/spi_helper.tbd | %FileCheck -check-prefix=CHECK-TBD %s
7+
// RUN: %llvm-nm %t/spi_helper.tbd | %FileCheck -check-prefix=CHECK-TBD %s
88

99
// Look for the SPI symbols in the IR
1010
// CHECK-IR: define swiftcc void @"$s10spi_helper0A4FuncyyF"

0 commit comments

Comments
 (0)