Skip to content

Commit cac7fda

Browse files
committed
Fix capture propagation for partial_apply [stack]
1 parent 7ea69d1 commit cac7fda

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/SILOptimizer/IPO/CapturePropagation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ void CapturePropagation::rewritePartialApply(PartialApplyInst *OrigPAI,
296296
auto *T2TF = Builder.createThinToThickFunction(OrigPAI->getLoc(), FuncRef,
297297
OrigPAI->getType());
298298
OrigPAI->replaceAllUsesWith(T2TF);
299+
// Remove any dealloc_stack users.
300+
for (auto *Use : T2TF->getUses())
301+
if (auto *DS = dyn_cast<DeallocStackInst>(Use->getUser()))
302+
DS->eraseFromParent();
299303
recursivelyDeleteTriviallyDeadInstructions(OrigPAI, true);
300304
LLVM_DEBUG(llvm::dbgs() << " Rewrote caller:\n" << *T2TF);
301305
}

test/SILOptimizer/capture_propagation.sil

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,40 @@ bb0:
470470
%2 = partial_apply %1(%0) : $@convention(method) (Int32, Int32, @thin Int32.Type) -> Bool
471471
return %2 : $@callee_owned (Int32, Int32) -> Bool
472472
}
473+
474+
sil shared @test_capture_propagation2_callee_on_stack : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> () {
475+
bb0(%0 : $@noescape @callee_guaranteed () -> ()):
476+
apply %0() : $@noescape @callee_guaranteed () -> ()
477+
%9999 = tuple()
478+
return %9999 : $()
479+
}
480+
481+
sil shared @test_capture_propagation2_thunk_on_stack : $@convention(thin) (Builtin.Int32, Builtin.FPIEEE32, Builtin.RawPointer, @in Builtin.Word, @noescape @callee_guaranteed (Builtin.Int32, Builtin.FPIEEE32, Builtin.RawPointer, @in Builtin.Word) -> ()) -> () {
482+
bb0(%0 : $Builtin.Int32, %1 : $Builtin.FPIEEE32, %2 : $Builtin.RawPointer, %3 : $*Builtin.Word, %4 : $@noescape @callee_guaranteed (Builtin.Int32, Builtin.FPIEEE32, Builtin.RawPointer, @in Builtin.Word) -> ()):
483+
apply %4(%0, %1, %2, %3) : $@noescape @callee_guaranteed (Builtin.Int32, Builtin.FPIEEE32, Builtin.RawPointer, @in Builtin.Word) -> ()
484+
%9999 = tuple()
485+
return %9999 : $()
486+
}
487+
488+
// CHECK: sil @test_capture_propagation2_caller_on_stack
489+
// CHECK: [[F:%.*]] = function_ref @test_capture_propagation2_callee_on_stack : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
490+
// CHECK: [[F2:%.*]] = function_ref @$s40test_capture_propagation2_thunk_on_stack4_12353globalinit_33_06E7F1D906492AE070936A9B58CBAE1C_token808_TFtest_b1_C8_closureTf3pi0pd0psbpgpf_n : $@convention(thin) @noescape () -> ()
491+
// CHECK: [[CL:%.*]] = thin_to_thick_function [[F2]] : $@convention(thin) @noescape () -> () to $@noescape @callee_guaranteed () -> ()
492+
// CHECK: apply [[F]]([[CL]]) : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
493+
// CHECK: return
494+
495+
sil @test_capture_propagation2_caller_on_stack : $@convention(thin) () -> () {
496+
%0 = integer_literal $Builtin.Int32, 0
497+
%1 = float_literal $Builtin.FPIEEE32, 0
498+
%2 = string_literal utf8 "123"
499+
%3 = global_addr @globalinit_33_06E7F1D906492AE070936A9B58CBAE1C_token8 : $*Builtin.Word
500+
%4 = function_ref @_TFtest_capture_propagation2_closure : $@convention(thin) (Builtin.Int32, Builtin.FPIEEE32, Builtin.RawPointer, @in Builtin.Word) -> ()
501+
%5 = thin_to_thick_function %4 : $@convention(thin) (Builtin.Int32, Builtin.FPIEEE32, Builtin.RawPointer, @in Builtin.Word) -> () to $@noescape @callee_guaranteed (Builtin.Int32, Builtin.FPIEEE32, Builtin.RawPointer, @in Builtin.Word) -> ()
502+
%6 = function_ref @test_capture_propagation2_callee_on_stack : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
503+
%7 = function_ref @test_capture_propagation2_thunk_on_stack : $@convention(thin) (Builtin.Int32, Builtin.FPIEEE32, Builtin.RawPointer, @in Builtin.Word, @noescape @callee_guaranteed (Builtin.Int32, Builtin.FPIEEE32, Builtin.RawPointer, @in Builtin.Word) -> ()) -> ()
504+
%8 = partial_apply [callee_guaranteed] [on_stack] %7(%0, %1, %2, %3, %5) : $@convention(thin) (Builtin.Int32, Builtin.FPIEEE32, Builtin.RawPointer, @in Builtin.Word, @noescape @callee_guaranteed (Builtin.Int32, Builtin.FPIEEE32, Builtin.RawPointer, @in Builtin.Word) -> ()) -> ()
505+
apply %6(%8) : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
506+
dealloc_stack %8 : $@noescape @callee_guaranteed () -> ()
507+
%9999 = tuple()
508+
return %9999 : $()
509+
}

0 commit comments

Comments
 (0)