Skip to content

Commit 603e837

Browse files
committed
Swift optimizations: make isSwift51RuntimeAvailable sensitive to the resilience domain of the function.
1 parent 5cff292 commit 603e837

File tree

8 files changed

+33
-17
lines changed

8 files changed

+33
-17
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionPasses/SimplifyStrongRetainRelease.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ private func isNotReferenceCounted(value: Value, context: PassContext) -> Bool {
7676
return isNotReferenceCounted(value: uci.operand, context: context)
7777
case let urc as UncheckedRefCastInst:
7878
return isNotReferenceCounted(value: urc.operand, context: context)
79-
case is GlobalValueInst:
79+
case let gvi as GlobalValueInst:
8080
// Since Swift 5.1, statically allocated objects have "immortal" reference
8181
// counts. Therefore we can safely eliminate unbalaced retains and
8282
// releases, because they are no-ops on immortal objects.
8383
// Note that the `simplifyGlobalValuePass` pass is deleting balanced
8484
// retains/releases, which doesn't require a Swift 5.1 minimum deployment
8585
// targert.
86-
return context.isSwift51RuntimeAvailable
86+
return gvi.function.isSwift51RuntimeAvailable
8787
case let rptr as RawPointerToRefInst:
8888
// Like `global_value` but for the empty collection singletons from the
8989
// stdlib, e.g. the empty Array singleton.
90-
if context.isSwift51RuntimeAvailable {
90+
if rptr.function.isSwift51RuntimeAvailable {
9191
// The pattern generated for empty collection singletons is:
9292
// %0 = global_addr @_swiftEmptyArrayStorage
9393
// %1 = address_to_pointer %0

SwiftCompilerSources/Sources/Optimizer/PassManager/PassUtils.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ struct PassContext {
2222

2323
let _bridged: BridgedPassContext
2424

25-
var isSwift51RuntimeAvailable: Bool {
26-
PassContext_isSwift51RuntimeAvailable(_bridged) != 0
27-
}
28-
2925
var aliasAnalysis: AliasAnalysis {
3026
let bridgedAA = PassContext_getAliasAnalysis(_bridged)
3127
return AliasAnalysis(bridged: bridgedAA)

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ final public class Function : CustomStringConvertible, HasName {
5252
public var argumentTypes: ArgumentTypeArray { ArgumentTypeArray(function: self) }
5353
public var resultType: Type { SILFunction_getSILResultType(bridged).type }
5454

55+
/// True, if the function runs with a swift 5.1 runtime.
56+
/// Note that this is function specific, because inlinable functions are de-serialized
57+
/// in a client module, which might be compiled with a different deployment target.
58+
public var isSwift51RuntimeAvailable: Bool {
59+
SILFunction_isSwift51RuntimeAvailable(bridged) != 0
60+
}
61+
5562
// Only to be called by PassContext
5663
public func _modifyEffects(_ body: (inout FunctionEffects) -> ()) {
5764
body(&effects)

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ SwiftInt SILFunction_getSelfArgumentIndex(BridgedFunction function);
205205
SwiftInt SILFunction_getNumSILArguments(BridgedFunction function);
206206
BridgedType SILFunction_getSILArgumentType(BridgedFunction function, SwiftInt idx);
207207
BridgedType SILFunction_getSILResultType(BridgedFunction function);
208+
SwiftInt SILFunction_isSwift51RuntimeAvailable(BridgedFunction function);
208209

209210
BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global);
210211
BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global);

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ void SILPassManager_registerFunctionPass(BridgedStringRef name,
8686
void SILCombine_registerInstructionPass(BridgedStringRef name,
8787
BridgedInstructionPassRunFn runFn);
8888

89-
SwiftInt PassContext_isSwift51RuntimeAvailable(BridgedPassContext context);
90-
9189
BridgedAliasAnalysis PassContext_getAliasAnalysis(BridgedPassContext context);
9290

9391
BridgedMemoryBehavior AliasAnalysis_getMemBehavior(BridgedAliasAnalysis aa,

lib/SIL/Utils/SILBridging.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ BridgedType SILFunction_getSILResultType(BridgedFunction function) {
213213
return {resTy.getOpaqueValue()};
214214
}
215215

216+
SwiftInt SILFunction_isSwift51RuntimeAvailable(BridgedFunction function) {
217+
SILFunction *f = castToFunction(function);
218+
if (f->getResilienceExpansion() != ResilienceExpansion::Maximal)
219+
return 0;
220+
221+
ASTContext &ctxt = f->getModule().getASTContext();
222+
return AvailabilityContext::forDeploymentTarget(ctxt).isContainedIn(
223+
ctxt.getSwift51Availability());
224+
}
225+
216226
//===----------------------------------------------------------------------===//
217227
// SILBasicBlock
218228
//===----------------------------------------------------------------------===//

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,13 +1240,6 @@ void PassContext_fixStackNesting(BridgedPassContext passContext,
12401240
}
12411241
}
12421242

1243-
SwiftInt PassContext_isSwift51RuntimeAvailable(BridgedPassContext context) {
1244-
SILPassManager *pm = castToPassInvocation(context)->getPassManager();
1245-
ASTContext &ctxt = pm->getModule()->getASTContext();
1246-
return AvailabilityContext::forDeploymentTarget(ctxt).isContainedIn(
1247-
ctxt.getSwift51Availability());
1248-
}
1249-
12501243
BridgedAliasAnalysis PassContext_getAliasAnalysis(BridgedPassContext context) {
12511244
SwiftPassInvocation *invocation = castToPassInvocation(context);
12521245
SILPassManager *pm = invocation->getPassManager();

test/SILOptimizer/immortal-arc-elimination.sil

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ bb0:
2828
}
2929

3030

31-
sil_global private @staticArray : $_ContiguousArrayStorage<Int> = {
31+
sil_global @staticArray : $_ContiguousArrayStorage<Int> = {
3232
%0 = integer_literal $Builtin.Int64, 0
3333
%1 = struct $UInt (%0 : $Builtin.Int64)
3434
%2 = struct $Int (%0 : $Builtin.Int64)
@@ -49,4 +49,15 @@ bb0:
4949
return %1 : $Builtin.BridgeObject
5050
}
5151

52+
// CHECK-LABEL: sil [serialized] @testGlobalValueSerialized
53+
// CHECK: global_value
54+
// CHECK: retain
55+
// CHECK: } // end sil function 'testGlobalValueSerialized'
56+
sil [serialized] @testGlobalValueSerialized : $@convention(thin) () -> @owned Builtin.BridgeObject {
57+
bb0:
58+
%0 = global_value @staticArray : $_ContiguousArrayStorage<Int>
59+
%1 = unchecked_ref_cast %0 : $_ContiguousArrayStorage<Int> to $Builtin.BridgeObject
60+
strong_retain %1 : $Builtin.BridgeObject
61+
return %1 : $Builtin.BridgeObject
62+
}
5263

0 commit comments

Comments
 (0)