Skip to content

Commit fc772ff

Browse files
committed
libswift: add more instructions and other small utilities
* ApplySite.arguments * BasicBlock != operator * some Function argument related properties * Operand.isTypeDependent * Type.isTrivial * bridging of raw_ostream::write
1 parent e1a9be5 commit fc772ff

File tree

10 files changed

+198
-58
lines changed

10 files changed

+198
-58
lines changed

include/swift/SIL/SILBridging.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ typedef struct {
5050
const void * _Nonnull opaqueCtxt;
5151
} BridgedPassContext;
5252

53+
typedef struct {
54+
void * _Nonnull streamAddr;
55+
} BridgedOStream;
56+
5357
typedef struct {
5458
void * _Null_unspecified word0;
5559
void * _Null_unspecified word1;
@@ -139,6 +143,8 @@ typedef long SwiftInt;
139143

140144
void registerBridgedClass(BridgedStringRef className, SwiftMetatype metatype);
141145

146+
void OStream_write(BridgedOStream os, BridgedStringRef str);
147+
142148
void freeBridgedStringRef(BridgedStringRef str);
143149

144150
void PassContext_notifyChanges(BridgedPassContext passContext,
@@ -155,6 +161,8 @@ BridgedStringRef SILFunction_getName(BridgedFunction function);
155161
std::string SILFunction_debugDescription(BridgedFunction function);
156162
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function);
157163
OptionalBridgedBasicBlock SILFunction_lastBlock(BridgedFunction function);
164+
SwiftInt SILFunction_numIndirectResultArguments(BridgedFunction function);
165+
SwiftInt SILFunction_getSelfArgumentIndex(BridgedFunction function);
158166

159167
BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global);
160168
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);
@@ -175,12 +183,14 @@ BridgedInstruction SILSuccessor_getContainingInst(BridgedSuccessor succ);
175183
BridgedValue Operand_getValue(BridgedOperand);
176184
OptionalBridgedOperand Operand_nextUse(BridgedOperand);
177185
BridgedInstruction Operand_getUser(BridgedOperand);
186+
SwiftInt Operand_isTypeDependent(BridgedOperand);
178187

179188
std::string SILNode_debugDescription(BridgedNode node);
180189
OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
181190
BridgedType SILValue_getType(BridgedValue value);
182191

183192
SwiftInt SILType_isAddress(BridgedType);
193+
SwiftInt SILType_isTrivial(BridgedType, BridgedFunction);
184194

185195
BridgedBasicBlock SILArgument_getParent(BridgedArgument argument);
186196

@@ -216,6 +226,7 @@ SwiftInt TryApplyInst_numArguments(BridgedInstruction ai);
216226
BridgedBasicBlock BranchInst_getTargetBlock(BridgedInstruction bi);
217227
SwiftInt SwitchEnumInst_getNumCases(BridgedInstruction se);
218228
SwiftInt SwitchEnumInst_getCaseIndex(BridgedInstruction se, SwiftInt idx);
229+
SwiftInt StoreInst_getStoreOwnership(BridgedInstruction store);
219230

