|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 | 13 | #include "swift/SILOptimizer/Utils/InstOptUtils.h"
|
| 14 | +#include "swift/AST/CanTypeVisitor.h" |
14 | 15 | #include "swift/AST/GenericSignature.h"
|
15 | 16 | #include "swift/AST/SemanticAttrs.h"
|
16 | 17 | #include "swift/AST/SubstitutionMap.h"
|
@@ -736,12 +737,133 @@ swift::castValueToABICompatibleType(SILBuilder *builder, SILLocation loc,
|
736 | 737 | false};
|
737 | 738 | }
|
738 | 739 | }
|
| 740 | + NominalTypeDecl *srcNominal = srcTy.getNominalOrBoundGenericNominal(); |
| 741 | + NominalTypeDecl *destNominal = destTy.getNominalOrBoundGenericNominal(); |
| 742 | + if (srcNominal && srcNominal == destNominal && |
| 743 | + !layoutIsTypeDependent(srcNominal) && |
| 744 | + srcTy.isObject() && destTy.isObject()) { |
| 745 | + |
| 746 | + // This can be a result from whole-module reasoning of protocol conformances. |
| 747 | + // If a protocol only has a single conformance where the associated type (`ID`) is some |
| 748 | + // concrete type (e.g. `Int`), then the devirtualizer knows that `p.get()` |
| 749 | + // can only return an `Int`: |
| 750 | + // ``` |
| 751 | + // public struct X2<ID> { |
| 752 | + // let p: any P2<ID> |
| 753 | + // public func testit(i: ID, x: ID) -> S2<ID> { |
| 754 | + // return p.get(x: x) |
| 755 | + // } |
| 756 | + // } |
| 757 | + // ``` |
| 758 | + // and after devirtualizing the `get` function, its result must be cast from `Int` to `ID`. |
| 759 | + // |
| 760 | + // The `layoutIsTypeDependent` utility is basically only used here to assert that this |
| 761 | + // cast can only happen between layout compatible types. |
| 762 | + return {builder->createUncheckedForwardingCast(loc, value, destTy), false}; |
| 763 | + } |
739 | 764 |
|
740 | 765 | llvm::errs() << "Source type: " << srcTy << "\n";
|
741 | 766 | llvm::errs() << "Destination type: " << destTy << "\n";
|
742 | 767 | llvm_unreachable("Unknown combination of types for casting");
|
743 | 768 | }
|
744 | 769 |
|
| 770 | +namespace { |
| 771 | + class TypeDependentVisitor : public CanTypeVisitor<TypeDependentVisitor, bool> { |
| 772 | + public: |
| 773 | + // If the type isn't actually dependent, we're okay. |
| 774 | + bool visit(CanType type) { |
| 775 | + if (!type->hasArchetype() && !type->hasTypeParameter()) |
| 776 | + return false; |
| 777 | + return CanTypeVisitor::visit(type); |
| 778 | + } |
| 779 | + |
| 780 | + bool visitStructType(CanStructType type) { |
| 781 | + return visitStructDecl(type->getDecl()); |
| 782 | + } |
| 783 | + bool visitBoundGenericStructType(CanBoundGenericStructType type) { |
| 784 | + return visitStructDecl(type->getDecl()); |
| 785 | + } |
| 786 | + bool visitStructDecl(StructDecl *decl) { |
| 787 | + auto rawLayout = decl->getAttrs().getAttribute<RawLayoutAttr>(); |
| 788 | + if (rawLayout) { |
| 789 | + if (auto likeType = rawLayout->getResolvedScalarLikeType(decl)) { |
| 790 | + return visit((*likeType)->getCanonicalType()); |
| 791 | + } else if (auto likeArray = rawLayout->getResolvedArrayLikeTypeAndCount(decl)) { |
| 792 | + return visit(likeArray->first->getCanonicalType()); |
| 793 | + } |
| 794 | + } |
| 795 | + |
| 796 | + for (auto field : decl->getStoredProperties()) { |
| 797 | + if (visit(field->getInterfaceType()->getCanonicalType())) |
| 798 | + return true; |
| 799 | + } |
| 800 | + return false; |
| 801 | + } |
| 802 | + |
| 803 | + bool visitEnumType(CanEnumType type) { |
| 804 | + return visitEnumDecl(type->getDecl()); |
| 805 | + } |
| 806 | + bool visitBoundGenericEnumType(CanBoundGenericEnumType type) { |
| 807 | + return visitEnumDecl(type->getDecl()); |
| 808 | + } |
| 809 | + bool visitEnumDecl(EnumDecl *decl) { |
| 810 | + if (decl->isIndirect()) |
| 811 | + return false; |
| 812 | + |
| 813 | + for (auto elt : decl->getAllElements()) { |
| 814 | + if (!elt->hasAssociatedValues() || elt->isIndirect()) |
| 815 | + continue; |
| 816 | + |
| 817 | + if (visit(elt->getArgumentInterfaceType()->getCanonicalType())) |
| 818 | + return true; |
| 819 | + } |
| 820 | + return false; |
| 821 | + } |
| 822 | + |
| 823 | + bool visitTupleType(CanTupleType type) { |
| 824 | + for (auto eltTy : type.getElementTypes()) { |
| 825 | + if (visit(eltTy->getCanonicalType())) |
| 826 | + return true; |
| 827 | + } |
| 828 | + return false; |
| 829 | + } |
| 830 | + |
| 831 | + // A class reference does not depend on the layout of the class. |
| 832 | + bool visitClassType(CanClassType type) { |
| 833 | + return false; |
| 834 | + } |
| 835 | + bool visitBoundGenericClassType(CanBoundGenericClassType type) { |
| 836 | + return false; |
| 837 | + } |
| 838 | + |
| 839 | + // The same for non-strong references. |
| 840 | + bool visitReferenceStorageType(CanReferenceStorageType type) { |
| 841 | + return false; |
| 842 | + } |
| 843 | + |
| 844 | + // All function types have the same layout. |
| 845 | + bool visitAnyFunctionType(CanAnyFunctionType type) { |
| 846 | + return false; |
| 847 | + } |
| 848 | + |
| 849 | + // The safe default for types we didn't handle above. |
| 850 | + bool visitType(CanType type) { |
| 851 | + return true; |
| 852 | + } |
| 853 | + }; |
| 854 | +} // end anonymous namespace |
| 855 | + |
| 856 | +bool swift::layoutIsTypeDependent(NominalTypeDecl *decl) { |
| 857 | + if (auto *classDecl = dyn_cast<ClassDecl>(decl)) { |
| 858 | + return false; |
| 859 | + } else if (auto *structDecl = dyn_cast<StructDecl>(decl)) { |
| 860 | + return TypeDependentVisitor().visitStructDecl(structDecl); |
| 861 | + } else { |
| 862 | + auto *enumDecl = cast<EnumDecl>(decl); |
| 863 | + return TypeDependentVisitor().visitEnumDecl(enumDecl); |
| 864 | + } |
| 865 | +} |
| 866 | + |
745 | 867 | ProjectBoxInst *swift::getOrCreateProjectBox(AllocBoxInst *abi,
|
746 | 868 | unsigned index) {
|
747 | 869 | SILBasicBlock::iterator iter(abi);
|
|
0 commit comments