Skip to content

Commit c4191de

Browse files
committed
Merge pull request #2857 from slavapestov/revert-public-fragile-hack
Revert public fragile hack
2 parents 4505d6e + 6b92d95 commit c4191de

16 files changed

+109
-57
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,11 +1189,34 @@ bool LinkEntity::isAvailableExternally(IRGenModule &IGM) const {
11891189
llvm_unreachable("bad link entity kind");
11901190
}
11911191

1192+
bool LinkEntity::isFragile(IRGenModule &IGM) const {
1193+
switch (getKind()) {
1194+
case Kind::SILFunction:
1195+
return getSILFunction()->isFragile();
1196+
1197+
case Kind::SILGlobalVariable:
1198+
return getSILGlobalVariable()->isFragile();
1199+
1200+
case Kind::DirectProtocolWitnessTable: {
1201+
if (auto wt = IGM.getSILModule().lookUpWitnessTable(
1202+
getProtocolConformance())) {
1203+
return wt->isFragile();
1204+
} else {
1205+
return false;
1206+
}
1207+
}
1208+
1209+
default:
1210+
break;
1211+
}
1212+
return false;
1213+
}
1214+
11921215

11931216
static std::pair<llvm::GlobalValue::LinkageTypes,
11941217
llvm::GlobalValue::VisibilityTypes>
11951218
getIRLinkage(IRGenModule &IGM,
1196-
SILLinkage linkage, ForDefinition_t isDefinition,
1219+
SILLinkage linkage, bool isFragile, ForDefinition_t isDefinition,
11971220
bool isWeakImported) {
11981221

11991222
#define RESULT(LINKAGE, VISIBILITY) \
@@ -1215,25 +1238,39 @@ llvm::GlobalValue::VISIBILITY##Visibility }
12151238
break;
12161239
}
12171240

