Skip to content

Serialize/deserialize lifetime dependence when present #71295

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
merged 4 commits into from
Feb 1, 2024
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
3 changes: 3 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7797,6 +7797,9 @@ class FuncDecl : public AbstractFunctionDecl {
SourceRange getSourceRange() const;

TypeRepr *getResultTypeRepr() const { return FnRetType.getTypeRepr(); }

void setDeserializedResultTypeLoc(TypeLoc ResultTyR);

SourceRange getResultTypeSourceRange() const {
return FnRetType.getSourceRange();
}
Expand Down
11 changes: 11 additions & 0 deletions include/swift/AST/LifetimeDependence.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,19 @@ class LifetimeDependenceInfo {
mutateLifetimeParamIndices == nullptr;
}

bool hasInheritLifetimeParamIndices() const {
return inheritLifetimeParamIndices != nullptr;
}
bool hasBorrowLifetimeParamIndices() const {
return borrowLifetimeParamIndices != nullptr;
}
bool hasMutateLifetimeParamIndices() const {
return mutateLifetimeParamIndices != nullptr;
}

std::string getString() const;
void Profile(llvm::FoldingSetNodeID &ID) const;
void getConcatenatedData(SmallVectorImpl<bool> &concatenatedData) const;

static llvm::Optional<LifetimeDependenceInfo>
get(AbstractFunctionDecl *decl, Type resultType, bool allowIndex = false);
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9976,6 +9976,10 @@ void FuncDecl::setResultInterfaceType(Type type) {
std::move(type));
}

void FuncDecl::setDeserializedResultTypeLoc(TypeLoc ResultTyR) {
FnRetType = ResultTyR;
}

FuncDecl *FuncDecl::createImpl(ASTContext &Context,
SourceLoc StaticLoc,
StaticSpellingKind StaticSpelling,
Expand Down
53 changes: 48 additions & 5 deletions lib/Sema/LifetimeDependence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@ LifetimeDependenceInfo LifetimeDependenceInfo::getForParamIndex(
/*mutateLifetimeParamIndices*/ indexSubset};
}

void LifetimeDependenceInfo::getConcatenatedData(
SmallVectorImpl<bool> &concatenatedData) const {
auto pushData = [&](IndexSubset *paramIndices) {
if (paramIndices == nullptr) {
return;
}
assert(!paramIndices->isEmpty());

for (unsigned i = 0; i < paramIndices->getCapacity(); i++) {
if (paramIndices->contains(i)) {
concatenatedData.push_back(true);
continue;
}
concatenatedData.push_back(false);
}
};
if (hasInheritLifetimeParamIndices()) {
pushData(inheritLifetimeParamIndices);
}
if (hasBorrowLifetimeParamIndices()) {
pushData(borrowLifetimeParamIndices);
}
if (hasMutateLifetimeParamIndices()) {
pushData(mutateLifetimeParamIndices);
}
}

llvm::Optional<LifetimeDependenceInfo>
LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
bool allowIndex) {
Expand Down Expand Up @@ -181,9 +208,19 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
paramIndex);
return llvm::None;
}
if (paramIndex == 0) {
if (!afd->hasImplicitSelfDecl()) {
diags.diagnose(specifier.getLoc(),
diag::lifetime_dependence_invalid_self);
return llvm::None;
}
}
auto ownership =
paramIndex == 0
? afd->getImplicitSelfDecl()->getValueOwnership()
: afd->getParameters()->get(paramIndex - 1)->getValueOwnership();
if (updateLifetimeDependenceInfo(
specifier, /*paramIndexToSet*/ specifier.getIndex() + 1,
afd->getParameters()->get(paramIndex)->getValueOwnership())) {
specifier, /*paramIndexToSet*/ specifier.getIndex(), ownership)) {
return llvm::None;
}
break;
Expand All @@ -205,9 +242,15 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
}

