Skip to content

Commit b1abccc

Browse files
authored
Merge branch 'main' into wip-generic-reqs-from-actor-da-for-accessor
2 parents f895a70 + b90b326 commit b1abccc

27 files changed

+1845
-62
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LetPropertyLowering.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ private func constructLetInitRegion(
135135
// root-class initializer).
136136
initRegion.insert(markUninitialized)
137137

138-
var beginBorrows = Stack<BeginBorrowInst>(context)
139-
defer { beginBorrows.deinitialize() }
138+
var borrows = Stack<BorrowIntroducingInstruction>(context)
139+
defer { borrows.deinitialize() }
140140

141141
for inst in markUninitialized.parentFunction.instructions {
142142
switch inst {
@@ -161,8 +161,13 @@ private func constructLetInitRegion(
161161
// Include let-field partial de-initializations in the region.
162162
initRegion.insert(inst)
163163

164-
case let beginBorrow as BeginBorrowInst:
165-
beginBorrows.append(beginBorrow)
164+
case let beginBorrow as BeginBorrowInst
165+
where beginBorrow.borrowedValue.referenceRoot == markUninitialized:
166+
borrows.append(beginBorrow)
167+
168+
case let storeBorrow as StoreBorrowInst
169+
where storeBorrow.source.referenceRoot == markUninitialized:
170+
borrows.append(storeBorrow)
166171

167172
default:
168173
break
@@ -171,8 +176,8 @@ private func constructLetInitRegion(
171176

172177
// Extend the region to whole borrow scopes to avoid that we insert an `end_init_let_ref` in the
173178
// middle of a borrow scope.
174-
for beginBorrow in beginBorrows where initRegion.contains(beginBorrow) {
175-
initRegion.insert(contentsOf: beginBorrow.endBorrows)
179+
for borrow in borrows where initRegion.contains(borrow) {
180+
initRegion.insert(borrowScopeOf: borrow, context)
176181
}
177182
}
178183

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,3 +532,25 @@ extension GlobalVariable {
532532
}
533533
}
534534
}
535+
536+
extension InstructionRange {
537+
/// Adds the instruction range of a borrow-scope by transitively visiting all (potential) re-borrows.
538+
mutating func insert(borrowScopeOf borrow: BorrowIntroducingInstruction, _ context: some Context) {
539+
var worklist = ValueWorklist(context)
540+
defer { worklist.deinitialize() }
541+
542+
worklist.pushIfNotVisited(borrow)
543+
while let value = worklist.pop() {
544+
for use in value.uses {
545+
switch use.instruction {
546+
case let endBorrow as EndBorrowInst:
547+
self.insert(endBorrow)
548+
case let branch as BranchInst:
549+
worklist.pushIfNotVisited(branch.getArgument(for: use))
550+
default:
551+
break
552+
}
553+
}
554+
}
555+
}
556+
}

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ extension UnaryInstruction {
234234
final public class UnimplementedInstruction : Instruction {
235235
}
236236

237-
public protocol StoringInstruction : AnyObject {
237+
public protocol StoringInstruction : Instruction {
238238
var operands: OperandArray { get }
239239
}
240240

@@ -270,6 +270,8 @@ final public class StoreInst : Instruction, StoringInstruction {
270270
final public class StoreWeakInst : Instruction, StoringInstruction { }
271271
final public class StoreUnownedInst : Instruction, StoringInstruction { }
272272

273+
final public class StoreBorrowInst : SingleValueInstruction, StoringInstruction, BorrowIntroducingInstruction { }
274+
273275
final public class AssignInst : Instruction, StoringInstruction {
274276
// must match with enum class swift::AssignOwnershipQualifier
275277
public enum AssignOwnership: Int {
@@ -495,6 +497,9 @@ extension LoadInstruction {
495497
public var address: Value { operand.value }
496498
}
497499

500+
/// Instructions, beginning a borrow-scope which must be ended by `end_borrow`.
501+
public protocol BorrowIntroducingInstruction : SingleValueInstruction {}
502+
498503
final public class LoadInst : SingleValueInstruction, LoadInstruction {
499504
// must match with enum class LoadOwnershipQualifier
500505
public enum LoadOwnership: Int {
@@ -507,7 +512,7 @@ final public class LoadInst : SingleValueInstruction, LoadInstruction {
507512

508513
final public class LoadWeakInst : SingleValueInstruction, LoadInstruction {}
509514
final public class LoadUnownedInst : SingleValueInstruction, LoadInstruction {}
510-
final public class LoadBorrowInst : SingleValueInstruction, LoadInstruction {}
515+
final public class LoadBorrowInst : SingleValueInstruction, LoadInstruction, BorrowIntroducingInstruction {}
511516

512517
final public class BuiltinInst : SingleValueInstruction {
513518
public typealias ID = BridgedInstruction.BuiltinValueKind
@@ -872,16 +877,9 @@ extension BeginAccessInst : ScopedInstruction {
872877
}
873878
}
874879

875-
final public class BeginBorrowInst : SingleValueInstruction, UnaryInstruction {
880+
final public class BeginBorrowInst : SingleValueInstruction, UnaryInstruction, BorrowIntroducingInstruction {
876881
public var borrowedValue: Value { operand.value }
877882

878-
public typealias EndBorrowSequence = LazyMapSequence<LazyFilterSequence<LazyMapSequence<UseList, EndBorrowInst?>>,
879-
EndBorrowInst>
880-
881-
public var endBorrows: EndBorrowSequence {
882-
uses.lazy.compactMap({ $0.instruction as? EndBorrowInst })
883-
}
884-
885883
public var isLexical: Bool { bridged.BeginBorrow_isLexical() }
886884
public var hasPointerEscape: Bool { bridged.BeginBorrow_hasPointerEscape() }
887885
}

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public func registerSILClasses() {
4141
register(StoreInst.self)
4242
register(StoreWeakInst.self)
4343
register(StoreUnownedInst.self)
44+
register(StoreBorrowInst.self)
4445
register(AssignInst.self)
4546
register(CopyAddrInst.self)
4647
register(EndAccessInst.self)

include/swift/AST/Attr.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,9 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(actor, Actor,
530530
CONTEXTUAL_SIMPLE_DECL_ATTR(_resultDependsOnSelf, ResultDependsOnSelf,
531531
OnFunc | DeclModifier | UserInaccessible | ABIBreakingToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove,
532532
150)
533+
SIMPLE_DECL_ATTR(_staticExclusiveOnly, StaticExclusiveOnly,
534+
OnStruct | UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove,
535+
151)
533536

534537
#undef TYPE_ATTR
535538
#undef DECL_ATTR_ALIAS

include/swift/AST/DiagnosticsSema.def

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,20 @@ WARNING(extern_c_maybe_invalid_name, none,
19171917
"C name '%0' may be invalid; explicitly specify the name in @_extern(c) to suppress this warning",
19181918
(StringRef))
19191919

1920+
// @_staticExclusiveOnly
1921+
ERROR(attr_static_exclusive_only_disabled,none,
1922+
"attribute requires '-enable-experimental feature StaticExclusiveOnly'", ())
1923+
ERROR(attr_static_exclusive_only_noncopyable,none,
1924+
"@_staticExclusiveOnly can only be applied to noncopyable types", ())
1925+
ERROR(attr_static_exclusive_only_let_only,none,
1926+
"variable of type %0 must be declared with a 'let'", (Type))
1927+
NOTE(attr_static_exclusive_only_type_nonmutating,none,
1928+
"%0 is a non-mutable type", (Type))
1929+
ERROR(attr_static_exclusive_only_let_only_param,none,
1930+
"parameter of type %0 must be declared as either 'borrowing' or 'consuming'", (Type))
1931+
ERROR(attr_static_exclusive_only_mutating,none,
1932+
"type %0 cannot have mutating function %1", (Type, ValueDecl *))
1933+
19201934
ERROR(c_func_variadic, none,
19211935
"cannot declare variadic argument %0 in %kind1",
19221936
(DeclName, const ValueDecl *))

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ EXPERIMENTAL_FEATURE(Extern, true)
259259
// global functions and key paths.
260260
EXPERIMENTAL_FEATURE(InferSendableFromCaptures, false)
261261

262+
/// Enable the `@_staticExclusiveOnly` attribute.
263+
EXPERIMENTAL_FEATURE(StaticExclusiveOnly, true)
264+
262265
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
263266
#undef EXPERIMENTAL_FEATURE
264267
#undef UPCOMING_FEATURE

include/swift/SILOptimizer/Utils/ConstantFolding.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ class SILOptFunctionBuilder;
3131
/// The \p ID must be the ID of a binary bit-operation builtin.
3232
APInt constantFoldBitOperation(APInt lhs, APInt rhs, BuiltinValueKind ID);
3333

34+
/// Evaluates the constant result of a floating point comparison.
35+
///
36+
/// The \p ID must be the ID of a floating point builtin operation.
37+
APInt constantFoldComparisonFloat(APFloat lhs, APFloat rhs,
38+
BuiltinValueKind ID);
39+
3440
/// Evaluates the constant result of an integer comparison.
3541
///
3642
/// The \p ID must be the ID of an integer builtin operation.
37-
APInt constantFoldComparison(APInt lhs, APInt rhs, BuiltinValueKind ID);
43+
APInt constantFoldComparisonInt(APInt lhs, APInt rhs, BuiltinValueKind ID);
3844

3945
/// Evaluates the constant result of a binary operation with overflow.
4046
///

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3640,6 +3640,10 @@ static bool usesFeatureExtern(Decl *decl) {
36403640
return decl->getAttrs().hasAttribute<ExternAttr>();
36413641
}
36423642

3643+
static bool usesFeatureStaticExclusiveOnly(Decl *decl) {
3644+
return decl->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>();
3645+
}
3646+
36433647
/// Suppress the printing of a particular feature.
36443648
static void suppressingFeature(PrintOptions &options, Feature feature,
36453649
llvm::function_ref<void()> action) {

lib/SILGen/SILGen.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ void SILGenModule::visitVarDecl(VarDecl *vd) {
18021802
if (vd->hasStorage())
18031803
addGlobalVariable(vd);
18041804

1805-
vd->visitEmittedAccessors([&](AccessorDecl *accessor) {
1805+
visitEmittedAccessors(vd, [&](AccessorDecl *accessor) {
18061806
emitFunction(accessor);
18071807
});
18081808

@@ -1825,6 +1825,16 @@ void SILGenModule::visitMacroExpansionDecl(MacroExpansionDecl *d) {
18251825
// Expansion already visited as auxiliary decls.
18261826
}
18271827

1828+
void SILGenModule::visitEmittedAccessors(
1829+
AbstractStorageDecl *D, llvm::function_ref<void(AccessorDecl *)> callback) {
1830+
D->visitEmittedAccessors([&](AccessorDecl *accessor) {
1831+
if (shouldSkipDecl(accessor))
1832+
return;
1833+
1834+
callback(accessor);
1835+
});
1836+
}
1837+
18281838
bool
18291839
SILGenModule::canStorageUseStoredKeyPathComponent(AbstractStorageDecl *decl,
18301840
ResilienceExpansion expansion) {

lib/SILGen/SILGen.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
301301
void visitMacroDecl(MacroDecl *d);
302302
void visitMacroExpansionDecl(MacroExpansionDecl *d);
303303

304+
// Same as AbstractStorageDecl::visitEmittedAccessors, but skips over skipped
305+
// (unavailable) decls.
306+
void visitEmittedAccessors(AbstractStorageDecl *D,
307+
llvm::function_ref<void(AccessorDecl *)>);
308+
304309
void emitEntryPoint(SourceFile *SF);
305310
void emitEntryPoint(SourceFile *SF, SILFunction *TopLevel);
306311

lib/SILGen/SILGenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ void SILGenFunction::visitVarDecl(VarDecl *D) {
16281628
});
16291629

16301630
// Emit the variable's accessors.
1631-
D->visitEmittedAccessors([&](AccessorDecl *accessor) {
1631+
SGM.visitEmittedAccessors(D, [&](AccessorDecl *accessor) {
16321632
SGM.emitFunction(accessor);
16331633
});
16341634
}

lib/SILGen/SILGenTopLevel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ void SILGenTopLevel::visitAbstractFunctionDecl(AbstractFunctionDecl *AFD) {
257257
}
258258

259259
void SILGenTopLevel::visitAbstractStorageDecl(AbstractStorageDecl *ASD) {
260-
ASD->visitEmittedAccessors(
260+
SGF.SGM.visitEmittedAccessors(ASD,
261261
[this](AccessorDecl *Accessor) { visitAbstractFunctionDecl(Accessor); });
262262
}
263263

@@ -338,7 +338,7 @@ void SILGenTopLevel::TypeVisitor::visitAbstractFunctionDecl(
338338

339339
void SILGenTopLevel::TypeVisitor::visitAbstractStorageDecl(
340340
AbstractStorageDecl *ASD) {
341-
ASD->visitEmittedAccessors(
341+
SGF.SGM.visitEmittedAccessors(ASD,
342342
[this](AccessorDecl *Accessor) { visitAbstractFunctionDecl(Accessor); });
343343
}
344344

lib/SILGen/SILGenType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ class SILGenType : public TypeMemberVisitor<SILGenType> {
12421242
}
12431243

12441244
void visitAccessors(AbstractStorageDecl *asd) {
1245-
asd->visitEmittedAccessors([&](AccessorDecl *accessor) {
1245+
SGM.visitEmittedAccessors(asd, [&](AccessorDecl *accessor) {
12461246
visitFuncDecl(accessor);
12471247
});
12481248
}
@@ -1422,7 +1422,7 @@ class SILGenExtension : public TypeMemberVisitor<SILGenExtension> {
14221422
}
14231423

14241424
void visitAccessors(AbstractStorageDecl *asd) {
1425-
asd->visitEmittedAccessors([&](AccessorDecl *accessor) {
1425+
SGM.visitEmittedAccessors(asd, [&](AccessorDecl *accessor) {
14261426
visitFuncDecl(accessor);
14271427
});
14281428
}

0 commit comments

Comments
 (0)