Skip to content

[cxx-interop] Fix inline operators of address-only types. #32869

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion lib/SIL/IR/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1572,13 +1572,27 @@ class DestructureInputs {
// Add any foreign parameters that are positioned here.
maybeAddForeignParameters();

bool isCxxOperator = origType.isCXXMethod() &&
origType.getCXXMethod()->isOverloadedOperator();

// Process all the non-self parameters.
for (unsigned i = 0; i != numNonSelfParams; ++i) {
auto ty = params[i].getParameterType();
auto eltPattern = origType.getFunctionParamType(i);
auto flags = params[i].getParameterFlags();
bool forSelf = false;

// If we have an inline operator, when it was imported, it was turned
// from a method into a static function. So, we need to shift over the
// params by one and use "self" as the first param's type.
Copy link
Contributor

@gribozavr gribozavr Jul 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't say what the right fix should be exactly, but I feel like this code is working around the problem more than solving it directly. Resetting NextOrigParamIndex does not feel right to me (so this code won't work for argument count greater than 2, I think?) Abstraction pattern and the code to handle self in this converter class are supposed to abstract away this issue, I think. Currently they handle the abstraction in the opposite direction (importing a C free function as a Swift method). Here, we have the opposite problem -- importing a C++ member function as a Swift static method.

WDYT about extending this code to skip both the Swift self which was mapped from C, and Swift parameters that correspond to C++ this, and letting the HandleForeignSelf handle them at the appropriate point?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about extending this code to skip both the Swift self which was mapped from C, and Swift parameters that correspond to C++ this, and letting the HandleForeignSelf handle them at the appropriate point?

The issue is CXXMethodConventions creates a CFunctionTypeConventions object with the function type T(T) when it should be T(T, T). This means that there are the wrong number of arguments. I think we have two choices, either we can pass forSelf=true for the first argument, then decrement NextOrigParamIndex (still a bit of a hack but also less bad than what's currently in this PR), or we can pass a more "correct" type to CFunctionTypeConventions. WDYT would be better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated this PR to use the first solution I suggested but I can change it to use the second if you'd rather.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hlopko @varungandhi-apple thoughts on the above conversation? I'd like to get this patch moving :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gribozavr @hlopko ping. Once re-reviewed I'll rebase.

if (isCxxOperator) {
if (i == 0)
forSelf = true;
else if (i == 1)
NextOrigParamIndex--;
}

visit(flags.getValueOwnership(), /*forSelf=*/false, eltPattern, ty,
visit(flags.getValueOwnership(), forSelf, eltPattern, ty,
flags.isNoDerivative());
}

Expand Down
10 changes: 10 additions & 0 deletions test/Interop/Cxx/operators/Inputs/member-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ struct HasDeletedOperator {
void operator!=(HasDeletedOperator) const = delete;
};

struct AddressOnlyIntWrapper {
int value;
AddressOnlyIntWrapper(int value) : value(value) {}
AddressOnlyIntWrapper(AddressOnlyIntWrapper const &other)
: value(other.value) {}
AddressOnlyIntWrapper operator-(AddressOnlyIntWrapper rhs) {
return AddressOnlyIntWrapper(value - rhs.value);
}
};

#endif
11 changes: 9 additions & 2 deletions test/Interop/Cxx/operators/member-inline-irgen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

import MemberInline

public func sub(_ lhs: inout LoadableIntWrapper, _ rhs: LoadableIntWrapper) -> LoadableIntWrapper { lhs - rhs }

// CHECK: call [[RES:i32|i64]] [[NAME:@(_ZN18LoadableIntWrappermiES_|"\?\?GLoadableIntWrapper@@QEAA\?AU0@U0@@Z")]](%struct.LoadableIntWrapper* {{%[0-9]+}}, {{i32|\[1 x i32\]|i64|%struct.LoadableIntWrapper\* byval align 4}} {{%[0-9]+}})

// CHECK: define linkonce_odr [[RES]] [[NAME]](%struct.LoadableIntWrapper* %this, {{i32 %rhs.coerce|\[1 x i32\] %rhs.coerce|i64 %rhs.coerce|%struct.LoadableIntWrapper\* byval\(%struct.LoadableIntWrapper\) align 4 %rhs}})
public func sub(_ lhs: inout LoadableIntWrapper, _ rhs: LoadableIntWrapper) -> LoadableIntWrapper { lhs - rhs }

// CHECK-LABEL: define {{.*}}void @"$s4main24subAddressOnlyIntWrapperySo0cdeF0VADz_ADtF"
// CHECK: call void [[NAME:@(_ZN21AddressOnlyIntWrappermiES_|"\?\?GAddressOnlyIntWrapper@@QEAA\?AU0@U0@@Z")]](%struct.AddressOnlyIntWrapper* {{%[0-9]+}}, %struct.AddressOnlyIntWrapper* {{%[0-9]+}}, %struct.AddressOnlyIntWrapper* {{%[0-9]+}})
// CHECK: ret void

// CHECK: define linkonce_odr void [[NAME]](%struct.AddressOnlyIntWrapper* {{.*}}%agg.result, %struct.AddressOnlyIntWrapper* %this, %struct.AddressOnlyIntWrapper* %rhs)
public func subAddressOnlyIntWrapper(_ lhs: inout AddressOnlyIntWrapper, _ rhs: AddressOnlyIntWrapper) -> AddressOnlyIntWrapper { lhs - rhs }
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@

// CHECK: struct HasDeletedOperator {
// CHECK: }

// CHECK: struct AddressOnlyIntWrapper {
// CHECK: static func - (lhs: inout AddressOnlyIntWrapper, rhs: AddressOnlyIntWrapper) -> AddressOnlyIntWrapper
// CHECK: }
14 changes: 14 additions & 0 deletions test/Interop/Cxx/operators/member-inline-silgen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@ public func sub(_ lhs: inout LoadableIntWrapper, _ rhs: LoadableIntWrapper) -> L
// CHECK: end_access [[SELFACCESS]] : $*LoadableIntWrapper

// CHECK: sil [clang LoadableIntWrapper."-"] [[NAME]] : $@convention(c) (@inout LoadableIntWrapper, LoadableIntWrapper) -> LoadableIntWrapper

// CHECK-LABEL: sil @$s4main24subAddressOnlyIntWrapperySo0cdeF0VADz_ADtF : $@convention(thin) (@inout AddressOnlyIntWrapper, @in_guaranteed AddressOnlyIntWrapper) -> @out AddressOnlyIntWrapper
// CHECK: bb0([[OUT:%.*]] : $*AddressOnlyIntWrapper, [[LHS:%.*]] : $*AddressOnlyIntWrapper, [[RHS:%.*]] : $*AddressOnlyIntWrapper):
// CHECK: [[RHS_TMP:%.*]] = alloc_stack $AddressOnlyIntWrapper
// CHECK: copy_addr [[RHS]] to [initialization] [[RHS_TMP]] : $*AddressOnlyIntWrapper
// CHECK: [[LHS_ACCESS:%.*]] = begin_access [modify] [static] [[LHS]] : $*AddressOnlyIntWrapper
// CHECK: [[FN:%.*]] = function_ref [[NAME:@(_ZN21AddressOnlyIntWrappermiES_|\?\?GAddressOnlyIntWrapper@@QEAA\?AU0@U0@@Z)]] : $@convention(c) (@inout AddressOnlyIntWrapper, @in AddressOnlyIntWrapper) -> @out AddressOnlyIntWrapper
// CHECK: apply [[FN]]([[OUT]], [[LHS_ACCESS]], [[RHS_TMP]]) : $@convention(c) (@inout AddressOnlyIntWrapper, @in AddressOnlyIntWrapper) -> @out AddressOnlyIntWrapper
// CHECK: end_access [[LHS_ACCESS]]
// CHECK: dealloc_stack [[RHS_TMP]]
// CHECK: end sil function '$s4main24subAddressOnlyIntWrapperySo0cdeF0VADz_ADtF'

// CHECK: sil [clang AddressOnlyIntWrapper."-"] [[NAME]] : $@convention(c) (@inout AddressOnlyIntWrapper, @in AddressOnlyIntWrapper) -> @out AddressOnlyIntWrapper
public func subAddressOnlyIntWrapper(_ lhs: inout AddressOnlyIntWrapper, _ rhs: AddressOnlyIntWrapper) -> AddressOnlyIntWrapper { lhs - rhs }
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ import MemberInline

var lhs = LoadableIntWrapper(value: 42)
let rhs = LoadableIntWrapper(value: 23)
let resultMinus = lhs - rhs

let resultPlus = lhs - rhs
var lhsAddressOnly = AddressOnlyIntWrapper(42)
var rhsAddressOnly = AddressOnlyIntWrapper(23)
let resultMinusAddressOnly = lhsAddressOnly - rhsAddressOnly
8 changes: 8 additions & 0 deletions test/Interop/Cxx/operators/member-inline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ OperatorsTestSuite.test("LoadableIntWrapper.plus") {
expectEqual(19, result.value)
}

OperatorsTestSuite.test("AddressOnlyIntWrapper.plus") {
var lhs = AddressOnlyIntWrapper(42)
let rhs = AddressOnlyIntWrapper(23)

let result = lhs - rhs
expectEqual(19, result.value)
}

runAllTests()