@@ -209,14 +209,10 @@ static Address emitFixedSizeMetadataPackRef(IRGenFunction &IGF,
209
209
return pack;
210
210
}
211
211
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 ,
214
214
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) {
220
216
// If the pack is on the heap, the LSB is set, so mask it off.
221
217
patternPack =
222
218
IGF.Builder .CreatePtrToInt (patternPack, IGF.IGM .SizeTy );
@@ -233,7 +229,18 @@ static llvm::Value *bindMetadataAtIndex(IRGenFunction &IGF,
233
229
IGF.Builder .CreateInBoundsGEP (patternPackAddress.getElementType (),
234
230
patternPackAddress.getAddress (), index),
235
231
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);
237
244
238
245
// Bind the metadata pack element to the element archetype.
239
246
IGF.setScopedLocalTypeMetadata (elementArchetype,
@@ -242,15 +249,10 @@ static llvm::Value *bindMetadataAtIndex(IRGenFunction &IGF,
242
249
return metadata;
243
250
}
244
251
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,
248
254
llvm::Value *wtablePack,
249
255
llvm::Value *index) {
250
- auto key = LocalTypeDataKind::forProtocolWitnessTable (conf);
251
- if (auto *wtable = IGF.tryGetLocalTypeData (elementArchetype, key))
252
- return wtable;
253
-
254
256
// If the pack is on the heap, the LSB is set, so mask it off.
255
257
wtablePack =
256
258
IGF.Builder .CreatePtrToInt (wtablePack, IGF.IGM .SizeTy );
@@ -267,7 +269,19 @@ static llvm::Value *bindWitnessTableAtIndex(IRGenFunction &IGF,
267
269
IGF.Builder .CreateInBoundsGEP (patternPackAddress.getElementType (),
268
270
patternPackAddress.getAddress (), index),
269
271
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);
271
285
272
286
// Bind the witness table pack element to the element archetype.
273
287
IGF.setScopedLocalTypeData (elementArchetype, key, wtable);
@@ -658,18 +672,10 @@ llvm::Value *irgen::emitTypeMetadataPackElementRef(
658
672
if (materializedMetadataPack &&
659
673
llvm::all_of (materializedWtablePacks,
660
674
[](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);
667
677
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);
673
679
wtables.push_back (wtable);
674
680
}
675
681
return metadata;
0 commit comments