|
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"
|
@@ -563,14 +564,26 @@ void SILGenFunction::emitClosure(AbstractClosureExpr *ace) {
|
563 | 564 | emitEpilog(ace);
|
564 | 565 | }
|
565 | 566 |
|
| 567 | +ManagedValue emitBuiltinCreateAsyncTask(SILGenFunction &SGF, SILLocation loc, |
| 568 | + SubstitutionMap subs, |
| 569 | + ArrayRef<ManagedValue> args, |
| 570 | + SGFContext C); |
| 571 | + |
566 | 572 | void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
|
567 | 573 | // Create the argc and argv arguments.
|
568 | 574 | auto entry = B.getInsertionBB();
|
569 | 575 | auto paramTypeIter = F.getConventions()
|
570 | 576 | .getParameterSILTypes(getTypeExpansionContext())
|
571 | 577 | .begin();
|
572 |
| - SILValue argc = entry->createFunctionArgument(*paramTypeIter); |
573 |
| - SILValue argv = entry->createFunctionArgument(*std::next(paramTypeIter)); |
| 578 | + |
| 579 | + SILValue argc; |
| 580 | + SILValue argv; |
| 581 | + const bool isAsyncFunc = |
| 582 | + isa<FuncDecl>(mainDecl) && static_cast<FuncDecl *>(mainDecl)->hasAsync(); |
| 583 | + if (!isAsyncFunc) { |
| 584 | + argc = entry->createFunctionArgument(*paramTypeIter); |
| 585 | + argv = entry->createFunctionArgument(*std::next(paramTypeIter)); |
| 586 | + } |
574 | 587 |
|
575 | 588 | switch (mainDecl->getArtificialMainKind()) {
|
576 | 589 | case ArtificialMainKind::UIApplicationMain: {
|
@@ -777,13 +790,32 @@ void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
|
777 | 790 | auto builtinInt32Type = SILType::getBuiltinIntegerType(32, getASTContext());
|
778 | 791 |
|
779 | 792 | auto *exitBlock = createBasicBlock();
|
780 |
| - B.setInsertionPoint(exitBlock); |
781 | 793 | SILValue exitCode =
|
782 | 794 | exitBlock->createPhiArgument(builtinInt32Type, OwnershipKind::None);
|
783 |
| - auto returnType = F.getConventions().getSingleSILResultType(B.getTypeExpansionContext()); |
784 |
| - if (exitCode->getType() != returnType) |
785 |
| - exitCode = B.createStruct(moduleLoc, returnType, exitCode); |
786 |
| - B.createReturn(moduleLoc, exitCode); |
| 795 | + B.setInsertionPoint(exitBlock); |
| 796 | + |
| 797 | + if (!mainFunc->hasAsync()) { |
| 798 | + auto returnType = F.getConventions().getSingleSILResultType( |
| 799 | + B.getTypeExpansionContext()); |
| 800 | + if (exitCode->getType() != returnType) |
| 801 | + exitCode = B.createStruct(moduleLoc, returnType, exitCode); |
| 802 | + B.createReturn(moduleLoc, exitCode); |
| 803 | + } else { |
| 804 | + FuncDecl *exitFuncDecl = SGM.getExit(); |
| 805 | + assert(exitFuncDecl && "Failed to find exit function declaration"); |
| 806 | + SILFunction *exitSILFunc = SGM.getFunction( |
| 807 | + SILDeclRef(exitFuncDecl, SILDeclRef::Kind::Func, /*isForeign*/ true), |
| 808 | + NotForDefinition); |
| 809 | + |
| 810 | + SILFunctionType &funcType = |
| 811 | + *exitSILFunc->getLoweredType().getAs<SILFunctionType>(); |
| 812 | + SILType retType = SILType::getPrimitiveObjectType( |
| 813 | + funcType.getParameters().front().getInterfaceType()); |
| 814 | + exitCode = B.createStruct(moduleLoc, retType, exitCode); |
| 815 | + SILValue exitCall = B.createFunctionRef(moduleLoc, exitSILFunc); |
| 816 | + B.createApply(moduleLoc, exitCall, {}, {exitCode}); |
| 817 | + B.createUnreachable(moduleLoc); |
| 818 | + } |
787 | 819 |
|
788 | 820 | if (mainFunc->hasThrows()) {
|
789 | 821 | auto *successBlock = createBasicBlock();
|
@@ -821,6 +853,107 @@ void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
|
821 | 853 | }
|
822 | 854 | }
|
823 | 855 |
|
| 856 | +void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) { |
| 857 | + auto moduleLoc = RegularLocation::getModuleLocation(); |
| 858 | + auto *entryBlock = B.getInsertionBB(); |
| 859 | + auto paramTypeIter = F.getConventions() |
| 860 | + .getParameterSILTypes(getTypeExpansionContext()) |
| 861 | + .begin(); |
| 862 | + |
| 863 | + entryBlock->createFunctionArgument(*paramTypeIter); // argc |
| 864 | + entryBlock->createFunctionArgument(*std::next(paramTypeIter)); // argv |
| 865 | + |
| 866 | + // Lookup necessary functions |
| 867 | + swift::ASTContext &ctx = entryPoint.getDecl()->getASTContext(); |
| 868 | + |
| 869 | + B.setInsertionPoint(entryBlock); |
| 870 | + |
| 871 | + /// Generates a reinterpret_cast for converting |
| 872 | + /// Builtin.Job -> UnownedJob |
| 873 | + /// Builtin.Executor -> UnownedSerialExecutor |
| 874 | + /// These are used by _swiftJobRun, which, ABI-wise, could take |
| 875 | + /// Builtin.Job or Builtin.Executor, but doesn't. |
| 876 | + auto createExplodyCastForCall = |
| 877 | + [this, &moduleLoc](SILValue originalValue, FuncDecl *jobRunFuncDecl, |
| 878 | + uint32_t paramIndex) -> SILValue { |
| 879 | + // The type coming from the _swiftJobRun function |
| 880 | + Type apiType = jobRunFuncDecl->getParameters()->get(paramIndex)->getType(); |
| 881 | + SILType apiSILType = |
| 882 | + SILType::getPrimitiveObjectType(apiType->getCanonicalType()); |
| 883 | + // If the types are the same, we don't need to do anything! |
| 884 | + if (apiSILType == originalValue->getType()) |
| 885 | + return originalValue; |
| 886 | + return this->B.createUncheckedReinterpretCast(moduleLoc, originalValue, |
| 887 | + apiSILType); |
| 888 | + }; |
| 889 | + |
| 890 | + // Call CreateAsyncTask |
| 891 | + FuncDecl *builtinDecl = cast<FuncDecl>(getBuiltinValueDecl( |
| 892 | + getASTContext(), |
| 893 | + ctx.getIdentifier(getBuiltinName(BuiltinValueKind::CreateAsyncTask)))); |
| 894 | + |
| 895 | + auto subs = SubstitutionMap::get(builtinDecl->getGenericSignature(), |
| 896 | + {TupleType::getEmpty(ctx)}, |
| 897 | + ArrayRef<ProtocolConformanceRef>{}); |
| 898 | + |
| 899 | + SILValue mainFunctionRef = emitGlobalFunctionRef(moduleLoc, entryPoint); |
| 900 | + |
| 901 | + // Emit the CreateAsyncTask builtin |
| 902 | + TaskCreateFlags taskCreationFlagMask(0); |
| 903 | + SILValue taskFlags = |
| 904 | + emitWrapIntegerLiteral(moduleLoc, getLoweredType(ctx.getIntType()), |
| 905 | + taskCreationFlagMask.getOpaqueValue()); |
| 906 | + |
| 907 | + SILValue task = |
| 908 | + emitBuiltinCreateAsyncTask(*this, moduleLoc, subs, |
| 909 | + {ManagedValue::forUnmanaged(taskFlags), |
| 910 | + ManagedValue::forUnmanaged(mainFunctionRef)}, |
| 911 | + {}) |
| 912 | + .forward(*this); |
| 913 | + DestructureTupleInst *structure = B.createDestructureTuple(moduleLoc, task); |
| 914 | + task = structure->getResult(0); |
| 915 | + |
| 916 | + // Get swiftJobRun |
| 917 | + FuncDecl *swiftJobRunFuncDecl = SGM.getSwiftJobRun(); |
| 918 | + SILFunction *swiftJobRunSILFunc = |
| 919 | + SGM.getFunction(SILDeclRef(swiftJobRunFuncDecl, SILDeclRef::Kind::Func), |
| 920 | + NotForDefinition); |
| 921 | + SILValue swiftJobRunFunc = |
| 922 | + B.createFunctionRefFor(moduleLoc, swiftJobRunSILFunc); |
| 923 | + |
| 924 | + // Convert task to job |
| 925 | + SILType JobType = SILType::getPrimitiveObjectType( |
| 926 | + getBuiltinType(ctx, "Job")->getCanonicalType()); |
| 927 | + SILValue jobResult = B.createBuiltin( |
| 928 | + moduleLoc, |
| 929 | + ctx.getIdentifier(getBuiltinName(BuiltinValueKind::ConvertTaskToJob)), |
| 930 | + JobType, {}, {task}); |
| 931 | + jobResult = createExplodyCastForCall(jobResult, swiftJobRunFuncDecl, 0); |
| 932 | + |
| 933 | + // Get main executor |
| 934 | + FuncDecl *getMainExecutorFuncDecl = SGM.getGetMainExecutor(); |
| 935 | + SILFunction *getMainExeutorSILFunc = SGM.getFunction( |
| 936 | + SILDeclRef(getMainExecutorFuncDecl, SILDeclRef::Kind::Func), |
| 937 | + NotForDefinition); |
| 938 | + SILValue getMainExeutorFunc = |
| 939 | + B.createFunctionRefFor(moduleLoc, getMainExeutorSILFunc); |
| 940 | + SILValue mainExecutor = B.createApply(moduleLoc, getMainExeutorFunc, {}, {}); |
| 941 | + mainExecutor = createExplodyCastForCall(mainExecutor, swiftJobRunFuncDecl, 1); |
| 942 | + |
| 943 | + // Run first part synchronously |
| 944 | + B.createApply(moduleLoc, swiftJobRunFunc, {}, {jobResult, mainExecutor}); |
| 945 | + |
| 946 | + // Start Main loop! |
| 947 | + FuncDecl *drainQueueFuncDecl = SGM.getAsyncMainDrainQueue(); |
| 948 | + SILFunction *drainQueueSILFunc = SGM.getFunction( |
| 949 | + SILDeclRef(drainQueueFuncDecl, SILDeclRef::Kind::Func), NotForDefinition); |
| 950 | + SILValue drainQueueFunc = |
| 951 | + B.createFunctionRefFor(moduleLoc, drainQueueSILFunc); |
| 952 | + B.createApply(moduleLoc, drainQueueFunc, {}, {}); |
| 953 | + B.createUnreachable(moduleLoc); |
| 954 | + return; |
| 955 | +} |
| 956 | + |
824 | 957 | void SILGenFunction::emitGeneratorFunction(SILDeclRef function, Expr *value,
|
825 | 958 | bool EmitProfilerIncrement) {
|
826 | 959 | auto *dc = function.getDecl()->getInnermostDeclContext();
|
|
0 commit comments