Skip to content

Commit db4b4c8

Browse files
author
Gabor Horvath
committed
Revert "Merge pull request #82480 from swiftlang/gaborh/addressable_params_copy"
Unfortunately, addressable parameters are viral, the whole dependency chain needs to be consistent otherwise we get deserialization errors when loading a module. The solution is to universally enable addressable parameters for C++ interop but there are some blockers at the moment that need to be solved first. Temporarily revert these changes until those blockers are resolved. This reverts commit b00ff45, reversing changes made to 396379e.
1 parent 3a28941 commit db4b4c8

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
@@ -4096,8 +4096,7 @@ namespace {
40964096
func->setSelfIndex(selfIdx.value());
40974097
if (Impl.SwiftContext.LangOpts.hasFeature(
40984098
Feature::AddressableParameters))
4099-
func->getAttrs().add(new (Impl.SwiftContext)
4100-
AddressableSelfAttr(true));
4099+
func->getImplicitSelfDecl()->setAddressable();
41014100
} else {
41024101
func->setStatic();
41034102
func->setImportAsStaticMember();
@@ -4145,34 +4144,6 @@ namespace {
41454144
return false;
41464145
}
41474146

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

4194-
auto swiftParams = result->getParameters();
4195-
bool hasSelf =
4196-
result->hasImplicitSelfDecl() && !isa<ConstructorDecl>(result);
4197-
auto returnIdx = swiftParams->size() + hasSelf;
4198-
4199-
if (inferSelfDependence(decl, result, returnIdx))
4200-
return;
4201-
42024165
// FIXME: this uses '0' as the result index. That only works for
42034166
// standalone functions with no parameters.
42044167
// See markReturnsUnsafeNonescapable() for a general approach.
42054168
auto &ASTContext = result->getASTContext();
4206-
42074169
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
42084170
LifetimeDependenceInfo immortalLifetime(nullptr, nullptr, 0,
42094171
/*isImmortal*/ true);
@@ -4226,7 +4188,10 @@ namespace {
42264188
}
42274189
};
42284190

4229-
const auto dependencyVecSize = returnIdx;
4191+
auto swiftParams = result->getParameters();
4192+
bool hasSelf =
4193+
result->hasImplicitSelfDecl() && !isa<ConstructorDecl>(result);
4194+
const auto dependencyVecSize = swiftParams->size() + hasSelf;
42304195
SmallBitVector inheritLifetimeParamIndicesForReturn(dependencyVecSize);
42314196
SmallBitVector scopedLifetimeParamIndicesForReturn(dependencyVecSize);
42324197
SmallBitVector paramHasAnnotation(dependencyVecSize);
@@ -4305,7 +4270,7 @@ namespace {
43054270
? IndexSubset::get(Impl.SwiftContext,
43064271
scopedLifetimeParamIndicesForReturn)
43074272
: nullptr,
4308-
returnIdx,
4273+
swiftParams->size() + hasSelf,
43094274
/*isImmortal*/ false));
43104275
else if (auto *ctordecl = dyn_cast<clang::CXXConstructorDecl>(decl)) {
43114276
// 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)