Skip to content

[cast-opt] Change one pathway through the cast-optimizer to use highe… #24895

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
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
10 changes: 5 additions & 5 deletions lib/SILOptimizer/Utils/CastOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ convertObjectToLoadableBridgeableType(SILBuilderWithScope &builder,
bool isConditional = dynamicCast.isConditional();

SILValue load =
builder.createLoad(loc, src, LoadOwnershipQualifier::Unqualified);
builder.emitLoadValueOperation(loc, src, LoadOwnershipQualifier::Take);

SILType silBridgedTy = *dynamicCast.getLoweredBridgedTargetObjectType();

Expand Down Expand Up @@ -295,25 +295,25 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
}

// Emit a retain.
Builder.createRetainValue(Loc, srcOp, Builder.getDefaultAtomicity());
SILValue srcArg = Builder.emitCopyValueOperation(Loc, srcOp);

SmallVector<SILValue, 1> Args;
Args.push_back(InOutOptionalParam);
Args.push_back(srcOp);
Args.push_back(srcArg);
Args.push_back(MetaTyVal);

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

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

// If we have an unconditional_checked_cast_addr, return early. We do not need
// to handle any conditional code.
if (isa<UnconditionalCheckedCastAddrInst>(Inst)) {
// Destroy the source value as unconditional_checked_cast_addr would.
Builder.createReleaseValue(Loc, srcOp, Builder.getDefaultAtomicity());
Builder.emitDestroyValueOperation(Loc, srcOp);
eraseInstAction(Inst);
return (newI) ? newI : AI;
}
Expand Down
38 changes: 38 additions & 0 deletions test/SILOptimizer/constant_propagation_ownership_objc.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// RUN: %target-sil-opt -enable-sil-verify-all %s -diagnostic-constant-propagation | %FileCheck %s
// RUN: %target-sil-opt -enable-sil-verify-all %s -performance-constant-propagation | %FileCheck %s

// REQUIRES: objc_interop

sil_stage canonical

import Swift
import Foundation
import Builtin

sil @$ss11AnyHashableVyABxcSHRzlufC : $@convention(method) <τ_0_0 where τ_0_0 : Hashable> (@in τ_0_0, @thin AnyHashable.Type) -> @out AnyHashable

// CHECK-LABEL: sil [ossa] @bridged_cast_anyhashable : $@convention(method) (@guaranteed NSArray) -> @out Optional<AnyHashable> {
// CHECK: [[FUNC:%.*]] = function_ref @$ss37_forceBridgeFromObjectiveC_bridgeableyx01_D5CTypeQz_xmts01_D11CBridgeableRzlF :
// CHECK: apply [[FUNC]]<Array<AnyHashable>>(
// CHECK: } // end sil function 'bridged_cast_anyhashable'
sil [ossa] @bridged_cast_anyhashable : $@convention(method) (@guaranteed NSArray) -> @out Optional<AnyHashable> {
bb0(%0 : $*Optional<AnyHashable>, %1 : @guaranteed $NSArray):
%3 = init_enum_data_addr %0 : $*Optional<AnyHashable>, #Optional.some!enumelt.1
%4 = metatype $@thin AnyHashable.Type
%5 = alloc_stack $NSArray
%6 = copy_value %1 : $NSArray
store %6 to [init] %5 : $*NSArray
%8 = alloc_stack $Array<AnyHashable>
unconditional_checked_cast_addr NSArray in %5 : $*NSArray to Array<AnyHashable> in %8 : $*Array<AnyHashable>
%10 = load [take] %8 : $*Array<AnyHashable>
%11 = alloc_stack $Array<AnyHashable>
store %10 to [init] %11 : $*Array<AnyHashable>
%13 = function_ref @$ss11AnyHashableVyABxcSHRzlufC : $@convention(method) <τ_0_0 where τ_0_0 : Hashable> (@in τ_0_0, @thin AnyHashable.Type) -> @out AnyHashable
%14 = apply %13<[AnyHashable]>(%3, %11, %4) : $@convention(method) <τ_0_0 where τ_0_0 : Hashable> (@in τ_0_0, @thin AnyHashable.Type) -> @out AnyHashable
dealloc_stack %11 : $*Array<AnyHashable>
dealloc_stack %8 : $*Array<AnyHashable>
dealloc_stack %5 : $*NSArray
inject_enum_addr %0 : $*Optional<AnyHashable>, #Optional.some!enumelt.1
%19 = tuple ()
return %19 : $()
}