Skip to content

Commit 4ec8276

Browse files
authored
[Runtime] Extract some layout string functionality into separate functions (#66577)
We want to re-use them for enum layout string instantiation, so best to pull them into separate functions.
1 parent f79938c commit 4ec8276

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

include/swift/Runtime/Metadata.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,36 @@ void swift_initStructMetadataWithLayoutString(StructMetadata *self,
666666
const uint8_t *fieldTags,
667667
uint32_t *fieldOffsets);
668668

669+
SWIFT_RUNTIME_STDLIB_INTERNAL
670+
size_t _swift_refCountBytesForMetatype(const Metadata *type);
671+
672+
enum LayoutStringFlags : uint64_t {
673+
Empty = 0,
674+
// TODO: Track other useful information tha can be used to optimize layout
675+
// strings, like different reference kinds contained in the string
676+
// number of ref counting operations (maybe up to 4), so we can
677+
// use witness functions optimized for these cases.
678+
HasRelativePointers = (1ULL << 63),
679+
};
680+
681+
inline bool operator&(LayoutStringFlags a, LayoutStringFlags b) {
682+
return (uint64_t(a) & uint64_t(b)) != 0;
683+
}
684+
inline LayoutStringFlags operator|(LayoutStringFlags a, LayoutStringFlags b) {
685+
return LayoutStringFlags(uint64_t(a) | uint64_t(b));
686+
}
687+
inline LayoutStringFlags &operator|=(LayoutStringFlags &a, LayoutStringFlags b) {
688+
return a = (a | b);
689+
}
690+
691+
SWIFT_RUNTIME_STDLIB_INTERNAL
692+
void _swift_addRefCountStringForMetatype(uint8_t *layoutStr,
693+
size_t &layoutStrOffset,
694+
LayoutStringFlags &flags,
695+
const Metadata *fieldType,
696+
size_t &fullOffset,
697+
size_t &previousFieldOffset);
698+
669699
/// Allocate the metadata for a class and copy fields from the given pattern.
670700
/// The final size of the metadata is calculated at runtime from the metadata
671701
/// bounds in the class descriptor.

stdlib/public/runtime/Enum.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,66 @@ swift::swift_initEnumMetadataMultiPayload(EnumMetadata *enumType,
214214
vwtable->publishLayout(layout);
215215
}
216216

217+
// void
218+
// swift::swift_initEnumMetadataMultiPayloadWithLayoutString(EnumMetadata *enumType,
219+
// EnumLayoutFlags layoutFlags,
220+
// unsigned numPayloads,
221+
// const TypeLayout * const *payloadLayouts) {
222+
// // Accumulate the layout requirements of the payloads.
223+
// size_t payloadSize = 0, alignMask = 0;
224+
// bool isPOD = true, isBT = true;
225+
// for (unsigned i = 0; i < numPayloads; ++i) {
226+
// const TypeLayout *payloadLayout = payloadLayouts[i];
227+
// payloadSize
228+
// = std::max(payloadSize, (size_t)payloadLayout->size);
229+
// alignMask |= payloadLayout->flags.getAlignmentMask();
230+
// isPOD &= payloadLayout->flags.isPOD();
231+
// isBT &= payloadLayout->flags.isBitwiseTakable();
232+
// }
233+
234+
// // Store the max payload size in the metadata.
235+
// assignUnlessEqual(enumType->getPayloadSize(), payloadSize);
236+
237+
// // The total size includes space for the tag.
238+
// auto tagCounts = getEnumTagCounts(payloadSize,
239+
// enumType->getDescription()->getNumEmptyCases(),
240+
// numPayloads);
241+
// unsigned totalSize = payloadSize + tagCounts.numTagBytes;
242+
243+
// // See whether there are extra inhabitants in the tag.
244+
// unsigned numExtraInhabitants = tagCounts.numTagBytes == 4
245+
// ? INT_MAX
246+
// : (1 << (tagCounts.numTagBytes * 8)) - tagCounts.numTags;
247+
// numExtraInhabitants = std::min(numExtraInhabitants,
248+
// unsigned(ValueWitnessFlags::MaxNumExtraInhabitants));
249+
250+
// auto vwtable = getMutableVWTableForInit(enumType, layoutFlags);
251+
252+
// // Set up the layout info in the vwtable.
253+
// auto rawStride = (totalSize + alignMask) & ~alignMask;
254+
// TypeLayout layout{totalSize,
255+
// rawStride == 0 ? 1 : rawStride,
256+
// ValueWitnessFlags()
257+
// .withAlignmentMask(alignMask)
258+
// .withPOD(isPOD)
259+
// .withBitwiseTakable(isBT)
260+
// .withEnumWitnesses(true)
261+
// .withInlineStorage(ValueWitnessTable::isValueInline(
262+
// isBT, totalSize, alignMask + 1)),
263+
// numExtraInhabitants};
264+
265+
// installCommonValueWitnesses(layout, vwtable);
266+
267+
// // Unconditionally overwrite the enum-tag witnesses.
268+
// // The compiler does not generate meaningful enum-tag witnesses for
269+
// // enums in this state.
270+
// vwtable->getEnumTagSinglePayload = swift_getMultiPayloadEnumTagSinglePayload;
271+
// vwtable->storeEnumTagSinglePayload =
272+
// swift_storeMultiPayloadEnumTagSinglePayload;
273+
274+
// vwtable->publishLayout(layout);
275+
//}
276+
217277
namespace {
218278
struct MultiPayloadLayout {
219279
size_t payloadSize;

0 commit comments

Comments
 (0)