Index: lib/Transforms/Coroutines/CoroFrame.cpp =================================================================== --- lib/Transforms/Coroutines/CoroFrame.cpp +++ lib/Transforms/Coroutines/CoroFrame.cpp @@ -19,6 +19,7 @@ #include "CoroInternal.h" #include "llvm/ADT/BitVector.h" +#include "llvm/Analysis/PtrUseVisitor.h" #include "llvm/IR/CFG.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/IRBuilder.h" @@ -500,6 +501,9 @@ } BasicBlock *FramePtrBB = FramePtr->getParent(); + + // Create AllocaSpillBlock that will become the new entry block for resume + // and destroy parts of the coroutine. All the alloca spills go here. Shape.AllocaSpillBlock = FramePtrBB->splitBasicBlock(FramePtr->getNextNode(), "AllocaSpillBB"); Shape.AllocaSpillBlock->splitBasicBlock(&Shape.AllocaSpillBlock->front(), @@ -761,6 +765,36 @@ I->moveBefore(InsertPt); } +// Checks if alloca escapes. +static bool allocaEscapes(Instruction *I) { + auto *AI = dyn_cast(I); + if (!AI) + return false; + + class AllocaEscapeChecker : public PtrUseVisitor { + friend class PtrUseVisitor; + friend class InstVisitor; + + public: + AllocaEscapeChecker(AllocaInst &AI) + : PtrUseVisitor(AI.getModule()->getDataLayout()), + PtrI(visitPtr(AI)) {} + + bool isEscapedOrAborted() const { + return PtrI.isEscaped() || PtrI.isAborted(); + } + + private: + void visitPHINode(PHINode &PN) { enqueueUsers(PN); } + void visitSelectInst(SelectInst &SI) { enqueueUsers(SI); } + + PtrInfo PtrI; + }; + + AllocaEscapeChecker Checker(*AI); + return Checker.isEscapedOrAborted(); +} + // Splits the block at a particular instruction unless it is the first // instruction in the block with a single predecessor. static BasicBlock *splitBlockIfNotFirst(Instruction *I, const Twine &Name) { @@ -803,6 +837,11 @@ for (CoroEndInst *CE : Shape.CoroEnds) splitAround(CE, "CoroEnd"); + // Split the block after CoroBegin so that spill reload code will never try + // to insert a reload before CoroBegin itself. + Shape.CoroBegin->getParent()->splitBasicBlock(Shape.CoroBegin->getNextNode(), + "AfterCoroBegin"); + // Transforms multi-edge PHI Nodes, so that any value feeding into a PHI will // never has its definition separated from the PHI by the suspend point. rewritePHIs(F); @@ -847,7 +886,7 @@ continue; for (User *U : I.users()) - if (Checker.isDefinitionAcrossSuspend(I, U)) { + if (Checker.isDefinitionAcrossSuspend(I, U) || allocaEscapes(&I)) { // We cannot spill a token. if (I.getType()->isTokenTy()) report_fatal_error( Index: test/Transforms/Coroutines/coro-spill-escape.ll =================================================================== --- /dev/null +++ test/Transforms/Coroutines/coro-spill-escape.ll @@ -0,0 +1,51 @@ +; Check that if alloca escapes it is always put into coroutine frame. +; Even if it is not accessed beyound suspend points. +; RUN: opt < %s -coro-split -S | FileCheck %s + +define i8* @escaped.case() "coroutine.presplit"="1" { +entry: + %sneaky = alloca i32 + %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null) + %size = call i32 @llvm.coro.size.i32() + %alloc = call i8* @malloc(i32 %size) + %hdl = call i8* @llvm.coro.begin(token %id, i8* %alloc) + + %hideit = ptrtoint i32* %sneaky to i64 + call void @escape(i64 %hideit) + + %tok = call i8 @llvm.coro.suspend(token none, i1 false) + switch i8 %tok, label %suspend [i8 0, label %resume + i8 1, label %cleanup] +resume: + br label %cleanup + +cleanup: + %mem = call i8* @llvm.coro.free(token %id, i8* %hdl) + call void @free(i8* %mem) + br label %suspend +suspend: + call i1 @llvm.coro.end(i8* %hdl, i1 0) + ret i8* %hdl +} + +; Verify that we spilled alloca due to escape. +; CHECK: %escaped.case.Frame = type { void (%escaped.case.Frame*)*, void (%escaped.case.Frame*)*, i1, i1, i32 } + +; CHECK-LABEL: @escaped.case( +; CHECK: %[[sneaky:.+]] = getelementptr inbounds %escaped.case.Frame, %escaped.case.Frame* %FramePtr, i32 0, i32 4 +; CHECK: %[[hideit:.+]] = ptrtoint i32* %[[sneaky]] to i64 +; CHECK: call void @escape(i64 %[[hideit]]) +; CHECK: ret i8* %hdl + +declare i8* @llvm.coro.free(token, i8*) +declare i32 @llvm.coro.size.i32() +declare i8 @llvm.coro.suspend(token, i1) + +declare token @llvm.coro.id(i32, i8*, i8*, i8*) +declare i1 @llvm.coro.alloc(token) +declare i8* @llvm.coro.begin(token, i8*) +declare i1 @llvm.coro.end(i8*, i1) + +declare noalias i8* @malloc(i32) +declare void @escape(i64) +declare void @free(i8*)