Skip to content

Commit a4225a9

Browse files
committed
Swift SIL: add a few SIL instructions and instruction APIs
1 parent 54aacf6 commit a4225a9

File tree

7 files changed

+123
-7
lines changed

7 files changed

+123
-7
lines changed

SwiftCompilerSources/Sources/Optimizer/TestPasses/RangeDumper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let rangeDumper = FunctionPass(name: "dump-ranges", {
2727

2828
for inst in function.instructions {
2929
if let sli = inst as? StringLiteralInst {
30-
switch sli.string {
30+
switch sli.value {
3131
case "begin":
3232
assert(begin == nil, "more than one begin instruction")
3333
begin = sli

SwiftCompilerSources/Sources/Optimizer/TestPasses/TestInstructionIteration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ let testInstructionIteration = FunctionPass(name: "test-instruction-iteration")
5353
private func handle(instruction: Instruction, _ context: FunctionPassContext) {
5454
print(instruction)
5555
if let sl = instruction as? StringLiteralInst {
56-
switch sl.string {
56+
switch sl.value {
5757
case "delete_strings":
5858
deleteAllInstructions(ofType: StringLiteralInst.self, in: instruction.parentBlock, context)
5959
case "delete_ints":

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ extension BridgedInstruction {
142142

143143
extension OptionalBridgedInstruction {
144144
public var instruction: Instruction? { obj.getAs(Instruction.self) }
145+
146+
public static var none: OptionalBridgedInstruction {
147+
OptionalBridgedInstruction()
148+
}
145149
}
146150

147151
public class SingleValueInstruction : Instruction, Value {
@@ -380,6 +384,10 @@ class UncheckedRefCastInst : SingleValueInstruction, UnaryInstruction {
380384
public var fromInstance: Value { operand.value }
381385
}
382386

387+
final public class UncheckedAddrCastInst : SingleValueInstruction, UnaryInstruction {
388+
public var fromAddress: Value { operand.value }
389+
}
390+
383391
final public
384392
class RawPointerToRefInst : SingleValueInstruction, UnaryInstruction {
385393
public var pointer: Value { operand.value }
@@ -490,8 +498,28 @@ final public class IntegerLiteralInst : SingleValueInstruction {
490498
public var value: llvm.APInt { bridged.IntegerLiteralInst_getValue() }
491499
}
492500

501+
final public class FloatLiteralInst : SingleValueInstruction {
502+
public var value: llvm.APFloat { bridged.FloatLiteralInst_getValue() }
503+
}
504+
493505
final public class StringLiteralInst : SingleValueInstruction {
494-
public var string: String { bridged.StringLiteralInst_getValue().string }
506+
public enum Encoding {
507+
case Bytes
508+
case UTF8
509+
/// UTF-8 encoding of an Objective-C selector.
510+
case ObjCSelector
511+
}
512+
513+
public var value: StringRef { StringRef(bridged: bridged.StringLiteralInst_getValue()) }
514+
515+
public var encoding: Encoding {
516+
switch bridged.StringLiteralInst_getEncoding() {
517+
case 0: return .Bytes
518+
case 1: return .UTF8
519+
case 2: return .ObjCSelector
520+
default: fatalError("invalid encoding in StringLiteralInst")
521+
}
522+
}
495523
}
496524

497525
final public class TupleInst : SingleValueInstruction {
@@ -675,6 +703,7 @@ final public class StrongCopyUnmanagedValueInst : SingleValueInstruction, UnaryI
675703

676704
final public class EndCOWMutationInst : SingleValueInstruction, UnaryInstruction {
677705
public var instance: Value { operand.value }
706+
public var doKeepUnique: Bool { bridged.EndCOWMutationInst_doKeepUnique() }
678707
}
679708

680709
final public
@@ -730,6 +759,17 @@ final public class IsEscapingClosureInst : SingleValueInstruction, UnaryInstruct
730759
final public
731760
class MarkMustCheckInst : SingleValueInstruction, UnaryInstruction {}
732761

762+
final public class ObjectInst : SingleValueInstruction {
763+
public var baseOperands: OperandArray {
764+
operands[0..<bridged.ObjectInst_getNumBaseElements()]
765+
}
766+
767+
public var tailOperands: OperandArray {
768+
let ops = operands
769+
return ops[bridged.ObjectInst_getNumBaseElements()..<ops.endIndex]
770+
}
771+
}
772+
733773
//===----------------------------------------------------------------------===//
734774
// single-value allocation instructions
735775
//===----------------------------------------------------------------------===//
@@ -746,12 +786,24 @@ public class AllocRefInstBase : SingleValueInstruction, Allocation {
746786
final public var canAllocOnStack: Bool {
747787
bridged.AllocRefInstBase_canAllocOnStack()
748788
}
789+
790+
final public var tailAllocatedCounts: OperandArray {
791+
let numTailTypes = bridged.AllocRefInstBase_getNumTailTypes()
792+
return operands[0..<numTailTypes]
793+
}
794+
795+
final public var tailAllocatedTypes: TypeArray {
796+
TypeArray(bridged: bridged.AllocRefInstBase_getTailAllocatedTypes())
797+
}
749798
}
750799

751800
final public class AllocRefInst : AllocRefInstBase {
752801
}
753802

754803
final public class AllocRefDynamicInst : AllocRefInstBase {
804+
public var isDynamicTypeDeinitAndSizeKnownEquivalentToBaseType: Bool {
805+
bridged.AllocRefDynamicInst_isDynamicTypeDeinitAndSizeKnownEquivalentToBaseType()
806+
}
755807
}
756808

757809
final public class AllocBoxInst : SingleValueInstruction, Allocation {

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public func registerSILClasses() {
7676
register(BuiltinInst.self)
7777
register(UpcastInst.self)
7878
register(UncheckedRefCastInst.self)
79+
register(UncheckedAddrCastInst.self)
7980
register(MarkMustCheckInst.self)
81+
register(ObjectInst.self)
8082
register(RawPointerToRefInst.self)
8183
register(AddressToPointerInst.self)
8284
register(PointerToAddressInst.self)
@@ -101,6 +103,7 @@ public func registerSILClasses() {
101103
register(GlobalValueInst.self)
102104
register(AllocGlobalInst.self)
103105
register(IntegerLiteralInst.self)
106+
register(FloatLiteralInst.self)
104107
register(StringLiteralInst.self)
105108
register(TupleInst.self)
106109
register(TupleExtractInst.self)

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ extension Type: Equatable {
8484
}
8585
}
8686

87+
public struct TypeArray : RandomAccessCollection, CustomReflectable {
88+
private let bridged: BridgedSILTypeArray
89+
90+
public var startIndex: Int { return 0 }
91+
public var endIndex: Int { return bridged.getCount() }
92+
93+
public init(bridged: BridgedSILTypeArray) {
94+
self.bridged = bridged
95+
}
96+
97+
public subscript(_ index: Int) -> Type {
98+
bridged.getAt(index).type
99+
}
100+
101+
public var customMirror: Mirror {
102+
let c: [Mirror.Child] = map { (label: nil, value: $0) }
103+
return Mirror(self, children: c)
104+
}
105+
}
106+
87107
public struct OptionalTypeArray : RandomAccessCollection, CustomReflectable {
88108
private let bridged: BridgedTypeArray
89109

include/swift/SIL/SILBridging.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,15 @@ struct BridgedTypeArray {
405405
}
406406
};
407407

408+
struct BridgedSILTypeArray {
409+
llvm::ArrayRef<swift::SILType> typeArray;
410+
411+
SwiftInt getCount() const { return SwiftInt(typeArray.size()); }
412+
413+
SWIFT_IMPORT_UNSAFE
414+
swift::SILType getAt(SwiftInt index) const { return typeArray[index]; }
415+
};
416+
408417
struct BridgedInstruction {
409418
SwiftObject obj;
410419

@@ -532,11 +541,20 @@ struct BridgedInstruction {
532541
return getAs<swift::IntegerLiteralInst>()->getValue();
533542
}
534543

544+
SWIFT_IMPORT_UNSAFE
545+
llvm::APFloat FloatLiteralInst_getValue() const {
546+
return getAs<swift::FloatLiteralInst>()->getValue();
547+
}
548+
535549
SWIFT_IMPORT_UNSAFE
536550
llvm::StringRef StringLiteralInst_getValue() const {
537551
return getAs<swift::StringLiteralInst>()->getValue();
538552
}
539553

554+
int StringLiteralInst_getEncoding() const {
555+
return (int)getAs<swift::StringLiteralInst>()->getEncoding();
556+
}
557+
540558
SwiftInt TupleExtractInst_fieldIndex() const {
541559
return getAs<swift::TupleExtractInst>()->getFieldIndex();
542560
}
@@ -561,6 +579,10 @@ struct BridgedInstruction {
561579
return getAs<swift::ProjectBoxInst>()->getFieldIndex();
562580
}
563581

582+
bool EndCOWMutationInst_doKeepUnique() const {
583+
return getAs<swift::EndCOWMutationInst>()->doKeepUnique();
584+
}
585+
564586
SwiftInt EnumInst_caseIndex() const {
565587
return getAs<swift::EnumInst>()->getCaseIndex();
566588
}
@@ -612,6 +634,10 @@ struct BridgedInstruction {
612634
return getAs<swift::ApplyInst>()->getSpecializationInfo();
613635
}
614636

637+
SwiftInt ObjectInst_getNumBaseElements() const {
638+
return getAs<swift::ObjectInst>()->getNumBaseElements();
639+
}
640+
615641
SwiftInt PartialApply_getCalleeArgIndexOfFirstAppliedArg() const {
616642
return swift::ApplySite(getInst()).getCalleeArgIndexOfFirstAppliedArg();
617643
}
@@ -632,6 +658,19 @@ struct BridgedInstruction {
632658
return getAs<swift::AllocRefInstBase>()->canAllocOnStack();
633659
}
634660

661+
SwiftInt AllocRefInstBase_getNumTailTypes() const {
662+
return getAs<swift::AllocRefInstBase>()->getNumTailTypes();
663+
}
664+
665+
SWIFT_IMPORT_UNSAFE
666+
BridgedSILTypeArray AllocRefInstBase_getTailAllocatedTypes() const {
667+
return {getAs<const swift::AllocRefInstBase>()->getTailAllocatedTypes()};
668+
}
669+
670+
bool AllocRefDynamicInst_isDynamicTypeDeinitAndSizeKnownEquivalentToBaseType() const {
671+
return getAs<swift::AllocRefDynamicInst>()->isDynamicTypeDeinitAndSizeKnownEquivalentToBaseType();
672+
}
673+
635674
SwiftInt BeginApplyInst_numArguments() const {
636675
return getAs<swift::BeginApplyInst>()->getNumArguments();
637676
}

include/swift/SIL/SILInstruction.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,11 +2294,11 @@ class AllocRefInstBase : public AllocationInst {
22942294
return const_cast<AllocRefInstBase*>(this)->getTypeStorage();
22952295
}
22962296

2297+
public:
22972298
unsigned getNumTailTypes() const {
22982299
return sharedUInt8().AllocRefInstBase.numTailTypes;
22992300
}
23002301

2301-
public:
23022302
bool canAllocOnStack() const {
23032303
return sharedUInt8().AllocRefInstBase.onStack;
23042304
}
@@ -4246,10 +4246,10 @@ class StringLiteralInst final
42464246

42474247
public:
42484248
enum class Encoding {
4249-
Bytes,
4250-
UTF8,
4249+
Bytes = 0,
4250+
UTF8 = 1,
42514251
/// UTF-8 encoding of an Objective-C selector.
4252-
ObjCSelector,
4252+
ObjCSelector = 2,
42534253
};
42544254

42554255
private:
@@ -6232,6 +6232,8 @@ class ObjectInst final : public InstructionBaseWithTrailingOperands<
62326232
ValueOwnershipKind forwardingOwnershipKind);
62336233

62346234
public:
6235+
unsigned getNumBaseElements() const { return numBaseElements; }
6236+
62356237
/// All elements referenced by this ObjectInst.
62366238
MutableArrayRef<Operand> getElementOperands() {
62376239
return getAllOperands();

0 commit comments

Comments
 (0)