Skip to content

Commit 12acde2

Browse files
authored
Merge pull request #75096 from meg-gupta/lifetimedeparg
Add support for lifetime dependence in parameter position
2 parents 8fca31e + 422ed2b commit 12acde2

File tree

64 files changed

+1008
-686
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1008
-686
lines changed

SwiftCompilerSources/Sources/SIL/FunctionConvention.swift

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,18 @@ public struct FunctionConvention : CustomStringConvertible {
7777
/// Collection of LifetimeDependenceConvention indexed on the
7878
/// function parameter.
7979
public var resultDependencies: ResultDependencies? {
80-
let deps = bridgedFunctionType.SILFunctionType_getLifetimeDependenceInfo()
81-
if deps.empty() {
82-
return nil
80+
let bridgedDependencies = bridgedFunctionType.SILFunctionType_getLifetimeDependencies()
81+
let dependencies = LifetimeDependencies(bridged: bridgedDependencies)
82+
let targetIndex = parameters.count
83+
84+
for dependence in dependencies {
85+
if dependence.getTargetIndex() == targetIndex {
86+
return ResultDependencies(bridged: dependence,
87+
parameterCount: parameters.count,
88+
hasSelfParameter: hasSelfParameter)
89+
}
8390
}
84-
return ResultDependencies(bridged: deps,
85-
parameterCount: parameters.count,
86-
hasSelfParameter: hasSelfParameter)
91+
return nil
8792
}
8893

8994
public var description: String {
@@ -242,6 +247,26 @@ public enum LifetimeDependenceConvention : CustomStringConvertible {
242247
}
243248
}
244249

250+
extension FunctionConvention {
251+
struct LifetimeDependencies : Collection {
252+
let bridged: BridgedLifetimeDependenceInfoArray
253+
254+
var startIndex: Int { 0 }
255+
256+
var endIndex: Int { bridged.count() }
257+
258+
func index(after index: Int) -> Int {
259+
return index + 1
260+
}
261+
// Create a Swift LifetimeDependenceInfo for BridgedLifetimeDependenceInfo if this method needs
262+
// to be exposed outside FunctionConvention.
263+
// That will likely need bridging IndexSubset to Swift.
264+
subscript(_ index: Int) -> BridgedLifetimeDependenceInfo {
265+
return bridged.at(index)
266+
}
267+
}
268+
}
269+
245270
extension FunctionConvention {
246271
/// Collection of LifetimeDependenceConvention? that parallels parameters.
247272
public struct ResultDependencies : Collection, CustomStringConvertible {

docs/ABI/Mangling.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ Types
727727
C-TYPE is mangled according to the Itanium ABI, and prefixed with the length.
728728
Non-ASCII identifiers are preserved as-is; we do not use Punycode.
729729

730-
function-signature ::= params-type params-type async? sendable? throws? differentiable? function-isolation? self-lifetime-dependence? // results and parameters
730+
function-signature ::= params-type params-type async? sendable? throws? differentiable? function-isolation? // results and parameters
731731

732732
params-type ::= type 'z'? 'h'? // tuple in case of multiple parameters or a single parameter with a single tuple type
733733
// with optional inout convention, shared convention. parameters don't have labels,
@@ -749,10 +749,8 @@ Types
749749
differentiable ::= 'Yjd' // @differentiable on function type
750750
differentiable ::= 'Yjl' // @differentiable(_linear) on function type
751751
#if SWIFT_RUNTIME_VERSION >= 5.TBD
752-
lifetime-dependence ::= 'Yli' // inherit lifetime dependence on param
753-
lifetime-dependence ::= 'Yls' // scoped lifetime dependence on param
754-
self-lifetime-dependence ::= 'YLi' // inherit lifetime dependence on self
755-
self-lifetime-dependence ::= 'YLs' // scoped lifetime dependence on self
752+
lifetime-dependence ::= 'Yli' INDEX-SUBSET '_' // inherit lifetime dependence
753+
lifetime-dependence ::= 'Yls' INDEX-SUBSET '_' // scoped lifetime dependence
756754
#endif
757755
type-list ::= list-type '_' list-type* // list of types
758756
type-list ::= empty-list

include/swift/AST/ASTMangler.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ class ASTMangler : public Mangler {
9191
/// a critical role.
9292
bool AllowTypedThrows = true;
9393

94+
/// If enabled, lifetime dependencies can be encoded in the mangled name.
95+
bool AllowLifetimeDependencies = true;
96+
9497
/// If enabled, declarations annotated with @_originallyDefinedIn are mangled
9598
/// as if they're part of their original module. Disabled for debug mangling,
9699
/// because lldb wants to find declarations in the modules they're currently
@@ -556,14 +559,15 @@ class ASTMangler : public Mangler {
556559
FunctionManglingKind functionMangling,
557560
bool isRecursedInto = true);
558561

559-
void appendFunctionInputType(ArrayRef<AnyFunctionType::Param> params,
560-
LifetimeDependenceInfo lifetimeDependenceInfo,
562+
void appendFunctionInputType(AnyFunctionType *fnType,
563+
ArrayRef<AnyFunctionType::Param> params,
561564
GenericSignature sig,
562565
const ValueDecl *forDecl = nullptr,
563566
bool isRecursedInto = true);
564-
void appendFunctionResultType(Type resultType,
565-
GenericSignature sig,
566-
const ValueDecl *forDecl = nullptr);
567+
void appendFunctionResultType(
568+
Type resultType, GenericSignature sig,
569+
std::optional<LifetimeDependenceInfo> lifetimeDependence,
570+
const ValueDecl *forDecl = nullptr);
567571

568572
void appendTypeList(Type listTy, GenericSignature sig,
569573
const ValueDecl *forDecl = nullptr);
@@ -573,7 +577,7 @@ class ASTMangler : public Mangler {
573577
const ValueDecl *forDecl = nullptr);
574578
void appendParameterTypeListElement(
575579
Identifier name, Type elementType, ParameterTypeFlags flags,
576-
std::optional<LifetimeDependenceKind> lifetimeDependenceKind,
580+
std::optional<LifetimeDependenceInfo> lifetimeDependence,
577581
GenericSignature sig, const ValueDecl *forDecl = nullptr);
578582
void appendTupleTypeListElement(Identifier name, Type elementType,
579583
GenericSignature sig,
@@ -749,8 +753,7 @@ class ASTMangler : public Mangler {
749753
void appendConstrainedExistential(Type base, GenericSignature sig,
750754
const ValueDecl *forDecl);
751755

752-
void appendLifetimeDependenceKind(LifetimeDependenceKind kind,
753-
bool isSelfDependence);
756+
void appendLifetimeDependence(LifetimeDependenceInfo info);
754757
};
755758

756759
} // end namespace Mangle

include/swift/AST/ASTPrinter.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,22 @@ class ASTPrinter {
364364
return printedClangDecl.insert(d).second;
365365
}
366366

367+
void printLifetimeDependence(
368+
std::optional<LifetimeDependenceInfo> lifetimeDependence) {
369+
if (!lifetimeDependence.has_value()) {
370+
return;
371+
}
372+
*this << lifetimeDependence->getString();
373+
}
374+
375+
void printLifetimeDependenceAt(
376+
ArrayRef<LifetimeDependenceInfo> lifetimeDependencies, unsigned index) {
377+
if (auto lifetimeDependence =
378+
getLifetimeDependenceFor(lifetimeDependencies, index)) {
379+
printLifetimeDependence(*lifetimeDependence);
380+
}
381+
}
382+
367383
private:
368384
virtual void anchor();
369385
};

include/swift/AST/Decl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7482,7 +7482,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
74827482

74837483
/// Add the given derivative function configuration.
74847484
void addDerivativeFunctionConfiguration(const AutoDiffConfig &config);
7485-
std::optional<LifetimeDependenceInfo> getLifetimeDependenceInfo() const;
7485+
std::optional<llvm::ArrayRef<LifetimeDependenceInfo>>
7486+
getLifetimeDependencies() const;
74867487

74877488
protected:
74887489
// If a function has a body at all, we have either a parsed body AST node or

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7984,8 +7984,8 @@ ERROR(lifetime_dependence_invalid_self_ownership, none,
79847984
ERROR(lifetime_dependence_only_on_function_method_init_result, none,
79857985
"lifetime dependence specifiers may only be used on result of "
79867986
"functions, methods, initializers", ())
7987-
ERROR(lifetime_dependence_invalid_return_type, none,
7988-
"lifetime dependence can only be specified on ~Escapable results", ())
7987+
ERROR(lifetime_dependence_invalid_type, none,
7988+
"lifetime dependence can only be specified on ~Escapable types", ())
79897989
ERROR(lifetime_dependence_cannot_infer_ambiguous_candidate, none,
79907990
"cannot infer lifetime dependence %0, multiple parameters qualifiy as a candidate", (StringRef))
79917991
ERROR(lifetime_dependence_cannot_infer_no_candidates, none,

0 commit comments

Comments
 (0)