Skip to content

Fix two problems with -cross-module-optimization #74893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ private extension Value {
private extension Function {
/// Analyzes the global initializer function and returns global it initializes (from `alloc_global` instruction).
func getInitializedGlobal() -> GlobalVariable? {
if !isDefinition {
return nil
}
for inst in self.entryBlock.instructions {
switch inst {
case let agi as AllocGlobalInst:
Expand Down
14 changes: 13 additions & 1 deletion lib/Serialization/DeserializeSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3651,11 +3651,23 @@ SILGlobalVariable *SILDeserializer::readGlobalVar(StringRef Name) {
return nullptr;
}

VarDecl *globalDecl = nullptr;
if (dID) {
llvm::Expected<Decl *> d = MF->getDeclChecked(dID);
if (d) {
globalDecl = cast<VarDecl>(d.get());
} else {
// This can happen with cross-module-optimizations, if the linkage of a
// private global variable is changed to public.
consumeError(d.takeError());
}
}

auto Ty = MF->getType(TyID);
SILGlobalVariable *v = SILGlobalVariable::create(
SILMod, linkage.value(), SerializedKind_t(serializedKind),
Name.str(), getSILType(Ty, SILValueCategory::Object, nullptr),
std::nullopt, dID ? cast<VarDecl>(MF->getDecl(dID)) : nullptr);
std::nullopt, globalDecl);
v->setLet(IsLet);
globalVarOrOffset.set(v, true /*isFullyDeserialized*/);
v->setDeclaration(IsDeclaration);
Expand Down
6 changes: 6 additions & 0 deletions test/SILOptimizer/Inputs/cross-module/cross-module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ public func callCImplementationOnly<T>(_ t: T) -> Int {

public let globalLet = 529387

private var privateVar = Int.random(in: 0..<100)

public func getRandom() -> Int {
return privateVar
}

public struct StructWithClosure {
public static let c = { (x: Int) -> Int in return x }
}
Expand Down
7 changes: 7 additions & 0 deletions test/SILOptimizer/cross-module-optimization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ func testImplementationOnly() {
// CHECK-SIL2: } // end sil function '$s4Main22testImplementationOnlyyyF'
}

@inline(never)
func testPrivateVar() {
// CHECK-OUTPUT: {{[0-9]+}}
print(getRandom())
}

testNestedTypes()
testClass()
testError()
Expand All @@ -175,4 +181,5 @@ testKeypath()
testMisc()
testGlobal()
testImplementationOnly()
testPrivateVar()

5 changes: 5 additions & 0 deletions test/SILOptimizer/mandatory_performance_optimizations.sil
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ bb0:
return %6 : $()
}

// Check that we don't crash on global init-once declarations.

// CHECK-LABEL: sil [global_init_once_fn] [no_locks] @external_global_init_once : $@convention(c) () -> ()
sil [global_init_once_fn] [no_locks] @external_global_init_once : $@convention(c) () -> ()

sil @yield_int_value : $@convention(thin) @yield_once () -> (@yields Int32) {
bb0:
%0 = integer_literal $Builtin.Int32, 10
Expand Down