Skip to content

Commit b707b5a

Browse files
committed
Swift SIL: improve the Builder
* add new create-functions for instructions * allow the Builder to build static initializer instructions for global variables * some refactoring to simplify the implementation
1 parent a4225a9 commit b707b5a

File tree

7 files changed

+190
-96
lines changed

7 files changed

+190
-96
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ReleaseDevirtualizer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private func tryDevirtualizeReleaseOfObject(
9292

9393
var object: Value = allocRefInstruction
9494
if object.type != type {
95-
object = builder.createUncheckedRefCast(object: object, type: type)
95+
object = builder.createUncheckedRefCast(from: object, to: type)
9696
}
9797

9898
// Do what a release would do before calling the deallocator: set the object

SwiftCompilerSources/Sources/SIL/BasicBlock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ final public class BasicBlock : CustomStringConvertible, HasShortDescription {
6565

6666
public var name: String { "bb\(index)" }
6767

68-
public var bridged: BridgedBasicBlock { BridgedBasicBlock(obj: SwiftObject(self)) }
68+
public var bridged: BridgedBasicBlock { BridgedBasicBlock(SwiftObject(self)) }
6969
}
7070

7171
public func == (lhs: BasicBlock, rhs: BasicBlock) -> Bool { lhs === rhs }

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public struct Builder {
1919
public enum InsertionPoint {
2020
case before(Instruction)
2121
case atEndOf(BasicBlock)
22+
case staticInitializer(GlobalVariable)
2223
}
2324

2425
let insertAt: InsertionPoint
@@ -29,12 +30,13 @@ public struct Builder {
2930
public var bridged: BridgedBuilder {
3031
switch insertAt {
3132
case .before(let inst):
32-
return BridgedBuilder(insertBefore: inst.bridged.optional,
33-
insertAtEnd: OptionalBridgedBasicBlock.none,
33+
return BridgedBuilder(insertAt: .beforeInst, insertionObj: inst.bridged.obj,
3434
loc: location.bridged)
3535
case .atEndOf(let block):
36-
return BridgedBuilder(insertBefore: OptionalBridgedInstruction(),
37-
insertAtEnd: block.bridged.optional,
36+
return BridgedBuilder(insertAt: .endOfBlock, insertionObj: block.bridged.obj,
37+
loc: location.bridged)
38+
case .staticInitializer(let global):
39+
return BridgedBuilder(insertAt: .intoGlobal, insertionObj: global.bridged.obj,
3840
loc: location.bridged)
3941
}
4042
}
@@ -101,9 +103,19 @@ public struct Builder {
101103
return notifyNew(dr.getAs(DeallocStackRefInst.self))
102104
}
103105

104-
public func createUncheckedRefCast(object: Value, type: Type) -> UncheckedRefCastInst {
105-
let object = bridged.createUncheckedRefCast(object.bridged, type.bridged)
106-
return notifyNew(object.getAs(UncheckedRefCastInst.self))
106+
public func createUncheckedRefCast(from value: Value, to type: Type) -> UncheckedRefCastInst {
107+
let cast = bridged.createUncheckedRefCast(value.bridged, type.bridged)
108+
return notifyNew(cast.getAs(UncheckedRefCastInst.self))
109+
}
110+
111+
public func createUpcast(from value: Value, to type: Type) -> UpcastInst {
112+
let cast = bridged.createUpcast(value.bridged, type.bridged)
113+
return notifyNew(cast.getAs(UpcastInst.self))
114+
}
115+
116+
public func createLoad(fromAddress: Value, ownership: LoadInst.LoadOwnership) -> LoadInst {
117+
let load = bridged.createLoad(fromAddress.bridged, ownership.rawValue)
118+
return notifyNew(load.getAs(LoadInst.self))
107119
}
108120

109121
@discardableResult
@@ -112,6 +124,18 @@ public struct Builder {
112124
return notifyNew(setDeallocating.getAs(SetDeallocatingInst.self))
113125
}
114126

127+
@discardableResult
128+
public func createStrongRetain(operand: Value) -> StrongRetainInst {
129+
let retain = bridged.createStrongRetain(operand.bridged)
130+
return notifyNew(retain.getAs(StrongRetainInst.self))
131+
}
132+
133+
@discardableResult
134+
public func createStrongRelease(operand: Value) -> StrongReleaseInst {
135+
let release = bridged.createStrongRelease(operand.bridged)
136+
return notifyNew(release.getAs(StrongReleaseInst.self))
137+
}
138+
115139
public func createFunctionRef(_ function: Function) -> FunctionRefInst {
116140
let functionRef = bridged.createFunctionRef(function.bridged)
117141
return notifyNew(functionRef.getAs(FunctionRefInst.self))
@@ -184,4 +208,37 @@ public struct Builder {
184208
let ui = bridged.createUnreachable()
185209
return notifyNew(ui.getAs(UnreachableInst.self))
186210
}
211+
212+
@discardableResult
213+
public func createObject(type: Type, arguments: [Value], numBaseElements: Int) -> ObjectInst {
214+
let objectInst = arguments.withBridgedValues { valuesRef in
215+
return bridged.createObject(type.bridged, valuesRef, numBaseElements)
216+
}
217+
return notifyNew(objectInst.getAs(ObjectInst.self))
218+
}
219+
220+
public func createGlobalAddr(global: GlobalVariable) -> GlobalAddrInst {
221+
return notifyNew(bridged.createGlobalAddr(global.bridged).getAs(GlobalAddrInst.self))
222+
}
223+
224+
public func createGlobalValue(global: GlobalVariable) -> GlobalValueInst {
225+
return notifyNew(bridged.createGlobalValue(global.bridged).getAs(GlobalValueInst.self))
226+
}
227+
228+
@discardableResult
229+
public func createStruct(type: Type, elements: [Value]) -> StructInst {
230+
let structInst = elements.withBridgedValues { valuesRef in
231+
return bridged.createStruct(type.bridged, valuesRef)
232+
}
233+
return notifyNew(structInst.getAs(StructInst.self))
234+
}
235+
236+
@discardableResult
237+
public func createTuple(type: Type, elements: [Value]) -> TupleInst {
238+
let tuple = elements.withBridgedValues { valuesRef in
239+
return bridged.createTuple(type.bridged, valuesRef)
240+
}
241+
return notifyNew(tuple.getAs(TupleInst.self))
242+
}
243+
187244
}

SwiftCompilerSources/Sources/SIL/GlobalVariable.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ final public class GlobalVariable : CustomStringConvertible, HasShortDescription
6161
hasher.combine(ObjectIdentifier(self))
6262
}
6363

64-
public var bridged: BridgedGlobalVar { BridgedGlobalVar(obj: SwiftObject(self)) }
64+
public var bridged: BridgedGlobalVar { BridgedGlobalVar(SwiftObject(self)) }
6565
}
6666

6767
extension Instruction {
@@ -78,4 +78,8 @@ extension BridgedGlobalVar {
7878

7979
extension OptionalBridgedGlobalVar {
8080
public var globalVar: GlobalVariable? { obj.getAs(GlobalVariable.self) }
81+
82+
public static var none: OptionalBridgedGlobalVar {
83+
OptionalBridgedGlobalVar(obj: nil)
84+
}
8185
}

0 commit comments

Comments
 (0)