Skip to content

Commit abb34e1

Browse files
committed
Implement RemoteInspection support for Builtin.FixedArray
1 parent fda213b commit abb34e1

File tree

7 files changed

+212
-6
lines changed

7 files changed

+212
-6
lines changed

include/swift/RemoteInspection/TypeLowering.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ enum class TypeInfoKind : unsigned {
117117
Reference,
118118
Invalid,
119119
Enum,
120+
Array,
120121
};
121122

122123
class TypeInfo {
@@ -350,6 +351,24 @@ class ReferenceTypeInfo : public TypeInfo {
350351
}
351352
};
352353

354+
/// Array based layouts like Builtin.FixedArray<N, T>
355+
class ArrayTypeInfo : public TypeInfo {
356+
const TypeInfo *ElementTI;
357+
358+
public:
359+
explicit ArrayTypeInfo(intptr_t size, const TypeInfo *elementTI);
360+
361+
bool readExtraInhabitantIndex(remote::MemoryReader &reader,
362+
remote::RemoteAddress address,
363+
int *extraInhabitantIndex) const override;
364+
365+
BitMask getSpareBits(TypeConverter &TC, bool &hasAddrOnly) const override;
366+
367+
static bool classof(const TypeInfo *TI) {
368+
return TI->getKind() == TypeInfoKind::Array;
369+
}
370+
};
371+
353372
/// This class owns the memory for all TypeInfo instances that it vends.
354373
class TypeConverter {
355374
TypeRefBuilder &Builder;

include/swift/RemoteInspection/TypeRef.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,67 @@ class SILBoxTypeWithLayoutTypeRef final : public TypeRef {
10951095
}
10961096
};
10971097

1098+
class IntegerTypeRef final : public TypeRef {
1099+
intptr_t Value;
1100+
1101+
static TypeRefID Profile(const intptr_t &Value) {
1102+
TypeRefID ID;
1103+
ID.addInteger((uint64_t)Value);
1104+
return ID;
1105+
}
1106+
1107+
public:
1108+
IntegerTypeRef(const intptr_t &Value)
1109+
: TypeRef(TypeRefKind::Integer), Value(Value) {}
1110+
1111+
template <typename Allocator>
1112+
static const IntegerTypeRef *create(Allocator &A, intptr_t Value) {
1113+
FIND_OR_CREATE_TYPEREF(A, IntegerTypeRef, Value);
1114+
}
1115+
1116+
const intptr_t &getValue() const {
1117+
return Value;
1118+
}
1119+
1120+
static bool classof(const TypeRef *TR) {
1121+
return TR->getKind() == TypeRefKind::Integer;
1122+
}
1123+
};
1124+
1125+
class BuiltinFixedArrayTypeRef final : public TypeRef {
1126+
intptr_t Size;
1127+
const TypeRef *Element;
1128+
1129+
static TypeRefID Profile(const intptr_t &Size, const TypeRef *Element) {
1130+
TypeRefID ID;
1131+
ID.addInteger((uint64_t)Size);
1132+
ID.addPointer(Element);
1133+
return ID;
1134+
}
1135+
1136+
public:
1137+
BuiltinFixedArrayTypeRef(const intptr_t &Size, const TypeRef *Element)
1138+
: TypeRef(TypeRefKind::BuiltinFixedArray), Size(Size), Element(Element) {}
1139+
1140+
template <typename Allocator>
1141+
static const BuiltinFixedArrayTypeRef *create(Allocator &A, intptr_t Size,
1142+
const TypeRef *Element) {
1143+
FIND_OR_CREATE_TYPEREF(A, BuiltinFixedArrayTypeRef, Size, Element);
1144+
}
1145+
1146+
const intptr_t &getSize() const {
1147+
return Size;
1148+
}
1149+
1150+
const TypeRef *getElementType() const {
1151+
return Element;
1152+
}
1153+
1154+
static bool classof(const TypeRef *TR) {
1155+
return TR->getKind() == TypeRefKind::BuiltinFixedArray;
1156+
}
1157+
};
1158+
10981159
template <typename ImplClass, typename RetTy = void, typename... Args>
10991160
class TypeRefVisitor {
11001161
public:

include/swift/RemoteInspection/TypeRefBuilder.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -924,19 +924,18 @@ class TypeRefBuilder {
924924
}
925925

926926
const TypeRef *createIntegerType(intptr_t value) {
927-
// FIXME: implement
928-
return nullptr;
927+
return IntegerTypeRef::create(*this, value);
929928
}
930929

931930
const TypeRef *createNegativeIntegerType(intptr_t value) {
932-
// FIXME: implement
933-
return nullptr;
931+
return IntegerTypeRef::create(*this, value);
934932
}
935933

936934
const TypeRef *createBuiltinFixedArrayType(const TypeRef *size,
937935
const TypeRef *element) {
938-
// FIXME: implement
939-
return nullptr;
936+
auto integer = cast<IntegerTypeRef>(size);
937+
return BuiltinFixedArrayTypeRef::create(*this, integer->getValue(),
938+
element);
940939
}
941940

942941
// Construct a bound generic type ref along with the parent type info

include/swift/RemoteInspection/TypeRefs.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ TYPEREF(OpaqueArchetype, TypeRef)
3737
#include "swift/AST/ReferenceStorage.def"
3838
TYPEREF(SILBox, TypeRef)
3939
TYPEREF(SILBoxTypeWithLayout, TypeRef)
40+
TYPEREF(Integer, TypeRef)
41+
TYPEREF(BuiltinFixedArray, TypeRef)
4042

4143
#undef TYPEREF

stdlib/public/RemoteInspection/TypeLowering.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ class PrintTypeInfo {
217217
stream << ")";
218218
return;
219219
}
220+
221+
case TypeInfoKind::Array: {
222+
printHeader("array");
223+
printBasic(TI);
224+
stream << ")";
225+
return;
226+
}
220227
}
221228