return LifetimeDependenceInfo(
IndexSubset::get(ctx, inheritLifetimeParamIndices),
IndexSubset::get(ctx, borrowLifetimeParamIndices),
IndexSubset::get(ctx, mutateLifetimeParamIndices));
inheritLifetimeParamIndices.any()
? IndexSubset::get(ctx, inheritLifetimeParamIndices)
: nullptr,
borrowLifetimeParamIndices.any()
? IndexSubset::get(ctx, borrowLifetimeParamIndices)
: nullptr,
mutateLifetimeParamIndices.any()
? IndexSubset::get(ctx, mutateLifetimeParamIndices)
: nullptr);
}

llvm::Optional<LifetimeDependenceInfo>
Expand Down
2 changes: 2 additions & 0 deletions lib/Serialization/DeclTypeRecordNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ OTHER(ERROR_FLAG, 155)
TRAILING_INFO(CONDITIONAL_SUBSTITUTION)
TRAILING_INFO(CONDITIONAL_SUBSTITUTION_COND)

OTHER(LIFETIME_DEPENDENCE, 158)

#ifndef DECL_ATTR
#define DECL_ATTR(NAME, CLASS, OPTIONS, CODE) RECORD_VAL(CLASS##_DECL_ATTR, 180+CODE)
#endif
Expand Down
62 changes: 62 additions & 0 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4105,6 +4105,13 @@ class DeclDeserializer {

ParameterList *paramList = MF.readParameterList();
fn->setParameters(paramList);
SmallVector<LifetimeDependenceSpecifier> specifierList;
if (MF.maybeReadLifetimeDependence(specifierList, paramList->size())) {
auto typeRepr = new (ctx) FixedTypeRepr(resultType, SourceLoc());
auto lifetimeTypeRepr =
LifetimeDependentReturnTypeRepr::create(ctx, typeRepr, specifierList);
fn->setDeserializedResultTypeLoc(TypeLoc(lifetimeTypeRepr, resultType));
}

if (auto errorConvention = MF.maybeReadForeignErrorConvention())
fn->setForeignErrorConvention(*errorConvention);
Expand Down Expand Up @@ -8548,3 +8555,58 @@ ModuleFile::maybeReadForeignAsyncConvention() {
completionHandlerErrorFlagParamIndex,
errorFlagPolarity);
}

bool ModuleFile::maybeReadLifetimeDependence(
SmallVectorImpl<LifetimeDependenceSpecifier> &specifierList,
unsigned numParams) {
using namespace decls_block;
SmallVector<uint64_t, 8> scratch;

BCOffsetRAII restoreOffset(DeclTypeCursor);

llvm::BitstreamEntry next =
fatalIfUnexpected(DeclTypeCursor.advance(AF_DontPopBlockAtEnd));
if (next.Kind != llvm::BitstreamEntry::Record)
return false;

unsigned recKind =
fatalIfUnexpected(DeclTypeCursor.readRecord(next.ID, scratch));
switch (recKind) {
case LIFETIME_DEPENDENCE:
restoreOffset.reset();
break;

default:
return false;
}

bool hasInheritLifetimeParamIndices, hasBorrowLifetimeParamIndices,
hasMutateLifetimeParamIndices;
ArrayRef<uint64_t> lifetimeDependenceData;
LifetimeDependenceLayout::readRecord(
scratch, hasInheritLifetimeParamIndices, hasBorrowLifetimeParamIndices,
hasMutateLifetimeParamIndices, lifetimeDependenceData);

unsigned startIndex = 0;
auto pushData = [&](LifetimeDependenceKind kind) {
for (unsigned i = 0; i < numParams + 1; i++) {
if (lifetimeDependenceData[startIndex + i]) {
specifierList.push_back(
LifetimeDependenceSpecifier::getOrderedLifetimeDependenceSpecifier(
SourceLoc(), kind, i));
}
}
startIndex += numParams + 1;
};

if (hasInheritLifetimeParamIndices) {
pushData(LifetimeDependenceKind::Consume);
}
if (hasBorrowLifetimeParamIndices) {
pushData(LifetimeDependenceKind::Borrow);
}
if (hasMutateLifetimeParamIndices) {
pushData(LifetimeDependenceKind::Mutate);
}
return true;
}
4 changes: 4 additions & 0 deletions lib/Serialization/ModuleFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,10 @@ class ModuleFile
/// Reads a foreign async convention from \c DeclTypeCursor, if present.
llvm::Optional<ForeignAsyncConvention> maybeReadForeignAsyncConvention();

