Skip to content

Commit 205c841

Browse files
committed
Swift SIL: add some Builder APIs
* `createStructExtract` * `createStructElementAddr` * `createTupleExtract` * `createTupleElementAddr`
1 parent 8bb3e45 commit 205c841

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,29 @@ public struct Builder {
250250
return notifyNew(structInst.getAs(StructInst.self))
251251
}
252252

253+
public func createStructExtract(struct: Value, fieldIndex: Int) -> StructExtractInst {
254+
return notifyNew(bridged.createStructExtract(`struct`.bridged, fieldIndex).getAs(StructExtractInst.self))
255+
}
256+
257+
public func createStructElementAddr(structAddress: Value, fieldIndex: Int) -> StructElementAddrInst {
258+
return notifyNew(bridged.createStructElementAddr(structAddress.bridged, fieldIndex).getAs(StructElementAddrInst.self))
259+
}
260+
253261
public func createTuple(type: Type, elements: [Value]) -> TupleInst {
254262
let tuple = elements.withBridgedValues { valuesRef in
255263
return bridged.createTuple(type.bridged, valuesRef)
256264
}
257265
return notifyNew(tuple.getAs(TupleInst.self))
258266
}
259267

268+
public func createTupleExtract(tuple: Value, elementIndex: Int) -> TupleExtractInst {
269+
return notifyNew(bridged.createTupleExtract(tuple.bridged, elementIndex).getAs(TupleExtractInst.self))
270+
}
271+
272+
public func createTupleElementAddr(tupleAddress: Value, elementIndex: Int) -> TupleElementAddrInst {
273+
return notifyNew(bridged.createTupleElementAddr(tupleAddress.bridged, elementIndex).getAs(TupleElementAddrInst.self))
274+
}
275+
260276
@discardableResult
261277
public func createStore(source: Value, destination: Value, ownership: StoreInst.Ownership) -> StoreInst {
262278
let store = bridged.createStore(source.bridged, destination.bridged, ownership.rawValue)

include/swift/SIL/SILBridging.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,12 +1268,36 @@ struct BridgedBuilder{
12681268
return {builder().createStruct(regularLoc(), type, elements.getValues(elementValues))};
12691269
}
12701270

1271+
SWIFT_IMPORT_UNSAFE
1272+
BridgedInstruction createStructExtract(BridgedValue str, SwiftInt fieldIndex) const {
1273+
swift::SILValue v = str.getSILValue();
1274+
return {builder().createStructExtract(regularLoc(), v, v->getType().getFieldDecl(fieldIndex))};
1275+
}
1276+
1277+
SWIFT_IMPORT_UNSAFE
1278+
BridgedInstruction createStructElementAddr(BridgedValue addr, SwiftInt fieldIndex) const {
1279+
swift::SILValue v = addr.getSILValue();
1280+
return {builder().createStructElementAddr(regularLoc(), v, v->getType().getFieldDecl(fieldIndex))};
1281+
}
1282+
12711283
SWIFT_IMPORT_UNSAFE
12721284
BridgedInstruction createTuple(swift::SILType type, BridgedValueArray elements) const {
12731285
llvm::SmallVector<swift::SILValue, 16> elementValues;
12741286
return {builder().createTuple(regularLoc(), type, elements.getValues(elementValues))};
12751287
}
12761288

1289+
SWIFT_IMPORT_UNSAFE
1290+
BridgedInstruction createTupleExtract(BridgedValue str, SwiftInt elementIndex) const {
1291+
swift::SILValue v = str.getSILValue();
1292+
return {builder().createTupleExtract(regularLoc(), v, elementIndex)};
1293+
}
1294+
1295+
SWIFT_IMPORT_UNSAFE
1296+
BridgedInstruction createTupleElementAddr(BridgedValue addr, SwiftInt elementIndex) const {
1297+
swift::SILValue v = addr.getSILValue();
1298+
return {builder().createTupleElementAddr(regularLoc(), v, elementIndex)};
1299+
}
1300+
12771301
SWIFT_IMPORT_UNSAFE
12781302
BridgedInstruction createStore(BridgedValue src, BridgedValue dst,
12791303
SwiftInt ownership) const {

include/swift/SIL/SILType.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ class SILType {
564564
bool isFunction() const { return is<SILFunctionType>(); }
565565
bool isMetatype() const { return is<MetatypeType>(); }
566566

567+
VarDecl *getFieldDecl(intptr_t fieldIndex) const;
568+
567569
/// Given that this is a nominal type, return the lowered type of
568570
/// the given field. Applies substitutions as necessary. The
569571
/// result will be an address type if the base type is an address

lib/SIL/IR/SILType.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ static void addFieldSubstitutionsIfNeeded(TypeConverter &TC, SILType ty,
320320
}
321321
}
322322

323+
VarDecl *SILType::getFieldDecl(intptr_t fieldIndex) const {
324+
NominalTypeDecl *decl = getNominalOrBoundGenericNominal();
325+
assert(decl && "expected nominal type");
326+
return getIndexedField(decl, fieldIndex);
327+
}
328+
323329
SILType SILType::getFieldType(VarDecl *field, TypeConverter &TC,
324330
TypeExpansionContext context) const {
325331
AbstractionPattern origFieldTy = TC.getAbstractionPattern(field);
@@ -361,9 +367,7 @@ SILType SILType::getFieldType(VarDecl *field, SILFunction *fn) const {
361367
}
362368

363369
SILType SILType::getFieldType(intptr_t fieldIndex, SILFunction *function) const {
364-
NominalTypeDecl *decl = getNominalOrBoundGenericNominal();
365-
assert(decl && "expected nominal type");
366-
VarDecl *field = getIndexedField(decl, fieldIndex);
370+
VarDecl *field = getFieldDecl(fieldIndex);
367371
return getFieldType(field, function->getModule(), function->getTypeExpansionContext());
368372
}
369373

0 commit comments

Comments
 (0)