Skip to content

Commit d02765f

Browse files
committed
Swift SIL: some new APIs and some refactoring
1 parent 5c0ae94 commit d02765f

File tree

7 files changed

+71
-11
lines changed

7 files changed

+71
-11
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ struct FunctionPassContext : MutatingContext {
168168
var notifyInstructionChanged: (Instruction) -> () { return { inst in } }
169169

170170
func continueWithNextSubpassRun(for inst: Instruction? = nil) -> Bool {
171-
let bridgedInst = OptionalBridgedInstruction(inst?.bridged.obj)
172-
return _bridged.continueWithNextSubpassRun(bridgedInst)
171+
return _bridged.continueWithNextSubpassRun(inst.bridged)
173172
}
174173

175174
func createSimplifyContext(preserveDebugInfo: Bool, notifyInstructionChanged: @escaping (Instruction) -> ()) -> SimplifyContext {

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public struct Builder {
113113
return notifyNew(cast.getAs(UpcastInst.self))
114114
}
115115

116-
public func createLoad(fromAddress: Value, ownership: LoadInst.LoadOwnership) -> LoadInst {
116+
public func createLoad(fromAddress: Value, ownership: LoadInst.Ownership) -> LoadInst {
117117
let load = bridged.createLoad(fromAddress.bridged, ownership.rawValue)
118118
return notifyNew(load.getAs(LoadInst.self))
119119
}
@@ -184,6 +184,12 @@ public struct Builder {
184184
let ued = bridged.createUncheckedEnumData(enumVal.bridged, caseIndex, resultType.bridged)
185185
return notifyNew(ued.getAs(UncheckedEnumDataInst.self))
186186
}
187+
188+
public func createEnum(caseIndex: Int, payload: Value?, enumType: Type) -> EnumInst {
189+
let enumInst = bridged.createEnum(caseIndex, payload.bridged, enumType.bridged)
190+
return notifyNew(enumInst.getAs(EnumInst.self))
191+
}
192+
187193
@discardableResult
188194
public func createSwitchEnum(enum enumVal: Value,
189195
cases: [(Int, BasicBlock)],
@@ -225,20 +231,23 @@ public struct Builder {
225231
return notifyNew(bridged.createGlobalValue(global.bridged).getAs(GlobalValueInst.self))
226232
}
227233

228-
@discardableResult
229234
public func createStruct(type: Type, elements: [Value]) -> StructInst {
230235
let structInst = elements.withBridgedValues { valuesRef in
231236
return bridged.createStruct(type.bridged, valuesRef)
232237
}
233238
return notifyNew(structInst.getAs(StructInst.self))
234239
}
235240

236-
@discardableResult
237241
public func createTuple(type: Type, elements: [Value]) -> TupleInst {
238242
let tuple = elements.withBridgedValues { valuesRef in
239243
return bridged.createTuple(type.bridged, valuesRef)
240244
}
241245
return notifyNew(tuple.getAs(TupleInst.self))
242246
}
243247

248+
@discardableResult
249+
public func createStore(source: Value, destination: Value, ownership: StoreInst.Ownership) -> StoreInst {
250+
let store = bridged.createStore(source.bridged, destination.bridged, ownership.rawValue)
251+
return notifyNew(store.getAs(StoreInst.self))
252+
}
244253
}

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ extension OptionalBridgedInstruction {
148148
}
149149
}
150150

151+
extension Optional where Wrapped == Instruction {
152+
public var bridged: OptionalBridgedInstruction {
153+
OptionalBridgedInstruction(self?.bridged.obj)
154+
}
155+
}
156+
151157
public class SingleValueInstruction : Instruction, Value {
152158
final public var definingInstruction: Instruction? { self }
153159

@@ -222,11 +228,23 @@ extension StoringInstruction {
222228

223229
final public class StoreInst : Instruction, StoringInstruction {
224230
// must match with enum class StoreOwnershipQualifier
225-
public enum StoreOwnership: Int {
231+
public enum Ownership: Int {
226232
case unqualified = 0, initialize = 1, assign = 2, trivial = 3
233+
234+
public init(for type: Type, in function: Function, initialize: Bool) {
235+
if function.hasOwnership {
236+
if type.isTrivial(in: function) {
237+
self = .trivial
238+
} else {
239+
self = initialize ? .initialize : .assign
240+
}
241+
} else {
242+
self = .unqualified
243+
}
244+
}
227245
}
228-
public var destinationOwnership: StoreOwnership {
229-
StoreOwnership(rawValue: bridged.StoreInst_getStoreOwnership())!
246+
public var destinationOwnership: Ownership {
247+
Ownership(rawValue: bridged.StoreInst_getStoreOwnership())!
230248
}
231249
}
232250

@@ -307,6 +325,7 @@ final public class DestroyAddrInst : Instruction, UnaryInstruction {
307325
}
308326

309327
final public class InjectEnumAddrInst : Instruction, UnaryInstruction, EnumInstruction {
328+
public var `enum`: Value { operand.value }
310329
public var caseIndex: Int { bridged.InjectEnumAddrInst_caseIndex() }
311330
}
312331

@@ -351,11 +370,11 @@ final public class LoadInst : SingleValueInstruction, UnaryInstruction {
351370
public var address: Value { operand.value }
352371

353372
// must match with enum class LoadOwnershipQualifier
354-
public enum LoadOwnership: Int {
373+
public enum Ownership: Int {
355374
case unqualified = 0, take = 1, copy = 2, trivial = 3
356375
}
357-
public var ownership: LoadOwnership {
358-
LoadOwnership(rawValue: bridged.LoadInst_getLoadOwnership())!
376+
public var ownership: Ownership {
377+
Ownership(rawValue: bridged.LoadInst_getLoadOwnership())!
359378
}
360379
}
361380

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
3131
return !bridged.isNonTrivialOrContainsRawPointer(function.bridged.getFunction())
3232
}
3333

34+
public func isLoadable(in function: Function) -> Bool {
35+
return bridged.isLoadable(function.bridged.getFunction())
36+
}
37+
3438
public func isReferenceCounted(in function: Function) -> Bool {
3539
return bridged.isReferenceCounted(function.bridged.getFunction())
3640
}

SwiftCompilerSources/Sources/SIL/Value.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,9 @@ final class PlaceholderValue : Value {
204204
extension OptionalBridgedValue {
205205
public var value: Value? { obj.getAs(AnyObject.self) as? Value }
206206
}
207+
208+
extension Optional where Wrapped == Value {
209+
public var bridged: OptionalBridgedValue {
210+
OptionalBridgedValue(obj: self?.bridged.obj)
211+
}
212+
}

include/swift/SIL/SILBridging.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ struct BridgedValue {
8888

8989
struct OptionalBridgedValue {
9090
OptionalSwiftObject obj;
91+
92+
swift::SILValue getSILValue() const {
93+
if (obj)
94+
return static_cast<swift::ValueBase *>(obj);
95+
return swift::SILValue();
96+
}
9197
};
9298

9399
inline swift::ValueOwnershipKind castToOwnership(BridgedValue::Ownership ownership) {
@@ -1173,6 +1179,14 @@ struct BridgedBuilder{
11731179
en->getType().getEnumElement(caseIdx), resultType)};
11741180
}
11751181

1182+
SWIFT_IMPORT_UNSAFE
1183+
BridgedInstruction createEnum(SwiftInt caseIdx, OptionalBridgedValue payload,
1184+
swift::SILType resultType) const {
1185+
swift::EnumElementDecl *caseDecl = resultType.getEnumElement(caseIdx);
1186+
swift::SILValue pl = payload.getSILValue();
1187+
return {builder().createEnum(regularLoc(), pl, caseDecl, resultType)};
1188+
}
1189+
11761190
SWIFT_IMPORT_UNSAFE
11771191
BridgedInstruction createBranch(BridgedBasicBlock destBlock, BridgedValueArray arguments) const {
11781192
llvm::SmallVector<swift::SILValue, 16> argValues;
@@ -1212,6 +1226,13 @@ struct BridgedBuilder{
12121226
llvm::SmallVector<swift::SILValue, 16> elementValues;
12131227
return {builder().createTuple(regularLoc(), type, elements.getValues(elementValues))};
12141228
}
1229+
1230+
SWIFT_IMPORT_UNSAFE
1231+
BridgedInstruction createStore(BridgedValue src, BridgedValue dst,
1232+
SwiftInt ownership) const {
1233+
return {builder().createStore(regularLoc(), src.getSILValue(), dst.getSILValue(),
1234+
(swift::StoreOwnershipQualifier)ownership)};
1235+
}
12151236
};
12161237

12171238
// AST bridging

include/swift/SIL/SILType.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ class SILType {
346346
return !isAddressOnly(F);
347347
}
348348

349+
bool isLoadable(const SILFunction *f) const { return isLoadable(*f); }
350+
349351
/// True if either:
350352
/// 1) The type, or the referenced type of an address type, is loadable.
351353
/// 2) The SIL Module conventions uses lowered addresses

0 commit comments

Comments
 (0)