220231
BridgedInstruction SILBuilder_createBuiltinBinaryFunction(
221232
BridgedInstruction insertionPoint,

include/swift/SIL/SILNodes.def

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,8 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
449449
SINGLE_VALUE_INST_RANGE(AllocationInst, AllocStackInst, AllocExistentialBoxInst)
450450

451451
ABSTRACT_SINGLE_VALUE_INST(IndexingInst, SingleValueInstruction)
452-
SINGLE_VALUE_INST(IndexAddrInst, index_addr,
453-
IndexingInst, None, DoesNotRelease)
452+
BRIDGED_SINGLE_VALUE_INST(IndexAddrInst, index_addr,
453+
IndexingInst, None, DoesNotRelease)
454454
SINGLE_VALUE_INST(TailAddrInst, tail_addr,
455455
IndexingInst, None, DoesNotRelease)
456456
SINGLE_VALUE_INST(IndexRawPointerInst, index_raw_pointer,
@@ -481,16 +481,16 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
481481

482482
// Dynamic Dispatch
483483
ABSTRACT_SINGLE_VALUE_INST(MethodInst, SingleValueInstruction)
484-
SINGLE_VALUE_INST(ClassMethodInst, class_method,
485-
MethodInst, None, DoesNotRelease)
486-
SINGLE_VALUE_INST(SuperMethodInst, super_method,
487-
MethodInst, None, DoesNotRelease)
488-
SINGLE_VALUE_INST(ObjCMethodInst, objc_method,
489-
MethodInst, None, DoesNotRelease)
490-
SINGLE_VALUE_INST(ObjCSuperMethodInst, objc_super_method,
491-
MethodInst, None, DoesNotRelease)
492-
SINGLE_VALUE_INST(WitnessMethodInst, witness_method,
493-
MethodInst, None, DoesNotRelease)
484+
BRIDGED_SINGLE_VALUE_INST(ClassMethodInst, class_method,
485+
MethodInst, None, DoesNotRelease)
486+
BRIDGED_SINGLE_VALUE_INST(SuperMethodInst, super_method,
487+
MethodInst, None, DoesNotRelease)
488+
BRIDGED_SINGLE_VALUE_INST(ObjCMethodInst, objc_method,
489+
MethodInst, None, DoesNotRelease)
490+
BRIDGED_SINGLE_VALUE_INST(ObjCSuperMethodInst, objc_super_method,
491+
MethodInst, None, DoesNotRelease)
492+
BRIDGED_SINGLE_VALUE_INST(WitnessMethodInst, witness_method,
493+
MethodInst, None, DoesNotRelease)
494494
SINGLE_VALUE_INST_RANGE(MethodInst, ClassMethodInst, WitnessMethodInst)
495495

496496
// Conversions
@@ -499,7 +499,7 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
499499
ConversionInst, None, DoesNotRelease)
500500
BRIDGED_SINGLE_VALUE_INST(AddressToPointerInst, address_to_pointer,
501501
ConversionInst, None, DoesNotRelease)
502-
SINGLE_VALUE_INST(PointerToAddressInst, pointer_to_address,
502+
BRIDGED_SINGLE_VALUE_INST(PointerToAddressInst, pointer_to_address,
503503
ConversionInst, None, DoesNotRelease)
504504
BRIDGED_SINGLE_VALUE_INST(UncheckedRefCastInst, unchecked_ref_cast,
505505
ConversionInst, None, DoesNotRelease)
@@ -531,8 +531,8 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
531531
ConversionInst, None, DoesNotRelease)
532532
SINGLE_VALUE_INST(RefToBridgeObjectInst, ref_to_bridge_object,
533533
ConversionInst, None, DoesNotRelease)
534-
SINGLE_VALUE_INST(BridgeObjectToRefInst, bridge_object_to_ref,
535-
ConversionInst, None, DoesNotRelease)
534+
BRIDGED_SINGLE_VALUE_INST(BridgeObjectToRefInst, bridge_object_to_ref,
535+
ConversionInst, None, DoesNotRelease)
536536
SINGLE_VALUE_INST(BridgeObjectToWordInst, bridge_object_to_word,
537537
ConversionInst, None, DoesNotRelease)
538538
BRIDGED_SINGLE_VALUE_INST(ThinToThickFunctionInst, thin_to_thick_function,
@@ -599,8 +599,8 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
599599
SINGLE_VALUE_INST(IsUniqueInst, is_unique,
600600
SingleValueInstruction, MayHaveSideEffects, DoesNotRelease)
601601

602-
SINGLE_VALUE_INST(EndCOWMutationInst, end_cow_mutation,
603-
SingleValueInstruction, None, DoesNotRelease)
602+
BRIDGED_SINGLE_VALUE_INST(EndCOWMutationInst, end_cow_mutation,
603+
SingleValueInstruction, None, DoesNotRelease)
604604

605605
SINGLE_VALUE_INST(IsEscapingClosureInst, is_escaping_closure,
606606
SingleValueInstruction, MayRead, DoesNotRelease)
@@ -687,26 +687,26 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
687687
SingleValueInstruction, None, DoesNotRelease)
688688

689689
// Protocol and Protocol Composition Types
690-
SINGLE_VALUE_INST(InitExistentialAddrInst, init_existential_addr,
691-
SingleValueInstruction, MayWrite, DoesNotRelease)
692-
SINGLE_VALUE_INST(InitExistentialValueInst, init_existential_value,
693-
SingleValueInstruction, MayWrite, DoesNotRelease)
694-
SINGLE_VALUE_INST(OpenExistentialAddrInst, open_existential_addr,
695-
SingleValueInstruction, MayRead, DoesNotRelease)
690+
BRIDGED_SINGLE_VALUE_INST(InitExistentialAddrInst, init_existential_addr,
691+
SingleValueInstruction, MayWrite, DoesNotRelease)
692+
BRIDGED_SINGLE_VALUE_INST(InitExistentialValueInst, init_existential_value,
693+
SingleValueInstruction, MayWrite, DoesNotRelease)
694+
BRIDGED_SINGLE_VALUE_INST(OpenExistentialAddrInst, open_existential_addr,
695+
SingleValueInstruction, MayRead, DoesNotRelease)
696696
BRIDGED_SINGLE_VALUE_INST(InitExistentialRefInst, init_existential_ref,
697-
SingleValueInstruction, None, DoesNotRelease)
697+
SingleValueInstruction, None, DoesNotRelease)
698698
BRIDGED_SINGLE_VALUE_INST(OpenExistentialRefInst, open_existential_ref,
699-
SingleValueInstruction, None, DoesNotRelease)
699+
SingleValueInstruction, None, DoesNotRelease)
700700
BRIDGED_SINGLE_VALUE_INST(InitExistentialMetatypeInst, init_existential_metatype,
701-
SingleValueInstruction, None, DoesNotRelease)
701+
SingleValueInstruction, None, DoesNotRelease)
702702
BRIDGED_SINGLE_VALUE_INST(OpenExistentialMetatypeInst, open_existential_metatype,
703-
SingleValueInstruction, None, DoesNotRelease)
704-
SINGLE_VALUE_INST(OpenExistentialBoxInst, open_existential_box,
705-
SingleValueInstruction, MayRead, DoesNotRelease)
706-
SINGLE_VALUE_INST(OpenExistentialValueInst, open_existential_value,
707-
SingleValueInstruction, MayRead, DoesNotRelease)
708-
SINGLE_VALUE_INST(OpenExistentialBoxValueInst, open_existential_box_value,
709-
SingleValueInstruction, MayRead, DoesNotRelease)
703+
SingleValueInstruction, None, DoesNotRelease)
704+
BRIDGED_SINGLE_VALUE_INST(OpenExistentialBoxInst, open_existential_box,
705+
SingleValueInstruction, MayRead, DoesNotRelease)
706+
BRIDGED_SINGLE_VALUE_INST(OpenExistentialValueInst, open_existential_value,
707+
SingleValueInstruction, MayRead, DoesNotRelease)
708+
BRIDGED_SINGLE_VALUE_INST(OpenExistentialBoxValueInst, open_existential_box_value,
709+
SingleValueInstruction, MayRead, DoesNotRelease)
710710

