Skip to content

Commit 902c821

Browse files
authored
Merge pull request #24895 from gottesmm/pr-cc3e82b30e1bd90538341f497c03daa8e7680288
[cast-opt] Change one pathway through the cast-optimizer to use highe…
2 parents e6c31d1 + 35c3a0f commit 902c821

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ convertObjectToLoadableBridgeableType(SILBuilderWithScope &builder,
9191
bool isConditional = dynamicCast.isConditional();
9292

9393
SILValue load =
94-
builder.createLoad(loc, src, LoadOwnershipQualifier::Unqualified);
94+
builder.emitLoadValueOperation(loc, src, LoadOwnershipQualifier::Take);
9595

9696
SILType silBridgedTy = *dynamicCast.getLoweredBridgedTargetObjectType();
9797

@@ -295,25 +295,25 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
295295
}
296296

297297
// Emit a retain.
298-
Builder.createRetainValue(Loc, srcOp, Builder.getDefaultAtomicity());
298+
SILValue srcArg = Builder.emitCopyValueOperation(Loc, srcOp);
299299

300300
SmallVector<SILValue, 1> Args;
301301
Args.push_back(InOutOptionalParam);
302-
Args.push_back(srcOp);
302+
Args.push_back(srcArg);
303303
Args.push_back(MetaTyVal);
304304

305305
auto *AI = Builder.createApply(Loc, funcRef, subMap, Args);
306306

307307
// If we have guaranteed normal arguments, insert the destroy.
308308
//
309309
// TODO: Is it safe to just eliminate the initial retain?
310-
Builder.createReleaseValue(Loc, srcOp, Builder.getDefaultAtomicity());
310+
Builder.emitDestroyValueOperation(Loc, srcArg);
311311

312312
// If we have an unconditional_checked_cast_addr, return early. We do not need
313313
// to handle any conditional code.
314314
if (isa<UnconditionalCheckedCastAddrInst>(Inst)) {
315315
// Destroy the source value as unconditional_checked_cast_addr would.
316-
Builder.createReleaseValue(Loc, srcOp, Builder.getDefaultAtomicity());
316+
Builder.emitDestroyValueOperation(Loc, srcOp);
317317
eraseInstAction(Inst);
318318
return (newI) ? newI : AI;
319319
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -diagnostic-constant-propagation | %FileCheck %s
2+
// RUN: %target-sil-opt -enable-sil-verify-all %s -performance-constant-propagation | %FileCheck %s
3+
4+
// REQUIRES: objc_interop
5+
6+
sil_stage canonical
7+
8+
import Swift
9+
import Foundation
10+
import Builtin
11+
12+
sil @$ss11AnyHashableVyABxcSHRzlufC : $@convention(method) <τ_0_0 where τ_0_0 : Hashable> (@in τ_0_0, @thin AnyHashable.Type) -> @out AnyHashable
13+
14+
// CHECK-LABEL: sil [ossa] @bridged_cast_anyhashable : $@convention(method) (@guaranteed NSArray) -> @out Optional<AnyHashable> {
15+
// CHECK: [[FUNC:%.*]] = function_ref @$ss37_forceBridgeFromObjectiveC_bridgeableyx01_D5CTypeQz_xmts01_D11CBridgeableRzlF :
16+
// CHECK: apply [[FUNC]]<Array<AnyHashable>>(
17+
// CHECK: } // end sil function 'bridged_cast_anyhashable'
18+
sil [ossa] @bridged_cast_anyhashable : $@convention(method) (@guaranteed NSArray) -> @out Optional<AnyHashable> {
19+
bb0(%0 : $*Optional<AnyHashable>, %1 : @guaranteed $NSArray):
20+
%3 = init_enum_data_addr %0 : $*Optional<AnyHashable>, #Optional.some!enumelt.1
21+
%4 = metatype $@thin AnyHashable.Type
22+
%5 = alloc_stack $NSArray
23+
%6 = copy_value %1 : $NSArray
24+
store %6 to [init] %5 : $*NSArray
25+
%8 = alloc_stack $Array<AnyHashable>
26+
unconditional_checked_cast_addr NSArray in %5 : $*NSArray to Array<AnyHashable> in %8 : $*Array<AnyHashable>
27+
%10 = load [take] %8 : $*Array<AnyHashable>
28+
%11 = alloc_stack $Array<AnyHashable>
29+
store %10 to [init] %11 : $*Array<AnyHashable>
30+
%13 = function_ref @$ss11AnyHashableVyABxcSHRzlufC : $@convention(method) <τ_0_0 where τ_0_0 : Hashable> (@in τ_0_0, @thin AnyHashable.Type) -> @out AnyHashable
31+
%14 = apply %13<[AnyHashable]>(%3, %11, %4) : $@convention(method) <τ_0_0 where τ_0_0 : Hashable> (@in τ_0_0, @thin AnyHashable.Type) -> @out AnyHashable
32+
dealloc_stack %11 : $*Array<AnyHashable>
33+
dealloc_stack %8 : $*Array<AnyHashable>
34+
dealloc_stack %5 : $*NSArray
35+
inject_enum_addr %0 : $*Optional<AnyHashable>, #Optional.some!enumelt.1
36+
%19 = tuple ()
37+
return %19 : $()
38+
}

0 commit comments

Comments
 (0)