Skip to content

Commit f1cb0d1

Browse files
committed
Reflection: Make RemoteRef template not depend on Runtime.
The only thing the Runtime affects is the width of the StoredPointer for the remote address, for which storing a uint64_t ought to be enough for anyone we care about so far. This will make it easier to store and use RemoteRefs in code that isn't or shouldn't ideally be templatized on Runtime (such as TypeRefBuilder, and ultimately ReflectionContext, from the Reflection library.)
1 parent 94265e8 commit f1cb0d1

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,8 @@ class TypeRefBuilder {
531531
// underlying type if available.
532532
if (context->getKind() == ContextDescriptorKind::OpaqueType) {
533533
return Dem.createNode(
534-
Node::Kind::OpaqueTypeDescriptorSymbolicReference,
535-
(uintptr_t)context.getAddress());
534+
Node::Kind::OpaqueTypeDescriptorSymbolicReference,
535+
(uintptr_t)context.template getAddress<Runtime>());
536536
}
537537

538538
return reader.buildContextMangling(context, Dem);

include/swift/Remote/MetadataReader.h

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,29 @@ using TypeDecoder = swift::Demangle::TypeDecoder<BuilderType>;
4343

4444
/// A pointer to the local buffer of an object that also remembers the
4545
/// address at which it was stored remotely.
46-
template <typename Runtime, typename T>
46+
template <typename T>
4747
class RemoteRef {
48-
public:
49-
using StoredPointer = typename Runtime::StoredPointer;
50-
5148
private:
52-
StoredPointer Address;
49+
uint64_t Address;
5350
const T *LocalBuffer;
5451

5552
public:
5653
/*implicit*/
5754
RemoteRef(std::nullptr_t _)
5855
: Address(0), LocalBuffer(nullptr) {}
5956

57+
template<typename StoredPointer>
6058
explicit RemoteRef(StoredPointer address, const T *localBuffer)
61-
: Address(address), LocalBuffer(localBuffer) {}
59+
: Address((uint64_t)address), LocalBuffer(localBuffer) {}
6260

63-
StoredPointer getAddress() const {
61+
uint64_t getAddressData() const {
6462
return Address;
6563
}
64+
65+
template<typename Runtime>
66+
typename Runtime::StoredPointer getAddress() const {
67+
return (typename Runtime::StoredPointer)getAddressData();
68+
}
6669

6770
const T *getLocalBuffer() const {
6871
return LocalBuffer;
@@ -76,6 +79,14 @@ class RemoteRef {
7679
assert(LocalBuffer);
7780
return LocalBuffer;
7881
}
82+
83+
bool operator==(RemoteRef<T> other) const {
84+
return Address == other.Address;
85+
}
86+
87+
bool operator!=(RemoteRef<T> other) const {
88+
return !operator==(other);
89+
}
7990
};
8091

8192
/// A structure, designed for use with std::unique_ptr, which destroys
@@ -127,17 +138,15 @@ class MetadataReader {
127138
/// A cache of built types, keyed by the address of the type.
128139
std::unordered_map<StoredPointer, BuiltType> TypeCache;
129140

130-
using MetadataRef =
131-
RemoteRef<Runtime, TargetMetadata<Runtime>>;
141+
using MetadataRef = RemoteRef<TargetMetadata<Runtime>>;
132142
using OwnedMetadataRef =
133143
std::unique_ptr<const TargetMetadata<Runtime>, delete_with_free>;
134144

135145
/// A cache of read type metadata, keyed by the address of the metadata.
136146
std::unordered_map<StoredPointer, OwnedMetadataRef>
137147
MetadataCache;
138148

139-
using ContextDescriptorRef =
140-
RemoteRef<Runtime, TargetContextDescriptor<Runtime>>;
149+
using ContextDescriptorRef = RemoteRef<TargetContextDescriptor<Runtime>>;
141150
using OwnedContextDescriptorRef =
142151
std::unique_ptr<const TargetContextDescriptor<Runtime>,
143152
delete_with_free>;
@@ -1166,7 +1175,7 @@ class MetadataReader {
11661175
return None;
11671176

11681177
auto addressOfGenericArgAddress =
1169-
(Meta.getAddress() +
1178+
(Meta.template getAddress<Runtime>() +
11701179
*offsetToGenericArgs * sizeof(StoredPointer) +
11711180
index * sizeof(StoredPointer));
11721181

@@ -1292,29 +1301,31 @@ class MetadataReader {
12921301

12931302
template<typename Base, typename Field>
12941303
StoredPointer resolveRelativeField(
1295-
RemoteRef<Runtime, Base> base, const Field &field) {
1304+
RemoteRef<Base> base, const Field &field) {
12961305
// Map the offset from within our local buffer to the remote address.
12971306
auto distance = (intptr_t)&field - (intptr_t)base.getLocalBuffer();
1298-
return resolveRelativeOffset<int32_t>(base.getAddress() + distance);
1307+
return resolveRelativeOffset<int32_t>(
1308+
base.template getAddress<Runtime>() + distance);
12991309
}
13001310

13011311
template<typename Base, typename Field>
13021312
Optional<StoredPointer> resolveNullableRelativeField(
1303-
RemoteRef<Runtime, Base> base, const Field &field) {
1313+
RemoteRef<Base> base, const Field &field) {
13041314
// Map the offset from within our local buffer to the remote address.
13051315
auto distance = (intptr_t)&field - (intptr_t)base.getLocalBuffer();
13061316

1307-
return resolveNullableRelativeOffset<int32_t>(base.getAddress() + distance);
1317+
return resolveNullableRelativeOffset<int32_t>(
1318+
base.template getAddress<Runtime>() + distance);
13081319
}
13091320

13101321
template<typename Base, typename Field>
13111322
Optional<StoredPointer> resolveNullableRelativeIndirectableField(
1312-
RemoteRef<Runtime, Base> base, const Field &field) {
1323+
RemoteRef<Base> base, const Field &field) {
13131324
// Map the offset from within our local buffer to the remote address.
13141325
auto distance = (intptr_t)&field - (intptr_t)base.getLocalBuffer();
13151326

13161327
return resolveNullableRelativeIndirectableOffset<int32_t>(
1317-
base.getAddress() + distance);
1328+
base.template getAddress<Runtime>() + distance);
13181329
}
13191330

13201331
/// Given a pointer to an Objective-C class, try to read its class name.
@@ -1838,7 +1849,8 @@ class MetadataReader {
18381849
const RelativeTargetProtocolDescriptorPointer<Runtime> &protocol) {
18391850
// Map the offset from within our local buffer to the remote address.
18401851
auto distance = (intptr_t)&protocol - (intptr_t)descriptor.getLocalBuffer();
1841-
StoredPointer targetAddress(descriptor.getAddress() + distance);
1852+
StoredPointer targetAddress(
1853+
descriptor.template getAddress<Runtime>() + distance);
18421854

18431855
// Read the relative offset.
18441856
int32_t relative;
@@ -2071,7 +2083,8 @@ class MetadataReader {
20712083
// address.
20722084
auto distance =
20732085
(intptr_t)&req.Layout - (intptr_t)descriptor.getLocalBuffer();
2074-
StoredPointer targetAddress(descriptor.getAddress() + distance);
2086+
StoredPointer targetAddress(
2087+
descriptor.template getAddress<Runtime>() + distance);
20752088

20762089
GenericRequirementLayoutKind kind;
20772090
if (!Reader->readBytes(RemoteAddress(targetAddress),
@@ -2106,7 +2119,7 @@ class MetadataReader {
21062119
// Use the remote address to identify the anonymous context.
21072120
char addressBuf[18];
21082121
snprintf(addressBuf, sizeof(addressBuf), "$%" PRIx64,
2109-
(uint64_t)descriptor.getAddress());
2122+
(uint64_t)descriptor.getAddressData());
21102123
auto anonNode = dem.createNode(Node::Kind::AnonymousContext);
21112124
CharVector addressStr;
21122125
addressStr.append(addressBuf, dem);
@@ -2269,7 +2282,7 @@ class MetadataReader {
22692282
if (!offsetToGenericArgs)
22702283
return {};
22712284

2272-
auto genericArgsAddr = metadata.getAddress()
2285+
auto genericArgsAddr = metadata.template getAddress<Runtime>()
22732286
+ sizeof(StoredPointer) * *offsetToGenericArgs;
22742287

22752288
std::vector<BuiltType> builtSubsts;
@@ -2326,9 +2339,8 @@ class MetadataReader {
23262339

23272340
// If we've skipped an artificial subclasses, check the cache at
23282341
// the superclass. (This also protects against recursion.)
2329-
if (skipArtificialSubclasses &&
2330-
metadata.getAddress() != origMetadata.getAddress()) {
2331-
auto it = TypeCache.find(metadata.getAddress());
2342+
if (skipArtificialSubclasses && metadata != origMetadata) {
2343+
auto it = TypeCache.find(metadata.template getAddress<Runtime>());
23322344
if (it != TypeCache.end())
23332345
return it->second;
23342346
}
@@ -2358,13 +2370,12 @@ class MetadataReader {
23582370
if (!nominal)
23592371
return BuiltType();
23602372

2361-
TypeCache[metadata.getAddress()] = nominal;
2373+
TypeCache[metadata.template getAddress<Runtime>()] = nominal;
23622374

23632375
// If we've skipped an artificial subclass, remove the
23642376
// recursion-protection entry we made for it.
2365-
if (skipArtificialSubclasses &&
2366-
metadata.getAddress() != origMetadata.getAddress()) {
2367-
TypeCache.erase(origMetadata.getAddress());
2377+
if (skipArtificialSubclasses && metadata != origMetadata) {
2378+
TypeCache.erase(origMetadata.template getAddress<Runtime>());
23682379
}
23692380

23702381
return nominal;
@@ -2377,7 +2388,8 @@ class MetadataReader {
23772388
return readNominalTypeFromMetadata(origMetadata, skipArtificialSubclasses);
23782389

23792390
std::string className;
2380-
if (!readObjCClassName(origMetadata.getAddress(), className))
2391+
auto origMetadataPtr = origMetadata.template getAddress<Runtime>();
2392+
if (!readObjCClassName(origMetadataPtr, className))
23812393
return BuiltType();
23822394

23832395
BuiltType BuiltObjCClass = Builder.createObjCClassType(std::move(className));
@@ -2390,7 +2402,7 @@ class MetadataReader {
23902402
skipArtificialSubclasses);
23912403
}
23922404

2393-
TypeCache[origMetadata.getAddress()] = BuiltObjCClass;
2405+
TypeCache[origMetadataPtr] = BuiltObjCClass;
23942406
return BuiltObjCClass;
23952407
}
23962408

@@ -2583,11 +2595,11 @@ class MetadataReader {
25832595
} // end namespace swift
25842596

25852597
namespace llvm {
2586-
template<typename Runtime, typename T>
2587-
struct simplify_type<swift::remote::RemoteRef<Runtime, T>> {
2598+
template<typename T>
2599+
struct simplify_type<swift::remote::RemoteRef<T>> {
25882600
using SimpleType = const T *;
25892601
static SimpleType
2590-
getSimplifiedValue(swift::remote::RemoteRef<Runtime, T> value) {
2602+
getSimplifiedValue(swift::remote::RemoteRef<T> value) {
25912603
return value.getLocalBuffer();
25922604
}
25932605
};

0 commit comments

Comments
 (0)