Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit b65d71b

Browse files
committed
[Async CC] Added conveniences to retrieve argumments.
Because all async functions have the same signature, namely void(%swift.task*, %swift.executor*, %swift.context*) it is always possible to provide access to the three argument values (the current task, current executor, and current context) within an IRGenFunction which is async. Here, that is provided in the form of IRGenFunction::getAsyncExecutor,IRGenFunction::getAsyncContext, and IRGenFunction::getAsyncTask.
1 parent b74052c commit b65d71b

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

lib/IRGen/GenCall.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,27 @@ static llvm::Value *getAsyncTask(IRGenFunction &IGF) {
273273
return llvm::Constant::getNullValue(IGF.IGM.SwiftTaskPtrTy);
274274
}
275275

276+
llvm::Value *IRGenFunction::getAsyncTask() {
277+
assert(isAsync());
278+
auto *value = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Task);
279+
assert(value->getType() == IGM.SwiftTaskPtrTy);
280+
return value;
281+
}
282+
283+
llvm::Value *IRGenFunction::getAsyncExecutor() {
284+
assert(isAsync());
285+
auto *value = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Executor);
286+
assert(value->getType() == IGM.SwiftExecutorPtrTy);
287+
return value;
288+
}
289+
290+
llvm::Value *IRGenFunction::getAsyncContext() {
291+
assert(isAsync());
292+
auto *value = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Context);
293+
assert(value->getType() == IGM.SwiftContextPtrTy);
294+
return value;
295+
}
296+
276297
llvm::Type *ExplosionSchema::getScalarResultType(IRGenModule &IGM) const {
277298
if (size() == 0) {
278299
return IGM.VoidTy;

lib/IRGen/GenCall.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ namespace irgen {
340340
CanSILFunctionType coroutineType,
341341
Explosion &yieldedValues);
342342

343+
enum class AsyncFunctionArgumentIndex : unsigned {
344+
Task = 0,
345+
Executor = 1,
346+
Context = 2,
347+
};
343348
} // end namespace irgen
344349
} // end namespace swift
345350

lib/IRGen/IRGenFunction.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class IRGenFunction {
8383
OptimizationMode Mode = OptimizationMode::NotSet,
8484
const SILDebugScope *DbgScope = nullptr,
8585
Optional<SILLocation> DbgLoc = None);
86-
~IRGenFunction();
86+
virtual ~IRGenFunction();
8787

8888
void unimplemented(SourceLoc Loc, StringRef Message);
8989

@@ -127,7 +127,11 @@ class IRGenFunction {
127127
assert(handle != nullptr && "setting a null handle");
128128
CoroutineHandle = handle;
129129
}
130-
130+
131+
virtual llvm::Value *getAsyncTask();
132+
virtual llvm::Value *getAsyncExecutor();
133+
virtual llvm::Value *getAsyncContext();
134+
131135
private:
132136
void emitPrologue();
133137
void emitEpilogue();

lib/IRGen/IRGenSIL.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,43 @@ class IRGenSILFunction :
852852
}
853853
}
854854
}
855-
855+
856+
llvm::Value *getAsyncTask() override {
857+
// FIXME: (1) Remove this override, (2) mark the IRGenFunction::getAsyncTask
858+
// declaration as non-virtual, and (3) mark IRGenFunction's
859+
// destructor non-virtual once Task.runDetached is available.
860+
// rdar://problem/70597390*/
861+
if (CurSILFn->getLoweredFunctionType()->getRepresentation() ==
862+
SILFunctionTypeRepresentation::CFunctionPointer) {
863+
return llvm::Constant::getNullValue(IGM.SwiftTaskPtrTy);
864+
}
865+
return IRGenFunction::getAsyncTask();
866+
}
867+
868+
llvm::Value *getAsyncExecutor() override {
869+
// FIXME: (1) Remove this override, (2) mark the
870+
// IRGenFunction::getAsyncExecutor declaration as non-virtual, and
871+
// (3) mark IRGenFunction's destructor non-virtual once
872+
// Task.runDetached is available. rdar://problem/70597390*/
873+
if (CurSILFn->getLoweredFunctionType()->getRepresentation() ==
874+
SILFunctionTypeRepresentation::CFunctionPointer) {
875+
return llvm::Constant::getNullValue(IGM.SwiftExecutorPtrTy);
876+
}
877+
return IRGenFunction::getAsyncExecutor();
878+
}
879+
880+
llvm::Value *getAsyncContext() override {
881+
// FIXME: (1) Remove this override, (2) mark the
882+
// IRGenFunction::getAsyncContext declaration as non-virtual, and
883+
// (3) mark IRGenFunction's destructor non-virtual once
884+
// Task.runDetached is available. rdar://problem/70597390*/
885+
if (CurSILFn->getLoweredFunctionType()->getRepresentation() ==
886+
SILFunctionTypeRepresentation::CFunctionPointer) {
887+
return llvm::Constant::getNullValue(IGM.SwiftContextPtrTy);
888+
}
889+
return IRGenFunction::getAsyncContext();
890+
}
891+
856892
//===--------------------------------------------------------------------===//
857893
// SIL instruction lowering
858894
//===--------------------------------------------------------------------===//

0 commit comments

Comments
 (0)