222229
swift_unreachable("Bad TypeInfo kind");
@@ -475,6 +482,26 @@ BitMask RecordTypeInfo::getSpareBits(TypeConverter &TC, bool &hasAddrOnly) const
475482
return mask;
476483
}
477484

485+
ArrayTypeInfo::ArrayTypeInfo(intptr_t size, const TypeInfo *elementTI)
486+
: TypeInfo(TypeInfoKind::Array,
487+
/* size */ elementTI->getStride() * size,
488+
/* alignment */ elementTI->getAlignment(),
489+
/* stride */ elementTI->getStride() * size,
490+
/* numExtraInhabitants */ elementTI->getNumExtraInhabitants(),
491+
/* isBitwiseTakable */ elementTI->isBitwiseTakable()),
492+
ElementTI(elementTI) {}
493+
494+
bool ArrayTypeInfo::readExtraInhabitantIndex(
495+
remote::MemoryReader &reader, remote::RemoteAddress address,
496+
int *extraInhabitantIndex) const {
497+
return ElementTI->readExtraInhabitantIndex(reader, address,
498+
extraInhabitantIndex);
499+
}
500+
501+
BitMask ArrayTypeInfo::getSpareBits(TypeConverter &TC, bool &hasAddrOnly) const {
502+
return ElementTI->getSpareBits(TC, hasAddrOnly);
503+
}
504+
478505
class UnsupportedEnumTypeInfo: public EnumTypeInfo {
479506
public:
480507
UnsupportedEnumTypeInfo(unsigned Size, unsigned Alignment,
@@ -1786,6 +1813,14 @@ class HasFixedSize
17861813
bool visitOpaqueArchetypeTypeRef(const OpaqueArchetypeTypeRef *O) {
17871814
return false;
17881815
}
1816+
1817+
bool visitIntegerTypeRef(const IntegerTypeRef *I) {
1818+
return false;
1819+
}
1820+
1821+
bool visitBuiltinFixedArrayTypeRef(const BuiltinFixedArrayTypeRef *BA) {
1822+
return visit(BA->getElementType());
1823+
}
17891824
};
17901825

17911826
bool TypeConverter::hasFixedSize(const TypeRef *TR) {
@@ -1921,6 +1956,14 @@ class HasSingletonMetatype
19211956
MetatypeRepresentation visitOpaqueArchetypeTypeRef(const OpaqueArchetypeTypeRef *O) {
19221957
return MetatypeRepresentation::Unknown;
19231958
}
1959+
1960+
MetatypeRepresentation visitIntegerTypeRef(const IntegerTypeRef *I) {
1961+
return MetatypeRepresentation::Unknown;
1962+
}
1963+
1964+
MetatypeRepresentation visitBuiltinFixedArrayTypeRef(const BuiltinFixedArrayTypeRef *BA) {
1965+
return visit(BA->getElementType());
1966+
}
19241967
};
19251968

19261969
class EnumTypeInfoBuilder {
@@ -2544,6 +2587,17 @@ class LowerType
25442587
DEBUG_LOG(fprintf(stderr, "Can't lower unresolved opaque archetype TypeRef"));
25452588
return nullptr;
25462589
}
2590+
2591+
const TypeInfo *visitIntegerTypeRef(const IntegerTypeRef *I) {
2592+
DEBUG_LOG(fprintf(stderr, "Can't lower integer TypeRef"));
2593+
return nullptr;
2594+
}
2595+
2596+
const TypeInfo *visitBuiltinFixedArrayTypeRef(const BuiltinFixedArrayTypeRef *BA) {
2597+
auto elementTI = visit(BA->getElementType());
2598+
2599+
return TC.makeTypeInfo<ArrayTypeInfo>(BA->getSize(), elementTI);
2600+
}
25472601
};
25482602

25492603
const TypeInfo *

