Skip to content

Commit 2f34bdc

Browse files
committed
IRGen: Don't hoist metadata for weakly linked types
rdar://46438608
1 parent f092df3 commit 2f34bdc

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

lib/IRGen/AllocStackHoisting.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ static bool isHoistable(AllocStackInst *Inst, irgen::IRGenModule &Mod) {
5757
if (TI.isFixedSize())
5858
return false;
5959

60+
// Don't hoist weakly imported (weakly linked) types.
61+
auto *nominalDecl = SILTy.getASTType()->getNominalOrBoundGenericNominal();
62+
if (nominalDecl->isWeakImported(Mod.getSILModule().getSwiftModule()))
63+
return false;
64+
6065
// Don't hoist generics with opened archetypes. We would have to hoist the
6166
// open archetype instruction which might not be possible.
6267
return Inst->getTypeDependentOperands().empty();

test/IRGen/Inputs/weakly_linked.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public func getVersion() -> Int {
2+
// Used to return 0.
3+
return 1
4+
}
5+
6+
@_weakLinked public struct ResilientStruct {
7+
public init() {}
8+
9+
public func fn(_ x: Int) {}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -emit-module -enable-resilience -emit-module-path=%t/weak.swiftmodule -module-name=weak %S/Inputs/weakly_linked.swift
4+
// RUN: %target-swift-frontend -emit-ir %s -I %t | %FileCheck %s -DINT=i%target-ptrsize
5+
import weak
6+
7+
// We should not hoist the metadata accessor accross the version check.
8+
9+
// CHECK-LABEL: define{{.*}} void @"$s24resilience_weakly_linked015test_not_hoist_b1_C0yyF"()
10+
// CHECK-NOT: 15ResilientStructVMa
11+
// CHECK: getVersion
12+
// CHECK: 15ResilientStructVMa
13+
// CHECK: ret
14+
15+
public func test_not_hoist_weakly_linked() {
16+
if getVersion() == 1 {
17+
var s = ResilientStruct()
18+
s.fn(5)
19+
}
20+
}

validation-test/Evolution/test_backward_deploy_struct.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,19 @@ var BackwardDeployStructTest = TestSuite("BackwardDeployStruct")
99

1010
BackwardDeployStructTest.test("ResilientStruct") {
1111
if getVersion() == 1 {
12-
// FIXME: IRGen inserts the metadata load outside the version check
13-
// apparently. Work around that here. <rdar://problem/46438608>
14-
@inline(never) func helper() {
15-
var s = ResilientStruct()
12+
var s = ResilientStruct()
1613

17-
s.fn(s.storedProp)
18-
s.storedProp = 1
19-
s.storedProp += 1
20-
21-
s.fn(s.computedProp)
22-
s.computedProp = 1
23-
s.computedProp += 1
14+
s.fn(s.storedProp)
15+
s.storedProp = 1
16+
s.storedProp += 1
2417

25-
s.fn(s[0])
26-
s[0] = 1
27-
s[0] += 1
28-
}
18+
s.fn(s.computedProp)
19+
s.computedProp = 1
20+
s.computedProp += 1
2921

30-
helper()
22+
s.fn(s[0])
23+
s[0] = 1
24+
s[0] += 1
3125
}
3226
}
3327

0 commit comments

Comments
 (0)