Skip to content

Commit 24f74f2

Browse files
author
Joe Shajrawi
authored
Merge pull request #12587 from shajrawi/fix_silcombien_arch
Relax SILCombiner's archtype tracker: better existential_metatype support
2 parents 24be7ec + 8fdca7c commit 24f74f2

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -867,10 +867,15 @@ getConformanceAndConcreteType(ASTContext &Ctx,
867867
// opened existential type, so we must keep track of the original
868868
// defining instruction.
869869
if (ConcreteType->isOpenedExistential()) {
870-
assert(!InitExistential->getTypeDependentOperands().empty() &&
871-
"init_existential is supposed to have a typedef operand");
872-
ConcreteTypeDef = cast<SingleValueInstruction>(
873-
InitExistential->getTypeDependentOperands()[0].get());
870+
if (InitExistential->getTypeDependentOperands().empty()) {
871+
auto op = InitExistential->getOperand(0);
872+
assert(op->getType().hasOpenedExistential() &&
873+
"init_existential is supposed to have a typedef operand");
874+
ConcreteTypeDef = cast<SingleValueInstruction>(op);
875+
} else {
876+
ConcreteTypeDef = cast<SingleValueInstruction>(
877+
InitExistential->getTypeDependentOperands()[0].get());
878+
}
874879
}
875880

876881
return std::make_tuple(*ExactConformance, ConcreteType,

test/SILOptimizer/sil_combine.sil

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3392,3 +3392,40 @@ bb3(%16 : $Int32): // Preds: bb1 bb2
33923392
return %16 : $Int32 // id: %17
33933393
}
33943394

3395+
protocol Prot0 : class {
3396+
static func newWithConfig() throws -> Builtin.Int32
3397+
}
3398+
3399+
3400+
protocol Prot1 : Prot0 {
3401+
static func newWithConfig() throws -> Builtin.Int32
3402+
}
3403+
3404+
protocol Prot2 : Prot1 {
3405+
static func newWithConfig() throws -> Builtin.Int32
3406+
}
3407+
3408+
sil @getMetaType : $@convention(thin) () -> @thick Prot2.Type
3409+
3410+
// CHECK-LABEL: meta_existential
3411+
// CHECK:bb0:
3412+
// CHECK: [[APPLY:%.*]] = apply {{%.*}}() : $@convention(thin) () -> @thick Prot2.Type
3413+
// CHECK: [[OE:%.*]] = open_existential_metatype [[APPLY]] : $@thick Prot2.Type to $@thick (@opened("690DA5F6-B5EA-11E7-B144-685B3593C496") Prot1).Type
3414+
// CHECK: [[WM:%.*]] = witness_method $@opened("690DA5F6-B5EA-11E7-B144-685B3593C496") Prot1, #Prot1.newWithConfig!1 : <Self where Self : Prot1> (Self.Type) -> () throws -> Builtin.Int32, [[OE]] : $@thick (@opened("690DA5F6-B5EA-11E7-B144-685B3593C496") Prot1).Type : $@convention(witness_method) <τ_0_0 where τ_0_0 : Prot1> (@thick τ_0_0.Type) -> (Builtin.Int32, @error MyErrorType)
3415+
// CHECK: try_apply [[WM]]<@opened("690DA5F6-B5EA-11E7-B144-685B3593C496") Prot1>([[OE]]) : $@convention(witness_method) <τ_0_0 where τ_0_0 : Prot1> (@thick τ_0_0.Type) -> (Builtin.Int32, @error MyErrorType)
3416+
sil @meta_existential : $@convention(thin) () -> (Builtin.Int32, @error MyErrorType) {
3417+
bb0:
3418+
%fref = function_ref @getMetaType : $@convention(thin) () -> @thick Prot2.Type
3419+
%apply = apply %fref() : $@convention(thin) () -> @thick Prot2.Type
3420+
%open = open_existential_metatype %apply : $@thick Prot2.Type to $@thick (@opened("690DA5F6-B5EA-11E7-B144-685B3593C496") Prot1).Type
3421+
%1 = init_existential_metatype %open : $@thick (@opened("690DA5F6-B5EA-11E7-B144-685B3593C496") Prot1).Type, $@thick Prot1.Type
3422+
%2 = open_existential_metatype %1 : $@thick Prot1.Type to $@thick (@opened("92105EE0-DCB0-11E5-865D-C8E0EB309913") Prot1).Type
3423+
%3 = witness_method $@opened("92105EE0-DCB0-11E5-865D-C8E0EB309913") Prot1, #Prot1.newWithConfig!1, %2 : $@thick (@opened("92105EE0-DCB0-11E5-865D-C8E0EB309913") Prot1).Type : $@convention(witness_method) <τ_0_0 where τ_0_0 : Prot1> (@thick τ_0_0.Type) -> (Builtin.Int32, @error MyErrorType)
3424+
try_apply %3<@opened("92105EE0-DCB0-11E5-865D-C8E0EB309913") Prot1>(%2) : $@convention(witness_method) <τ_0_0 where τ_0_0 : Prot1> (@thick τ_0_0.Type) -> (Builtin.Int32, @error MyErrorType), normal bb1, error bb2
3425+
3426+
bb1(%4 : $Builtin.Int32):
3427+
return %4 : $Builtin.Int32
3428+
3429+
bb2(%5 : $MyErrorType):
3430+
throw %5 : $MyErrorType
3431+
}

0 commit comments

Comments
 (0)