Skip to content

Commit d7bdbcb

Browse files
authored
Merge pull request #83231 from Xazax-hun/addressable-params-copy-revert
Revert [cxx-interop] Avoid copies when accessing pointee
2 parents 792f4f3 + db4b4c8 commit d7bdbcb

File tree

7 files changed

+9
-147
lines changed

7 files changed

+9
-147
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4098,8 +4098,7 @@ namespace {
40984098
func->setSelfIndex(selfIdx.value());
40994099
if (Impl.SwiftContext.LangOpts.hasFeature(
41004100
Feature::AddressableParameters))
4101-
func->getAttrs().add(new (Impl.SwiftContext)
4102-
AddressableSelfAttr(true));
4101+
func->getImplicitSelfDecl()->setAddressable();
41034102
} else {
41044103
func->setStatic();
41054104
func->setImportAsStaticMember();
@@ -4147,34 +4146,6 @@ namespace {
41474146
return false;
41484147
}
41494148

4150-
// Inject lifetime annotations selectively for some STL types so we can use
4151-
// unsafeAddress to avoid copies.
4152-
bool inferSelfDependence(const clang::FunctionDecl *decl,
4153-
AbstractFunctionDecl *result, size_t returnIdx) {
4154-
const auto *method = dyn_cast<clang::CXXMethodDecl>(decl);
4155-
if (!method)
4156-
return false;
4157-
const auto *enclosing = method->getParent();
4158-
if (enclosing->isInStdNamespace() &&
4159-
(enclosing->getName() == "unique_ptr" ||
4160-
enclosing->getName() == "shared_ptr") &&
4161-
method->isOverloadedOperator() &&
4162-
method->getOverloadedOperator() == clang::OO_Star) {
4163-
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
4164-
SmallBitVector dependenciesOfRet(returnIdx);
4165-
dependenciesOfRet[result->getSelfIndex()] = true;
4166-
lifetimeDependencies.push_back(LifetimeDependenceInfo(
4167-
nullptr, IndexSubset::get(Impl.SwiftContext, dependenciesOfRet),
4168-
returnIdx,
4169-
/*isImmortal*/ false));
4170-
Impl.SwiftContext.evaluator.cacheOutput(
4171-
LifetimeDependenceInfoRequest{result},
4172-
Impl.SwiftContext.AllocateCopy(lifetimeDependencies));
4173-
return true;
4174-
}
4175-
return false;
4176-
}
4177-
41784149
void addLifetimeDependencies(const clang::FunctionDecl *decl,
41794150
AbstractFunctionDecl *result) {
41804151
if (decl->getTemplatedKind() == clang::FunctionDecl::TK_FunctionTemplate)
@@ -4193,19 +4164,10 @@ namespace {
41934164
CxxEscapability::Unknown) != CxxEscapability::NonEscapable;
41944165
};
41954166

4196-
auto swiftParams = result->getParameters();
4197-
bool hasSelf =
4198-
result->hasImplicitSelfDecl() && !isa<ConstructorDecl>(result);
4199-
auto returnIdx = swiftParams->size() + hasSelf;
4200-
4201-
if (inferSelfDependence(decl, result, returnIdx))
4202-
return;
4203-
42044167
// FIXME: this uses '0' as the result index. That only works for
42054168
// standalone functions with no parameters.
42064169
// See markReturnsUnsafeNonescapable() for a general approach.
42074170
auto &ASTContext = result->getASTContext();
4208-
42094171
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
42104172
LifetimeDependenceInfo immortalLifetime(nullptr, nullptr, 0,
42114173
/*isImmortal*/ true);
@@ -4228,7 +4190,10 @@ namespace {
42284190
}
42294191
};
42304192

