Skip to content

Commit 4dd6c73

Browse files
authored
Merge pull request #64380 from eeckstein/sil-bridging
use C++ interop for SIL bridging
2 parents e010d7d + 67299b4 commit 4dd6c73

Some content is hidden

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

56 files changed

+2300
-2792
lines changed

SwiftCompilerSources/Sources/Optimizer/Analysis/AliasAnalysis.swift

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,26 @@ struct AliasAnalysis {
1717
let bridged: BridgedAliasAnalysis
1818

1919
func mayRead(_ inst: Instruction, fromAddress: Value) -> Bool {
20-
switch AliasAnalysis_getMemBehavior(bridged, inst.bridged, fromAddress.bridged) {
21-
case MayReadBehavior, MayReadWriteBehavior, MayHaveSideEffectsBehavior:
20+
switch bridged.getMemBehavior(inst.bridged, fromAddress.bridged) {
21+
case .MayRead, .MayReadWrite, .MayHaveSideEffects:
2222
return true
2323
default:
2424
return false
2525
}
2626
}
2727

2828
func mayWrite(_ inst: Instruction, toAddress: Value) -> Bool {
29-
switch AliasAnalysis_getMemBehavior(bridged, inst.bridged, toAddress.bridged) {
30-
case MayWriteBehavior, MayReadWriteBehavior, MayHaveSideEffectsBehavior:
29+
switch bridged.getMemBehavior(inst.bridged, toAddress.bridged) {
30+
case .MayWrite, .MayReadWrite, .MayHaveSideEffects:
3131
return true
3232
default:
3333
return false
3434
}
3535
}
3636

3737
func mayReadOrWrite(_ inst: Instruction, address: Value) -> Bool {
38-
switch AliasAnalysis_getMemBehavior(bridged, inst.bridged, address.bridged) {
39-
case MayReadBehavior, MayWriteBehavior, MayReadWriteBehavior,
40-
MayHaveSideEffectsBehavior:
38+
switch bridged.getMemBehavior(inst.bridged, address.bridged) {
39+
case .MayRead, .MayWrite, .MayReadWrite, .MayHaveSideEffects:
4140
return true
4241
default:
4342
return false
@@ -62,26 +61,26 @@ struct AliasAnalysis {
6261
}
6362

6463
static func register() {
65-
AliasAnalysis_register(
64+
BridgedAliasAnalysis.registerAnalysis(
6665
// getMemEffectsFn
67-
{ (bridgedCtxt: BridgedPassContext, bridgedVal: BridgedValue, bridgedInst: BridgedInstruction) -> BridgedMemoryBehavior in
66+
{ (bridgedCtxt: BridgedPassContext, bridgedVal: BridgedValue, bridgedInst: BridgedInstruction) -> swift.MemoryBehavior in
6867
let context = FunctionPassContext(_bridged: bridgedCtxt)
6968
let inst = bridgedInst.instruction
7069
let val = bridgedVal.value
7170
let path = AliasAnalysis.getPtrOrAddressPath(for: val)
7271
if let apply = inst as? ApplySite {
7372
let effect = getMemoryEffect(of: apply, for: val, path: path, context)
7473
switch (effect.read, effect.write) {
75-
case (false, false): return NoneBehavior
76-
case (true, false): return MayReadBehavior
77-
case (false, true): return MayWriteBehavior
78-
case (true, true): return MayReadWriteBehavior
74+
case (false, false): return .None
75+
case (true, false): return .MayRead
76+
case (false, true): return .MayWrite
77+
case (true, true): return .MayReadWrite
7978
}
8079
}
8180
if val.at(path).isEscaping(using: EscapesToInstructionVisitor(target: inst, isAddress: true), context) {
82-
return MayReadWriteBehavior
81+
return .MayReadWrite
8382
}
84-
return NoneBehavior
83+
return .None
8584
},
8685

8786
// isObjReleasedFn

SwiftCompilerSources/Sources/Optimizer/Analysis/CalleeAnalysis.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public struct CalleeAnalysis {
1717
let bridged: BridgedCalleeAnalysis
1818

1919
static func register() {
20-
CalleeAnalysis_register(
20+
BridgedCalleeAnalysis.registerAnalysis(
2121
// isDeinitBarrierFn:
2222
{ (inst : BridgedInstruction, bca: BridgedCalleeAnalysis) -> Bool in
2323
return inst.instruction.isDeinitBarrier(bca.analysis)
2424
},
2525
// getMemBehaviorFn
26-
{ (bridgedCtxt: BridgedPassContext, bridgedApply: BridgedInstruction, observeRetains: Bool) -> BridgedMemoryBehavior in
26+
{ (bridgedCtxt: BridgedPassContext, bridgedApply: BridgedInstruction, observeRetains: Bool) -> swift.MemoryBehavior in
2727
let context = FunctionPassContext(_bridged: bridgedCtxt)
2828
let apply = bridgedApply.instruction as! ApplySite
2929
let e = context.calleeAnalysis.getSideEffects(of: apply)
@@ -33,28 +33,28 @@ public struct CalleeAnalysis {
3333
}
3434

3535
public func getCallees(callee: Value) -> FunctionArray? {
36-
let bridgedFuncs = CalleeAnalysis_getCallees(bridged, callee.bridged)
37-
if bridgedFuncs.incomplete != 0 {
36+
let bridgedFuncs = bridged.getCallees(callee.bridged)
37+
if bridgedFuncs.isIncomplete() {
3838
return nil
3939
}
4040
return FunctionArray(bridged: bridgedFuncs)
4141
}
4242

4343
public func getIncompleteCallees(callee: Value) -> FunctionArray {
44-
return FunctionArray(bridged: CalleeAnalysis_getCallees(bridged, callee.bridged))
44+
return FunctionArray(bridged: bridged.getCallees(callee.bridged))
4545
}
4646

4747
public func getDestructor(ofExactType type: Type) -> Function? {
48-
let destructors = FunctionArray(bridged: CalleeAnalysis_getDestructors(bridged, type.bridged, /*isExactType*/ 1))
48+
let destructors = FunctionArray(bridged: bridged.getDestructors(type.bridged, /*isExactType*/ true))
4949
if destructors.count == 1 {
5050
return destructors[0]
5151
}
5252
return nil
5353
}
5454

5555
public func getDestructors(of type: Type) -> FunctionArray? {
56-
let bridgedDtors = CalleeAnalysis_getDestructors(bridged, type.bridged, /*isExactType*/ 0)
57-
if bridgedDtors.incomplete != 0 {
56+
let bridgedDtors = bridged.getDestructors(type.bridged, /*isExactType*/ false)
57+
if bridgedDtors.isIncomplete() {
5858
return nil
5959
}
6060
return FunctionArray(bridged: bridgedDtors)
@@ -123,13 +123,13 @@ extension Instruction {
123123
}
124124

125125
public struct FunctionArray : RandomAccessCollection, FormattedLikeArray {
126-
fileprivate let bridged: BridgedCalleeList
126+
fileprivate let bridged: swift.CalleeList
127127

128128
public var startIndex: Int { 0 }
129-
public var endIndex: Int { BridgedFunctionArray_size(bridged) }
129+
public var endIndex: Int { Int(bridged.getCount()) }
130130

131131
public subscript(_ index: Int) -> Function {
132-
return BridgedFunctionArray_get(bridged, index).function
132+
return BridgedCalleeAnalysis.getCallee(bridged, index).function
133133
}
134134
}
135135
// Bridging utilities

SwiftCompilerSources/Sources/Optimizer/Analysis/DeadEndBlocksAnalysis.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public struct DeadEndBlocksAnalysis {
1717
let bridged: BridgedDeadEndBlocksAnalysis
1818

1919
public func isDeadEnd(_ block: BasicBlock) -> Bool {
20-
return DeadEndBlocksAnalysis_isDeadEnd(bridged, block.bridged) != 0
20+
return bridged.isDeadEnd(block.bridged)
2121
}
2222
}

SwiftCompilerSources/Sources/Optimizer/Analysis/DominatorTree.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public struct DominatorTree {
1919

2020
extension BasicBlock {
2121
func dominates(_ other: BasicBlock, _ domTree: DominatorTree) -> Bool {
22-
DominatorTree_dominates(domTree.bridged, self.bridged, other.bridged) != 0
22+
domTree.bridged.dominates(self.bridged, other.bridged)
2323
}
2424

2525
func strictlyDominates(_ other: BasicBlock, _ domTree: DominatorTree) -> Bool {

SwiftCompilerSources/Sources/Optimizer/Analysis/PostDominatorTree.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public struct PostDominatorTree {
1919

2020
extension BasicBlock {
2121
func postDominates(_ other: BasicBlock, _ pdomTree: PostDominatorTree) -> Bool {
22-
PostDominatorTree_postDominates(pdomTree.bridged, self.bridged, other.bridged) != 0
22+
pdomTree.bridged.postDominates(self.bridged, other.bridged)
2323
}
2424

2525
func strictlyPostDominates(_ other: BasicBlock, _ pdomTree: PostDominatorTree) -> Bool {

SwiftCompilerSources/Sources/Optimizer/DataStructures/Set.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,33 @@ struct BasicBlockSet : IntrusiveSet {
3838

3939
init(_ context: some Context) {
4040
self.context = context._bridged
41-
self.bridged = PassContext_allocBasicBlockSet(self.context)
41+
self.bridged = self.context.allocBasicBlockSet()
4242
}
4343

4444
func contains(_ block: BasicBlock) -> Bool {
45-
BasicBlockSet_contains(bridged, block.bridged) != 0
45+
bridged.contains(block.bridged)
4646
}
4747

4848
/// Returns true if `block` was not contained in the set before inserting.
4949
@discardableResult
5050
mutating func insert(_ block: BasicBlock) -> Bool {
51-
BasicBlockSet_insert(bridged, block.bridged) != 0
51+
bridged.insert(block.bridged)
5252
}
5353

5454
mutating func erase(_ block: BasicBlock) {
55-
BasicBlockSet_erase(bridged, block.bridged)
55+
bridged.erase(block.bridged)
5656
}
5757

5858
var description: String {
59-
let function = BasicBlockSet_getFunction(bridged).function
59+
let function = bridged.getFunction().function
6060
let blockNames = function.blocks.enumerated().filter { contains($0.1) }
6161
.map { "bb\($0.0)"}
6262
return "{" + blockNames.joined(separator: ", ") + "}"
6363
}
6464

6565
/// TODO: once we have move-only types, make this a real deinit.
6666
mutating func deinitialize() {
67-
PassContext_freeBasicBlockSet(context, bridged)
67+
context.freeBasicBlockSet(bridged)
6868
}
6969
}
7070

@@ -83,25 +83,25 @@ struct ValueSet : IntrusiveSet {
8383

8484
init(_ context: some Context) {
8585
self.context = context._bridged
86-
self.bridged = PassContext_allocNodeSet(self.context)
86+
self.bridged = self.context.allocNodeSet()
8787
}
8888

8989
func contains(_ value: Value) -> Bool {
90-
NodeSet_containsValue(bridged, value.bridged) != 0
90+
bridged.containsValue(value.bridged)
9191
}
9292

9393
/// Returns true if `value` was not contained in the set before inserting.
9494
@discardableResult
9595
mutating func insert(_ value: Value) -> Bool {
96-
NodeSet_insertValue(bridged, value.bridged) != 0
96+
bridged.insertValue(value.bridged)
9797
}
9898

9999
mutating func erase(_ value: Value) {
100-
NodeSet_eraseValue(bridged, value.bridged)
100+
bridged.eraseValue(value.bridged)
101101
}
102102

103103
var description: String {
104-
let function = NodeSet_getFunction(bridged).function
104+
let function = bridged.getFunction().function
105105
var d = "{\n"
106106
for block in function.blocks {
107107
for arg in block.arguments {
@@ -123,7 +123,7 @@ struct ValueSet : IntrusiveSet {
123123

124124
/// TODO: once we have move-only types, make this a real deinit.
125125
mutating func deinitialize() {
126-
PassContext_freeNodeSet(context, bridged)
126+
context.freeNodeSet(bridged)
127127
}
128128
}
129129

@@ -142,25 +142,25 @@ struct InstructionSet : IntrusiveSet {
142142

143143
init(_ context: some Context) {
144144
self.context = context._bridged
145-
self.bridged = PassContext_allocNodeSet(self.context)
145+
self.bridged = self.context.allocNodeSet()
146146
}
147147

148148
func contains(_ inst: Instruction) -> Bool {
149-
NodeSet_containsInstruction(bridged, inst.bridged) != 0
149+
bridged.containsInstruction(inst.bridged)
150150
}
151151

152152
/// Returns true if `inst` was not contained in the set before inserting.
153153
@discardableResult
154154
mutating func insert(_ inst: Instruction) -> Bool {
155-
NodeSet_insertInstruction(bridged, inst.bridged) != 0
155+
bridged.insertInstruction(inst.bridged)
156156
}
157157

158158
mutating func erase(_ inst: Instruction) {
159-
NodeSet_eraseInstruction(bridged, inst.bridged)
159+
bridged.eraseInstruction(inst.bridged)
160160
}
161161

162162
var description: String {
163-
let function = NodeSet_getFunction(bridged).function
163+
let function = bridged.getFunction().function
164164
var d = "{\n"
165165
for inst in function.instructions {
166166
if contains(inst) {
@@ -173,6 +173,6 @@ struct InstructionSet : IntrusiveSet {
173173

174174
/// TODO: once we have move-only types, make this a real deinit.
175175
mutating func deinitialize() {
176-
PassContext_freeNodeSet(context, bridged)
176+
context.freeNodeSet(bridged)
177177
}
178178
}

SwiftCompilerSources/Sources/Optimizer/DataStructures/Stack.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ import SIL
2727
struct Stack<Element> : CollectionLikeSequence {
2828

2929
private let bridgedContext: BridgedPassContext
30-
private var firstSlab = BridgedSlab(data: nil)
31-
private var lastSlab = BridgedSlab(data: nil)
30+
private var firstSlab = BridgedPassContext.Slab(nil)
31+
private var lastSlab = BridgedPassContext.Slab(nil)
3232
private var endIndex: Int = 0
3333

3434
private static var slabCapacity: Int {
35-
BridgedSlabCapacity / MemoryLayout<Element>.stride
35+
BridgedPassContext.Slab.getCapacity() / MemoryLayout<Element>.stride
3636
}
3737

38-
private static func bind(_ slab: BridgedSlab) -> UnsafeMutablePointer<Element> {
39-
return slab.data!.bindMemory(to: Element.self, capacity: Stack.slabCapacity)
38+
private static func bind(_ slab: BridgedPassContext.Slab) -> UnsafeMutablePointer<Element> {
39+
return UnsafeMutableRawPointer(slab.data!).bindMemory(to: Element.self, capacity: Stack.slabCapacity)
4040
}
4141

4242
struct Iterator : IteratorProtocol {
43-
var slab: BridgedSlab
43+
var slab: BridgedPassContext.Slab
4444
var index: Int
45-
let lastSlab: BridgedSlab
45+
let lastSlab: BridgedPassContext.Slab
4646
let endIndex: Int
4747

4848
mutating func next() -> Element? {
@@ -52,7 +52,7 @@ struct Stack<Element> : CollectionLikeSequence {
5252
index += 1
5353

5454
if index >= end && slab.data != lastSlab.data {
55-
slab = PassContext_getNextSlab(slab)
55+
slab = slab.getNext()
5656
index = 0
5757
}
5858
return elem
@@ -77,11 +77,11 @@ struct Stack<Element> : CollectionLikeSequence {
7777

7878
mutating func push(_ element: Element) {
7979
if endIndex >= Stack.slabCapacity {
80-
lastSlab = PassContext_allocSlab(bridgedContext, lastSlab)
80+
lastSlab = bridgedContext.allocSlab(lastSlab)
8181
endIndex = 0
8282
} else if firstSlab.data == nil {
8383
assert(endIndex == 0)
84-
firstSlab = PassContext_allocSlab(bridgedContext, lastSlab)
84+
firstSlab = bridgedContext.allocSlab(lastSlab)
8585
lastSlab = firstSlab
8686
}
8787
(Stack.bind(lastSlab) + endIndex).initialize(to: element)
@@ -109,12 +109,12 @@ struct Stack<Element> : CollectionLikeSequence {
109109

110110
if endIndex == 0 {
111111
if lastSlab.data == firstSlab.data {
112-
_ = PassContext_freeSlab(bridgedContext, lastSlab)
112+
_ = bridgedContext.freeSlab(lastSlab)
113113
firstSlab.data = nil
114114
lastSlab.data = nil
115115
endIndex = 0
116116
} else {
117-
lastSlab = PassContext_freeSlab(bridgedContext, lastSlab)
117+
lastSlab = bridgedContext.freeSlab(lastSlab)
118118
endIndex = Stack.slabCapacity
119119
}
120120
}

0 commit comments

Comments
 (0)