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