Skip to content

Commit cf28ff4

Browse files
committed
Remove TwoWordPair and use SwiftCC instead.
1 parent e43ff71 commit cf28ff4

19 files changed

+99
-147
lines changed

include/swift/Runtime/HeapObject.h

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -90,66 +90,10 @@ HeapObject *swift_initStaticObject(HeapMetadata const *metadata,
9090
SWIFT_RUNTIME_EXPORT
9191
void swift_verifyEndOfLifetime(HeapObject *object);
9292

93-
/// A structure that's two pointers in size.
94-
///
95-
/// C functions can use the TwoWordPair::Return type to return a value in
96-
/// two registers, compatible with Swift's calling convention for tuples
97-
/// and structs of two word-sized elements.
98-
template<typename A, typename B>
99-
struct TwoWordPair {
100-
A first;
101-
B second;
102-
103-
TwoWordPair() = default;
104-
TwoWordPair(A first, B second);
105-
106-
// FIXME: rdar://36755525 clang miscompiles swiftcall functions
107-
// Structs are returned indirectly on these platforms, but we want to return
108-
// in registers, so cram the result into an unsigned long long.
109-
// Use an enum class with implicit conversions so we don't dirty C callers
110-
// too much.
111-
#if __arm__ || __i386__ || defined(__CYGWIN__) || defined(_MSC_VER)
112-
#if defined(__CYGWIN__)
113-
enum class Return : unsigned __int128 {};
114-
#else
115-
enum class Return : unsigned long long {};
116-
#endif
117-
118-
operator Return() const {
119-
union {
120-
TwoWordPair value;
121-
Return mangled;
122-
} reinterpret = {*this};
123-
124-
return reinterpret.mangled;
125-
}
126-
127-
/*implicit*/ TwoWordPair(Return r) {
128-
union {
129-
Return mangled;
130-
TwoWordPair value;
131-
} reinterpret = {r};
132-
133-
*this = reinterpret.value;
134-
}
135-
#else
136-
using Return = TwoWordPair;
137-
#endif
93+
struct BoxPair {
94+
HeapObject *object;
95+
OpaqueValue *buffer;
13896
};
139-
140-
template<typename A, typename B>
141-
inline TwoWordPair<A,B>::TwoWordPair(A first, B second)
142-
: first(first), second(second)
143-
{
144-
static_assert(sizeof(A) == sizeof(void*),
145-
"first type must be word-sized");
146-
static_assert(sizeof(B) == sizeof(void*),
147-
"second type must be word-sized");
148-
static_assert(alignof(TwoWordPair) == alignof(void*),
149-
"pair must be word-aligned");
150-
}
151-
152-
using BoxPair = TwoWordPair<HeapObject *, OpaqueValue *>;
15397

15498
/// Allocates a heap object that can contain a value of the given type.
15599
/// Returns a Box structure containing a HeapObject* pointer to the
@@ -158,16 +102,16 @@ using BoxPair = TwoWordPair<HeapObject *, OpaqueValue *>;
158102
/// appropriate to store a value of the given type.
159103
/// The heap object has an initial retain count of 1, and its metadata is set
160104
/// such that destroying the heap object destroys the contained value.
161-
SWIFT_RUNTIME_EXPORT
162-
BoxPair::Return swift_allocBox(Metadata const *type);
105+
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT
106+
BoxPair swift_allocBox(Metadata const *type);
163107

164108
/// Performs a uniqueness check on the pointer to a box structure. If the check
165109
/// fails allocates a new box and stores the pointer in the buffer.
166110
///
167111
/// if (!isUnique(buffer[0]))
168112
/// buffer[0] = swift_allocBox(type)
169-
SWIFT_RUNTIME_EXPORT
170-
BoxPair::Return swift_makeBoxUnique(OpaqueValue *buffer, Metadata const *type,
113+
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT
114+
BoxPair swift_makeBoxUnique(OpaqueValue *buffer, Metadata const *type,
171115
size_t alignMask);
172116

173117
/// Returns the address of a heap object representing all empty box types.
@@ -1147,11 +1091,16 @@ static inline bool swift_unknownUnownedIsEqual(UnownedReference *ref,
11471091

11481092
#endif /* SWIFT_OBJC_INTEROP */
11491093

1094+
struct TypeNamePair {
1095+
const char *data;
1096+
uintptr_t length;
1097+
};
1098+
11501099
/// Return the name of a Swift type represented by a metadata object.
11511100
/// func _getTypeName(_ type: Any.Type, qualified: Bool)
11521101
/// -> (UnsafePointer<UInt8>, Int)
11531102
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
1154-
TwoWordPair<const char *, uintptr_t>::Return
1103+
TypeNamePair
11551104
swift_getTypeName(const Metadata *type, bool qualified);
11561105

11571106
} // end namespace swift

include/swift/Runtime/InstrumentsSupport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ SWIFT_RUNTIME_EXPORT
2525
HeapObject *(*_swift_allocObject)(HeapMetadata const *metadata,
2626
size_t requiredSize,
2727
size_t requiredAlignmentMask);
28-
SWIFT_RUNTIME_EXPORT
29-
BoxPair::Return (*_swift_allocBox)(Metadata const *type);
28+
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT
29+
BoxPair (*_swift_allocBox)(Metadata const *type);
3030
SWIFT_RUNTIME_EXPORT
3131
HeapObject *(*_swift_retain)(HeapObject *object);
3232
SWIFT_RUNTIME_EXPORT

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
#define FUNCTION(Id, Name, CC, ReturnTys, ArgTys, Attrs) FUNCTION_ID(Id)
3939
#endif
4040

41-
FUNCTION(AllocBox, swift_allocBox, C_CC,
41+
FUNCTION(AllocBox, swift_allocBox, SwiftCC,
4242
RETURNS(RefCountedPtrTy, OpaquePtrTy),
4343
ARGS(TypeMetadataPtrTy),
4444
ATTRS(NoUnwind))
4545

4646
// BoxPair swift_makeBoxUnique(OpaqueValue *buffer, Metadata *type, size_t alignMask);
4747
FUNCTION(MakeBoxUnique,
4848
swift_makeBoxUnique,
49-
C_CC,
49+
SwiftCC,
5050
RETURNS(RefCountedPtrTy, OpaquePtrTy),
5151
ARGS(OpaquePtrTy, TypeMetadataPtrTy, SizeTy),
5252
ATTRS(NoUnwind))
@@ -1311,7 +1311,7 @@ FUNCTION(DeletedMethodError, swift_deletedMethodError, C_CC,
13111311
ARGS(),
13121312
ATTRS(NoUnwind))
13131313

1314-
FUNCTION(AllocError, swift_allocError, C_CC,
1314+
FUNCTION(AllocError, swift_allocError, SwiftCC,
13151315
RETURNS(ErrorPtrTy, OpaquePtrTy),
13161316
ARGS(TypeMetadataPtrTy, WitnessTablePtrTy, OpaquePtrTy, Int1Ty),
13171317
ATTRS(NoUnwind))

stdlib/public/SDK/Foundation/CheckClass.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ static void logIfFirstOccurrence(Class objcClass, void (^log)(void)) {
5959
template <size_t N>
6060
StringRefLite(const char (&staticStr)[N]) : data(staticStr), length(N) {}
6161

62-
StringRefLite(swift::TwoWordPair<const char *, uintptr_t>::Return rawValue)
63-
: data(swift::TwoWordPair<const char *, uintptr_t>(rawValue).first),
64-
length(swift::TwoWordPair<const char *, uintptr_t>(rawValue).second){}
62+
StringRefLite(swift::TypeNamePair rawValue)
63+
: data(rawValue.data),
64+
length(rawValue.length){}
6565

6666
NS_RETURNS_RETAINED
6767
NSString *newNSStringNoCopy() const {

stdlib/public/runtime/Casting.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ std::string swift::nameForMetadata(const Metadata *type,
113113
return result;
114114
}
115115

116-
TwoWordPair<const char *, uintptr_t>::Return
116+
TypeNamePair
117117
swift::swift_getTypeName(const Metadata *type, bool qualified) {
118-
using Pair = TwoWordPair<const char *, uintptr_t>;
119118
using Key = llvm::PointerIntPair<const Metadata *, 1, bool>;
120119

121120
static StaticReadWriteLock TypeNameCacheLock;
@@ -132,7 +131,7 @@ swift::swift_getTypeName(const Metadata *type, bool qualified) {
132131
auto found = cache.find(key);
133132
if (found != cache.end()) {
134133
auto result = found->second;
135-
return Pair{result.first, result.second};
134+
return TypeNamePair{result.first, result.second};
136135
}
137136
}
138137

@@ -145,7 +144,7 @@ swift::swift_getTypeName(const Metadata *type, bool qualified) {
145144
auto found = cache.find(key);
146145
if (found != cache.end()) {
147146
auto result = found->second;
148-
return Pair{result.first, result.second};
147+
return TypeNamePair{result.first, result.second};
149148
}
150149

151150
// Build the metadata name.
@@ -157,7 +156,7 @@ swift::swift_getTypeName(const Metadata *type, bool qualified) {
157156
result[size] = 0;
158157

159158
cache.insert({key, {result, size}});
160-
return Pair{result, size};
159+
return TypeNamePair{result, size};
161160
}
162161
}
163162

@@ -954,7 +953,7 @@ static bool _dynamicCastToExistential(OpaqueValue *dest,
954953
(canConsumeDynamicValue && (flags & DynamicCastFlags::TakeOnSuccess));
955954
BoxPair destBox = swift_allocError(srcDynamicType, errorWitness,
956955
srcDynamicValue, isTake);
957-
*destBoxAddr = reinterpret_cast<SwiftError*>(destBox.first);
956+
*destBoxAddr = reinterpret_cast<SwiftError*>(destBox.object);
958957
maybeDeallocateSource(true);
959958
return true;
960959
}
@@ -1976,7 +1975,7 @@ static id dynamicCastValueToNSError(OpaqueValue *src,
19761975

19771976
BoxPair errorBox = swift_allocError(srcType, srcErrorWitness, src,
19781977
/*isTake*/ flags & DynamicCastFlags::TakeOnSuccess);
1979-
return _swift_stdlib_bridgeErrorToNSError((SwiftError*)errorBox.first);
1978+
return _swift_stdlib_bridgeErrorToNSError((SwiftError*)errorBox.object);
19801979
}
19811980

19821981
#endif

stdlib/public/runtime/ErrorObject.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ struct SwiftError : SwiftErrorHeader {
171171
/// copied (or taken if \c isTake is true) into the newly-allocated error box.
172172
/// If value is null, the box's contents will be left uninitialized, and
173173
/// \c isTake should be false.
174-
SWIFT_RUNTIME_STDLIB_API
175-
BoxPair::Return swift_allocError(const Metadata *type,
176-
const WitnessTable *errorConformance,
177-
OpaqueValue *value, bool isTake);
174+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
175+
BoxPair swift_allocError(const Metadata *type,
176+
const WitnessTable *errorConformance,
177+
OpaqueValue *value, bool isTake);
178178

179179
/// Deallocate an error object whose contained object has already been
180180
/// destroyed.

stdlib/public/runtime/ErrorObject.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static Class getSwiftNativeNSErrorClass() {
161161
}
162162

163163
/// Allocate a catchable error object.
164-
BoxPair::Return
164+
BoxPair
165165
swift::swift_allocError(const Metadata *type,
166166
const WitnessTable *errorConformance,
167167
OpaqueValue *initialValue,

stdlib/public/runtime/ErrorObjectNative.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static const FullMetadata<HeapMetadata> ErrorMetadata{
6464
Metadata{MetadataKind::ErrorObject},
6565
};
6666

67-
BoxPair::Return
67+
BoxPair
6868
swift::swift_allocError(const swift::Metadata *type,
6969
const swift::WitnessTable *errorConformance,
7070
OpaqueValue *initialValue,

stdlib/public/runtime/HeapObject.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class BoxCacheEntry {
210210

211211
static SimpleGlobalCache<BoxCacheEntry> Boxes;
212212

213-
BoxPair::Return swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type,
213+
BoxPair swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type,
214214
size_t alignMask) {
215215
auto *inlineBuffer = reinterpret_cast<ValueBuffer*>(buffer);
216216
HeapObject *box = reinterpret_cast<HeapObject *>(inlineBuffer->PrivateData[0]);
@@ -222,8 +222,8 @@ BoxPair::Return swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *
222222
auto *oldObjectAddr = reinterpret_cast<OpaqueValue *>(
223223
reinterpret_cast<char *>(box) + headerOffset);
224224
// Copy the data.
225-
type->vw_initializeWithCopy(refAndObjectAddr.second, oldObjectAddr);
226-
inlineBuffer->PrivateData[0] = refAndObjectAddr.first;
225+
type->vw_initializeWithCopy(refAndObjectAddr.buffer, oldObjectAddr);
226+
inlineBuffer->PrivateData[0] = refAndObjectAddr.object;
227227
// Release ownership of the old box.
228228
swift_release(box);
229229
return refAndObjectAddr;
@@ -235,11 +235,12 @@ BoxPair::Return swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *
235235
}
236236
}
237237

238-
BoxPair::Return swift::swift_allocBox(const Metadata *type) {
238+
BoxPair swift::swift_allocBox(const Metadata *type) {
239239
return _swift_allocBox(type);
240240
}
241241

242-
static BoxPair::Return _swift_allocBox_(const Metadata *type) {
242+
SWIFT_CC(swift)
243+
static BoxPair _swift_allocBox_(const Metadata *type) {
243244
// Get the heap metadata for the box.
244245
auto metadata = &Boxes.getOrInsert(type).first->Data;
245246

stdlib/public/runtime/Metadata.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,8 @@ static OpaqueValue *tuple_allocateBuffer(ValueBuffer *buffer,
591591
if (IsInline)
592592
return reinterpret_cast<OpaqueValue*>(buffer);
593593
BoxPair refAndValueAddr(swift_allocBox(metatype));
594-
*reinterpret_cast<HeapObject **>(buffer) = refAndValueAddr.first;
595-
return refAndValueAddr.second;
594+
*reinterpret_cast<HeapObject **>(buffer) = refAndValueAddr.object;
595+
return refAndValueAddr.buffer;
596596
}
597597

598598
/// Generic tuple value witness for 'destroy'.
@@ -2672,8 +2672,8 @@ template <> OpaqueValue *Metadata::allocateBoxForExistentialIn(ValueBuffer *buff
26722672

26732673
// Allocate the box.
26742674
BoxPair refAndValueAddr(swift_allocBox(this));
2675-
buffer->PrivateData[0] = refAndValueAddr.first;
2676-
return refAndValueAddr.second;
2675+
buffer->PrivateData[0] = refAndValueAddr.object;
2676+
return refAndValueAddr.buffer;
26772677
}
26782678

26792679
template <> OpaqueValue *Metadata::allocateBufferIn(ValueBuffer *buffer) const {

stdlib/public/runtime/ProtocolConformance.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ ProtocolConformanceDescriptor::getWitnessTable(const Metadata *type) const {
174174
// array of witness tables to pass along to the accessor.
175175

176176
// Pretty-print the type name.
177-
auto typeNamePair = TwoWordPair<const char *, uintptr_t>(
177+
auto typeNamePair = TypeNamePair(
178178
swift_getTypeName(type, /*qualified=*/true));
179-
std::string typeName(typeNamePair.first,
180-
typeNamePair.first + typeNamePair.second);
179+
std::string typeName(typeNamePair.data,
180+
typeNamePair.data + typeNamePair.length);
181181

182182
// Demangle the protocol name.
183183
DemangleOptions options;

stdlib/public/runtime/Reflection.mm

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -714,21 +714,21 @@ void swift_EnumMirror_subscript(String *outString,
714714
BoxPair pair = swift_allocBox(boxType);
715715

716716
type->vw_destructiveProjectEnumData(const_cast<OpaqueValue *>(value));
717-
boxType->vw_initializeWithCopy(pair.second, const_cast<OpaqueValue *>(value));
717+
boxType->vw_initializeWithCopy(pair.buffer, const_cast<OpaqueValue *>(value));
718718
type->vw_destructiveInjectEnumTag(const_cast<OpaqueValue *>(value),
719719
(int) (tag - Description.getNumPayloadCases()));
720720

721721
SWIFT_CC_PLUSONE_GUARD(swift_release(owner));
722722

723-
owner = pair.first;
724-
value = pair.second;
723+
owner = pair.object;
724+
value = pair.buffer;
725725

726726
// If the payload is indirect, we need to jump through the box to get it.
727727
if (indirect) {
728728
owner = *reinterpret_cast<HeapObject * const *>(value);
729729
value = swift_projectBox(const_cast<HeapObject *>(owner));
730730
swift_retain(owner);
731-
swift_release(pair.first);
731+
swift_release(pair.object);
732732
}
733733

734734
new (outString) String(getFieldName(Description.CaseNames, tag));
@@ -1129,12 +1129,12 @@ static Mirror ObjC_getMirrorForSuperclass(Class sup,
11291129
BoxPair box = swift_allocBox(T);
11301130

11311131
if (take)
1132-
T->vw_initializeWithTake(box.second, value);
1132+
T->vw_initializeWithTake(box.buffer, value);
11331133
else
1134-
T->vw_initializeWithCopy(box.second, value);
1135-
std::tie(T, Self, MirrorWitness) = getImplementationForType(T, box.second);
1134+
T->vw_initializeWithCopy(box.buffer, value);
1135+
std::tie(T, Self, MirrorWitness) = getImplementationForType(T, box.buffer);
11361136

1137-
Data = {box.first, box.second, T};
1137+
Data = {box.object, box.buffer, T};
11381138
}
11391139

11401140
/// MagicMirror ownership-sharing subvalue constructor.

stdlib/public/runtime/SwiftObject.mm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,10 +1398,13 @@ static bool usesNativeSwiftReferenceCounting_nonNull(
13981398
return object->refCounts.isUniquelyReferencedOrPinned();
13991399
}
14001400

1401-
using ClassExtents = TwoWordPair<size_t, size_t>;
1401+
struct ClassExtents {
1402+
size_t negative;
1403+
size_t positive;
1404+
};
14021405

14031406
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
1404-
ClassExtents::Return
1407+
ClassExtents
14051408
_getSwiftClassInstanceExtents(const Metadata *c) {
14061409
assert(c && c->isClassObject());
14071410
auto metaData = c->getClassObject();
@@ -1414,7 +1417,7 @@ static bool usesNativeSwiftReferenceCounting_nonNull(
14141417
#if SWIFT_OBJC_INTEROP
14151418

14161419
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
1417-
ClassExtents::Return
1420+
ClassExtents
14181421
_getObjCClassInstanceExtents(const ClassMetadata* c) {
14191422
// Pure ObjC classes never have negative extents.
14201423
if (c->isPureObjC())

0 commit comments

Comments
 (0)