Skip to content

Commit 1d20b96

Browse files
authored
Merge pull request #74930 from eeckstein/fix-cmo-problems-6.0
[6.0] Fix two problems with `-cross-module-optimization`
2 parents 5682a1f + d4de1a0 commit 1d20b96

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ private extension Value {
355355
private extension Function {
356356
/// Analyzes the global initializer function and returns global it initializes (from `alloc_global` instruction).
357357
func getInitializedGlobal() -> GlobalVariable? {
358+
if !isDefinition {
359+
return nil
360+
}
358361
for inst in self.entryBlock.instructions {
359362
switch inst {
360363
case let agi as AllocGlobalInst:

lib/Serialization/DeserializeSIL.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3643,11 +3643,23 @@ SILGlobalVariable *SILDeserializer::readGlobalVar(StringRef Name) {
36433643
return nullptr;
36443644
}
36453645

3646+
VarDecl *globalDecl = nullptr;
3647+
if (dID) {
3648+
llvm::Expected<Decl *> d = MF->getDeclChecked(dID);
3649+
if (d) {
3650+
globalDecl = cast<VarDecl>(d.get());
3651+
} else {
3652+
// This can happen with cross-module-optimizations, if the linkage of a
3653+
// private global variable is changed to public.
3654+
consumeError(d.takeError());
3655+
}
3656+
}
3657+
36463658
auto Ty = MF->getType(TyID);
36473659
SILGlobalVariable *v = SILGlobalVariable::create(
36483660
SILMod, linkage.value(), SerializedKind_t(serializedKind),
36493661
Name.str(), getSILType(Ty, SILValueCategory::Object, nullptr),
3650-
std::nullopt, dID ? cast<VarDecl>(MF->getDecl(dID)) : nullptr);
3662+
std::nullopt, globalDecl);
36513663
v->setLet(IsLet);
36523664
globalVarOrOffset.set(v, true /*isFullyDeserialized*/);
36533665
v->setDeclaration(IsDeclaration);

test/SILOptimizer/Inputs/cross-module/cross-module.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ public func callCImplementationOnly<T>(_ t: T) -> Int {
287287

288288
public let globalLet = 529387
289289

290+
private var privateVar = Int.random(in: 0..<100)
291+
292+
public func getRandom() -> Int {
293+
return privateVar
294+
}
295+
290296
public struct StructWithClosure {
291297
public static let c = { (x: Int) -> Int in return x }
292298
}

test/SILOptimizer/cross-module-optimization.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ func testImplementationOnly() {
165165
// CHECK-SIL2: } // end sil function '$s4Main22testImplementationOnlyyyF'
166166
}
167167

168+
@inline(never)
169+
func testPrivateVar() {
170+
// CHECK-OUTPUT: {{[0-9]+}}
171+
print(getRandom())
172+
}
173+
168174
testNestedTypes()
169175
testClass()
170176
testError()
@@ -175,4 +181,5 @@ testKeypath()
175181
testMisc()
176182
testGlobal()
177183
testImplementationOnly()
184+
testPrivateVar()
178185

test/SILOptimizer/mandatory_performance_optimizations.sil

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ bb0:
167167
return %6 : $()
168168
}
169169

170+
// Check that we don't crash on global init-once declarations.
171+
172+
// CHECK-LABEL: sil [global_init_once_fn] [no_locks] @external_global_init_once : $@convention(c) () -> ()
173+
sil [global_init_once_fn] [no_locks] @external_global_init_once : $@convention(c) () -> ()
174+
170175
sil @yield_int_value : $@convention(thin) @yield_once () -> (@yields Int32) {
171176
bb0:
172177
%0 = integer_literal $Builtin.Int32, 10

0 commit comments

Comments
 (0)