Skip to content

Commit 570c364

Browse files
authored
Merge pull request #66021 from eeckstein/fix-read-only-var-pass
ReadOnlyGlobalVariables: don't convert external global vars to lets
2 parents a06c1ac + 7bbf66a commit 570c364

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

SwiftCompilerSources/Sources/Optimizer/ModulePasses/ReadOnlyGlobalVariables.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ let readOnlyGlobalVariablesPass = ModulePass(name: "read-only-global-variables")
3535
}
3636

3737
for g in moduleContext.globalVariables {
38-
if !g.isPossiblyUsedExternally,
38+
if !g.isAvailableExternally,
39+
!g.isPossiblyUsedExternally,
3940
!g.isLet,
4041
!writtenGlobals.contains(g) {
4142
g.setIsLet(to: true, moduleContext)

SwiftCompilerSources/Sources/SIL/GlobalVariable.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ final public class GlobalVariable : CustomStringConvertible, HasShortDescription
3535
return bridged.isPossiblyUsedExternally()
3636
}
3737

38+
/// True, if the linkage of the global indicates that it has a definition outside the
39+
/// current compilation unit.
40+
///
41+
/// For example, `public_external` linkage.
42+
public var isAvailableExternally: Bool {
43+
return bridged.isAvailableExternally()
44+
}
45+
3846
public var staticInitValue: SingleValueInstruction? {
3947
bridged.getStaticInitializerValue().instruction as? SingleValueInstruction
4048
}

include/swift/SIL/SILBridging.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ struct BridgedGlobalVar {
341341
return getGlobal()->isPossiblyUsedExternally();
342342
}
343343

344+
bool isAvailableExternally() const {
345+
return swift::isAvailableExternally(getGlobal()->getLinkage());
346+
}
347+
344348
SWIFT_IMPORT_UNSAFE
345349
inline OptionalBridgedInstruction getStaticInitializerValue() const;
346350

test/SILOptimizer/default-cmo.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@
1212
import Module
1313
import ModuleTBD
1414

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

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

2121
// CHECK-LABEL: sil @$s4Main25callPublicFunctionPointeryS2iF :
22-
// CHECK-NOT: global_addr
23-
// CHECK-NOT: apply
24-
// CHECK: builtin "sadd
25-
// CHECK-NOT: global_addr
26-
// CHECK-NOT: apply
22+
// CHECK: global_addr
23+
// CHECK: load
24+
// CHECK: apply
2725
// CHECK: } // end sil function '$s4Main25callPublicFunctionPointeryS2iF'
2826
public func callPrivateFunctionPointer(_ x: Int) -> Int {
2927
return Module.ModuleStruct.privateFunctionPointer(x)

test/SILOptimizer/read_only_global_vars.sil

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ sil_global @public_var : $Int32 = {
2323
%initval = struct $Int32 (%0 : $Builtin.Int32)
2424
}
2525

26+
// CHECK-LABEL: sil_global public_external @external_var : $Int32 = {
27+
sil_global public_external @external_var : $Int32 = {
28+
%0 = integer_literal $Builtin.Int32, 27
29+
%initval = struct $Int32 (%0 : $Builtin.Int32)
30+
}
31+
2632
sil @unknown_read_func : $@convention(thin) (@in Int32) -> ()
2733

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

0 commit comments

Comments
 (0)