Skip to content

Commit 24b62d4

Browse files
committed
Swift SIL: add a few utilities and instructions
* instructions: function_ref, mark_dependence * add `BuiltinInst.id` * add isObjC and canAllocOnStack for alloc_ref and alloc_ref_dynamic * add `ApplySite::referencedFunction` * add `Builder.createDeallocStackRef` * add == and != operators for `Function` * add `List.first` and `ReverseList.first`
1 parent 372ada0 commit 24b62d4

File tree

9 files changed

+93
-5
lines changed

9 files changed

+93
-5
lines changed

SwiftCompilerSources/Sources/SIL/ApplySite.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ extension ApplySite {
3636
return nil
3737
}
3838

39+
public var referencedFunction: Function? {
40+
if let fri = callee as? FunctionRefInst {
41+
return fri.referencedFunction
42+
}
43+
return nil
44+
}
3945
}
4046

4147
public protocol FullApplySite : ApplySite {

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,10 @@ public struct Builder {
6767
bridgedInsPoint, location.bridgedLocation, type.bridged, value)
6868
return literal.getAs(IntegerLiteralInst.self)
6969
}
70+
71+
public func createDeallocStackRef(_ operand: Value) -> DeallocStackRefInst {
72+
notifyInstructionsChanged()
73+
let dr = SILBuilder_createDeallocStackRef(bridgedInsPoint, location.bridgedLocation, operand.bridged)
74+
return dr.getAs(DeallocStackRefInst.self)
75+
}
7076
}

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ final public class Function : CustomStringConvertible {
5454
public var bridged: BridgedFunction { BridgedFunction(obj: SwiftObject(self)) }
5555
}
5656

57+
public func == (lhs: Function, rhs: Function) -> Bool { lhs === rhs }
58+
public func != (lhs: Function, rhs: Function) -> Bool { lhs !== rhs }
59+
5760
// Bridging utilities
5861

5962
extension BridgedFunction {

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class Instruction : ListNode, CustomStringConvertible, Hashable {
2929
SILInstruction_getParent(bridged).block
3030
}
3131

32+
final public var function: Function { block.function }
33+
3234
final public var description: String {
3335
SILNode_debugDescription(bridgedNode).takeString()
3436
}
@@ -263,7 +265,19 @@ final public class LoadInst : SingleValueInstruction, UnaryInstruction {}
263265

264266
final public class LoadBorrowInst : SingleValueInstruction, UnaryInstruction {}
265267

266-
final public class BuiltinInst : SingleValueInstruction {}
268+
final public class BuiltinInst : SingleValueInstruction {
269+
// TODO: find a way to directly reuse the BuiltinValueKind enum
270+
public enum ID {
271+
case None
272+
case DestroyArray
273+
}
274+
public var id: ID? {
275+
switch BuiltinInst_getID(bridged) {
276+
case DestroyArrayBuiltin: return .DestroyArray
277+
default: return .None
278+
}
279+
}
280+
}
267281

268282
final public class UpcastInst : SingleValueInstruction, UnaryInstruction {}
269283

@@ -324,6 +338,12 @@ public class GlobalAccessInst : SingleValueInstruction {
324338
}
325339
}
326340

341+
final public class FunctionRefInst : GlobalAccessInst {
342+
public var referencedFunction: Function {
343+
FunctionRefInst_getReferencedFunction(bridged).function
344+
}
345+
}
346+
327347
final public class GlobalAddrInst : GlobalAccessInst {}
328348

329349
final public class GlobalValueInst : GlobalAccessInst {}
@@ -398,6 +418,12 @@ class ObjCMetatypeToObjectInst : SingleValueInstruction, UnaryInstruction {}
398418
final public
399419
class ValueToBridgeObjectInst : SingleValueInstruction, UnaryInstruction {}
400420

421+
final public
422+
class MarkDependenceInst : SingleValueInstruction {
423+
public var value: Value { return operands[0].value }
424+
public var base: Value { return operands[1].value }
425+
}
426+
401427
final public class BridgeObjectToRefInst : SingleValueInstruction,
402428
UnaryInstruction {}
403429

@@ -442,10 +468,18 @@ public protocol Allocation : AnyObject { }
442468
final public class AllocStackInst : SingleValueInstruction, Allocation {
443469
}
444470

