Skip to content

Commit 2361a38

Browse files
authored
Merge branch 'apple:main' into main
2 parents 49ea89d + 51580e3 commit 2361a38

File tree

120 files changed

+3579
-2079
lines changed

Some content is hidden

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

120 files changed

+3579
-2079
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjectOutliner.swift

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import SIL
4141
///
4242
let objectOutliner = FunctionPass(name: "object-outliner") {
4343
(function: Function, context: FunctionPassContext) in
44-
4544
for inst in function.instructions {
4645
if let ari = inst as? AllocRefInstBase {
4746
if let globalValue = optimizeObjectAllocation(allocRef: ari, context) {
@@ -85,6 +84,10 @@ private func findEndCOWMutation(of object: Value) -> EndCOWMutationInst? {
8584
if let ecm = findEndCOWMutation(of: uci) {
8685
return ecm
8786
}
87+
case let urci as UncheckedRefCastInst:
88+
if let ecm = findEndCOWMutation(of: urci) {
89+
return ecm
90+
}
8891
case let mv as MoveValueInst:
8992
if let ecm = findEndCOWMutation(of: mv) {
9093
return ecm
@@ -147,7 +150,7 @@ private func findInitStores(of object: Value,
147150
return false
148151
}
149152
default:
150-
if !isValidUseOfObject(use.instruction) {
153+
if !isValidUseOfObject(use) {
151154
return false
152155
}
153156
}
@@ -174,6 +177,18 @@ private func findStores(toTailAddress tailAddr: Value, tailElementIndex: Int, st
174177
if !findStores(inUsesOf: tea, index: tailElementIndex * numTupleElements + tupleIdx, stores: &stores) {
175178
return false
176179
}
180+
case let atp as AddressToPointerInst:
181+
if !findStores(toTailAddress: atp, tailElementIndex: tailElementIndex, stores: &stores) {
182+
return false
183+
}
184+
case let mdi as MarkDependenceInst:
185+
if !findStores(toTailAddress: mdi, tailElementIndex: tailElementIndex, stores: &stores) {
186+
return false
187+
}
188+
case let pta as PointerToAddressInst:
189+
if !findStores(toTailAddress: pta, tailElementIndex: tailElementIndex, stores: &stores) {
190+
return false
191+
}
177192
case let store as StoreInst:
178193
if store.source.type.isTuple {
179194
// This kind of SIL is never generated because tuples are stored with separated stores to tuple_element_addr.
@@ -184,7 +199,7 @@ private func findStores(toTailAddress tailAddr: Value, tailElementIndex: Int, st
184199
return false
185200
}
186201
default:
187-
if !isValidUseOfObject(use.instruction) {
202+
if !isValidUseOfObject(use) {
188203
return false
189204
}
190205
}
@@ -198,7 +213,7 @@ private func findStores(inUsesOf address: Value, index: Int, stores: inout [Stor
198213
if !handleStore(store, index: index, stores: &stores) {
199214
return false
200215
}
201-
} else if !isValidUseOfObject(use.instruction) {
216+
} else if !isValidUseOfObject(use) {
202217
return false
203218
}
204219
}
@@ -215,7 +230,8 @@ private func handleStore(_ store: StoreInst, index: Int, stores: inout [StoreIns
215230
return false
216231
}
217232

218-
private func isValidUseOfObject(_ inst: Instruction) -> Bool {
233+
private func isValidUseOfObject(_ use: Operand) -> Bool {
234+
let inst = use.instruction
219235
switch inst {
220236
case is DebugValueInst,
221237
is LoadInst,
@@ -227,6 +243,17 @@ private func isValidUseOfObject(_ inst: Instruction) -> Bool {
227243
is EndCOWMutationInst:
228244
return true
229245

246+
case let mdi as MarkDependenceInst:
247+
if (use == mdi.baseOperand) {
248+
return true;
249+
}
250+
for mdiUse in mdi.uses {
251+
if !isValidUseOfObject(mdiUse) {
252+
return false
253+
}
254+
}
255+
return true
256+
230257
case is StructElementAddrInst,
231258
is AddressToPointerInst,
232259
is StructInst,
@@ -238,9 +265,12 @@ private func isValidUseOfObject(_ inst: Instruction) -> Bool {
238265
is UpcastInst,
239266
is BeginDeallocRefInst,
240267
is RefTailAddrInst,
241-
is RefElementAddrInst:
242-
for use in (inst as! SingleValueInstruction).uses {
243-
if !isValidUseOfObject(use.instruction) {
268+
is RefElementAddrInst,
269+
is StructInst,
270+
is PointerToAddressInst,
271+
is IndexAddrInst:
272+
for instUse in (inst as! SingleValueInstruction).uses {
273+
if !isValidUseOfObject(instUse) {
244274
return false
245275
}
246276
}
@@ -342,6 +372,8 @@ private func rewriteUses(of startValue: Value, _ context: FunctionPassContext) {
342372
context.erase(instruction: endMutation)
343373
case let upCast as UpcastInst:
344374
worklist.pushIfNotVisited(usersOf: upCast)
375+
case let urci as UncheckedRefCastInst:
376+
worklist.pushIfNotVisited(usersOf: urci)
345377
case let moveValue as MoveValueInst:
346378
worklist.pushIfNotVisited(usersOf: moveValue)
347379
case is DeallocRefInst, is DeallocStackRefInst:

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,10 @@ final public class ThrowInst : TermInst, UnaryInstruction {
10941094
public override var isFunctionExiting: Bool { true }
10951095
}
10961096

1097+
final public class ThrowAddrInst : TermInst {
1098+
public override var isFunctionExiting: Bool { true }
1099+
}
1100+
10971101
final public class YieldInst : TermInst {
10981102
}
10991103

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public func registerSILClasses() {
172172
register(UnreachableInst.self)
173173
register(ReturnInst.self)
174174
register(ThrowInst.self)
175+
register(ThrowAddrInst.self)
175176
register(YieldInst.self)
176177
register(UnwindInst.self)
177178
register(TryApplyInst.self)

docs/SIL.rst

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,8 @@ the caller. A non-autoreleased ``apply`` of a function that is defined
613613
with an autoreleased result has the effect of performing an
614614
autorelease in the callee.
615615

616-
- SIL function types may provide an optional error result, written by
617-
placing ``@error`` on a result. An error result is always
616+
- SIL function types may provide an optional direct error result, written by
617+
placing ``@error`` on a result. A direct error result is always
618618
implicitly ``@owned``. Only functions with a native calling
619619
convention may have an error result.
620620

@@ -8006,6 +8006,28 @@ the basic block argument will be the operand of the ``throw``.
80068006

80078007
A function must not contain more than one ``throw`` instruction.
80088008

8009+
throw_addr
8010+
``````````
8011+
::
8012+
8013+
sil-terminator ::= 'throw_addr'
8014+
8015+
throw_addr
8016+
// indirect error result must be initialized at this point
8017+
8018+
Exits the current function and returns control to the calling
8019+
function. The current function must have an indirect error result,
8020+
and so the function must have been invoked with a ``try_apply``
8021+
instruction. Control will resume in the error destination of
8022+
that instruction.
8023+
8024+
The function is responsible for initializing its error result
8025+
before the ``throw_addr``.
8026+
8027+
``throw_addr`` does not retain or release any values.
8028+
8029+
A function must not contain more than one ``throw_addr`` instruction.
8030+
80098031
yield
80108032
`````
80118033
::

include/swift/AST/ASTBridging.h

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,73 @@
2626
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2727

2828
namespace swift {
29-
class DiagnosticArgument;
30-
class DiagnosticEngine;
29+
class ASTContext;
30+
class DiagnosticArgument;
31+
class DiagnosticEngine;
3132
}
3233

3334
//===----------------------------------------------------------------------===//
3435
// MARK: Identifier
3536
//===----------------------------------------------------------------------===//
3637

37-
struct BridgedIdentifier {
38-
const void *_Nullable raw;
38+
class BridgedIdentifier {
39+
public:
40+
SWIFT_UNAVAILABLE("Use '.raw' instead")
41+
const void *_Nullable Raw;
42+
43+
BridgedIdentifier() : Raw(nullptr) {}
44+
45+
SWIFT_NAME("init(raw:)")
46+
BridgedIdentifier(const void *_Nullable raw) : Raw(raw) {}
47+
48+
#ifdef USED_IN_CPP_SOURCE
49+
BridgedIdentifier(swift::Identifier ident)
50+
: Raw(ident.getAsOpaquePointer()) {}
51+
52+
swift::Identifier unbridged() const {
53+
return swift::Identifier::getFromOpaquePointer(Raw);
54+
}
55+
#endif
3956
};
4057

58+
SWIFT_NAME("getter:BridgedIdentifier.raw(self:)")
59+
inline const void *_Nullable BridgedIdentifier_raw(BridgedIdentifier ident) {
60+
return ident.Raw;
61+
}
62+
4163
struct BridgedIdentifierAndSourceLoc {
42-
BridgedIdentifier name;
43-
BridgedSourceLoc nameLoc;
64+
SWIFT_NAME("name")
65+
BridgedIdentifier Name;
66+
67+
SWIFT_NAME("nameLoc")
68+
BridgedSourceLoc NameLoc;
4469
};
4570

4671
//===----------------------------------------------------------------------===//
4772
// MARK: ASTContext
4873
//===----------------------------------------------------------------------===//
4974

50-
struct BridgedASTContext {
51-
void *_Nonnull raw;
75+
class BridgedASTContext {
76+
swift::ASTContext * _Nonnull Ctx;
77+
78+
public:
79+
#ifdef USED_IN_CPP_SOURCE
80+
SWIFT_UNAVAILABLE("Use init(raw:) instead")
81+
BridgedASTContext(swift::ASTContext &ctx) : Ctx(&ctx) {}
82+
83+
SWIFT_UNAVAILABLE("Use '.raw' instead")
84+
swift::ASTContext &unbridged() const { return *Ctx; }
85+
#endif
5286
};
5387

88+
SWIFT_NAME("getter:BridgedASTContext.raw(self:)")
89+
BRIDGED_INLINE
90+
void * _Nonnull BridgedASTContext_raw(BridgedASTContext bridged);
91+
92+
SWIFT_NAME("BridgedASTContext.init(raw:)")
93+
BRIDGED_INLINE
94+
BridgedASTContext BridgedASTContext_fromRaw(void * _Nonnull ptr);
95+
5496
SWIFT_NAME("BridgedASTContext.getIdentifier(self:_:)")
5597
BridgedIdentifier BridgedASTContext_getIdentifier(BridgedASTContext cContext,
5698
BridgedStringRef cStr);
@@ -70,8 +112,11 @@ enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : size_t {
70112
};
71113

72114
struct BridgedASTNode {
73-
void *_Nonnull ptr;
74-
ASTNodeKind kind;
115+
SWIFT_NAME("raw")
116+
void *_Nonnull Raw;
117+
118+
SWIFT_NAME("kind")
119+
ASTNodeKind Kind;
75120
};
76121

77122
// Forward declare the underlying AST node type for each wrapper.
@@ -81,11 +126,12 @@ namespace swift {
81126
} // end namespace swift
82127

83128
// Define the bridging wrappers for each AST node.
84-
#define AST_BRIDGING_WRAPPER(Name) BRIDGING_WRAPPER_NONNULL(Name)
129+
#define AST_BRIDGING_WRAPPER(Name) BRIDGING_WRAPPER_NONNULL(swift::Name, Name)
85130
#include "swift/AST/ASTBridgingWrappers.def"
86131

87132
// For nullable nodes, also define a nullable variant.
88-
#define AST_BRIDGING_WRAPPER_NULLABLE(Name) BRIDGING_WRAPPER_NULLABLE(Name)
133+
#define AST_BRIDGING_WRAPPER_NULLABLE(Name) \
134+
BRIDGING_WRAPPER_NULLABLE(swift::Name, Name)
89135
#define AST_BRIDGING_WRAPPER_NONNULL(Name)
90136
#include "swift/AST/ASTBridgingWrappers.def"
91137

@@ -150,7 +196,7 @@ class BridgedDiagnosticArgument {
150196
BridgedDiagnosticArgument(const swift::DiagnosticArgument &arg) {
151197
*reinterpret_cast<swift::DiagnosticArgument *>(&storage) = arg;
152198
}
153-
const swift::DiagnosticArgument &get() const {
199+
const swift::DiagnosticArgument &unbridged() const {
154200
return *reinterpret_cast<const swift::DiagnosticArgument *>(&storage);
155201
}
156202
#endif
@@ -167,7 +213,7 @@ class BridgedDiagnosticFixIt {
167213
BridgedDiagnosticFixIt(const swift::DiagnosticInfo::FixIt &fixit){
168214
*reinterpret_cast<swift::DiagnosticInfo::FixIt *>(&storage) = fixit;
169215
}
170-
const swift::DiagnosticInfo::FixIt &get() const {
216+
const swift::DiagnosticInfo::FixIt &unbridged() const {
171217
return *reinterpret_cast<const swift::DiagnosticInfo::FixIt *>(&storage);
172218
}
173219
#endif
@@ -184,8 +230,18 @@ enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : size_t {
184230
BridgedNote,
185231
};
186232

187-
struct BridgedDiagnostic {
188-
void *_Nonnull raw;
233+
class BridgedDiagnostic {
234+
public:
235+
struct Impl;
236+
237+
SWIFT_UNAVAILABLE("Unavailable in Swift")
238+
Impl *_Nonnull Raw;
239+
240+
SWIFT_UNAVAILABLE("Unavailable in Swift")
241+
BridgedDiagnostic(Impl *_Nonnull raw) : Raw(raw) {}
242+
243+
SWIFT_UNAVAILABLE("Unavailable in Swift")
244+
Impl *_Nonnull unbridged() const { return Raw; }
189245
};
190246

191247
// FIXME: Can we bridge InFlightDiagnostic?

include/swift/AST/ASTBridgingImpl.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,36 @@
1717

1818
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
1919

20+
//===----------------------------------------------------------------------===//
21+
// MARK: BridgedASTContext
22+
//===----------------------------------------------------------------------===//
23+
24+
void * _Nonnull BridgedASTContext_raw(BridgedASTContext bridged) {
25+
return &bridged.unbridged();
26+
}
27+
28+
BridgedASTContext BridgedASTContext_fromRaw(void * _Nonnull ptr) {
29+
return *static_cast<swift::ASTContext *>(ptr);
30+
}
31+
2032
//===----------------------------------------------------------------------===//
2133
// MARK: BridgedNominalTypeDecl
2234
//===----------------------------------------------------------------------===//
2335

2436
BridgedStringRef BridgedNominalTypeDecl_getName(BridgedNominalTypeDecl decl) {
25-
return decl.get()->getName().str();
37+
return decl.unbridged()->getName().str();
2638
}
2739

2840
bool BridgedNominalTypeDecl_isGlobalActor(BridgedNominalTypeDecl decl) {
29-
return decl.get()->isGlobalActor();
41+
return decl.unbridged()->isGlobalActor();
3042
}
3143

3244
//===----------------------------------------------------------------------===//
3345
// MARK: BridgedVarDecl
3446
//===----------------------------------------------------------------------===//
3547

3648
BridgedStringRef BridgedVarDecl_getUserFacingName(BridgedVarDecl decl) {
37-
return decl.get()->getBaseName().userFacingName();
49+
return decl.unbridged()->getBaseName().userFacingName();
3850
}
3951

4052
SWIFT_END_NULLABILITY_ANNOTATIONS

0 commit comments

Comments
 (0)