@@ -209,14 +209,10 @@ static Address emitFixedSizeMetadataPackRef(IRGenFunction &IGF,
209209 return pack;
210210}
211211
212- static llvm::Value * bindMetadataAtIndex (IRGenFunction &IGF,
213- CanType elementArchetype ,
212+ // / Use this to index into packs to correctly handle on-heap packs.
213+ static llvm::Value * loadMetadataAtIndex (IRGenFunction &IGF ,
214214 llvm::Value *patternPack,
215- llvm::Value *index,
216- DynamicMetadataRequest request) {
217- if (auto response = IGF.tryGetLocalTypeMetadata (elementArchetype, request))
218- return response.getMetadata ();
219-
215+ llvm::Value *index) {
220216 // If the pack is on the heap, the LSB is set, so mask it off.
221217 patternPack =
222218 IGF.Builder .CreatePtrToInt (patternPack, IGF.IGM .SizeTy );
@@ -233,7 +229,18 @@ static llvm::Value *bindMetadataAtIndex(IRGenFunction &IGF,
233229 IGF.Builder .CreateInBoundsGEP (patternPackAddress.getElementType (),
234230 patternPackAddress.getAddress (), index),
235231 patternPackAddress.getElementType (), patternPackAddress.getAlignment ());
236- llvm::Value *metadata = IGF.Builder .CreateLoad (fromPtr);
232+ return IGF.Builder .CreateLoad (fromPtr);
233+ }
234+
235+ static llvm::Value *bindMetadataAtIndex (IRGenFunction &IGF,
236+ CanType elementArchetype,
237+ llvm::Value *patternPack,
238+ llvm::Value *index,
239+ DynamicMetadataRequest request) {
240+ if (auto response = IGF.tryGetLocalTypeMetadata (elementArchetype, request))
241+ return response.getMetadata ();
242+
243+ llvm::Value *metadata = loadMetadataAtIndex (IGF, patternPack, index);
237244
238245 // Bind the metadata pack element to the element archetype.
239246 IGF.setScopedLocalTypeMetadata (elementArchetype,
@@ -242,15 +249,10 @@ static llvm::Value *bindMetadataAtIndex(IRGenFunction &IGF,
242249 return metadata;
243250}
244251
245- static llvm::Value *bindWitnessTableAtIndex (IRGenFunction &IGF,
246- CanType elementArchetype,
247- ProtocolConformanceRef conf,
252+ // / Use this to index into packs to correctly handle on-heap packs.
253+ static llvm::Value *loadWitnessTableAtIndex (IRGenFunction &IGF,
248254 llvm::Value *wtablePack,
249255 llvm::Value *index) {
250- auto key = LocalTypeDataKind::forProtocolWitnessTable (conf);
251- if (auto *wtable = IGF.tryGetLocalTypeData (elementArchetype, key))
252- return wtable;
253-
254256 // If the pack is on the heap, the LSB is set, so mask it off.
255257 wtablePack =
256258 IGF.Builder .CreatePtrToInt (wtablePack, IGF.IGM .SizeTy );
@@ -267,7 +269,19 @@ static llvm::Value *bindWitnessTableAtIndex(IRGenFunction &IGF,
267269 IGF.Builder .CreateInBoundsGEP (patternPackAddress.getElementType (),
268270 patternPackAddress.getAddress (), index),
269271 patternPackAddress.getElementType (), patternPackAddress.getAlignment ());
270- auto *wtable = IGF.Builder .CreateLoad (fromPtr);
272+ return IGF.Builder .CreateLoad (fromPtr);
273+ }
274+
275+ static llvm::Value *bindWitnessTableAtIndex (IRGenFunction &IGF,
276+ CanType elementArchetype,
277+ ProtocolConformanceRef conf,
278+ llvm::Value *wtablePack,
279+ llvm::Value *index) {
280+ auto key = LocalTypeDataKind::forProtocolWitnessTable (conf);
281+ if (auto *wtable = IGF.tryGetLocalTypeData (elementArchetype, key))
282+ return wtable;
283+
284+ auto *wtable = loadWitnessTableAtIndex (IGF, wtablePack, index);
271285
272286 // Bind the witness table pack element to the element archetype.
273287 IGF.setScopedLocalTypeData (elementArchetype, key, wtable);
@@ -658,18 +672,10 @@ llvm::Value *irgen::emitTypeMetadataPackElementRef(
658672 if (materializedMetadataPack &&
659673 llvm::all_of (materializedWtablePacks,
660674 [](auto *wtablePack) { return wtablePack; })) {
661- auto *gep = IGF.Builder .CreateInBoundsGEP (
662- IGF.IGM .TypeMetadataPtrTy , materializedMetadataPack.getMetadata (),
663- index);
664- auto addr =
665- Address (gep, IGF.IGM .TypeMetadataPtrTy , IGF.IGM .getPointerAlignment ());
666- auto *metadata = IGF.Builder .CreateLoad (addr);
675+ auto *metadataPack = materializedMetadataPack.getMetadata ();
676+ auto *metadata = loadMetadataAtIndex (IGF, metadataPack, index);
667677 for (auto *wtablePack : materializedWtablePacks) {
668- auto *gep = IGF.Builder .CreateInBoundsGEP (IGF.IGM .WitnessTablePtrTy ,
669- wtablePack, index);
670- auto addr = Address (gep, IGF.IGM .WitnessTablePtrTy ,
671- IGF.IGM .getPointerAlignment ());
672- auto *wtable = IGF.Builder .CreateLoad (addr);
678+ auto *wtable = loadWitnessTableAtIndex (IGF, wtablePack, index);
673679 wtables.push_back (wtable);
674680 }
675681 return metadata;
0 commit comments