Skip to content

Commit 9e21bd5

Browse files
Merge pull request swiftlang#67947 from nate-chandler/opaque-values/20230815/1/partial-apply-indexing
[AddressLowering] Fix partial_apply argument indexing.
2 parents 44198d1 + 35113d8 commit 9e21bd5

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

include/swift/SIL/ApplySite.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,20 @@ class ApplySite {
295295
/// Return the apply operand for the given applied argument index.
296296
Operand &getArgumentRef(unsigned i) const { return getArgumentOperands()[i]; }
297297

298+
// The apply operand at the given index into the callee's function's
299+
// arguments.
300+
Operand &getArgumentRefAtCalleeArgIndex(unsigned i) const {
301+
return getArgumentRef(i - getCalleeArgIndexOfFirstAppliedArg());
302+
}
303+
298304
/// Return the ith applied argument.
299305
SILValue getArgument(unsigned i) const { return getArguments()[i]; }
300306

307+
// The argument at the given index into the callee's function's arguments.
308+
SILValue getArgumentAtCalleeArgIndex(unsigned i) const {
309+
return getArgument(i - getCalleeArgIndexOfFirstAppliedArg());
310+
}
311+
301312
/// Set the ith applied argument.
302313
void setArgument(unsigned i, SILValue V) const {
303314
getArgumentOperands()[i].set(V);

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,14 +2190,17 @@ bool CallArgRewriter::rewriteArguments() {
21902190
bool changed = false;
21912191

21922192
auto origConv = apply.getSubstCalleeConv();
2193-
assert(apply.getNumArguments() == origConv.getNumParameters() &&
2194-
"results should not yet be rewritten");
2193+
assert((apply.getNumArguments() == origConv.getNumParameters() &&
2194+
apply.asFullApplySite()) ||
2195+
(apply.getNumArguments() <= origConv.getNumParameters() &&
2196+
!apply.asFullApplySite()) &&
2197+
"results should not yet be rewritten");
21952198

21962199
for (unsigned argIdx = apply.getCalleeArgIndexOfFirstAppliedArg(),
21972200
endArgIdx = argIdx + apply.getNumArguments();
21982201
argIdx < endArgIdx; ++argIdx) {
21992202

2200-
Operand &operand = apply.getArgumentRef(argIdx);
2203+
Operand &operand = apply.getArgumentRefAtCalleeArgIndex(argIdx);
22012204
// Ignore arguments that have already been rewritten with an address.
22022205
if (operand.get()->getType().isAddress())
22032206
continue;

test/SILOptimizer/address_lowering.sil

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,22 @@ sil [ossa] @test_partial_apply_4_loadable_stack : $() -> () {
26192619
return %retval : $()
26202620
}
26212621

2622+
// CHECK-LABEL: sil [ossa] @test_partial_apply_5_indexing : {{.*}} {
2623+
// CHECK: {{bb[0-9]+}}([[C:%[^,]+]] :
2624+
// CHECK: [[CALLEE:%[^,]+]] = function_ref @test_partial_apply_5_indexing_callee
2625+
// CHECK: [[CLOSURE:%[^,]+]] = partial_apply [callee_guaranteed] [[CALLEE]]([[C]])
2626+
// CHECK: destroy_value [[CLOSURE]]
2627+
// CHECK-LABEL: } // end sil function 'test_partial_apply_5_indexing'
2628+
sil @test_partial_apply_5_indexing_callee : $@convention(thin) (@inout Int, @guaranteed Klass) -> (@out Int)
2629+
sil [ossa] @test_partial_apply_5_indexing : $@convention(thin) (@owned Klass) -> () {
2630+
bb0(%c : @owned $Klass):
2631+
%callee = function_ref @test_partial_apply_5_indexing_callee : $@convention(thin) (@inout Int, @guaranteed Klass) -> (@out Int)
2632+
%closure = partial_apply [callee_guaranteed] %callee(%c) : $@convention(thin) (@inout Int, @guaranteed Klass) -> (@out Int)
2633+
destroy_value %closure : $@callee_guaranteed (@inout Int) -> (@out Int)
2634+
%retval = tuple ()
2635+
return %retval : $()
2636+
}
2637+
26222638
// CHECK-LABEL: sil hidden [ossa] @test_store_1 : {{.*}} {
26232639
// CHECK: [[MAYBE_ADDR:%[^,]+]] = alloc_stack $Optional<Self>
26242640
// CHECK: [[LOAD_ADDR:%[^,]+]] = alloc_stack $Self

0 commit comments

Comments
 (0)