445-
final public class AllocRefInst : SingleValueInstruction, Allocation {
471+
public class AllocRefInstBase : SingleValueInstruction, Allocation {
472+
final public var isObjC: Bool { AllocRefInstBase_isObjc(bridged) != 0 }
473+
474+
final public var canAllocOnStack: Bool {
475+
AllocRefInstBase_canAllocOnStack(bridged) != 0
476+
}
477+
}
478+
479+
final public class AllocRefInst : AllocRefInstBase {
446480
}
447481

448-
final public class AllocRefDynamicInst : SingleValueInstruction, Allocation {
482+
final public class AllocRefDynamicInst : AllocRefInstBase {
449483
}
450484

451485
final public class AllocBoxInst : SingleValueInstruction, Allocation {

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public func registerSILClasses() {
7676
register(OpenExistentialMetatypeInst.self)
7777
register(ValueMetatypeInst.self)
7878
register(ExistentialMetatypeInst.self)
79+
register(FunctionRefInst.self)
7980
register(GlobalAddrInst.self)
8081
register(GlobalValueInst.self)
8182
register(IntegerLiteralInst.self)
@@ -96,6 +97,7 @@ public func registerSILClasses() {
9697
register(ObjCExistentialMetatypeToObjectInst.self)
9798
register(ObjCMetatypeToObjectInst.self)
9899
register(ValueToBridgeObjectInst.self)
100+
register(MarkDependenceInst.self)
99101
register(BridgeObjectToRefInst.self)
100102
register(BeginAccessInst.self)
101103
register(BeginBorrowInst.self)

SwiftCompilerSources/Sources/SIL/Utils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public struct List<NodeType: ListNode> :
3737
return nil
3838
}
3939

40+
public var first: NodeType? { currentNode }
41+
4042
public var customMirror: Mirror {
4143
let c: [Mirror.Child] = map { (label: nil, value: $0) }
4244
return Mirror(self, children: c)
@@ -58,6 +60,8 @@ public struct ReverseList<NodeType: ListNode> :
5860
return nil
5961
}
6062

63+
public var first: NodeType? { currentNode }
64+
6165
public var customMirror: Mirror {
6266
let c: [Mirror.Child] = map { (label: nil, value: $0) }
6367
return Mirror(self, children: c)

include/swift/SIL/SILBridging.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ typedef enum {
144144
MayHaveSideEffectsBehavior
145145
} BridgedMemoryBehavior;
146146

147+
typedef enum {
148+
UnknownBuiltin = 0,
149+
#define BUILTIN(Id, Name, Attrs) Id##Builtin,
150+
#include "swift/AST/Builtins.def"
151+
} BridgedBuiltinID;
147152

148153
typedef intptr_t SwiftInt;
149154

@@ -217,7 +222,9 @@ BridgedMultiValueResult
217222
BridgedArrayRef TermInst_getSuccessors(BridgedInstruction term);
218223

219224
BridgedStringRef CondFailInst_getMessage(BridgedInstruction cfi);
225+
BridgedBuiltinID BuiltinInst_getID(BridgedInstruction bi);
220226
BridgedGlobalVar GlobalAccessInst_getGlobal(BridgedInstruction globalInst);
227+
BridgedFunction FunctionRefInst_getReferencedFunction(BridgedInstruction fri);
221228
SwiftInt TupleExtractInst_fieldIndex(BridgedInstruction tei);
222229
SwiftInt TupleElementAddrInst_fieldIndex(BridgedInstruction teai);
223230
SwiftInt StructExtractInst_fieldIndex(BridgedInstruction sei);
@@ -227,6 +234,8 @@ SwiftInt UncheckedEnumDataInst_caseIndex(BridgedInstruction uedi);
227234
SwiftInt RefElementAddrInst_fieldIndex(BridgedInstruction reai);
228235
SwiftInt PartialApplyInst_numArguments(BridgedInstruction ai);
229236
SwiftInt ApplyInst_numArguments(BridgedInstruction ai);
237+
SwiftInt AllocRefInstBase_isObjc(BridgedInstruction arb);
238+
SwiftInt AllocRefInstBase_canAllocOnStack(BridgedInstruction arb);
230239
SwiftInt BeginApplyInst_numArguments(BridgedInstruction ai);
231240
SwiftInt TryApplyInst_numArguments(BridgedInstruction ai);
232241
BridgedBasicBlock BranchInst_getTargetBlock(BridgedInstruction bi);
@@ -243,6 +252,8 @@ BridgedInstruction SILBuilder_createCondFail(BridgedInstruction insertionPoint,
243252
BridgedLocation loc, BridgedValue condition, BridgedStringRef messge);
244253
BridgedInstruction SILBuilder_createIntegerLiteral(BridgedInstruction insertionPoint,
245254
BridgedLocation loc, BridgedType type, SwiftInt value);
255+
BridgedInstruction SILBuilder_createDeallocStackRef(BridgedInstruction insertionPoint,
256+
BridgedLocation loc, BridgedValue operand);
246257

247258
SWIFT_END_NULLABILITY_ANNOTATIONS
248259

include/swift/SIL/SILNodes.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
457457

458458
// Literals
459459
ABSTRACT_SINGLE_VALUE_INST(LiteralInst, SingleValueInstruction)
460-
SINGLE_VALUE_INST(FunctionRefInst, function_ref,
460+
BRIDGED_SINGLE_VALUE_INST(FunctionRefInst, function_ref,
461461
LiteralInst, None, DoesNotRelease)
462462
SINGLE_VALUE_INST(DynamicFunctionRefInst, dynamic_function_ref,
463463
LiteralInst, None, DoesNotRelease)
@@ -566,7 +566,7 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
566566
SingleValueInstruction, None, DoesNotRelease)
567567
BRIDGED_SINGLE_VALUE_INST(ValueToBridgeObjectInst, value_to_bridge_object,
568568
SingleValueInstruction, None, DoesNotRelease)
569-
SINGLE_VALUE_INST(MarkDependenceInst, mark_dependence,
569+
BRIDGED_SINGLE_VALUE_INST(MarkDependenceInst, mark_dependence,
570570
SingleValueInstruction, None, DoesNotRelease)
571571
SINGLE_VALUE_INST(CopyBlockInst, copy_block,
572572
SingleValueInstruction, MayHaveSideEffects, DoesNotRelease)

lib/SIL/Utils/SILBridging.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,18 @@ BridgedStringRef CondFailInst_getMessage(BridgedInstruction cfi) {
419419
return getBridgedStringRef(castToInst<CondFailInst>(cfi)->getMessage());
420420
}
421421

422+
BridgedBuiltinID BuiltinInst_getID(BridgedInstruction bi) {
423+
return (BridgedBuiltinID)castToInst<BuiltinInst>(bi)->getBuiltinInfo().ID;
424+
}
425+
422426
BridgedGlobalVar GlobalAccessInst_getGlobal(BridgedInstruction globalInst) {
423427
return {castToInst<GlobalAccessInst>(globalInst)->getReferencedGlobal()};
424428
}
425429

430+
BridgedFunction FunctionRefInst_getReferencedFunction(BridgedInstruction fri) {
431+
return {castToInst<FunctionRefInst>(fri)->getReferencedFunction()};
432+
}
433+
426434
SwiftInt TupleExtractInst_fieldIndex(BridgedInstruction tei) {
427435
return castToInst<TupleExtractInst>(tei)->getFieldIndex();
428436
}
@@ -459,6 +467,14 @@ SwiftInt ApplyInst_numArguments(BridgedInstruction ai) {
459467
return castToInst<ApplyInst>(ai)->getNumArguments();
460468
}
461469

470+
SwiftInt AllocRefInstBase_isObjc(BridgedInstruction arb) {
471+
return castToInst<AllocRefInstBase>(arb)->isObjC();
472+
}
473+
474+
SwiftInt AllocRefInstBase_canAllocOnStack(BridgedInstruction arb) {
475+
return castToInst<AllocRefInstBase>(arb)->canAllocOnStack();
476+
}
477+
462478
SwiftInt BeginApplyInst_numArguments(BridgedInstruction tai) {
463479
return castToInst<BeginApplyInst>(tai)->getNumArguments();
464480
}
@@ -519,3 +535,9 @@ BridgedInstruction SILBuilder_createIntegerLiteral(BridgedInstruction insertionP
519535
getSILType(type), value)};
520536
}
521537

538+
BridgedInstruction SILBuilder_createDeallocStackRef(BridgedInstruction insertionPoint,
539+
BridgedLocation loc, BridgedValue operand) {
540+
SILBuilder builder(castToInst(insertionPoint), getSILDebugScope(loc));
541+
return {builder.createDeallocStackRef(getRegularLocation(loc),
542+
castToSILValue(operand))};
543+
}

0 commit comments

Comments
 (0)