|
18 | 18 | #define SWIFT_SIL_SILCLONER_H
|
19 | 19 |
|
20 | 20 | #include "swift/AST/ProtocolConformance.h"
|
21 |
| -#include "swift/SIL/SILOpenedArchetypesTracker.h" |
| 21 | +#include "swift/SIL/InstructionUtils.h" |
22 | 22 | #include "swift/SIL/SILBuilder.h"
|
23 | 23 | #include "swift/SIL/SILDebugScope.h"
|
| 24 | +#include "swift/SIL/SILOpenedArchetypesTracker.h" |
24 | 25 | #include "swift/SIL/SILVisitor.h"
|
25 | 26 |
|
26 | 27 | namespace swift {
|
@@ -866,13 +867,71 @@ SILCloner<ImplClass>::visitAllocValueBufferInst(AllocValueBufferInst *Inst) {
|
866 | 867 | template<typename ImplClass>
|
867 | 868 | void
|
868 | 869 | SILCloner<ImplClass>::visitBuiltinInst(BuiltinInst *Inst) {
|
869 |
| - auto Args = getOpValueArray<8>(Inst->getArguments()); |
| 870 | + auto RawArgsStorage = getOpValueArray<8>(Inst->getArguments()); |
870 | 871 | getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
|
871 |
| - recordClonedInstruction( |
872 |
| - Inst, getBuilder().createBuiltin( |
873 |
| - getOpLocation(Inst->getLoc()), Inst->getName(), |
874 |
| - getOpType(Inst->getType()), |
875 |
| - getOpSubstitutionMap(Inst->getSubstitutions()), Args)); |
| 872 | + |
| 873 | + // First check if we have a polymorphic builtin that has indirect |
| 874 | + // parameters. If we do not, just clone the builtin and return. |
| 875 | + // |
| 876 | + // NOTE: We cannot use the *PBSOI::init that takes a |
| 877 | + // BuiltinInst. This is b/c our BuiltinInst is going to be generic |
| 878 | + // and thus we will not try to specialize. Instead we have to infer |
| 879 | + // the types that the builtin in *Op* land would have. |
| 880 | + // |
| 881 | + // *PBSOI == PolymorphicBuiltinSpecializedOverloadInfo. |
| 882 | + auto Kind = Inst->getBuiltinKind(); |
| 883 | + PolymorphicBuiltinSpecializedOverloadInfo Info; |
| 884 | + SmallVector<SILType, 8> RawOperandTypes; |
| 885 | + transform(RawArgsStorage, std::back_inserter(RawOperandTypes), |
| 886 | + [](SILValue v) -> SILType { return v->getType(); }); |
| 887 | + SILType RawResultType = getOpType(Inst->getType()); |
| 888 | + if (!Kind.hasValue() || |
| 889 | + !Info.init(Inst->getModule(), *Kind, RawOperandTypes, RawResultType)) { |
| 890 | + return recordClonedInstruction( |
| 891 | + Inst, |
| 892 | + getBuilder().createBuiltin( |
| 893 | + getOpLocation(Inst->getLoc()), Inst->getName(), RawResultType, |
| 894 | + getOpSubstitutionMap(Inst->getSubstitutions()), RawArgsStorage)); |
| 895 | + } |
| 896 | + |
| 897 | + // Otherwise, see if we can instead of inserting the polymorphic builtin, |
| 898 | + // dispatch it to the relevant static builtin as we go. This will ensure that |
| 899 | + // we specialize the generic calls to these polymorphic builtins before |
| 900 | + // predictable memory access optimizations so we form SSA around this value at |
| 901 | + // -Onone. |
| 902 | + ArrayRef<SILValue> RawArgs(RawArgsStorage); |
| 903 | + SILValue Result; |
| 904 | + SILType ResultType = RawResultType; |
| 905 | + if (Info.hasOutParam) { |
| 906 | + Result = RawArgs.front(); |
| 907 | + ResultType = Result->getType().getObjectType(); |
| 908 | + RawArgs = RawArgs.drop_front(); |
| 909 | + } |
| 910 | + |
| 911 | + SmallVector<SILValue, 8> PromotedArgs; |
| 912 | + for (SILValue v : RawArgs) { |
| 913 | + if (v->getType().isAddress()) { |
| 914 | + PromotedArgs.push_back(getBuilder().emitLoadValueOperation( |
| 915 | + getOpLocation(Inst->getLoc()), v, LoadOwnershipQualifier::Take)); |
| 916 | + continue; |
| 917 | + } |
| 918 | + PromotedArgs.push_back(v); |
| 919 | + } |
| 920 | + |
| 921 | + // If we specialize, our type no longer has any substitutions. |
| 922 | + BuiltinInst *NewInst = getBuilder().createBuiltin( |
| 923 | + getOpLocation(Inst->getLoc()), Info.staticOverloadIdentifier, ResultType, |
| 924 | + {}, // If we specialize, the function no longer has any substitutions. |
| 925 | + PromotedArgs); |
| 926 | + |
| 927 | + if (Result) { |
| 928 | + assert(Result->getType().isAddress()); |
| 929 | + getBuilder().emitStoreValueOperation(getOpLocation(Inst->getLoc()), |
| 930 | + NewInst->getResult(0), Result, |
| 931 | + StoreOwnershipQualifier::Init); |
| 932 | + } |
| 933 | + |
| 934 | + recordClonedInstruction(Inst, NewInst); |
876 | 935 | }
|
877 | 936 |
|
878 | 937 | template<typename ImplClass>
|
|
0 commit comments