1241+
if (isFragile) {
1242+
// Fragile functions/globals must be visible from outside, regardless of
1243+
// their accessibility. If a caller is also fragile and inlined into another
1244+
// module it must be able to access this (not-inlined) function/global.
1245+
switch (linkage) {
1246+
case SILLinkage::Hidden:
1247+
case SILLinkage::Private:
1248+
linkage = SILLinkage::Public;
1249+
break;
1250+
1251+
case SILLinkage::Public:
1252+
case SILLinkage::Shared:
1253+
case SILLinkage::HiddenExternal:
1254+
case SILLinkage::PrivateExternal:
1255+
case SILLinkage::PublicExternal:
1256+
case SILLinkage::SharedExternal:
1257+
break;
1258+
}
1259+
}
1260+
12181261
switch (linkage) {
12191262
case SILLinkage::Public:
12201263
return {llvm::GlobalValue::ExternalLinkage, PublicDefinitionVisibility};
1221-
12221264
case SILLinkage::Shared:
1223-
case SILLinkage::SharedExternal:
1224-
return RESULT(LinkOnceODR, Hidden);
1225-
1226-
case SILLinkage::Hidden:
1227-
return RESULT(External, Hidden);
1228-
1265+
case SILLinkage::SharedExternal: return RESULT(LinkOnceODR, Hidden);
1266+
case SILLinkage::Hidden: return RESULT(External, Hidden);
12291267
case SILLinkage::Private:
12301268
if (IGM.IRGen.hasMultipleIGMs()) {
12311269
// In case of multiple llvm modules (in multi-threaded compilation) all
12321270
// private decls must be visible from other files.
12331271
return RESULT(External, Hidden);
12341272
}
12351273
return RESULT(Internal, Default);
1236-
12371274
case SILLinkage::PublicExternal:
12381275
if (isDefinition) {
12391276
return RESULT(AvailableExternally, Default);
@@ -1242,12 +1279,15 @@ llvm::GlobalValue::VISIBILITY##Visibility }
12421279
if (isWeakImported)
12431280
return RESULT(ExternalWeak, Default);
12441281
return RESULT(External, Default);
1245-
12461282
case SILLinkage::HiddenExternal:
1247-
case SILLinkage::PrivateExternal:
1248-
if (isDefinition)
1249-
return RESULT(AvailableExternally, Hidden);
1250-
return RESULT(External, Hidden);
1283+
case SILLinkage::PrivateExternal: {
1284+
auto visibility = isFragile ? llvm::GlobalValue::DefaultVisibility
1285+
: llvm::GlobalValue::HiddenVisibility;
1286+
if (isDefinition) {
1287+
return {llvm::GlobalValue::AvailableExternallyLinkage, visibility};
1288+
}
1289+
return {llvm::GlobalValue::ExternalLinkage, visibility};
1290+
}
12511291
}
12521292
llvm_unreachable("bad SIL linkage");
12531293
}
@@ -1262,6 +1302,7 @@ static void updateLinkageForDefinition(IRGenModule &IGM,
12621302
auto linkage = getIRLinkage(
12631303
IGM,
12641304
entity.getLinkage(IGM, ForDefinition),
1305+
entity.isFragile(IGM),
12651306
ForDefinition,
12661307
entity.isWeakImported(IGM.getSwiftModule()));
12671308
global->setLinkage(linkage.first);
@@ -1287,6 +1328,7 @@ LinkInfo LinkInfo::get(IRGenModule &IGM, const LinkEntity &entity,
12871328

12881329
std::tie(result.Linkage, result.Visibility) =
12891330
getIRLinkage(IGM, entity.getLinkage(IGM, isDefinition),
1331+
entity.isFragile(IGM),
12901332
isDefinition,
12911333
entity.isWeakImported(IGM.getSwiftModule()));
12921334

lib/IRGen/Linking.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,11 @@ class LinkEntity {
506506
///
507507
bool isAvailableExternally(IRGenModule &IGM) const;
508508

509+
/// Returns true if this function or global variable may be inlined into
510+
/// another module.
511+
///
512+
bool isFragile(IRGenModule &IGM) const;
513+
509514
ValueDecl *getDecl() const {
510515
assert(isDeclKind(getKind()));
511516
return reinterpret_cast<ValueDecl*>(Pointer);

lib/SIL/SILDeclRef.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,8 @@ SILLinkage SILDeclRef::getLinkage(ForDefinition_t forDefinition) const {
332332
if (isa<ClangModuleUnit>(derivedFor->getModuleScopeContext()))
333333
return ClangLinkage;
334334
}
335-
336-
// If the module is being built with -sil-serialize-all, everything has
337-
// to have public linkage.
338-
if (moduleContext->getParentModule()->getResilienceStrategy()
339-
== ResilienceStrategy::Fragile) {
340-
return (forDefinition ? SILLinkage::Public : SILLinkage::PublicExternal);
341-
}
342-
343-
// Otherwise, linkage is determined by accessibility at the AST level.
335+
336+
// Otherwise, we have external linkage.
344337
switch (d->getEffectiveAccess()) {
345338
case Accessibility::Private:
346339
return (forDefinition ? SILLinkage::Private : SILLinkage::PrivateExternal);

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,9 +875,7 @@ SILFunction *SILGenModule::emitLazyGlobalInitializer(StringRef funcName,
875875
auto initSILType = getLoweredType(initType).castTo<SILFunctionType>();
876876

877877
auto *f =
878-
M.createFunction(makeModuleFragile
879-
? SILLinkage::Public
880-
: SILLinkage::Private,
878+
M.createFunction(SILLinkage::Private,
881879
funcName, initSILType, nullptr,
882880
SILLocation(binding), IsNotBare, IsNotTransparent,
883881
makeModuleFragile

lib/SILGen/SILGenGlobalVariable.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,7 @@ void SILGenModule::emitGlobalInitialization(PatternBindingDecl *pd,
224224

225225
// TODO: include the module in the onceToken's name mangling.
226226
// Then we can make it fragile.
227-
auto onceToken = SILGlobalVariable::create(M,
228-
makeModuleFragile
229-
? SILLinkage::Public
230-
: SILLinkage::Private,
227+
auto onceToken = SILGlobalVariable::create(M, SILLinkage::Private,
231228
makeModuleFragile,
232229
onceTokenBuffer, onceSILTy);
233230
onceToken->setDeclaration(false);

lib/SILGen/SILGenType.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ SILGenModule::emitVTableMethod(SILDeclRef derived, SILDeclRef base) {
9191
auto *derivedDecl = cast<AbstractFunctionDecl>(derived.getDecl());
9292
SILLocation loc(derivedDecl);
9393
auto thunk =
94-
M.createFunction(makeModuleFragile
95-
? SILLinkage::Public
96-
: SILLinkage::Private,
94+
M.createFunction(SILLinkage::Private,
9795
name, overrideInfo.SILFnType,
9896
derivedDecl->getGenericParams(), loc, IsBare,
9997
IsNotTransparent, IsNotFragile);
@@ -243,8 +241,9 @@ class SILGenVTable : public Lowering::ASTVisitor<SILGenVTable> {
243241
}
244242

245243
void visitConstructorDecl(ConstructorDecl *cd) {
246-
// Stub constructors don't get an entry.
247-
if (cd->hasStubImplementation())
244+
// Stub constructors don't get an entry, unless they were synthesized to
245+
// override a non-required designated initializer in the superclass.
246+
if (cd->hasStubImplementation() && !cd->getOverriddenDecl())
248247
return;
249248

250249
// Required constructors (or overrides thereof) have their allocating entry

lib/Sema/CodeSynthesis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2155,7 +2155,7 @@ swift::createDesignatedInitOverride(TypeChecker &tc,
21552155

21562156
// Wire up the overrides.
21572157
ctor->getAttrs().add(new (tc.Context) OverrideAttr(/*Implicit=*/true));
2158-
checkOverrides(tc, ctor);
2158+
ctor->setOverriddenDecl(superclassCtor);
21592159

21602160
if (kind == DesignatedInitKind::Stub) {
21612161
// Make this a stub implementation.

lib/Sema/TypeCheckDecl.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7547,7 +7547,10 @@ void TypeChecker::addImplicitConstructors(NominalTypeDecl *decl) {
75477547
}
75487548

75497549
auto superclassTy = classDecl->getSuperclass();
7550-
for (auto memberResult : lookupConstructors(classDecl, superclassTy)) {
7550+
auto ctors = lookupConstructors(classDecl, superclassTy,
7551+
NameLookupFlags::IgnoreAccessibility);
7552+
7553+
for (auto memberResult : ctors) {
75517554
auto member = memberResult.Decl;
75527555

75537556
// Skip unavailable superclass initializers.
@@ -7595,12 +7598,23 @@ void TypeChecker::addImplicitConstructors(NominalTypeDecl *decl) {
75957598
getInitializerParamType(superclassCtor)).second)
75967599
continue;
75977600

7601+
// If we're inheriting initializers, create an override delegating
7602+
// to 'super.init'. Otherwise, create a stub which traps at runtime.
7603+
auto kind = canInheritInitializers
7604+
? DesignatedInitKind::Chaining
7605+
: DesignatedInitKind::Stub;
7606+
7607+
// If the superclass initializer is not accessible from the derived
7608+
// class, we cannot chain to 'super.init' either -- create a stub.
7609+
if (!superclassCtor->isAccessibleFrom(classDecl)) {
7610+
assert(!superclassCtor->isRequired() &&
7611+
"required initializer less visible than the class?");
7612+
kind = DesignatedInitKind::Stub;
7613+
}
7614+
75987615
// We have a designated initializer. Create an override of it.
75997616
if (auto ctor = createDesignatedInitOverride(
7600-
*this, classDecl, superclassCtor,
7601-
canInheritInitializers
7602-
? DesignatedInitKind::Chaining
7603-
: DesignatedInitKind::Stub)) {
7617+
*this, classDecl, superclassCtor, kind)) {
76047618
classDecl->addMember(ctor);
76057619
}
76067620
}

test/IRGen/sil_linkage.sil

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
sil_stage canonical
44

55
// CHECK: define{{( protected)?}} void @public_fragile_function_test() {{.*}} {
6-
// CHECK: define hidden void @hidden_fragile_function_test() {{.*}} {
6+
// CHECK: define{{( protected)?}} void @hidden_fragile_function_test() {{.*}} {
77
// CHECK: define linkonce_odr hidden void @shared_fragile_function_test() {{.*}} {
8-
// CHECK: define internal void @private_fragile_function_test() {{.*}} {
8+
// CHECK: define{{( protected)?}} void @private_fragile_function_test() {{.*}} {
99
// CHECK: define linkonce_odr hidden void @public_external_fragile_function_def_test() {{.*}} {
10-
// CHECK: define available_externally hidden void @hidden_external_fragile_function_def_test() {{.*}} {
10+
// CHECK: define{{( protected)?}} available_externally void @hidden_external_fragile_function_def_test() {{.*}} {
1111
// CHECK: define linkonce_odr hidden void @shared_external_fragile_function_def_test() {{.*}} {
12-
// CHECK: define available_externally hidden void @private_external_fragile_function_def_test() {{.*}} {
12+
// CHECK: define{{( protected)?}} available_externally void @private_external_fragile_function_def_test() {{.*}} {
1313
// CHECK: define{{( protected)?}} void @public_resilient_function_test() {{.*}} {
1414
// CHECK: define hidden void @hidden_resilient_function_test() {{.*}} {
1515
// CHECK: define linkonce_odr hidden void @shared_resilient_function_test() {{.*}} {

test/SILGen/accessibility_vtables.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ class Sub : Base {
1212
}
1313
}
1414

15+
// CHECK-LABEL: sil hidden @_TFC21accessibility_vtables3SubcfT_S0_ : $@convention(method) (@owned Sub) -> @owned Sub
16+
// CHECK: bb0(%0 : $Sub):
17+
// CHECK: function_ref @_TFs26_unimplemented_initializerFT9classNameVs12StaticString8initNameS_4fileS_4lineSu6columnSu_T_
18+
1519
// CHECK-LABEL: sil_vtable Sub {
1620
// CHECK-NEXT: #Base.internalMethod!1: _TFC28accessibility_vtables_helper4Base14internalMethod
1721
// CHECK-NEXT: #Base.prop!getter.1: _TFC21accessibility_vtables3Subg4propSi // accessibility_vtables.Sub.prop.getter : Swift.Int
1822
// CHECK-NEXT: #Base.prop!setter.1: _TFC28accessibility_vtables_helper4Bases4propSi // accessibility_vtables_helper.Base.prop.setter : Swift.Int
1923
// CHECK-NEXT: #Base.prop!materializeForSet.1: _TFC28accessibility_vtables_helper4Basem4propSi // accessibility_vtables_helper.Base.prop.materializeForSet : Swift.Int
20-
// CHECK-NEXT: #Base.init!initializer.1: _TFC28accessibility_vtables_helper4Basec
24+
// CHECK-NEXT: #Base.init!initializer.1: _TFC21accessibility_vtables3SubcfT_S0_ // accessibility_vtables.Sub.init () -> accessibility_vtables.Sub
2125
// CHECK-NEXT: #Sub.internalMethod!1: _TFC21accessibility_vtables3Sub14internalMethod
2226
// CHECK-NEXT: #Sub.prop!setter.1: _TFC21accessibility_vtables3Subs4propSi // accessibility_vtables.Sub.prop.setter : Swift.Int
2327
// CHECK-NEXT: #Sub.prop!materializeForSet.1: _TFC21accessibility_vtables3Subm4propSi // accessibility_vtables.Sub.prop.materializeForSet : Swift.Int

test/SILGen/fragile_globals.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ var mygg = 29
1111

1212
// Check if we have three tokens: 2 from the imported modules, one from mygg.
1313

14-
// CHECK: sil_global private @globalinit_[[T1:.*]]_token0
15-
// CHECK: sil_global [fragile] @globalinit_[[T2:.*]]_token0
16-
// CHECK: sil_global [fragile] @globalinit_[[T3:.*]]_token0
14+
// CHECK: sil_global private{{.*}} @globalinit_[[T1:.*]]_token0
15+
// CHECK: sil_global private{{.*}} @globalinit_[[T2:.*]]_token0
16+
// CHECK: sil_global private{{.*}} @globalinit_[[T3:.*]]_token0
1717

1818
public func sum() -> Int {
1919
return mygg + get_gg_a() + get_gg_b()

test/SILOptimizer/dead_inlined_func.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %target-swift-frontend -O -g %s -emit-sil | FileCheck %s -check-prefix=CHECK-SIL
2-
// RUN: %target-swift-frontend -O -g %s -emit-ir | FileCheck %s -check-prefix=CHECK-IR
2+
// RUN: %target-swift-frontend -O -g %s -sil-serialize-all -emit-ir | FileCheck %s -check-prefix=CHECK-IR
33

44
// The dead inlined function should not be in the SIL
55
// CHECK-SIL-NOT: sil {{.*}}to_be_inlined

test/Serialization/global_init.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ var MyVar = 3
1919
// CHECK: let MyConst: Int
2020
// CHECK: var MyVar: Int
2121

22-
// CHECK-DAG: sil [fragile] [global_init] @_TF11global_initau7MyConstSi : $@convention(thin) () -> Builtin.RawPointer
23-
// CHECK-DAG: sil [fragile] [global_init] @_TF11global_initau5MyVarSi : $@convention(thin) () -> Builtin.RawPointer
22+
// CHECK-DAG: sil hidden [fragile] [global_init] @_TF11global_initau7MyConstSi : $@convention(thin) () -> Builtin.RawPointer
23+
// CHECK-DAG: sil hidden [fragile] [global_init] @_TF11global_initau5MyVarSi : $@convention(thin) () -> Builtin.RawPointer
2424

2525
func getGlobals() -> Int {
2626
return MyVar + MyConst

test/Serialization/serialize_attr.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ class CC<T : PP> {
5252
}
5353
}
5454

55-
// CHECK-DAG: sil [fragile] [_specialize <Int, Float>] @_TF14serialize_attr14specializeThisu0_rFTx1uq__T_ : $@convention(thin) <T, U> (@in T, @in U) -> () {
55+
// CHECK-DAG: sil hidden [fragile] [_specialize <Int, Float>] @_TF14serialize_attr14specializeThisu0_rFTx1uq__T_ : $@convention(thin) <T, U> (@in T, @in U) -> () {
5656

57-
// CHECK-DAG: sil [fragile] [noinline] [_specialize <RR, Float, SS, Int>] @_TFC14serialize_attr2CC3foouRd__S_2QQrfTqd__1gGVS_2GGx__Tqd__GS2_x__ : $@convention(method) <T where T : PP><U where U : QQ> (@in U, GG<T>, @guaranteed CC<T>) -> (@out U, GG<T>) {
57+
// CHECK-DAG: sil hidden [fragile] [noinline] [_specialize <RR, Float, SS, Int>] @_TFC14serialize_attr2CC3foouRd__S_2QQrfTqd__1gGVS_2GGx__Tqd__GS2_x__ : $@convention(method) <T where T : PP><U where U : QQ> (@in U, GG<T>, @guaranteed CC<T>) -> (@out U, GG<T>) {

test/sil-extract/load-serialized-sil.swift

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

2121

2222

23-
// CHECK-LABEL: sil [fragile] @_TFVs1X4testfT_T_ : $@convention(method) (X) -> ()
23+
// CHECK-LABEL: sil hidden [fragile] @_TFVs1X4testfT_T_ : $@convention(method) (X) -> ()
2424
// CHECK: bb0
2525
// CHECK-NEXT: function_ref
2626
// CHECK-NEXT: function_ref @unknown : $@convention(thin) () -> ()

test/sil-opt/sil-opt.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// CHECK: func unknown()
1919
// SIB-CHECK: func unknown()
2020

21-
// CHECK-LABEL: sil [fragile] @_TFVs1X4testfT_T_ : $@convention(method) (X) -> ()
21+
// CHECK-LABEL: sil hidden [fragile] @_TFVs1X4testfT_T_ : $@convention(method) (X) -> ()
2222
// CHECK: bb0
2323
// CHECK-NEXT: function_ref
2424
// CHECK-NEXT: function_ref @unknown : $@convention(thin) () -> ()
@@ -36,7 +36,7 @@
3636
// CHECK: sil @unknown : $@convention(thin) () -> ()
3737
// SIB-CHECK: sil @unknown : $@convention(thin) () -> ()
3838

39-
// CHECK: sil [fragile] @_TFVs1XCfT_S_ : $@convention(method) (@thin X.Type) -> X
39+
// CHECK: sil hidden [fragile] @_TFVs1XCfT_S_ : $@convention(method) (@thin X.Type) -> X
4040
// CHECK: bb0
4141
// CHECK-NEXT: struct $X ()
4242
// CHECK-NEXT: return

0 commit comments

Comments
 (0)