|
| 1 | +//===- CoroSplit.cpp - Converts a coroutine into a state machine ----------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#include "llvm/Transforms/Coroutines/CoroAnnotationElide.h" |
| 12 | + |
| 13 | +#include "llvm/Analysis/LazyCallGraph.h" |
| 14 | +#include "llvm/IR/Analysis.h" |
| 15 | +#include "llvm/IR/IRBuilder.h" |
| 16 | +#include "llvm/IR/InstIterator.h" |
| 17 | +#include "llvm/IR/Instruction.h" |
| 18 | +#include "llvm/IR/Module.h" |
| 19 | + |
| 20 | +#include <cassert> |
| 21 | + |
| 22 | +using namespace llvm; |
| 23 | + |
| 24 | +#define DEBUG_TYPE "coro-annotation-elide" |
| 25 | + |
| 26 | +#define CORO_MUST_ELIDE_ANNOTATION "coro_must_elide" |
| 27 | + |
| 28 | +static Instruction *getFirstNonAllocaInTheEntryBlock(Function *F) { |
| 29 | + for (Instruction &I : F->getEntryBlock()) |
| 30 | + if (!isa<AllocaInst>(&I)) |
| 31 | + return &I; |
| 32 | + llvm_unreachable("no terminator in the entry block"); |
| 33 | +} |
| 34 | + |
| 35 | +static Value *allocateFrameInCaller(Function *Caller, uint64_t FrameSize, |
| 36 | + Align FrameAlign) { |
| 37 | + LLVMContext &C = Caller->getContext(); |
| 38 | + BasicBlock::iterator InsertPt = |
| 39 | + getFirstNonAllocaInTheEntryBlock(Caller)->getIterator(); |
| 40 | + const DataLayout &DL = Caller->getDataLayout(); |
| 41 | + auto FrameTy = ArrayType::get(Type::getInt8Ty(C), FrameSize); |
| 42 | + auto *Frame = new AllocaInst(FrameTy, DL.getAllocaAddrSpace(), "", InsertPt); |
| 43 | + Frame->setAlignment(FrameAlign); |
| 44 | + return new BitCastInst(Frame, PointerType::getUnqual(C), "vFrame", InsertPt); |
| 45 | +} |
| 46 | + |
| 47 | +static void processCall(CallBase *CB, Function *Caller, Function *NewCallee, |
| 48 | + uint64_t FrameSize, Align FrameAlign) { |
| 49 | + auto *FramePtr = allocateFrameInCaller(Caller, FrameSize, FrameAlign); |
| 50 | + CB->setCalledFunction(NewCallee->getFunctionType(), NewCallee); |
| 51 | + auto NewCBInsertPt = CB->getIterator(); |
| 52 | + llvm::CallBase *NewCB = nullptr; |
| 53 | + SmallVector<Value *, 4> NewArgs; |
| 54 | + NewArgs.append(CB->arg_begin(), CB->arg_end()); |
| 55 | + NewArgs.push_back(FramePtr); |
| 56 | + |
| 57 | + // TODO: See CallBase::Create(CallBase*, ...) |
| 58 | + if (auto *CI = dyn_cast<CallInst>(CB)) { |
| 59 | + auto *NewCI = CallInst::Create(NewCallee->getFunctionType(), NewCallee, |
| 60 | + NewArgs, "", NewCBInsertPt); |
| 61 | + NewCI->setTailCallKind(CI->getTailCallKind()); |
| 62 | + NewCB = NewCI; |
| 63 | + } else if (auto *II = dyn_cast<InvokeInst>(CB)) { |
| 64 | + NewCB = InvokeInst::Create(NewCallee->getFunctionType(), NewCallee, |
| 65 | + II->getNormalDest(), II->getUnwindDest(), |
| 66 | + NewArgs, std::nullopt, "", NewCBInsertPt); |
| 67 | + } else { |
| 68 | + llvm_unreachable("CallBase should either be Call or Invoke!"); |
| 69 | + } |
| 70 | + |
| 71 | + NewCB->setCallingConv(CB->getCallingConv()); |
| 72 | + NewCB->setAttributes(CB->getAttributes()); |
| 73 | + NewCB->setDebugLoc(CB->getDebugLoc()); |
| 74 | + std::copy(CB->bundle_op_info_begin(), CB->bundle_op_info_end(), |
| 75 | + NewCB->bundle_op_info_begin()); |
| 76 | + |
| 77 | + CB->replaceAllUsesWith(NewCB); |
| 78 | + CB->eraseFromParent(); |
| 79 | +} |
| 80 | + |
| 81 | +PreservedAnalyses CoroAnnotationElidePass::run(LazyCallGraph::SCC &C, |
| 82 | + CGSCCAnalysisManager &AM, |
| 83 | + LazyCallGraph &CG, |
| 84 | + CGSCCUpdateResult &UR) { |
| 85 | + bool Changed = false; |
| 86 | + |
| 87 | + for (LazyCallGraph::Node &N : C) { |
| 88 | + Function *Callee = &N.getFunction(); |
| 89 | + Function *NewCallee = Callee->getParent()->getFunction( |
| 90 | + (Callee->getName() + ".noalloc").str()); |
| 91 | + if (!NewCallee) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + auto FramePtrArgPosition = NewCallee->arg_size() - 1; |
| 96 | + auto FrameSize = |
| 97 | + NewCallee->getParamDereferenceableBytes(FramePtrArgPosition); |
| 98 | + auto FrameAlign = |
| 99 | + NewCallee->getParamAlign(FramePtrArgPosition).valueOrOne(); |
| 100 | + |
| 101 | + SmallVector<CallBase *, 4> Users; |
| 102 | + for (auto *U : Callee->users()) { |
| 103 | + if (auto *CB = dyn_cast<CallBase>(U)) { |
| 104 | + Users.push_back(CB); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + for (auto *CB : Users) { |
| 109 | + auto *Caller = CB->getFunction(); |
| 110 | + if (Caller && Caller->isPresplitCoroutine() && |
| 111 | + CB->hasFnAttr(llvm::Attribute::CoroMustElide)) { |
| 112 | + processCall(CB, Caller, NewCallee, FrameSize, FrameAlign); |
| 113 | + C.getOuterRefSCC().replaceNodeFunction(N, *NewCallee); |
| 114 | + Changed = true; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); |
| 119 | +} |
0 commit comments