stdlib/public/RemoteInspection/TypeRef.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,19 @@ class PrintTypeRef : public TypeRefVisitor<PrintTypeRef, void> {
395395
printHeader("opaque");
396396
stream << ")";
397397
}
398+
399+
void visitIntegerTypeRef(const IntegerTypeRef *I) {
400+
printHeader("integer");
401+
printField("value", std::to_string(I->getValue()));
402+
stream << ")";
403+
}
404+
405+
void visitBuiltinFixedArrayTypeRef(const BuiltinFixedArrayTypeRef *BA) {
406+
printHeader("builtin_fixed_array");
407+
printField("size", std::to_string(BA->getSize()));
408+
printRec(BA->getElementType());
409+
stream << ")";
410+
}
398411
};
399412

400413
struct TypeRefIsConcrete
@@ -506,6 +519,14 @@ struct TypeRefIsConcrete
506519
bool visitSILBoxTypeWithLayoutTypeRef(const SILBoxTypeWithLayoutTypeRef *SB) {
507520
return true;
508521
}
522+
523+
bool visitIntegerTypeRef(const IntegerTypeRef *I) {
524+
return true;
525+
}
526+
527+
bool visitBuiltinFixedArrayTypeRef(const BuiltinFixedArrayTypeRef *BA) {
528+
return visit(BA->getElementType());
529+
}
509530
};
510531

511532
const OpaqueTypeRef *
@@ -1049,6 +1070,30 @@ class DemanglingForTypeRef
10491070

10501071
return node;
10511072
}
1073+
1074+
Demangle::NodePointer createInteger(intptr_t value) {
1075+
if (value >= 0) {
1076+
return Dem.createNode(Node::Kind::Integer, value);
1077+
} else {
1078+
return Dem.createNode(Node::Kind::NegativeInteger, value);
1079+
}
1080+
}
1081+
1082+
Demangle::NodePointer visitIntegerTypeRef(const IntegerTypeRef *I) {
1083+
return createInteger(I->getValue());
1084+
}
1085+
1086+
Demangle::NodePointer visitBuiltinFixedArrayTypeRef(const BuiltinFixedArrayTypeRef *BA) {
1087+
auto ba = Dem.createNode(Node::Kind::BuiltinFixedArray);
1088+
1089+
auto size = Dem.createNode(Node::Kind::Type);
1090+
size->addChild(createInteger(BA->getSize()), Dem);
1091+
ba->addChild(size, Dem);
1092+
1093+
ba->addChild(visit(BA->getElementType()), Dem);
1094+
1095+
return ba;
1096+
}
10521097
};
10531098

10541099
Demangle::NodePointer TypeRef::getDemangling(Demangle::Demangler &Dem) const {
@@ -1277,6 +1322,14 @@ class ThickenMetatype
12771322
return O;
12781323
}
12791324

1325+
const TypeRef *visitIntegerTypeRef(const IntegerTypeRef *I) {
1326+
return I;
1327+
}
1328+
1329+
const TypeRef *visitBuiltinFixedArrayTypeRef(const BuiltinFixedArrayTypeRef *BA) {
1330+
return BuiltinFixedArrayTypeRef::create(Builder, BA->getSize(),
1331+
visit(BA->getElementType()));
1332+
}
12801333
};
12811334

12821335
static const TypeRef *
@@ -1531,6 +1584,15 @@ class TypeRefSubstitution
15311584
O->getOrdinal(),
15321585
newArgLists);
15331586
}
1587+
1588+
const TypeRef *visitIntegerTypeRef(const IntegerTypeRef *I) {
1589+
return I;
1590+
}
1591+
1592+
const TypeRef *visitBuiltinFixedArrayTypeRef(const BuiltinFixedArrayTypeRef *BA) {
1593+
return BuiltinFixedArrayTypeRef::create(Builder, BA->getSize(),
1594+
visit(BA->getElementType()));
1595+
}
15341596
};
15351597

15361598
const TypeRef *TypeRef::subst(TypeRefBuilder &Builder,

test/Reflection/typeref_lowering.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,3 +1269,12 @@ BD
12691269

12701270
// CHECK-32: (builtin Builtin.DefaultActorStorage)
12711271
// CHECK-32-NEXT: (builtin size=48 alignment=8 stride=48 num_extra_inhabitants=0 bitwise_takable=1)
1272+
1273+
$1_SiBV
1274+
// CHECK-64: (builtin_fixed_array size=2
1275+
// CHECK-64-NEXT: (struct Swift.Int))
1276+
// CHECK-64-NEXT: (array size=16 alignment=8 stride=16 num_extra_inhabitants=0 bitwise_takable=1)
1277+
1278+
// CHECK-32: (builtin_fixed_array size=2
1279+
// CHECK-32-NEXT: (struct Swift.Int))
1280+
// CHECK-32-NEXT: (array size=8 alignment=4 stride=8 num_extra_inhabitants=0 bitwise_takable=1)

0 commit comments

Comments
 (0)