Skip to content

Commit c32df33

Browse files
committed
[sil-combine] Enable alloc_ref_dynamic -> alloc_ref canonicalization
This involves folding a metatype into the alloc_ref_dynamic and we always replace the alloc_ref_dynamic with an alloc_ref so this is always safe from an ownership perspective.
1 parent ddba98d commit c32df33

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,9 +2055,6 @@ SILInstruction *SILCombiner::visitFixLifetimeInst(FixLifetimeInst *fli) {
20552055
SILInstruction *
20562056
SILCombiner::
20572057
visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI) {
2058-
if (ARDI->getFunction()->hasOwnership())
2059-
return nullptr;
2060-
20612058
SmallVector<SILValue, 4> Counts;
20622059
auto getCounts = [&] (AllocRefDynamicInst *AI) -> ArrayRef<SILValue> {
20632060
for (Operand &Op : AI->getTailAllocatedCounts()) {
@@ -2073,8 +2070,8 @@ visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI) {
20732070
Builder.setCurrentDebugScope(ARDI->getDebugScope());
20742071

20752072
SILValue MDVal = ARDI->getMetatypeOperand();
2076-
if (auto *UC = dyn_cast<UpcastInst>(MDVal))
2077-
MDVal = UC->getOperand();
2073+
while (auto *UCI = dyn_cast<UpcastInst>(MDVal))
2074+
MDVal = UCI->getOperand();
20782075

20792076
SingleValueInstruction *NewInst = nullptr;
20802077
if (auto *MI = dyn_cast<MetatypeInst>(MDVal)) {

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,12 +2930,13 @@ bb0(%0 : $Builtin.Word, %1 : $Builtin.Word):
29302930
return %24 : $()
29312931
}
29322932

2933-
// CHECK-LABEL: sil [ossa] @alloc_ref_dynamic_with_metatype
2934-
// XHECK: bb0
2935-
// XHECK-NOT: alloc_ref_dynamic
2936-
// XHECK-NEXT: alloc_ref $B
2937-
// XHECK-NEXT: destroy_value
2938-
// XHECK: return
2933+
// CHECK-LABEL: sil [ossa] @alloc_ref_dynamic_with_metatype :
2934+
// CHECK: bb0
2935+
// CHECK-NOT: alloc_ref_dynamic
2936+
// CHECK-NEXT: alloc_ref $B
2937+
// CHECK-NEXT: destroy_value
2938+
// CHECK: return
2939+
// CHECK: } // end sil function 'alloc_ref_dynamic_with_metatype'
29392940
sil [ossa] @alloc_ref_dynamic_with_metatype : $() -> () {
29402941
%1 = metatype $@thick B.Type
29412942
%2 = alloc_ref_dynamic %1 : $@thick B.Type, $B
@@ -2944,11 +2945,12 @@ sil [ossa] @alloc_ref_dynamic_with_metatype : $() -> () {
29442945
return %4 : $()
29452946
}
29462947

2947-
// XHECK: sil [ossa] @alloc_ref_dynamic_with_metatype_genneric
2948-
// XHECK: metatype
2949-
// XHECK: upcast
2950-
// XHECK: alloc_ref_dynamic
2951-
// XHECK: return
2948+
// CHECK: sil [ossa] @alloc_ref_dynamic_with_metatype_genneric :
2949+
// CHECK: metatype
2950+
// CHECK: upcast
2951+
// CHECK: alloc_ref_dynamic
2952+
// CHECK: return
2953+
// CHECK: } // end sil function 'alloc_ref_dynamic_with_metatype_genneric'
29522954
sil [ossa] @alloc_ref_dynamic_with_metatype_genneric : $<T where T : B>() -> () {
29532955
%1 = metatype $@thick T.Type
29542956
%2 = upcast %1 : $@thick T.Type to $@thick B.Type
@@ -2958,12 +2960,13 @@ sil [ossa] @alloc_ref_dynamic_with_metatype_genneric : $<T where T : B>() -> ()
29582960
return %4 : $()
29592961
}
29602962

2961-
// CHECK-LABEL: sil [ossa] @alloc_ref_dynamic_with_upcast_metatype
2962-
// XHECK: bb0
2963-
// XHECK-NOT: alloc_ref_dynamic
2964-
// XHECK-NEXT: [[R:%[0-9]+]] = alloc_ref $E
2965-
// XHECK-NEXT: [[C:%[0-9]+]] = upcast [[R]] : $E to $B
2966-
// XHECK-NEXT: return [[C]]
2963+
// CHECK-LABEL: sil [ossa] @alloc_ref_dynamic_with_upcast_metatype :
2964+
// CHECK: bb0
2965+
// CHECK-NOT: alloc_ref_dynamic
2966+
// CHECK-NEXT: [[R:%[0-9]+]] = alloc_ref $E
2967+
// CHECK-NEXT: [[C:%[0-9]+]] = upcast [[R]] : $E to $B
2968+
// CHECK-NEXT: return [[C]]
2969+
// CHECK: } // end sil function 'alloc_ref_dynamic_with_upcast_metatype'
29672970
sil [ossa] @alloc_ref_dynamic_with_upcast_metatype : $@convention(thin) () -> @owned B {
29682971
%1 = metatype $@thick E.Type
29692972
%2 = upcast %1 : $@thick E.Type to $@thick B.Type
@@ -2972,10 +2975,11 @@ sil [ossa] @alloc_ref_dynamic_with_upcast_metatype : $@convention(thin) () -> @o
29722975
}
29732976

29742977
// CHECK-LABEL: @alloc_ref_dynamic_after_successful_checked_cast_br :
2975-
// XHECK: checked_cast_br
2976-
// XHECK: bb1
2977-
// XHECK-NOT: alloc_ref_dynamic
2978-
// XHECK: alloc_ref $B
2978+
// CHECK: checked_cast_br
2979+
// CHECK: bb1
2980+
// CHECK-NOT: alloc_ref_dynamic
2981+
// CHECK: alloc_ref $B
2982+
// CHECK: } // end sil function 'alloc_ref_dynamic_after_successful_checked_cast_br'
29792983
sil [ossa] @alloc_ref_dynamic_after_successful_checked_cast_br : $(@thick B.Type) -> Builtin.Int32 {
29802984
bb0(%1 : $@thick B.Type):
29812985
checked_cast_br [exact] %1 : $@thick B.Type to B.Type, bb1, bb2
@@ -2995,11 +2999,12 @@ bb3 (%10: $Builtin.Int32):
29952999
}
29963000

29973001
// CHECK-LABEL: @alloc_ref_dynamic_upcast_after_successful_checked_cast_br
2998-
// XHECK: checked_cast_br
2999-
// XHECK: bb1
3000-
// XHECK-NOT: alloc_ref_dynamic
3001-
// XHECK: [[R:%[0-9]+]] = alloc_ref $E
3002-
// XHECK-NEXT: destroy_value [[R]]
3002+
// CHECK: checked_cast_br
3003+
// CHECK: bb1
3004+
// CHECK-NOT: alloc_ref_dynamic
3005+
// CHECK: [[R:%[0-9]+]] = alloc_ref $E
3006+
// CHECK-NEXT: destroy_value [[R]]
3007+
// CHECK: } // end sil function 'alloc_ref_dynamic_upcast_after_successful_checked_cast_br'
30033008
sil [ossa] @alloc_ref_dynamic_upcast_after_successful_checked_cast_br : $(@thick B.Type) -> Builtin.Int32 {
30043009
bb0(%1 : $@thick B.Type):
30053010
checked_cast_br [exact] %1 : $@thick B.Type to E.Type, bb1, bb2
@@ -3261,9 +3266,10 @@ enum ErrorEnum: Error {
32613266
// the behavior specified.
32623267
//
32633268
// CHECK-LABEL: sil [ossa] @insert_compensating_release_at_release_of_box :
3264-
// XHECK: bb0(
3265-
// XHECK-NEXT: tuple
3266-
// XHECK-NEXT: return
3269+
// CHECK: bb0(
3270+
// CHECK-NEXT: tuple
3271+
// CHECK-NEXT: return
3272+
// CHECK: } // end sil function 'insert_compensating_release_at_release_of_box'
32673273
sil [ossa] @insert_compensating_release_at_release_of_box : $@convention(method) (@in_guaranteed Array<Error>) -> () {
32683274
bb0(%0 : $*Array<Error>):
32693275
%20 = load [copy] %0 : $*Array<Error>
@@ -3278,9 +3284,10 @@ bb0(%0 : $*Array<Error>):
32783284
}
32793285

32803286
// CHECK-LABEL: sil [ossa] @insert_compensating_release_at_release_of_box_2 :
3281-
// XHECK: bb0(
3282-
// XHECK-NEXT: tuple
3283-
// XHECK-NEXT: return
3287+
// CHECK: bb0(
3288+
// CHECK-NEXT: tuple
3289+
// CHECK-NEXT: return
3290+
// CHECK: } // end sil function 'insert_compensating_release_at_release_of_box_2'
32843291
sil [ossa] @insert_compensating_release_at_release_of_box_2 : $@convention(method) (@in_guaranteed Array<Error>) -> () {
32853292
bb0(%0 : $*Array<Error>):
32863293
%20 = load_borrow %0 : $*Array<Error>

0 commit comments

Comments
 (0)