#include "llvm/Transforms/Coroutines/CoroEarly.h"
#include "CoroInternal.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Module.h"
using namespace llvm;
#define DEBUG_TYPE "coro-early"
namespace {
class Lowerer : public coro::LowererBase {
IRBuilder<> Builder;
PointerType *const AnyResumeFnPtrTy;
Constant *NoopCoro = nullptr;
void lowerResumeOrDestroy(CallBase &CB, CoroSubFnInst::ResumeKind);
void lowerCoroPromise(CoroPromiseInst *Intrin);
void lowerCoroDone(IntrinsicInst *II);
void lowerCoroNoop(IntrinsicInst *II);
public:
Lowerer(Module &M)
: LowererBase(M), Builder(Context),
AnyResumeFnPtrTy(FunctionType::get(Type::getVoidTy(Context), Int8Ptr,
false)
->getPointerTo()) {}
void lowerEarlyIntrinsics(Function &F);
};
}
void Lowerer::lowerResumeOrDestroy(CallBase &CB,
CoroSubFnInst::ResumeKind Index) {
Value *ResumeAddr = makeSubFnCall(CB.getArgOperand(0), Index, &CB);
CB.setCalledOperand(ResumeAddr);
CB.setCallingConv(CallingConv::Fast);
}
void Lowerer::lowerCoroPromise(CoroPromiseInst *Intrin) {
Value *Operand = Intrin->getArgOperand(0);
Align Alignment = Intrin->getAlignment();
Type *Int8Ty = Builder.getInt8Ty();
auto *SampleStruct =
StructType::get(Context, {AnyResumeFnPtrTy, AnyResumeFnPtrTy, Int8Ty});
const DataLayout &DL = TheModule.getDataLayout();
int64_t Offset = alignTo(
DL.getStructLayout(SampleStruct)->getElementOffset(2), Alignment);
if (Intrin->isFromPromise())
Offset = -Offset;
Builder.SetInsertPoint(Intrin);
Value *Replacement =
Builder.CreateConstInBoundsGEP1_32(Int8Ty, Operand, Offset);
Intrin->replaceAllUsesWith(Replacement);
Intrin->eraseFromParent();
}
void Lowerer::lowerCoroDone(IntrinsicInst *II) {
Value *Operand = II->getArgOperand(0);
static_assert(coro::Shape::SwitchFieldIndex::Resume == 0,
"resume function not at offset zero");
auto *FrameTy = Int8Ptr;
PointerType *FramePtrTy = FrameTy->getPointerTo();
Builder.SetInsertPoint(II);
auto *BCI = Builder.CreateBitCast(Operand, FramePtrTy);
auto *Load = Builder.CreateLoad(FrameTy, BCI);
auto *Cond = Builder.CreateICmpEQ(Load, NullPtr);
II->replaceAllUsesWith(Cond);
II->eraseFromParent();
}
void Lowerer::lowerCoroNoop(IntrinsicInst *II) {
if (!NoopCoro) {
LLVMContext &C = Builder.getContext();
Module &M = *II->getModule();
StructType *FrameTy = StructType::create(C, "NoopCoro.Frame");
auto *FramePtrTy = FrameTy->getPointerTo();
auto *FnTy = FunctionType::get(Type::getVoidTy(C), FramePtrTy,
false);
auto *FnPtrTy = FnTy->getPointerTo();
FrameTy->setBody({FnPtrTy, FnPtrTy});
Function *NoopFn =
Function::Create(FnTy, GlobalValue::LinkageTypes::PrivateLinkage,
"NoopCoro.ResumeDestroy", &M);
NoopFn->setCallingConv(CallingConv::Fast);
auto *Entry = BasicBlock::Create(C, "entry", NoopFn);
ReturnInst::Create(C, Entry);
Constant* Values[] = {NoopFn, NoopFn};
Constant* NoopCoroConst = ConstantStruct::get(FrameTy, Values);
NoopCoro = new GlobalVariable(M, NoopCoroConst->getType(), true,
GlobalVariable::PrivateLinkage, NoopCoroConst,
"NoopCoro.Frame.Const");
}
Builder.SetInsertPoint(II);
auto *NoopCoroVoidPtr = Builder.CreateBitCast(NoopCoro, Int8Ptr);
II->replaceAllUsesWith(NoopCoroVoidPtr);
II->eraseFromParent();
}
static void setCannotDuplicate(CoroIdInst *CoroId) {
for (User *U : CoroId->users())
if (auto *CB = dyn_cast<CoroBeginInst>(U))
CB->setCannotDuplicate();
}
void Lowerer::lowerEarlyIntrinsics(Function &F) {
CoroIdInst *CoroId = nullptr;
SmallVector<CoroFreeInst *, 4> CoroFrees;
bool HasCoroSuspend = false;
for (Instruction &I : llvm::make_early_inc_range(instructions(F))) {
auto *CB = dyn_cast<CallBase>(&I);
if (!CB)
continue;
switch (CB->getIntrinsicID()) {
default:
continue;
case Intrinsic::coro_free:
CoroFrees.push_back(cast<CoroFreeInst>(&I));
break;
case Intrinsic::coro_suspend:
if (cast<CoroSuspendInst>(&I)->isFinal())
CB->setCannotDuplicate();
HasCoroSuspend = true;
break;
case Intrinsic::coro_end_async:
case Intrinsic::coro_end:
if (cast<AnyCoroEndInst>(&I)->isFallthrough())
CB->setCannotDuplicate();
break;
case Intrinsic::coro_noop:
lowerCoroNoop(cast<IntrinsicInst>(&I));
break;
case Intrinsic::coro_id:
if (auto *CII = cast<CoroIdInst>(&I)) {
if (CII->getInfo().isPreSplit()) {
assert(F.isPresplitCoroutine() &&
"The frontend uses Swtich-Resumed ABI should emit "
"\"coroutine.presplit\" attribute for the coroutine.");
setCannotDuplicate(CII);
CII->setCoroutineSelf();
CoroId = cast<CoroIdInst>(&I);
}
}
break;
case Intrinsic::coro_id_retcon:
case Intrinsic::coro_id_retcon_once:
case Intrinsic::coro_id_async:
F.setPresplitCoroutine();
break;
case Intrinsic::coro_resume:
lowerResumeOrDestroy(*CB, CoroSubFnInst::ResumeIndex);
break;
case Intrinsic::coro_destroy:
lowerResumeOrDestroy(*CB, CoroSubFnInst::DestroyIndex);
break;
case Intrinsic::coro_promise:
lowerCoroPromise(cast<CoroPromiseInst>(&I));
break;
case Intrinsic::coro_done:
lowerCoroDone(cast<IntrinsicInst>(&I));
break;
}
}
if (CoroId)
for (CoroFreeInst *CF : CoroFrees)
CF->setArgOperand(0, CoroId);
if (HasCoroSuspend)
for (Argument &A : F.args())
if (A.hasNoAliasAttr())
A.removeAttr(Attribute::NoAlias);
}
static bool declaresCoroEarlyIntrinsics(const Module &M) {
return coro::declaresIntrinsics(
M, {"llvm.coro.id", "llvm.coro.id.retcon", "llvm.coro.id.retcon.once",
"llvm.coro.id.async", "llvm.coro.destroy", "llvm.coro.done",
"llvm.coro.end", "llvm.coro.end.async", "llvm.coro.noop",
"llvm.coro.free", "llvm.coro.promise", "llvm.coro.resume",
"llvm.coro.suspend"});
}
PreservedAnalyses CoroEarlyPass::run(Module &M, ModuleAnalysisManager &) {
if (!declaresCoroEarlyIntrinsics(M))
return PreservedAnalyses::all();
Lowerer L(M);
for (auto &F : M)
L.lowerEarlyIntrinsics(F);
PreservedAnalyses PA;
PA.preserveSet<CFGAnalyses>();
return PA;
}