Skip to content

[Async CC] Added conveniences to retrieve arguments. #34453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,27 @@ static llvm::Value *getAsyncTask(IRGenFunction &IGF) {
return llvm::Constant::getNullValue(IGF.IGM.SwiftTaskPtrTy);
}

llvm::Value *IRGenFunction::getAsyncTask() {
assert(isAsync());
auto *value = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Task);
assert(value->getType() == IGM.SwiftTaskPtrTy);
return value;
}

llvm::Value *IRGenFunction::getAsyncExecutor() {
assert(isAsync());
auto *value = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Executor);
assert(value->getType() == IGM.SwiftExecutorPtrTy);
return value;
}

llvm::Value *IRGenFunction::getAsyncContext() {
assert(isAsync());
auto *value = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Context);
assert(value->getType() == IGM.SwiftContextPtrTy);
return value;
}

llvm::Type *ExplosionSchema::getScalarResultType(IRGenModule &IGM) const {
if (size() == 0) {
return IGM.VoidTy;
Expand Down
5 changes: 5 additions & 0 deletions lib/IRGen/GenCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ namespace irgen {
CanSILFunctionType coroutineType,
Explosion &yieldedValues);

enum class AsyncFunctionArgumentIndex : unsigned {
Task = 0,
Executor = 1,
Context = 2,
};
} // end namespace irgen
} // end namespace swift

Expand Down
8 changes: 6 additions & 2 deletions lib/IRGen/IRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class IRGenFunction {
OptimizationMode Mode = OptimizationMode::NotSet,
const SILDebugScope *DbgScope = nullptr,
Optional<SILLocation> DbgLoc = None);
~IRGenFunction();
virtual ~IRGenFunction();

void unimplemented(SourceLoc Loc, StringRef Message);

Expand Down Expand Up @@ -127,7 +127,11 @@ class IRGenFunction {
assert(handle != nullptr && "setting a null handle");
CoroutineHandle = handle;
}


virtual llvm::Value *getAsyncTask();
virtual llvm::Value *getAsyncExecutor();
virtual llvm::Value *getAsyncContext();

private:
void emitPrologue();
void emitEpilogue();
Expand Down
40 changes: 38 additions & 2 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,43 @@ class IRGenSILFunction :
}
}
}


llvm::Value *getAsyncTask() override {
// FIXME: (1) Remove this override, (2) mark the IRGenFunction::getAsyncTask
// declaration as non-virtual, and (3) mark IRGenFunction's
// destructor non-virtual once Task.runDetached is available.
// rdar://problem/70597390*/
if (CurSILFn->getLoweredFunctionType()->getRepresentation() ==
SILFunctionTypeRepresentation::CFunctionPointer) {
return llvm::Constant::getNullValue(IGM.SwiftTaskPtrTy);
}
return IRGenFunction::getAsyncTask();
}

llvm::Value *getAsyncExecutor() override {
// FIXME: (1) Remove this override, (2) mark the
// IRGenFunction::getAsyncExecutor declaration as non-virtual, and
// (3) mark IRGenFunction's destructor non-virtual once
// Task.runDetached is available. rdar://problem/70597390*/
if (CurSILFn->getLoweredFunctionType()->getRepresentation() ==
SILFunctionTypeRepresentation::CFunctionPointer) {
return llvm::Constant::getNullValue(IGM.SwiftExecutorPtrTy);
}
return IRGenFunction::getAsyncExecutor();
}

llvm::Value *getAsyncContext() override {
// FIXME: (1) Remove this override, (2) mark the
// IRGenFunction::getAsyncContext declaration as non-virtual, and
// (3) mark IRGenFunction's destructor non-virtual once
// Task.runDetached is available. rdar://problem/70597390*/
if (CurSILFn->getLoweredFunctionType()->getRepresentation() ==
SILFunctionTypeRepresentation::CFunctionPointer) {
return llvm::Constant::getNullValue(IGM.SwiftContextPtrTy);
}
return IRGenFunction::getAsyncContext();
}

//===--------------------------------------------------------------------===//
// SIL instruction lowering
//===--------------------------------------------------------------------===//
Expand Down Expand Up @@ -3179,7 +3215,7 @@ static void emitReturnInst(IRGenSILFunction &IGF,
assert(!IGF.IndirectReturn.isValid() &&
"Formally direct results should stay direct results for async "
"functions");
llvm::Value *context = IGF.CurFn->getArg(2);
llvm::Value *context = IGF.getAsyncContext();
auto layout = getAsyncContextLayout(IGF);

Address dataAddr = layout.emitCastTo(IGF, context);
Expand Down