|
19 | 19 | #include "RValue.h"
|
20 | 20 | #include "SILGenFunctionBuilder.h"
|
21 | 21 | #include "Scope.h"
|
| 22 | +#include "swift/ABI/MetadataValues.h" |
22 | 23 | #include "swift/AST/ClangModuleLoader.h"
|
23 | 24 | #include "swift/AST/DiagnosticsSIL.h"
|
24 | 25 | #include "swift/AST/FileUnit.h"
|
@@ -609,14 +610,26 @@ void SILGenFunction::emitClosure(AbstractClosureExpr *ace) {
|
609 | 610 | emitEpilog(ace);
|
610 | 611 | }
|
611 | 612 |
|
| 613 | +ManagedValue emitBuiltinCreateAsyncTask(SILGenFunction &SGF, SILLocation loc, |
| 614 | + SubstitutionMap subs, |
| 615 | + ArrayRef<ManagedValue> args, |
| 616 | + SGFContext C); |
| 617 | + |
612 | 618 | void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
|
613 | 619 | // Create the argc and argv arguments.
|
614 | 620 | auto entry = B.getInsertionBB();
|
615 | 621 | auto paramTypeIter = F.getConventions()
|
616 | 622 | .getParameterSILTypes(getTypeExpansionContext())
|
617 | 623 | .begin();
|
618 |
| - SILValue argc = entry->createFunctionArgument(*paramTypeIter); |
619 |
| - SILValue argv = entry->createFunctionArgument(*std::next(paramTypeIter)); |
| 624 | + |
| 625 | + SILValue argc; |
| 626 | + SILValue argv; |
| 627 | + const bool isAsyncFunc = |
| 628 | + isa<FuncDecl>(mainDecl) && static_cast<FuncDecl *>(mainDecl)->hasAsync(); |
| 629 | + if (!isAsyncFunc) { |
| 630 | + argc = entry->createFunctionArgument(*paramTypeIter); |
| 631 | + argv = entry->createFunctionArgument(*std::next(paramTypeIter)); |
| 632 | + } |
620 | 633 |
|
621 | 634 | switch (mainDecl->getArtificialMainKind()) {
|
622 | 635 | case ArtificialMainKind::UIApplicationMain: {
|
@@ -823,13 +836,32 @@ void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
|
823 | 836 | auto builtinInt32Type = SILType::getBuiltinIntegerType(32, getASTContext());
|
824 | 837 |
|
825 | 838 | auto *exitBlock = createBasicBlock();
|
826 |
| - B.setInsertionPoint(exitBlock); |
827 | 839 | SILValue exitCode =
|
828 | 840 | exitBlock->createPhiArgument(builtinInt32Type, OwnershipKind::None);
|
829 |
| - auto returnType = F.getConventions().getSingleSILResultType(B.getTypeExpansionContext()); |
830 |
| - if (exitCode->getType() != returnType) |
831 |
| - exitCode = B.createStruct(moduleLoc, returnType, exitCode); |
832 |
| - B.createReturn(moduleLoc, exitCode); |
| 841 | + B.setInsertionPoint(exitBlock); |
| 842 | + |
| 843 | + if (!mainFunc->hasAsync()) { |
| 844 | + auto returnType = F.getConventions().getSingleSILResultType( |
| 845 | + B.getTypeExpansionContext()); |
| 846 | + if (exitCode->getType() != returnType) |
| 847 | + exitCode = B.createStruct(moduleLoc, returnType, exitCode); |
| 848 | + B.createReturn(moduleLoc, exitCode); |
| 849 | + } else { |
| 850 | + FuncDecl *exitFuncDecl = SGM.getExit(); |
| 851 | + assert(exitFuncDecl && "Failed to find exit function declaration"); |
| 852 | + SILFunction *exitSILFunc = SGM.getFunction( |
| 853 | + SILDeclRef(exitFuncDecl, SILDeclRef::Kind::Func, /*isForeign*/ true), |
| 854 | + NotForDefinition); |
| 855 | + |
| 856 | + SILFunctionType &funcType = |
| 857 | + *exitSILFunc->getLoweredType().getAs<SILFunctionType>(); |
| 858 | + SILType retType = SILType::getPrimitiveObjectType( |
| 859 | + funcType.getParameters().front().getInterfaceType()); |
| 860 | + exitCode = B.createStruct(moduleLoc, retType, exitCode); |
| 861 | + SILValue exitCall = B.createFunctionRef(moduleLoc, exitSILFunc); |
| 862 | + B.createApply(moduleLoc, exitCall, {}, {exitCode}); |
| 863 | + B.createUnreachable(moduleLoc); |
| 864 | + } |
833 | 865 |
|
834 | 866 | if (mainFunc->hasThrows()) {
|
835 | 867 | auto *successBlock = createBasicBlock();
|
@@ -867,6 +899,107 @@ void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
|
867 | 899 | }
|
868 | 900 | }
|
869 | 901 |
|
| 902 | +void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) { |
| 903 | + auto moduleLoc = RegularLocation::getModuleLocation(); |
| 904 | + auto *entryBlock = B.getInsertionBB(); |
| 905 | + auto paramTypeIter = F.getConventions() |
| 906 | + .getParameterSILTypes(getTypeExpansionContext()) |
| 907 | + .begin(); |
| 908 | + |
| 909 | + entryBlock->createFunctionArgument(*paramTypeIter); // argc |
| 910 | + entryBlock->createFunctionArgument(*std::next(paramTypeIter)); // argv |
| 911 | + |
| 912 | + // Lookup necessary functions |
| 913 | + swift::ASTContext &ctx = entryPoint.getDecl()->getASTContext(); |
| 914 | + |
| 915 | + B.setInsertionPoint(entryBlock); |
| 916 | + |
| 917 | + /// Generates a reinterpret_cast for converting |
| 918 | + /// Builtin.Job -> UnownedJob |
| 919 | + /// Builtin.Executor -> UnownedSerialExecutor |
| 920 | + /// These are used by _swiftJobRun, which, ABI-wise, could take |
| 921 | + /// Builtin.Job or Builtin.Executor, but doesn't. |
| 922 | + auto createExplodyCastForCall = |
| 923 | + [this, &moduleLoc](SILValue originalValue, FuncDecl *jobRunFuncDecl, |
| 924 | + uint32_t paramIndex) -> SILValue { |
| 925 | + // The type coming from the _swiftJobRun function |
| 926 | + Type apiType = jobRunFuncDecl->getParameters()->get(paramIndex)->getType(); |
| 927 | + SILType apiSILType = |
| 928 | + SILType::getPrimitiveObjectType(apiType->getCanonicalType()); |
| 929 | + // If the types are the same, we don't need to do anything! |
| 930 | + if (apiSILType == originalValue->getType()) |
| 931 | + return originalValue; |
| 932 | + return this->B.createUncheckedReinterpretCast(moduleLoc, originalValue, |
| 933 | + apiSILType); |
| 934 | + }; |
| 935 | + |
| 936 | + // Call CreateAsyncTask |
| 937 | + FuncDecl *builtinDecl = cast<FuncDecl>(getBuiltinValueDecl( |
| 938 | + getASTContext(), |
| 939 | + ctx.getIdentifier(getBuiltinName(BuiltinValueKind::CreateAsyncTask)))); |
| 940 | + |
| 941 | + auto subs = SubstitutionMap::get(builtinDecl->getGenericSignature(), |
| 942 | + {TupleType::getEmpty(ctx)}, |
| 943 | + ArrayRef<ProtocolConformanceRef>{}); |
| 944 | + |
| 945 | + SILValue mainFunctionRef = emitGlobalFunctionRef(moduleLoc, entryPoint); |
| 946 | + |
| 947 | + // Emit the CreateAsyncTask builtin |
| 948 | + TaskCreateFlags taskCreationFlagMask(0); |
| 949 | + SILValue taskFlags = |
| 950 | + emitWrapIntegerLiteral(moduleLoc, getLoweredType(ctx.getIntType()), |
| 951 | + taskCreationFlagMask.getOpaqueValue()); |
| 952 | + |
| 953 | + SILValue task = |
| 954 | + emitBuiltinCreateAsyncTask(*this, moduleLoc, subs, |
| 955 | + {ManagedValue::forUnmanaged(taskFlags), |
| 956 | + ManagedValue::forUnmanaged(mainFunctionRef)}, |
| 957 | + {}) |
| 958 | + .forward(*this); |
| 959 | + DestructureTupleInst *structure = B.createDestructureTuple(moduleLoc, task); |
| 960 | + task = structure->getResult(0); |
| 961 | + |
| 962 | + // Get swiftJobRun |
| 963 | + FuncDecl *swiftJobRunFuncDecl = SGM.getSwiftJobRun(); |
| 964 | + SILFunction *swiftJobRunSILFunc = |
| 965 | + SGM.getFunction(SILDeclRef(swiftJobRunFuncDecl, SILDeclRef::Kind::Func), |
| 966 | + NotForDefinition); |
| 967 | + SILValue swiftJobRunFunc = |
| 968 | + B.createFunctionRefFor(moduleLoc, swiftJobRunSILFunc); |
| 969 | + |
| 970 | + // Convert task to job |
| 971 | + SILType JobType = SILType::getPrimitiveObjectType( |
| 972 | + getBuiltinType(ctx, "Job")->getCanonicalType()); |
| 973 | + SILValue jobResult = B.createBuiltin( |
| 974 | + moduleLoc, |
| 975 | + ctx.getIdentifier(getBuiltinName(BuiltinValueKind::ConvertTaskToJob)), |
| 976 | + JobType, {}, {task}); |
| 977 | + jobResult = createExplodyCastForCall(jobResult, swiftJobRunFuncDecl, 0); |
| 978 | + |
| 979 | + // Get main executor |
| 980 | + FuncDecl *getMainExecutorFuncDecl = SGM.getGetMainExecutor(); |
| 981 | + SILFunction *getMainExeutorSILFunc = SGM.getFunction( |
| 982 | + SILDeclRef(getMainExecutorFuncDecl, SILDeclRef::Kind::Func), |
| 983 | + NotForDefinition); |
| 984 | + SILValue getMainExeutorFunc = |
| 985 | + B.createFunctionRefFor(moduleLoc, getMainExeutorSILFunc); |
| 986 | + SILValue mainExecutor = B.createApply(moduleLoc, getMainExeutorFunc, {}, {}); |
| 987 | + mainExecutor = createExplodyCastForCall(mainExecutor, swiftJobRunFuncDecl, 1); |
| 988 | + |
| 989 | + // Run first part synchronously |
| 990 | + B.createApply(moduleLoc, swiftJobRunFunc, {}, {jobResult, mainExecutor}); |
| 991 | + |
| 992 | + // Start Main loop! |
| 993 | + FuncDecl *drainQueueFuncDecl = SGM.getAsyncMainDrainQueue(); |
| 994 | + SILFunction *drainQueueSILFunc = SGM.getFunction( |
| 995 | + SILDeclRef(drainQueueFuncDecl, SILDeclRef::Kind::Func), NotForDefinition); |
| 996 | + SILValue drainQueueFunc = |
| 997 | + B.createFunctionRefFor(moduleLoc, drainQueueSILFunc); |
| 998 | + B.createApply(moduleLoc, drainQueueFunc, {}, {}); |
| 999 | + B.createUnreachable(moduleLoc); |
| 1000 | + return; |
| 1001 | +} |
| 1002 | + |
870 | 1003 | void SILGenFunction::emitGeneratorFunction(SILDeclRef function, Expr *value,
|
871 | 1004 | bool EmitProfilerIncrement) {
|
872 | 1005 | auto *dc = function.getDecl()->getInnermostDeclContext();
|
|
0 commit comments