bool maybeReadLifetimeDependence(
SmallVectorImpl<LifetimeDependenceSpecifier> &specifierList,
unsigned numParams);

/// Reads inlinable body text from \c DeclTypeCursor, if present.
llvm::Optional<StringRef> maybeReadInlinableBodyText();

Expand Down
10 changes: 9 additions & 1 deletion lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t SWIFTMODULE_VERSION_MINOR = 846; // always trigger mismatch for NoncopyableGenerics
const uint16_t SWIFTMODULE_VERSION_MINOR = 847; // add LifetimeDependence

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down Expand Up @@ -2174,6 +2174,14 @@ namespace decls_block {
BCFixed<1> // completion handler error flag polarity
>;

using LifetimeDependenceLayout =
BCRecordLayout<LIFETIME_DEPENDENCE,
BCFixed<1>, // hasInheritLifetimeParamIndices
BCFixed<1>, // hasBorrowLifetimeParamIndices
BCFixed<1>, // hasMutateLifetimeParamIndices
BCArray<BCFixed<1>> // concatenated param indices
>;

using AbstractClosureExprLayout = BCRecordLayout<
ABSTRACT_CLOSURE_EXPR_CONTEXT,
TypeIDField, // type
Expand Down
21 changes: 21 additions & 0 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3509,6 +3509,20 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
fac.completionHandlerFlagIsErrorOnZero());
}

void
writeLifetimeDependenceInfo(LifetimeDependenceInfo lifetimeDependenceInfo) {
using namespace decls_block;
SmallVector<bool> paramIndices;
lifetimeDependenceInfo.getConcatenatedData(paramIndices);

auto abbrCode = S.DeclTypeAbbrCodes[LifetimeDependenceLayout::Code];
LifetimeDependenceLayout::emitRecord(
S.Out, S.ScratchRecord, abbrCode,
lifetimeDependenceInfo.hasInheritLifetimeParamIndices(),
lifetimeDependenceInfo.hasBorrowLifetimeParamIndices(),
lifetimeDependenceInfo.hasMutateLifetimeParamIndices(), paramIndices);
}

