Skip to content

Commit ebdeecf

Browse files
Merge pull request #64013 from nate-chandler/mandatory-generic-specializer/partial-apply-stack-discipline
[MandatoryGenericSpecializer] Fix stack nesting when lowering OSSA during inlining.
2 parents 8f022b9 + 1dda30e commit ebdeecf

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/SILOptimizer/Transforms/GenericSpecializer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ optimizeInst(SILInstruction *inst, SILOptFunctionBuilder &funcBuilder,
353353
// If the de-virtualized callee is a transparent function, inline it.
354354
SILInliner::inlineFullApply(fas, SILInliner::InlineKind::MandatoryInline,
355355
funcBuilder, deleter);
356+
if (callee->hasOwnership() && !inst->getFunction()->hasOwnership())
357+
invalidatedStackNesting = true;
356358
return true;
357359
}
358360
if (auto *bi = dyn_cast<BuiltinInst>(inst)) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -mandatory-generic-specializer | %FileCheck %s
2+
3+
import Builtin
4+
5+
sil @paable : $@convention(thin) (Builtin.Int64) -> ()
6+
7+
sil [ossa] [transparent] @partial_apply_on_stack_nesting_violator : $@convention(thin) <T> () -> () {
8+
%paable = function_ref @paable : $@convention(thin) (Builtin.Int64) -> ()
9+
%one = integer_literal $Builtin.Int64, 1
10+
%first = partial_apply [callee_guaranteed] [on_stack] %paable(%one) : $@convention(thin) (Builtin.Int64) -> ()
11+
%two = integer_literal $Builtin.Int64, 2
12+
%second = partial_apply [callee_guaranteed] [on_stack] %paable(%two) : $@convention(thin) (Builtin.Int64) -> ()
13+
// Note that the destroy_values do not occur in an order which coincides
14+
// with stack disciplined dealloc_stacks.
15+
destroy_value %first : $@noescape @callee_guaranteed () -> ()
16+
destroy_value %second : $@noescape @callee_guaranteed () -> ()
17+
%retval = tuple ()
18+
return %retval : $()
19+
}
20+
21+
// Verify that when inlining partial_apply_on_stack_nesting_violator, the stack
22+
// nesting of the on_stack closures is fixed.
23+
// CHECK-LABEL: sil [no_locks] @test_inline_stack_violating_ossa_func : {{.*}} {
24+
// CHECK: [[PAABLE:%[^,]+]] = function_ref @paable
25+
// CHECK: [[FIRST:%[^,]+]] = partial_apply [callee_guaranteed] [on_stack] [[PAABLE]]
26+
// CHECK: [[SECOND:%[^,]+]] = partial_apply [callee_guaranteed] [on_stack] [[PAABLE]]
27+
// CHECK: dealloc_stack [[SECOND]]
28+
// CHECK: dealloc_stack [[FIRST]]
29+
// CHECK-LABEL: } // end sil function 'test_inline_stack_violating_ossa_func'
30+
sil [no_locks] @test_inline_stack_violating_ossa_func : $@convention(thin) () -> () {
31+
%callee = function_ref @partial_apply_on_stack_nesting_violator : $@convention(thin) <T> () -> ()
32+
apply %callee<Builtin.Int64>() : $@convention(thin) <T> () -> ()
33+
%retval = tuple ()
34+
return %retval : $()
35+
}
36+
37+

0 commit comments

Comments
 (0)