Skip to content

Commit d7144c0

Browse files
committed
libswift: implement isFieldOnlyNonTrivialField
1 parent 848fa70 commit d7144c0

File tree

5 files changed

+71
-39
lines changed

5 files changed

+71
-39
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ReleaseDevirtualizer.swift

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ let releaseDevirtualizerPass = FunctionPass(
5959
}
6060
)
6161

62-
/// Tries to de-virtualize the final release of a stack promoted object.
62+
/// Tries to de-virtualize the final release of a stack-promoted object.
6363
private func tryDevirtualizeReleaseOfObject(
6464
_ context: PassContext,
6565
_ release: RefCountingInst,
@@ -157,12 +157,12 @@ private extension Instruction {
157157
let function = self.function
158158

159159
// For each operand...
160-
for op in operands.enumerated() {
160+
for op in operands {
161161
// If the operand is not trivial...
162-
if !op.type.isTrivial(in: function) {
162+
if !op.value.type.isTrivial(in: function) {
163163
// And we have not found a `candidateElt` yet, set index to `op` and continue.
164164
if candidateElt == nil {
165-
candidateElt = op
165+
candidateElt = op.value
166166
continue
167167
}
168168

@@ -186,7 +186,7 @@ private extension TupleExtractInst {
186186

187187
// Ok, we know that the elt we are extracting is non-trivial. Make sure that
188188
// we have no other non-trivial elts.
189-
let opTy = operand[0].type
189+
let opType = operand.type
190190
let fieldNo = self.fieldIndex
191191

192192
// For each element index of the tuple...
@@ -212,3 +212,25 @@ private extension TupleExtractInst {
212212
return true
213213
}
214214
}
215+
216+
private extension StructExtractInst {
217+
var isFieldOnlyNonTrivialField: Bool {
218+
let function = self.function
219+
220+
if type.isTrivial(in: function) {
221+
return false
222+
}
223+
224+
let structType = operand.type
225+
226+
for (i, fieldType) in structType.getStructFields(in: function).enumerated() {
227+
if i == fieldIndex || fieldType.isTrivial(in: function) {
228+
continue
229+
}
230+
231+
return false
232+
}
233+
234+
return true
235+
}
236+
}

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,6 @@ public final class StructInst: SingleValueInstruction {
388388

389389
public final class StructExtractInst: SingleValueInstruction, UnaryInstruction {
390390
public var fieldIndex: Int { StructExtractInst_fieldIndex(bridged) }
391-
public var isFieldOnlyNonTrivialField: Bool {
392-
StructExtractInst_isFieldOnlyNonTrivialField(bridged)
393-
}
394391
}
395392

396393
public

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import SILBridging
1414

1515
public struct Type : CustomStringConvertible, CustomReflectable {
16-
var bridged: BridgedType
16+
public let bridged: BridgedType
1717

1818
public var isAddress: Bool { SILType_isAddress(bridged) != 0 }
1919
public var isObject: Bool { !isAddress }
@@ -37,17 +37,23 @@ public struct Type : CustomStringConvertible, CustomReflectable {
3737
return idx >= 0 ? idx : nil
3838
}
3939

40-
public func getFieldTypeOfNominal(fieldIndex: Int, in function: Function) -> Type {
41-
SILType_getTypeOfField(bridged, fieldIndex, function.bridged).type
40+
public func getStructFields(in function: Function) -> StructElementArray {
41+
StructElementArray(type: self, function: function)
4242
}
4343

4444
public var description: String { SILType_debugDescription(bridged).takeString() }
4545

4646
public var customMirror: Mirror { Mirror(self, children: []) }
4747
}
4848

49+
extension Type: Equatable {
50+
public static func ==(lhs: Type, rhs: Type) -> Bool {
51+
lhs.bridged.typePtr == rhs.bridged.typePtr
52+
}
53+
}
54+
4955
public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {
50-
fileprivate let type: Type
56+
public let type: Type
5157

5258
public var startIndex: Int { return 0 }
5359
public var endIndex: Int { SILType_getNumTupleElements(type.bridged) }
@@ -60,3 +66,15 @@ public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {
6066
extension BridgedType {
6167
var type: Type { Type(bridged: self) }
6268
}
69+
70+
public struct StructElementArray : RandomAccessCollection, FormattedLikeArray {
71+
public let type: Type
72+
let function: Function
73+
74+
public var startIndex: Int { return 0 }
75+
public var endIndex: Int { SILType_getNumStructFields(type.bridged) }
76+
77+
public subscript(_ index: Int) -> Type {
78+
SILType_getStructFieldType(type.bridged, index, function.bridged).type
79+
}
80+
}

include/swift/SIL/SILBridging.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,12 @@ SwiftInt SILType_isTuple(BridgedType type);
214214
SwiftInt SILType_isEnum(BridgedType type);
215215
SwiftInt SILType_getFieldIdxOfNominalType(BridgedType type,
216216
BridgedStringRef fieldName);
217-
BridgedType SILType_getTypeOfField(BridgedType type, SwiftInt fieldIndex,
218-
BridgedFunction inFunction);
219217
SwiftInt SILType_getNumTupleElements(BridgedType type);
220218
BridgedType SILType_getTupleElementType(BridgedType type, SwiftInt elementIdx);
221219
BridgedSubstitutionMap SILType_getContextSubstitutionMap(BridgedType);
220+
SwiftInt SILType_getNumStructFields(BridgedType type);
221+
BridgedType SILType_getStructFieldType(BridgedType type, SwiftInt index,
222+
BridgedFunction function);
222223

223224
BridgedBasicBlock SILArgument_getParent(BridgedArgument argument);
224225

@@ -246,7 +247,6 @@ BridgedFunction FunctionRefInst_getReferencedFunction(BridgedInstruction fri);
246247
SwiftInt TupleExtractInst_fieldIndex(BridgedInstruction tei);
247248
SwiftInt TupleElementAddrInst_fieldIndex(BridgedInstruction teai);
248249
SwiftInt StructExtractInst_fieldIndex(BridgedInstruction sei);
249-
bool StructExtractInst_isFieldOnlyNonTrivialField(BridgedInstruction sei);
250250
OptionalBridgedValue StructInst_getUniqueNonTrivialFieldValue(BridgedInstruction si);
251251
SwiftInt StructElementAddrInst_fieldIndex(BridgedInstruction seai);
252252
SwiftInt EnumInst_caseIndex(BridgedInstruction ei);

lib/SIL/Utils/SILBridging.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,6 @@ SwiftInt SILType_isEnum(BridgedType type) {
380380
return castToSILType(type).getEnumOrBoundGenericEnum() ? 1 : 0;
381381
}
382382

383-
SwiftInt SILType_isNonTrivialOrContainsRawPointer(BridgedType type,
384-
BridgedFunction function) {
385-
SILFunction *f = castToFunction(function);
386-
return castToSILType(type).isNonTrivialOrContainsRawPointer(*f);
387-
}
388-
389383
SwiftInt SILType_getFieldIdxOfNominalType(BridgedType type,
390384
BridgedStringRef fieldName) {
391385
SILType ty = castToSILType(type);
@@ -414,20 +408,6 @@ SwiftInt SILType_getFieldIdxOfNominalType(BridgedType type,
414408
return -1;
415409
}
416410

417-
BridgedType SILType_getTypeOfField(BridgedType type, SwiftInt fieldIndex,
418-
BridgedFunction inFunction) {
419-
SILType ty = castToSILType(type);
420-
auto *nominal = ty.getNominalOrBoundGenericNominal();
421-
assert(nominal);
422-
423-
VarDecl *field = getIndexedField(nominal, (unsigned)fieldIndex);
424-
assert(field);
425-
426-
SILFunction *f = castToFunction(inFunction);
427-
SILType fieldTy = ty.getFieldType(field, f->getModule(),f->getTypeExpansionContext());
428-
return {fieldTy.getOpaqueValue()};
429-
}
430-
431411
SwiftInt SILType_getNumTupleElements(BridgedType type) {
432412
TupleType *tupleTy = castToSILType(type).castTo<TupleType>();
433413
return tupleTy->getNumElements();
@@ -439,6 +419,25 @@ BridgedType SILType_getTupleElementType(BridgedType type, SwiftInt elementIdx) {
439419
return {elmtTy.getOpaqueValue()};
440420
}
441421

422+
SwiftInt SILType_getNumStructFields(BridgedType type) {
423+
StructType *ty = castToSILType(type).castTo<StructType>();
424+
return ty->getDecl()->getStoredProperties().size();
425+
}
426+
427+
BridgedType SILType_getStructFieldType(BridgedType type, SwiftInt index,
428+
BridgedFunction function) {
429+
SILType silType = castToSILType(type);
430+
SILFunction *silFunction = castToFunction(function);
431+
432+
StructType *ty = silType.castTo<StructType>();
433+
VarDecl *property = ty->getDecl()->getStoredProperties()[index];
434+
435+
SILType propertyType = silType.getFieldType(
436+
property, silFunction->getModule(), TypeExpansionContext(*silFunction));
437+
438+
return {propertyType.getOpaqueValue()};
439+
}
440+
442441
//===----------------------------------------------------------------------===//
443442
// SILGlobalVariable
444443
//===----------------------------------------------------------------------===//
@@ -553,10 +552,6 @@ SwiftInt StructExtractInst_fieldIndex(BridgedInstruction sei) {
553552
return castToInst<StructExtractInst>(sei)->getFieldIndex();
554553
}
555554

556-
bool StructExtractInst_isFieldOnlyNonTrivialField(BridgedInstruction sei) {
557-
return castToInst<StructExtractInst>(sei)->isFieldOnlyNonTrivialField();
558-
}
559-
560555
OptionalBridgedValue StructInst_getUniqueNonTrivialFieldValue(BridgedInstruction si) {
561556
return {castToInst<StructInst>(si)->getUniqueNonTrivialFieldValue()};
562557
}

0 commit comments

Comments
 (0)