Skip to content

[AddressLowering] Fix partial_apply argument indexing. #67947

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
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
11 changes: 11 additions & 0 deletions include/swift/SIL/ApplySite.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,20 @@ class ApplySite {
/// Return the apply operand for the given applied argument index.
Operand &getArgumentRef(unsigned i) const { return getArgumentOperands()[i]; }

// The apply operand at the given index into the callee's function's
// arguments.
Operand &getArgumentRefAtCalleeArgIndex(unsigned i) const {
return getArgumentRef(i - getCalleeArgIndexOfFirstAppliedArg());
}

/// Return the ith applied argument.
SILValue getArgument(unsigned i) const { return getArguments()[i]; }

// The argument at the given index into the callee's function's arguments.
SILValue getArgumentAtCalleeArgIndex(unsigned i) const {
return getArgument(i - getCalleeArgIndexOfFirstAppliedArg());
}

/// Set the ith applied argument.
void setArgument(unsigned i, SILValue V) const {
getArgumentOperands()[i].set(V);
Expand Down
9 changes: 6 additions & 3 deletions lib/SILOptimizer/Mandatory/AddressLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2190,14 +2190,17 @@ bool CallArgRewriter::rewriteArguments() {
bool changed = false;

auto origConv = apply.getSubstCalleeConv();
assert(apply.getNumArguments() == origConv.getNumParameters() &&
"results should not yet be rewritten");
assert((apply.getNumArguments() == origConv.getNumParameters() &&
apply.asFullApplySite()) ||
(apply.getNumArguments() <= origConv.getNumParameters() &&
!apply.asFullApplySite()) &&
"results should not yet be rewritten");

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

Operand &operand = apply.getArgumentRef(argIdx);
Operand &operand = apply.getArgumentRefAtCalleeArgIndex(argIdx);
// Ignore arguments that have already been rewritten with an address.
if (operand.get()->getType().isAddress())
continue;
Expand Down
16 changes: 16 additions & 0 deletions test/SILOptimizer/address_lowering.sil
Original file line number Diff line number Diff line change
Expand Up @@ -2619,6 +2619,22 @@ sil [ossa] @test_partial_apply_4_loadable_stack : $() -> () {
return %retval : $()
}

// CHECK-LABEL: sil [ossa] @test_partial_apply_5_indexing : {{.*}} {
// CHECK: {{bb[0-9]+}}([[C:%[^,]+]] :
// CHECK: [[CALLEE:%[^,]+]] = function_ref @test_partial_apply_5_indexing_callee
// CHECK: [[CLOSURE:%[^,]+]] = partial_apply [callee_guaranteed] [[CALLEE]]([[C]])
// CHECK: destroy_value [[CLOSURE]]
// CHECK-LABEL: } // end sil function 'test_partial_apply_5_indexing'
sil @test_partial_apply_5_indexing_callee : $@convention(thin) (@inout Int, @guaranteed Klass) -> (@out Int)
sil [ossa] @test_partial_apply_5_indexing : $@convention(thin) (@owned Klass) -> () {
bb0(%c : @owned $Klass):
%callee = function_ref @test_partial_apply_5_indexing_callee : $@convention(thin) (@inout Int, @guaranteed Klass) -> (@out Int)
%closure = partial_apply [callee_guaranteed] %callee(%c) : $@convention(thin) (@inout Int, @guaranteed Klass) -> (@out Int)
destroy_value %closure : $@callee_guaranteed (@inout Int) -> (@out Int)
%retval = tuple ()
return %retval : $()
}

// CHECK-LABEL: sil hidden [ossa] @test_store_1 : {{.*}} {
// CHECK: [[MAYBE_ADDR:%[^,]+]] = alloc_stack $Optional<Self>
// CHECK: [[LOAD_ADDR:%[^,]+]] = alloc_stack $Self
Expand Down