|
15 | 15 | //===----------------------------------------------------------------------===//
|
16 | 16 |
|
17 | 17 | #include "GenPack.h"
|
| 18 | +#include "GenProto.h" |
18 | 19 | #include "swift/AST/Decl.h"
|
19 | 20 | #include "swift/AST/GenericEnvironment.h"
|
20 | 21 | #include "swift/AST/IRGenOptions.h"
|
| 22 | +#include "swift/AST/PackConformance.h" |
21 | 23 | #include "swift/AST/Types.h"
|
22 | 24 | #include "swift/SIL/SILModule.h"
|
23 | 25 | #include "swift/SIL/SILType.h"
|
@@ -364,6 +366,230 @@ irgen::emitTypeMetadataPackRef(IRGenFunction &IGF, CanPackType packType,
|
364 | 366 | return response;
|
365 | 367 | }
|
366 | 368 |
|
| 369 | +static Address emitFixedSizeWitnessTablePack(IRGenFunction &IGF, |
| 370 | + CanPackType packType, |
| 371 | + PackConformance *packConformance) { |
| 372 | + assert(!packType->containsPackExpansionType()); |
| 373 | + |
| 374 | + unsigned elementCount = packType->getNumElements(); |
| 375 | + auto allocType = |
| 376 | + llvm::ArrayType::get(IGF.IGM.WitnessTablePtrTy, elementCount); |
| 377 | + |
| 378 | + auto pack = IGF.createAlloca(allocType, IGF.IGM.getPointerAlignment()); |
| 379 | + IGF.Builder.CreateLifetimeStart(pack, |
| 380 | + IGF.IGM.getPointerSize() * elementCount); |
| 381 | + |
| 382 | + for (unsigned i : indices(packType->getElementTypes())) { |
| 383 | + Address slot = |
| 384 | + IGF.Builder.CreateStructGEP(pack, i, IGF.IGM.getPointerSize()); |
| 385 | + |
| 386 | + auto conformance = packConformance->getPatternConformances()[i]; |
| 387 | + auto *wtable = |
| 388 | + emitWitnessTableRef(IGF, packType.getElementType(i), |
| 389 | + /*srcMetadataCache=*/nullptr, conformance); |
| 390 | + |
| 391 | + IGF.Builder.CreateStore(wtable, slot); |
| 392 | + } |
| 393 | + |
| 394 | + return pack; |
| 395 | +} |
| 396 | + |
| 397 | +static llvm::Value *emitPackExpansionElementWitnessTable( |
| 398 | + IRGenFunction &IGF, CanPackExpansionType expansionTy, |
| 399 | + ProtocolConformanceRef conformance, llvm::Value *index) { |
| 400 | + auto patternTy = expansionTy.getPatternType(); |
| 401 | + |
| 402 | + // Find all the pack archetypes appearing in the pattern type. |
| 403 | + SmallVector<Type, 2> patternPacks; |
| 404 | + patternTy->getTypeParameterPacks(patternPacks); |
| 405 | + |
| 406 | + // Get the outer generic signature and environment. |
| 407 | + auto *genericEnv = cast<PackArchetypeType>(expansionTy.getCountType()) |
| 408 | + ->getGenericEnvironment(); |
| 409 | + auto subMap = genericEnv->getForwardingSubstitutionMap(); |
| 410 | + |
| 411 | + auto genericSig = genericEnv->getGenericSignature().getCanonicalSignature(); |
| 412 | + |
| 413 | + // Create an opened element signature and environment. |
| 414 | + auto elementSig = IGF.IGM.Context.getOpenedElementSignature( |
| 415 | + genericSig, expansionTy.getCountType()); |
| 416 | + auto *elementEnv = GenericEnvironment::forOpenedElement( |
| 417 | + elementSig, UUID::fromTime(), expansionTy.getCountType(), subMap); |
| 418 | + |
| 419 | + // Open each pack archetype. |
| 420 | + for (auto patternPackType : patternPacks) { |
| 421 | + // Get the witness table for the pack archetype. |
| 422 | + auto patternPackArchetype = |
| 423 | + cast<PackArchetypeType>(patternPackType->getCanonicalType()); |
| 424 | + for (auto *proto : patternPackArchetype->getConformsTo()) { |
| 425 | + auto conf = ProtocolConformanceRef(proto); |
| 426 | + auto patternPack = emitWitnessTableRef( |
| 427 | + IGF, patternPackArchetype, /*srcMetadataCache=*/nullptr, conf); |
| 428 | + |
| 429 | + patternPack = IGF.Builder.CreatePointerCast(patternPack, |
| 430 | + IGF.IGM.WitnessTablePtrPtrTy); |
| 431 | + |
| 432 | + Address patternPackAddress(patternPack, IGF.IGM.WitnessTablePtrTy, |
| 433 | + IGF.IGM.getPointerAlignment()); |
| 434 | + |
| 435 | + // Load the witness table pack element from the current source index. |
| 436 | + Address fromPtr( |
| 437 | + IGF.Builder.CreateInBoundsGEP(patternPackAddress.getElementType(), |
| 438 | + patternPackAddress.getAddress(), index), |
| 439 | + patternPackAddress.getElementType(), |
| 440 | + patternPackAddress.getAlignment()); |
| 441 | + auto *wtable = IGF.Builder.CreateLoad(fromPtr); |
| 442 | + |
| 443 | + // Bind the witness table pack element to the element archetype. |
| 444 | + auto elementArchetype = elementEnv->mapPackTypeIntoElementContext( |
| 445 | + patternPackArchetype->getInterfaceType()); |
| 446 | + |
| 447 | + IGF.setScopedLocalTypeData( |
| 448 | + CanType(elementArchetype), |
| 449 | + LocalTypeDataKind::forProtocolWitnessTable(conf), wtable); |
| 450 | + } |
| 451 | + } |
| 452 | + |
| 453 | + // Replace pack archetypes with element archetypes in the pattern type. |
| 454 | + auto instantiatedPatternTy = |
| 455 | + elementEnv |
| 456 | + ->mapPackTypeIntoElementContext(patternTy->mapTypeOutOfContext()) |
| 457 | + ->getCanonicalType(); |
| 458 | + |
| 459 | + // FIXME: Handle witness table packs for associatedtype's conformances. |
| 460 | + |
| 461 | + // Emit the element witness table. |
| 462 | + auto *wtable = emitWitnessTableRef(IGF, instantiatedPatternTy, |
| 463 | + /*srcMetadataCache=*/nullptr, conformance); |
| 464 | + return wtable; |
| 465 | +} |
| 466 | + |
| 467 | +static void emitExpansionWitnessTablePack(IRGenFunction &IGF, Address pack, |
| 468 | + CanPackExpansionType expansionTy, |
| 469 | + ProtocolConformanceRef conformance, |
| 470 | + llvm::Value *dynamicIndex, |
| 471 | + llvm::Value *dynamicLength) { |
| 472 | + auto *prev = IGF.Builder.GetInsertBlock(); |
| 473 | + auto *check = IGF.createBasicBlock("pack-expansion-check"); |
| 474 | + auto *loop = IGF.createBasicBlock("pack-expansion-loop"); |
| 475 | + auto *rest = IGF.createBasicBlock("pack-expansion-rest"); |
| 476 | + |
| 477 | + IGF.Builder.CreateBr(check); |
| 478 | + IGF.Builder.emitBlock(check); |
| 479 | + |
| 480 | + // An index into the source witness table pack. |
| 481 | + auto *phi = IGF.Builder.CreatePHI(IGF.IGM.SizeTy, 2); |
| 482 | + phi->addIncoming(llvm::ConstantInt::get(IGF.IGM.SizeTy, 0), prev); |
| 483 | + |
| 484 | + // If we reach the end, jump to the continuation block. |
| 485 | + auto *cond = IGF.Builder.CreateICmpULT(phi, dynamicLength); |
| 486 | + IGF.Builder.CreateCondBr(cond, loop, rest); |
| 487 | + |
| 488 | + IGF.Builder.emitBlock(loop); |
| 489 | + |
| 490 | + auto *element = |
| 491 | + emitPackExpansionElementWitnessTable(IGF, expansionTy, conformance, phi); |
| 492 | + |
| 493 | + // Store the element witness table into to the current destination index. |
| 494 | + auto *eltIndex = IGF.Builder.CreateAdd(dynamicIndex, phi); |
| 495 | + Address eltPtr(IGF.Builder.CreateInBoundsGEP(pack.getElementType(), |
| 496 | + pack.getAddress(), eltIndex), |
| 497 | + pack.getElementType(), pack.getAlignment()); |
| 498 | + |
| 499 | + IGF.Builder.CreateStore(element, eltPtr); |
| 500 | + |
| 501 | + // Increment our counter. |
| 502 | + auto *next = |
| 503 | + IGF.Builder.CreateAdd(phi, llvm::ConstantInt::get(IGF.IGM.SizeTy, 1)); |
| 504 | + |
| 505 | + phi->addIncoming(next, loop); |
| 506 | + |
| 507 | + // Repeat the loop. |
| 508 | + IGF.Builder.CreateBr(check); |
| 509 | + |
| 510 | + // Fall through. |
| 511 | + IGF.Builder.emitBlock(rest); |
| 512 | +} |
| 513 | + |
| 514 | +StackAddress irgen::emitWitnessTablePack(IRGenFunction &IGF, |
| 515 | + CanPackType packType, |
| 516 | + PackConformance *packConformance) { |
| 517 | + auto *shape = IGF.emitPackShapeExpression(packType); |
| 518 | + |
| 519 | + if (auto *constantInt = dyn_cast<llvm::ConstantInt>(shape)) { |
| 520 | + assert(packType->getNumElements() == constantInt->getValue()); |
| 521 | + return StackAddress( |
| 522 | + emitFixedSizeWitnessTablePack(IGF, packType, packConformance)); |
| 523 | + } |
| 524 | + |
| 525 | + assert(packType->containsPackExpansionType()); |
| 526 | + auto pack = IGF.emitDynamicAlloca(IGF.IGM.WitnessTablePtrTy, shape, |
| 527 | + IGF.IGM.getPointerAlignment(), |
| 528 | + /*allowTaskAlloc=*/true); |
| 529 | + |
| 530 | + auto index = 0; |
| 531 | + auto visitFn = [&](CanType eltTy, unsigned staticIndex, |
| 532 | + llvm::Value *dynamicIndex, llvm::Value *dynamicLength) { |
| 533 | + if (staticIndex != 0 || dynamicIndex == nullptr) { |
| 534 | + auto *constant = llvm::ConstantInt::get(IGF.IGM.SizeTy, staticIndex); |
| 535 | + accumulateSum(IGF, dynamicIndex, constant); |
| 536 | + } |
| 537 | + |
| 538 | + auto conformance = packConformance->getPatternConformances()[index]; |
| 539 | + if (auto expansionTy = dyn_cast<PackExpansionType>(eltTy)) { |
| 540 | + emitExpansionWitnessTablePack(IGF, pack.getAddress(), expansionTy, |
| 541 | + conformance, dynamicIndex, dynamicLength); |
| 542 | + } else { |
| 543 | + Address eltPtr( |
| 544 | + IGF.Builder.CreateInBoundsGEP(pack.getAddress().getElementType(), |
| 545 | + pack.getAddressPointer(), dynamicIndex), |
| 546 | + pack.getAddress().getElementType(), pack.getAlignment()); |
| 547 | + |
| 548 | + auto *wtable = emitWitnessTableRef( |
| 549 | + IGF, eltTy, /*srcMetadataCache=*/nullptr, conformance); |
| 550 | + IGF.Builder.CreateStore(wtable, eltPtr); |
| 551 | + } |
| 552 | + ++index; |
| 553 | + }; |
| 554 | + |
| 555 | + visitPackExplosion(IGF, packType, visitFn); |
| 556 | + |
| 557 | + return pack; |
| 558 | +} |
| 559 | + |
| 560 | +void irgen::cleanupWitnessTablePack(IRGenFunction &IGF, StackAddress pack, |
| 561 | + Optional<unsigned> elementCount) { |
| 562 | + if (pack.getExtraInfo()) { |
| 563 | + IGF.emitDeallocateDynamicAlloca(pack); |
| 564 | + } else { |
| 565 | + IGF.Builder.CreateLifetimeEnd(pack.getAddress(), |
| 566 | + IGF.IGM.getPointerSize() * (*elementCount)); |
| 567 | + } |
| 568 | +} |
| 569 | + |
| 570 | +llvm::Value *irgen::emitWitnessTablePackRef(IRGenFunction &IGF, |
| 571 | + CanPackType packType, |
| 572 | + PackConformance *conformance) { |
| 573 | + assert(Lowering::TypeConverter::protocolRequiresWitnessTable( |
| 574 | + conformance->getProtocol()) && |
| 575 | + "looking up witness table for protocol that doesn't have one"); |
| 576 | + |
| 577 | + auto localDataKind = |
| 578 | + LocalTypeDataKind::forProtocolWitnessTablePack(conformance); |
| 579 | + |
| 580 | + auto wtable = IGF.tryGetLocalTypeData(packType, localDataKind); |
| 581 | + if (wtable) |
| 582 | + return wtable; |
| 583 | + |
| 584 | + auto pack = emitWitnessTablePack(IGF, packType, conformance); |
| 585 | + |
| 586 | + auto *result = pack.getAddress().getAddress(); |
| 587 | + |
| 588 | + IGF.setScopedLocalTypeData(packType, localDataKind, result); |
| 589 | + |
| 590 | + return result; |
| 591 | +} |
| 592 | + |
367 | 593 | llvm::Value *
|
368 | 594 | irgen::emitTypeMetadataPackElementRef(IRGenFunction &IGF, CanPackType packType,
|
369 | 595 | llvm::Value *index,
|
|
0 commit comments