4231-
const auto dependencyVecSize = returnIdx;
4193+
auto swiftParams = result->getParameters();
4194+
bool hasSelf =
4195+
result->hasImplicitSelfDecl() && !isa<ConstructorDecl>(result);
4196+
const auto dependencyVecSize = swiftParams->size() + hasSelf;
42324197
SmallBitVector inheritLifetimeParamIndicesForReturn(dependencyVecSize);
42334198
SmallBitVector scopedLifetimeParamIndicesForReturn(dependencyVecSize);
42344199
SmallBitVector paramHasAnnotation(dependencyVecSize);
@@ -4307,7 +4272,7 @@ namespace {
43074272
? IndexSubset::get(Impl.SwiftContext,
43084273
scopedLifetimeParamIndicesForReturn)
43094274
: nullptr,
4310-
returnIdx,
4275+
swiftParams->size() + hasSelf,
43114276
/*isImmortal*/ false));
43124277
else if (auto *ctordecl = dyn_cast<clang::CXXConstructorDecl>(decl)) {
43134278
// Assume default constructed view types have no dependencies.

lib/ClangImporter/SwiftDeclSynthesizer.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,6 @@ SubscriptDecl *SwiftDeclSynthesizer::makeSubscript(FuncDecl *getter,
16811681
FuncDecl *getterImpl = getter ? getter : setter;
16821682
FuncDecl *setterImpl = setter;
16831683

1684-
// FIXME: support unsafeAddress accessors.
16851684
// Get the return type wrapped in `Unsafe(Mutable)Pointer<T>`.
16861685
const auto rawElementTy = getterImpl->getResultInterfaceType();
16871686
// Unwrap `T`. Use rawElementTy for return by value.
@@ -1764,23 +1763,6 @@ SubscriptDecl *SwiftDeclSynthesizer::makeSubscript(FuncDecl *getter,
17641763
return subscript;
17651764
}
17661765

1767-
static bool doesReturnDependsOnSelf(FuncDecl *f) {
1768-
if (!f->getASTContext().LangOpts.hasFeature(Feature::AddressableParameters))
1769-
return false;
1770-
if (!f->hasImplicitSelfDecl())
1771-
return false;
1772-
if (auto deps = f->getLifetimeDependencies()) {
1773-
for (auto dependence : *deps) {
1774-
auto returnIdx = f->getParameters()->size() + !isa<ConstructorDecl>(f);
1775-
if (!dependence.hasInheritLifetimeParamIndices() &&
1776-
dependence.hasScopeLifetimeParamIndices() &&
1777-
dependence.getTargetIndex() == returnIdx)
1778-
return dependence.getScopeIndices()->contains(f->getSelfIndex());
1779-
}
1780-
}
1781-
return false;
1782-
}
1783-
17841766
// MARK: C++ dereference operator
17851767

17861768
VarDecl *
@@ -1792,7 +1774,6 @@ SwiftDeclSynthesizer::makeDereferencedPointeeProperty(FuncDecl *getter,
17921774
FuncDecl *getterImpl = getter ? getter : setter;
17931775
FuncDecl *setterImpl = setter;
17941776
auto dc = getterImpl->getDeclContext();
1795-
bool resultDependsOnSelf = doesReturnDependsOnSelf(getterImpl);
17961777

17971778
// Get the return type wrapped in `Unsafe(Mutable)Pointer<T>`.
17981779
const auto rawElementTy = getterImpl->getResultInterfaceType();
@@ -1803,9 +1784,9 @@ SwiftDeclSynthesizer::makeDereferencedPointeeProperty(FuncDecl *getter,
18031784
// Use 'address' or 'mutableAddress' accessors for non-copyable
18041785
// types that are returned indirectly.
18051786
bool isNoncopyable = dc->mapTypeIntoContext(elementTy)->isNoncopyable();
1806-
bool isImplicit = !(isNoncopyable || resultDependsOnSelf);
1787+
bool isImplicit = !isNoncopyable;
18071788
bool useAddress =
1808-
rawElementTy->getAnyPointerElementType() && (isNoncopyable || resultDependsOnSelf);
1789+
rawElementTy->getAnyPointerElementType() && isNoncopyable;
18091790

18101791
auto result = new (ctx)
18111792
VarDecl(/*isStatic*/ false, VarDecl::Introducer::Var,

test/Interop/Cxx/class/method/methods-addressable-silgen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public func addressableTest(x: borrowing @_addressable NonTrivialInWrapper, y: i
1717
// CHECK: %{{[0-9]+}} = apply %{{[0-9]+}}([[UNWRAPPED]], %{{[0-9]+}}) : $@convention(cxx_method) (@in_guaranteed NonTrivialInWrapper, @in_guaranteed HasMethods) -> ()
1818
var m2 = HasMethods()
1919
// CHECK: [[ACCESS:%[0-9]+]] = begin_access [modify] [unknown] [[INPUT2]]
20-
// CHECK: %{{[0-9]+}} = apply %{{[0-9]+}}([[ACCESS]], %{{[0-9]+}}) : $@convention(cxx_method) (@inout NonTrivialInWrapper, @in_guaranteed HasMethods) -> ()
20+
// CHECK: %{{[0-9]+}} = apply %32([[ACCESS]], %{{[0-9]+}}) : $@convention(cxx_method) (@inout NonTrivialInWrapper, @in_guaranteed HasMethods) -> ()
2121
// CHECK-NEXT: end_access [[ACCESS]]
2222
m2.nonTrivialTakesRef(&y)
2323
}

test/Interop/Cxx/stdlib/Inputs/custom-smart-ptr.h

Lines changed: 0 additions & 22 deletions
This file was deleted.

test/Interop/Cxx/stdlib/Inputs/module.modulemap

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,3 @@ module NoCXXStdlib {
8181
requires cplusplus
8282
export *
8383
}
84-
85-
module CustomSmartPtr {
86-
header "custom-smart-ptr.h"
87-
requires cplusplus
88-
export *
89-
}

test/Interop/Cxx/stdlib/Inputs/std-unique-ptr.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,4 @@ std::shared_ptr<std::string> makeStringShared() {
6666
return std::make_unique<std::string>("Shared string");
6767
}
6868

69-
static int copies = 0;
70-
71-
struct CountCopies {
72-
CountCopies() = default;
73-
CountCopies(const CountCopies& other) { ++copies; }
74-
~CountCopies() {}
75-
76-
int getCopies() const { return copies; }
77-
void method() {}
78-
void constMethod() const {}
79-
int field = 42;
80-
};
81-
82-
inline std::unique_ptr<CountCopies> getCopyCountedUniquePtr() { return std::make_unique<CountCopies>(); }
83-
8469
#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_STD_UNIQUE_PTR_H

test/Interop/Cxx/stdlib/avoid-redundant-copies.swift

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)