void writeGenericParams(const GenericParamList *genericParams) {
using namespace decls_block;

Expand Down Expand Up @@ -4482,6 +4496,12 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
// Write the body parameters.
writeParameterList(fn->getParameters());

auto fnType = ty->getAs<FunctionType>();
if (fnType && fnType->hasLifetimeDependenceInfo()) {
assert(!fnType->getLifetimeDependenceInfo().empty());
writeLifetimeDependenceInfo(fnType->getLifetimeDependenceInfo());
}

if (auto errorConvention = fn->getForeignErrorConvention())
writeForeignErrorConvention(*errorConvention);
if (auto asyncConvention = fn->getForeignAsyncConvention())
Expand Down Expand Up @@ -5949,6 +5969,7 @@ void Serializer::writeAllDeclsAndTypes() {

registerDeclTypeAbbr<ForeignErrorConventionLayout>();
registerDeclTypeAbbr<ForeignAsyncConventionLayout>();
registerDeclTypeAbbr<LifetimeDependenceLayout>();
registerDeclTypeAbbr<AbstractClosureExprLayout>();
registerDeclTypeAbbr<PatternBindingInitializerLayout>();
registerDeclTypeAbbr<DefaultArgumentInitializerLayout>();
Expand Down
3 changes: 2 additions & 1 deletion test/Parse/explicit_lifetime_dependence_specifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func invalidSpecifier3(_ x: borrowing BufferView) -> _borrow(*) BufferView { //
return BufferView(x.ptr)
}

func invalidSpecifier4(_ x: borrowing BufferView) -> _borrow(0) BufferView {
// TODO: Diagnose using param indices on func decls in sema
func invalidSpecifier4(_ x: borrowing BufferView) -> _borrow(0) BufferView { // expected-error{{invalid lifetime dependence specifier, self is valid in non-static methods only}}
return BufferView(x.ptr)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
public struct BufferView : ~Escapable {
let ptr: UnsafeRawBufferPointer
@_unsafeNonescapableResult
public init(_ ptr: UnsafeRawBufferPointer) {
self.ptr = ptr
}
}

public struct MutableBufferView : ~Escapable, ~Copyable {
let ptr: UnsafeMutableRawBufferPointer
@_unsafeNonescapableResult
public init(_ ptr: UnsafeMutableRawBufferPointer) {
self.ptr = ptr
}
}

public func derive(_ x: borrowing BufferView) -> _borrow(x) BufferView {
return BufferView(x.ptr)
}

public func use(_ x: borrowing BufferView) {}

public func borrowAndCreate(_ view: borrowing BufferView) -> _borrow(view) BufferView {
return BufferView(view.ptr)
}

public func consumeAndCreate(_ view: consuming BufferView) -> _consume(view) BufferView {
return BufferView(view.ptr)
}

public func deriveThisOrThat(_ this: borrowing BufferView, _ that: borrowing BufferView) -> _borrow(this, that) BufferView {
if (Int.random(in: 1..<100) == 0) {
return BufferView(this.ptr)
}
return BufferView(that.ptr)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
public struct BufferView : ~Escapable {
let ptr: UnsafeRawBufferPointer
@_unsafeNonescapableResult
public init(_ ptr: UnsafeRawBufferPointer) {
self.ptr = ptr
}
}

public struct MutableBufferView : ~Escapable, ~Copyable {
let ptr: UnsafeMutableRawBufferPointer
@_unsafeNonescapableResult
public init(_ ptr: UnsafeMutableRawBufferPointer) {
self.ptr = ptr
}
}

public func derive(_ x: borrowing BufferView) -> BufferView {
return BufferView(x.ptr)
}

public func use(_ x: borrowing BufferView) {}

public func borrowAndCreate(_ view: borrowing BufferView) -> BufferView {
return BufferView(view.ptr)
}

public func consumeAndCreate(_ view: consuming BufferView) -> BufferView {
return BufferView(view.ptr)
}

34 changes: 34 additions & 0 deletions test/Serialization/explicit_lifetime_dependence.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/def_explicit_lifetime_dependence.swift \
// RUN: -enable-experimental-feature NonescapableTypes \
// RUN: -disable-experimental-parser-round-trip \
// RUN: -enable-experimental-feature NoncopyableGenerics

// RUN: llvm-bcanalyzer %t/def_explicit_lifetime_dependence.swiftmodule

// RUN: %target-swift-frontend -module-name lifetime-dependence -emit-silgen -I %t %s \
// RUN: -enable-experimental-feature NonescapableTypes \
// RUN: -disable-experimental-parser-round-trip \
// RUN: -enable-experimental-feature NoncopyableGenerics | %FileCheck %s

// REQUIRES: noncopyable_generics

import def_explicit_lifetime_dependence

func testBasic() {
let capacity = 4
let a = Array(0..<capacity)
a.withUnsafeBytes {
let view = BufferView($0)
let derivedView = derive(view)
let consumedView = consumeAndCreate(derivedView)
let borrowedView = borrowAndCreate(consumedView)
let mysteryView = deriveThisOrThat(borrowedView, consumedView)
use(mysteryView)
}
}

// CHECK: sil @$s32def_explicit_lifetime_dependence6deriveyAA10BufferViewVADF : $@convention(thin) (@guaranteed BufferView) -> _borrow(1) @owned BufferView
// CHECK: sil @$s32def_explicit_lifetime_dependence16consumeAndCreateyAA10BufferViewVADnF : $@convention(thin) (@owned BufferView) -> _inherit(1) @owned BufferView
// CHECK: sil @$s32def_explicit_lifetime_dependence15borrowAndCreateyAA10BufferViewVADF : $@convention(thin) (@guaranteed BufferView) -> _borrow(1) @owned BufferView
// CHECK: sil @$s32def_explicit_lifetime_dependence16deriveThisOrThatyAA10BufferViewVAD_ADtF : $@convention(thin) (@guaranteed BufferView, @guaranteed BufferView) -> _borrow(1, 2) @owned BufferView
Loading