711711
// Blocks
712712
SINGLE_VALUE_INST(ProjectBlockStorageInst, project_block_storage,

lib/SIL/Utils/SILBridging.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ void registerBridgedClass(BridgedStringRef className, SwiftMetatype metatype) {
148148
// Bridging C functions
149149
//===----------------------------------------------------------------------===//
150150

151+
void OStream_write(BridgedOStream os, BridgedStringRef str) {
152+
static_cast<raw_ostream *>(os.streamAddr)->write((const char*)(str.data), str.length);
153+
}
154+
151155
/// Frees a string which was allocated by getCopiedBridgedStringRef.
152156
void freeBridgedStringRef(BridgedStringRef str) {
153157
llvm::MallocAllocator().Deallocate(str.data, str.length);
@@ -183,6 +187,18 @@ OptionalBridgedBasicBlock SILFunction_lastBlock(BridgedFunction function) {
183187
return {&*f->rbegin()};
184188
}
185189

190+
SwiftInt SILFunction_numIndirectResultArguments(BridgedFunction function) {
191+
return castToFunction(function)->getLoweredFunctionType()->
192+
getNumIndirectFormalResults();
193+
}
194+
195+
SwiftInt SILFunction_getSelfArgumentIndex(BridgedFunction function) {
196+
CanSILFunctionType fTy = castToFunction(function)->getLoweredFunctionType();
197+
if (!fTy->hasSelfParam())
198+
return -1;
199+
return fTy->getNumParameters() + fTy->getNumIndirectFormalResults() - 1;
200+
}
201+
186202
//===----------------------------------------------------------------------===//
187203
// SILBasicBlock
188204
//===----------------------------------------------------------------------===//
@@ -299,6 +315,10 @@ BridgedInstruction Operand_getUser(BridgedOperand operand) {
299315
return {castToOperand(operand)->getUser()->asSILNode()};
300316
}
301317

318+
SwiftInt Operand_isTypeDependent(BridgedOperand operand) {
319+
return castToOperand(operand)->isTypeDependent() ? 1 : 0;
320+
}
321+
302322
OptionalBridgedOperand SILValue_firstUse(BridgedValue value) {
303323
return {*castToSILValue(value)->use_begin()};
304324
}
@@ -315,6 +335,10 @@ SwiftInt SILType_isAddress(BridgedType type) {
315335
return castToSILType(type).isAddress();
316336
}
317337

338+
SwiftInt SILType_isTrivial(BridgedType type, BridgedFunction function) {
339+
return castToSILType(type).isTrivial(*castToFunction(function));
340+
}
341+
318342
//===----------------------------------------------------------------------===//
319343
// SILGlobalVariable
320344
//===----------------------------------------------------------------------===//
@@ -462,6 +486,11 @@ SwiftInt SwitchEnumInst_getCaseIndex(BridgedInstruction se, SwiftInt idx) {
462486
return getCaseIndex(castToInst<SwitchEnumInst>(se)->getCase(idx).first);
463487
}
464488

489+
SwiftInt StoreInst_getStoreOwnership(BridgedInstruction store) {
490+
return (SwiftInt)castToInst<StoreInst>(store)->getOwnershipQualifier();
491+
}
492+
493+
465494
//===----------------------------------------------------------------------===//
466495
// SILBuilder
467496
//===----------------------------------------------------------------------===//

libswift/Sources/SIL/ApplySite.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public protocol ApplySite : AnyObject {
2323
extension ApplySite {
2424
public var callee: Value { operands[ApplyOperands.calleeOperandIndex].value }
2525

26+
public var arguments: LazyMapSequence<OperandArray, Value> {
27+
operands[1..<operands.count].lazy.map { $0.value }
28+
}
29+
2630
public func argumentIndex(of operand: Operand) -> Int? {
2731
let opIdx = operand.index
2832
if opIdx >= ApplyOperands.firstArgumentIndex &&

libswift/Sources/SIL/BasicBlock.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ final public class BasicBlock : ListNode, CustomStringConvertible {
6767
}
6868

6969
public func == (lhs: BasicBlock, rhs: BasicBlock) -> Bool { lhs === rhs }
70+
public func != (lhs: BasicBlock, rhs: BasicBlock) -> Bool { lhs !== rhs }
7071

7172
public struct ArgumentArray : RandomAccessCollection {
7273
fileprivate let block: BasicBlock

libswift/Sources/SIL/Function.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ final public class Function : CustomStringConvertible {
3333
return ReverseList(startAt: SILFunction_lastBlock(bridged).block)
3434
}
3535

36+
public var arguments: LazyMapSequence<ArgumentArray, FunctionArgument> {
37+
entryBlock.arguments.lazy.map { $0 as! FunctionArgument }
38+
}
39+
40+
public var numIndirectResultArguments: Int {
41+
SILFunction_numIndirectResultArguments(bridged)
42+
}
43+
44+
public var hasSelfArgument: Bool {
45+
SILFunction_getSelfArgumentIndex(bridged) >= 0
46+
}
47+
48+
public var selfArgumentIndex: Int {
49+
let selfIdx = SILFunction_getSelfArgumentIndex(bridged)
50+
assert(selfIdx >= 0)
51+
return selfIdx
52+
}
53+
3654
public var bridged: BridgedFunction { BridgedFunction(obj: SwiftObject(self)) }
3755
}
3856

0 commit comments

Comments
 (0)