Skip to content

ReadOnlyGlobalVariables: don't convert external global vars to lets #66021

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 1 commit into from
May 20, 2023
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 @@ -35,7 +35,8 @@ let readOnlyGlobalVariablesPass = ModulePass(name: "read-only-global-variables")
}

for g in moduleContext.globalVariables {
if !g.isPossiblyUsedExternally,
if !g.isAvailableExternally,
!g.isPossiblyUsedExternally,
!g.isLet,
!writtenGlobals.contains(g) {
g.setIsLet(to: true, moduleContext)
Expand Down
8 changes: 8 additions & 0 deletions SwiftCompilerSources/Sources/SIL/GlobalVariable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ final public class GlobalVariable : CustomStringConvertible, HasShortDescription
return bridged.isPossiblyUsedExternally()
}

/// True, if the linkage of the global indicates that it has a definition outside the
/// current compilation unit.
///
/// For example, `public_external` linkage.
public var isAvailableExternally: Bool {
return bridged.isAvailableExternally()
}

public var staticInitValue: SingleValueInstruction? {
bridged.getStaticInitializerValue().instruction as? SingleValueInstruction
}
Expand Down
4 changes: 4 additions & 0 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ struct BridgedGlobalVar {
return getGlobal()->isPossiblyUsedExternally();
}

bool isAvailableExternally() const {
return swift::isAvailableExternally(getGlobal()->getLinkage());
}

SWIFT_IMPORT_UNSAFE
inline OptionalBridgedInstruction getStaticInitializerValue() const;

Expand Down
10 changes: 4 additions & 6 deletions test/SILOptimizer/default-cmo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@
import Module
import ModuleTBD

// CHECK-LABEL: sil_global public_external [let] @$s6Module0A6StructV22privateFunctionPointeryS2icvpZ : $@callee_guaranteed (Int) -> Int{{$}}
// CHECK-LABEL: sil_global public_external @$s6Module0A6StructV22privateFunctionPointeryS2icvpZ : $@callee_guaranteed (Int) -> Int{{$}}

public func callPublicFunctionPointer(_ x: Int) -> Int {
return Module.ModuleStruct.publicFunctionPointer(x)
}

// CHECK-LABEL: sil @$s4Main25callPublicFunctionPointeryS2iF :
// CHECK-NOT: global_addr
// CHECK-NOT: apply
// CHECK: builtin "sadd
// CHECK-NOT: global_addr
// CHECK-NOT: apply
// CHECK: global_addr
// CHECK: load
// CHECK: apply
// CHECK: } // end sil function '$s4Main25callPublicFunctionPointeryS2iF'
public func callPrivateFunctionPointer(_ x: Int) -> Int {
return Module.ModuleStruct.privateFunctionPointer(x)
Expand Down
6 changes: 6 additions & 0 deletions test/SILOptimizer/read_only_global_vars.sil
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ sil_global @public_var : $Int32 = {
%initval = struct $Int32 (%0 : $Builtin.Int32)
}

// CHECK-LABEL: sil_global public_external @external_var : $Int32 = {
sil_global public_external @external_var : $Int32 = {
%0 = integer_literal $Builtin.Int32, 27
%initval = struct $Int32 (%0 : $Builtin.Int32)
}

sil @unknown_read_func : $@convention(thin) (@in Int32) -> ()

sil @read_var : $@convention(thin) (@inout Int32) -> Int32 {
Expand Down