Skip to content

[5.9] SILCombine: correctly set the [stack] flag when replacing alloc_ref_dynamic with alloc_ref #66816

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
merged 1 commit into from
Jun 22, 2023
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
15 changes: 12 additions & 3 deletions lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2145,8 +2145,13 @@ visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI) {
Builder.setCurrentDebugScope(ARDI->getDebugScope());

SILValue MDVal = ARDI->getMetatypeOperand();
while (auto *UCI = dyn_cast<UpcastInst>(MDVal))
while (auto *UCI = dyn_cast<UpcastInst>(MDVal)) {
// For simplicity ignore a cast of an `alloc_ref [stack]`. It would need more
// work to keep its `dealloc_stack_ref` correct.
if (ARDI->canAllocOnStack())
return nullptr;
MDVal = UCI->getOperand();
}

SingleValueInstruction *NewInst = nullptr;
if (auto *MI = dyn_cast<MetatypeInst>(MDVal)) {
Expand All @@ -2159,7 +2164,7 @@ visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI) {
return nullptr;

NewInst = Builder.createAllocRef(ARDI->getLoc(), SILInstanceTy,
ARDI->isObjC(), false,
ARDI->isObjC(), ARDI->canAllocOnStack(),
ARDI->getTailAllocatedTypes(),
getCounts(ARDI));

Expand All @@ -2184,11 +2189,14 @@ visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI) {
if (!SILInstanceTy.getClassOrBoundGenericClass())
return nullptr;
NewInst = Builder.createAllocRef(ARDI->getLoc(), SILInstanceTy,
ARDI->isObjC(), false,
ARDI->isObjC(), ARDI->canAllocOnStack(),
ARDI->getTailAllocatedTypes(),
getCounts(ARDI));
}
} else if (auto *AI = dyn_cast<ApplyInst>(MDVal)) {
if (ARDI->canAllocOnStack())
return nullptr;

SILFunction *SF = AI->getReferencedFunctionOrNull();
if (!SF)
return nullptr;
Expand Down Expand Up @@ -2217,6 +2225,7 @@ visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI) {
if (NewInst && NewInst->getType() != ARDI->getType()) {
// In case the argument was an upcast of the metatype, we have to upcast the
// resulting reference.
assert(!ARDI->canAllocOnStack() && "upcasting alloc_ref [stack] not supported");
NewInst = Builder.createUpcast(ARDI->getLoc(), NewInst, ARDI->getType());
}
return NewInst;
Expand Down
55 changes: 55 additions & 0 deletions test/SILOptimizer/sil_combine_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -3149,6 +3149,61 @@ bb3 (%10: $Builtin.Int32):
return %10 : $Builtin.Int32
}

// CHECK-LABEL: sil [ossa] @alloc_ref_dynamic_stack_with_metatype :
// CHECK: bb0:
// CHECK-NOT: alloc_ref_dynamic
// CHECK-NEXT: alloc_ref [stack] $B
// CHECK-NEXT: destroy_value
// CHECK-NEXT: dealloc_stack_ref
// CHECK: return
// CHECK: } // end sil function 'alloc_ref_dynamic_stack_with_metatype'
sil [ossa] @alloc_ref_dynamic_stack_with_metatype : $() -> () {
%1 = metatype $@thick B.Type
%2 = alloc_ref_dynamic [stack] %1 : $@thick B.Type, $B
destroy_value %2 : $B
dealloc_stack_ref %2 : $B
%4 = tuple()
return %4 : $()
}

// CHECK-LABEL: sil [ossa] @alloc_ref_dynamic_stack_with_upcast_metatype :
// CHECK: alloc_ref_dynamic [stack]
// CHECK: } // end sil function 'alloc_ref_dynamic_stack_with_upcast_metatype'
sil [ossa] @alloc_ref_dynamic_stack_with_upcast_metatype : $() -> () {
%1 = metatype $@thick E.Type
%2 = upcast %1 : $@thick E.Type to $@thick B.Type
%3 = alloc_ref_dynamic [stack] %2 : $@thick B.Type, $B
destroy_value %3 : $B
dealloc_stack_ref %3 : $B
%4 = tuple()
return %4 : $()
}

// CHECK-LABEL: @alloc_ref_dynamic_stack_after_successful_checked_cast_br :
// CHECK: checked_cast_br
// CHECK: bb1
// CHECK-NOT: alloc_ref_dynamic
// CHECK: alloc_ref [stack] $B
// CHECK: } // end sil function 'alloc_ref_dynamic_stack_after_successful_checked_cast_br'
sil [ossa] @alloc_ref_dynamic_stack_after_successful_checked_cast_br : $(@thick B.Type) -> Builtin.Int32 {
bb0(%1 : $@thick B.Type):
checked_cast_br [exact] %1 : $@thick B.Type to B.Type, bb1, bb2

bb1(%2 : $@thick B.Type):
%3 = alloc_ref_dynamic [stack] %2 : $@thick B.Type, $B
destroy_value %3 : $B
dealloc_stack_ref %3 : $B
%4 = integer_literal $Builtin.Int32, 1
br bb3 (%4 : $Builtin.Int32)

bb2(%2a : $@thick B.Type):
%5 = integer_literal $Builtin.Int32, 2
br bb3 (%5 : $Builtin.Int32)

bb3 (%10: $Builtin.Int32):
return %10 : $Builtin.Int32
}

// CHECK-LABEL: sil [ossa] @delete_dead_alloc_stack
// XHECK: bb0
// XHECK-NEXT: tuple
Expand Down