1 //===- CoroElide.cpp - Coroutine Frame Allocation Elision Pass ------------===// 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 #include "llvm/Transforms/Coroutines/CoroElide.h" 10 #include "CoroInternal.h" 11 #include "llvm/ADT/DenseMap.h" 12 #include "llvm/ADT/Statistic.h" 13 #include "llvm/Analysis/AliasAnalysis.h" 14 #include "llvm/Analysis/InstructionSimplify.h" 15 #include "llvm/IR/Dominators.h" 16 #include "llvm/IR/InstIterator.h" 17 #include "llvm/InitializePasses.h" 18 #include "llvm/Pass.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include "llvm/Support/FileSystem.h" 21 22 using namespace llvm; 23 24 #define DEBUG_TYPE "coro-elide" 25 26 STATISTIC(NumOfCoroElided, "The # of coroutine get elided."); 27 28 #ifndef NDEBUG 29 static cl::opt<std::string> CoroElideInfoOutputFilename( 30 "coro-elide-info-output-file", cl::value_desc("filename"), 31 cl::desc("File to record the coroutines got elided"), cl::Hidden); 32 #endif 33 34 namespace { 35 // Created on demand if the coro-elide pass has work to do. 36 struct Lowerer : coro::LowererBase { 37 SmallVector<CoroIdInst *, 4> CoroIds; 38 SmallVector<CoroBeginInst *, 1> CoroBegins; 39 SmallVector<CoroAllocInst *, 1> CoroAllocs; 40 SmallVector<CoroSubFnInst *, 4> ResumeAddr; 41 DenseMap<CoroBeginInst *, SmallVector<CoroSubFnInst *, 4>> DestroyAddr; 42 SmallPtrSet<const SwitchInst *, 4> CoroSuspendSwitches; 43 44 Lowerer(Module &M) : LowererBase(M) {} 45 46 void elideHeapAllocations(Function *F, uint64_t FrameSize, Align FrameAlign, 47 AAResults &AA); 48 bool shouldElide(Function *F, DominatorTree &DT) const; 49 void collectPostSplitCoroIds(Function *F); 50 bool processCoroId(CoroIdInst *, AAResults &AA, DominatorTree &DT); 51 bool hasEscapePath(const CoroBeginInst *, 52 const SmallPtrSetImpl<BasicBlock *> &) const; 53 }; 54 } // end anonymous namespace 55 56 // Go through the list of coro.subfn.addr intrinsics and replace them with the 57 // provided constant. 58 static void replaceWithConstant(Constant *Value, 59 SmallVectorImpl<CoroSubFnInst *> &Users) { 60 if (Users.empty()) 61 return; 62 63 // See if we need to bitcast the constant to match the type of the intrinsic 64 // being replaced. Note: All coro.subfn.addr intrinsics return the same type, 65 // so we only need to examine the type of the first one in the list. 66 Type *IntrTy = Users.front()->getType(); 67 Type *ValueTy = Value->getType(); 68 if (ValueTy != IntrTy) { 69 // May need to tweak the function type to match the type expected at the 70 // use site. 71 assert(ValueTy->isPointerTy() && IntrTy->isPointerTy()); 72 Value = ConstantExpr::getBitCast(Value, IntrTy); 73 } 74 75 // Now the value type matches the type of the intrinsic. Replace them all! 76 for (CoroSubFnInst *I : Users) 77 replaceAndRecursivelySimplify(I, Value); 78 } 79 80 // See if any operand of the call instruction references the coroutine frame. 81 static bool operandReferences(CallInst *CI, AllocaInst *Frame, AAResults &AA) { 82 for (Value *Op : CI->operand_values()) 83 if (!AA.isNoAlias(Op, Frame)) 84 return true; 85 return false; 86 } 87 88 // Look for any tail calls referencing the coroutine frame and remove tail 89 // attribute from them, since now coroutine frame resides on the stack and tail 90 // call implies that the function does not references anything on the stack. 91 // However if it's a musttail call, we cannot remove the tailcall attribute. 92 // It's safe to keep it there as the musttail call is for symmetric transfer, 93 // and by that point the frame should have been destroyed and hence not 94 // interfering with operands. 95 static void removeTailCallAttribute(AllocaInst *Frame, AAResults &AA) { 96 Function &F = *Frame->getFunction(); 97 for (Instruction &I : instructions(F)) 98 if (auto *Call = dyn_cast<CallInst>(&I)) 99 if (Call->isTailCall() && operandReferences(Call, Frame, AA) && 100 !Call->isMustTailCall()) 101 Call->setTailCall(false); 102 } 103 104 // Given a resume function @f.resume(%f.frame* %frame), returns the size 105 // and expected alignment of %f.frame type. 106 static std::pair<uint64_t, Align> getFrameLayout(Function *Resume) { 107 // Prefer to pull information from the function attributes. 108 auto Size = Resume->getParamDereferenceableBytes(0); 109 auto Align = Resume->getParamAlign(0); 110 111 // If those aren't given, extract them from the type. 112 if (Size == 0 || !Align) { 113 auto *FrameTy = Resume->arg_begin()->getType()->getPointerElementType(); 114 115 const DataLayout &DL = Resume->getParent()->getDataLayout(); 116 if (!Size) Size = DL.getTypeAllocSize(FrameTy); 117 if (!Align) Align = DL.getABITypeAlign(FrameTy); 118 } 119 120 return std::make_pair(Size, *Align); 121 } 122 123 // Finds first non alloca instruction in the entry block of a function. 124 static Instruction *getFirstNonAllocaInTheEntryBlock(Function *F) { 125 for (Instruction &I : F->getEntryBlock()) 126 if (!isa<AllocaInst>(&I)) 127 return &I; 128 llvm_unreachable("no terminator in the entry block"); 129 } 130 131 #ifndef NDEBUG 132 static std::unique_ptr<raw_fd_ostream> getOrCreateLogFile() { 133 assert(!CoroElideInfoOutputFilename.empty() && 134 "coro-elide-info-output-file shouldn't be empty"); 135 std::error_code EC; 136 auto Result = std::make_unique<raw_fd_ostream>(CoroElideInfoOutputFilename, 137 EC, sys::fs::OF_Append); 138 if (!EC) 139 return Result; 140 llvm::errs() << "Error opening coro-elide-info-output-file '" 141 << CoroElideInfoOutputFilename << " for appending!\n"; 142 return std::make_unique<raw_fd_ostream>(2, false); // stderr. 143 } 144 #endif 145 146 // To elide heap allocations we need to suppress code blocks guarded by 147 // llvm.coro.alloc and llvm.coro.free instructions. 148 void Lowerer::elideHeapAllocations(Function *F, uint64_t FrameSize, 149 Align FrameAlign, AAResults &AA) { 150 LLVMContext &C = F->getContext(); 151 auto *InsertPt = 152 getFirstNonAllocaInTheEntryBlock(CoroIds.front()->getFunction()); 153 154 // Replacing llvm.coro.alloc with false will suppress dynamic 155 // allocation as it is expected for the frontend to generate the code that 156 // looks like: 157 // id = coro.id(...) 158 // mem = coro.alloc(id) ? malloc(coro.size()) : 0; 159 // coro.begin(id, mem) 160 auto *False = ConstantInt::getFalse(C); 161 for (auto *CA : CoroAllocs) { 162 CA->replaceAllUsesWith(False); 163 CA->eraseFromParent(); 164 } 165 166 // FIXME: Design how to transmit alignment information for every alloca that 167 // is spilled into the coroutine frame and recreate the alignment information 168 // here. Possibly we will need to do a mini SROA here and break the coroutine 169 // frame into individual AllocaInst recreating the original alignment. 170 const DataLayout &DL = F->getParent()->getDataLayout(); 171 auto FrameTy = ArrayType::get(Type::getInt8Ty(C), FrameSize); 172 auto *Frame = new AllocaInst(FrameTy, DL.getAllocaAddrSpace(), "", InsertPt); 173 Frame->setAlignment(FrameAlign); 174 auto *FrameVoidPtr = 175 new BitCastInst(Frame, Type::getInt8PtrTy(C), "vFrame", InsertPt); 176 177 for (auto *CB : CoroBegins) { 178 CB->replaceAllUsesWith(FrameVoidPtr); 179 CB->eraseFromParent(); 180 } 181 182 // Since now coroutine frame lives on the stack we need to make sure that 183 // any tail call referencing it, must be made non-tail call. 184 removeTailCallAttribute(Frame, AA); 185 } 186 187 bool Lowerer::hasEscapePath(const CoroBeginInst *CB, 188 const SmallPtrSetImpl<BasicBlock *> &TIs) const { 189 const auto &It = DestroyAddr.find(CB); 190 assert(It != DestroyAddr.end()); 191 192 // Limit the number of blocks we visit. 193 unsigned Limit = 32 * (1 + It->second.size()); 194 195 SmallVector<const BasicBlock *, 32> Worklist; 196 Worklist.push_back(CB->getParent()); 197 198 SmallPtrSet<const BasicBlock *, 32> Visited; 199 // Consider basicblock of coro.destroy as visited one, so that we 200 // skip the path pass through coro.destroy. 201 for (auto *DA : It->second) 202 Visited.insert(DA->getParent()); 203 204 do { 205 const auto *BB = Worklist.pop_back_val(); 206 if (!Visited.insert(BB).second) 207 continue; 208 if (TIs.count(BB)) 209 return true; 210 211 // Conservatively say that there is potentially a path. 212 if (!--Limit) 213 return true; 214 215 auto TI = BB->getTerminator(); 216 // Although the default dest of coro.suspend switches is suspend pointer 217 // which means a escape path to normal terminator, it is reasonable to skip 218 // it since coroutine frame doesn't change outside the coroutine body. 219 if (isa<SwitchInst>(TI) && 220 CoroSuspendSwitches.count(cast<SwitchInst>(TI))) { 221 Worklist.push_back(cast<SwitchInst>(TI)->getSuccessor(1)); 222 Worklist.push_back(cast<SwitchInst>(TI)->getSuccessor(2)); 223 } else 224 Worklist.append(succ_begin(BB), succ_end(BB)); 225 226 } while (!Worklist.empty()); 227 228 // We have exhausted all possible paths and are certain that coro.begin can 229 // not reach to any of terminators. 230 return false; 231 } 232 233 bool Lowerer::shouldElide(Function *F, DominatorTree &DT) const { 234 // If no CoroAllocs, we cannot suppress allocation, so elision is not 235 // possible. 236 if (CoroAllocs.empty()) 237 return false; 238 239 // Check that for every coro.begin there is at least one coro.destroy directly 240 // referencing the SSA value of that coro.begin along each 241 // non-exceptional path. 242 // If the value escaped, then coro.destroy would have been referencing a 243 // memory location storing that value and not the virtual register. 244 245 SmallPtrSet<BasicBlock *, 8> Terminators; 246 // First gather all of the non-exceptional terminators for the function. 247 // Consider the final coro.suspend as the real terminator when the current 248 // function is a coroutine. 249 for (BasicBlock &B : *F) { 250 auto *TI = B.getTerminator(); 251 if (TI->getNumSuccessors() == 0 && !TI->isExceptionalTerminator() && 252 !isa<UnreachableInst>(TI)) 253 Terminators.insert(&B); 254 } 255 256 // Filter out the coro.destroy that lie along exceptional paths. 257 SmallPtrSet<CoroBeginInst *, 8> ReferencedCoroBegins; 258 for (auto &It : DestroyAddr) { 259 // If there is any coro.destroy dominates all of the terminators for the 260 // coro.begin, we could know the corresponding coro.begin wouldn't escape. 261 for (Instruction *DA : It.second) { 262 if (llvm::all_of(Terminators, [&](auto *TI) { 263 return DT.dominates(DA, TI->getTerminator()); 264 })) { 265 ReferencedCoroBegins.insert(It.first); 266 break; 267 } 268 } 269 270 // Whether there is any paths from coro.begin to Terminators which not pass 271 // through any of the coro.destroys. 272 // 273 // hasEscapePath is relatively slow, so we avoid to run it as much as 274 // possible. 275 if (!ReferencedCoroBegins.count(It.first) && 276 !hasEscapePath(It.first, Terminators)) 277 ReferencedCoroBegins.insert(It.first); 278 } 279 280 // If size of the set is the same as total number of coro.begin, that means we 281 // found a coro.free or coro.destroy referencing each coro.begin, so we can 282 // perform heap elision. 283 return ReferencedCoroBegins.size() == CoroBegins.size(); 284 } 285 286 void Lowerer::collectPostSplitCoroIds(Function *F) { 287 CoroIds.clear(); 288 CoroSuspendSwitches.clear(); 289 for (auto &I : instructions(F)) { 290 if (auto *CII = dyn_cast<CoroIdInst>(&I)) 291 if (CII->getInfo().isPostSplit()) 292 // If it is the coroutine itself, don't touch it. 293 if (CII->getCoroutine() != CII->getFunction()) 294 CoroIds.push_back(CII); 295 296 // Consider case like: 297 // %0 = call i8 @llvm.coro.suspend(...) 298 // switch i8 %0, label %suspend [i8 0, label %resume 299 // i8 1, label %cleanup] 300 // and collect the SwitchInsts which are used by escape analysis later. 301 if (auto *CSI = dyn_cast<CoroSuspendInst>(&I)) 302 if (CSI->hasOneUse() && isa<SwitchInst>(CSI->use_begin()->getUser())) { 303 SwitchInst *SWI = cast<SwitchInst>(CSI->use_begin()->getUser()); 304 if (SWI->getNumCases() == 2) 305 CoroSuspendSwitches.insert(SWI); 306 } 307 } 308 } 309 310 bool Lowerer::processCoroId(CoroIdInst *CoroId, AAResults &AA, 311 DominatorTree &DT) { 312 CoroBegins.clear(); 313 CoroAllocs.clear(); 314 ResumeAddr.clear(); 315 DestroyAddr.clear(); 316 317 // Collect all coro.begin and coro.allocs associated with this coro.id. 318 for (User *U : CoroId->users()) { 319 if (auto *CB = dyn_cast<CoroBeginInst>(U)) 320 CoroBegins.push_back(CB); 321 else if (auto *CA = dyn_cast<CoroAllocInst>(U)) 322 CoroAllocs.push_back(CA); 323 } 324 325 // Collect all coro.subfn.addrs associated with coro.begin. 326 // Note, we only devirtualize the calls if their coro.subfn.addr refers to 327 // coro.begin directly. If we run into cases where this check is too 328 // conservative, we can consider relaxing the check. 329 for (CoroBeginInst *CB : CoroBegins) { 330 for (User *U : CB->users()) 331 if (auto *II = dyn_cast<CoroSubFnInst>(U)) 332 switch (II->getIndex()) { 333 case CoroSubFnInst::ResumeIndex: 334 ResumeAddr.push_back(II); 335 break; 336 case CoroSubFnInst::DestroyIndex: 337 DestroyAddr[CB].push_back(II); 338 break; 339 default: 340 llvm_unreachable("unexpected coro.subfn.addr constant"); 341 } 342 } 343 344 // PostSplit coro.id refers to an array of subfunctions in its Info 345 // argument. 346 ConstantArray *Resumers = CoroId->getInfo().Resumers; 347 assert(Resumers && "PostSplit coro.id Info argument must refer to an array" 348 "of coroutine subfunctions"); 349 auto *ResumeAddrConstant = 350 ConstantExpr::getExtractValue(Resumers, CoroSubFnInst::ResumeIndex); 351 352 replaceWithConstant(ResumeAddrConstant, ResumeAddr); 353 354 bool ShouldElide = shouldElide(CoroId->getFunction(), DT); 355 356 auto *DestroyAddrConstant = ConstantExpr::getExtractValue( 357 Resumers, 358 ShouldElide ? CoroSubFnInst::CleanupIndex : CoroSubFnInst::DestroyIndex); 359 360 for (auto &It : DestroyAddr) 361 replaceWithConstant(DestroyAddrConstant, It.second); 362 363 if (ShouldElide) { 364 auto FrameSizeAndAlign = getFrameLayout(cast<Function>(ResumeAddrConstant)); 365 elideHeapAllocations(CoroId->getFunction(), FrameSizeAndAlign.first, 366 FrameSizeAndAlign.second, AA); 367 coro::replaceCoroFree(CoroId, /*Elide=*/true); 368 NumOfCoroElided++; 369 #ifndef NDEBUG 370 if (!CoroElideInfoOutputFilename.empty()) 371 *getOrCreateLogFile() 372 << "Elide " << CoroId->getCoroutine()->getName() << " in " 373 << CoroId->getFunction()->getName() << "\n"; 374 #endif 375 } 376 377 return true; 378 } 379 380 // See if there are any coro.subfn.addr instructions referring to coro.devirt 381 // trigger, if so, replace them with a direct call to devirt trigger function. 382 static bool replaceDevirtTrigger(Function &F) { 383 SmallVector<CoroSubFnInst *, 1> DevirtAddr; 384 for (auto &I : instructions(F)) 385 if (auto *SubFn = dyn_cast<CoroSubFnInst>(&I)) 386 if (SubFn->getIndex() == CoroSubFnInst::RestartTrigger) 387 DevirtAddr.push_back(SubFn); 388 389 if (DevirtAddr.empty()) 390 return false; 391 392 Module &M = *F.getParent(); 393 Function *DevirtFn = M.getFunction(CORO_DEVIRT_TRIGGER_FN); 394 assert(DevirtFn && "coro.devirt.fn not found"); 395 replaceWithConstant(DevirtFn, DevirtAddr); 396 397 return true; 398 } 399 400 static bool declaresCoroElideIntrinsics(Module &M) { 401 return coro::declaresIntrinsics(M, {"llvm.coro.id", "llvm.coro.id.async"}); 402 } 403 404 PreservedAnalyses CoroElidePass::run(Function &F, FunctionAnalysisManager &AM) { 405 auto &M = *F.getParent(); 406 if (!declaresCoroElideIntrinsics(M)) 407 return PreservedAnalyses::all(); 408 409 Lowerer L(M); 410 L.CoroIds.clear(); 411 L.collectPostSplitCoroIds(&F); 412 // If we did not find any coro.id, there is nothing to do. 413 if (L.CoroIds.empty()) 414 return PreservedAnalyses::all(); 415 416 AAResults &AA = AM.getResult<AAManager>(F); 417 DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F); 418 419 bool Changed = false; 420 for (auto *CII : L.CoroIds) 421 Changed |= L.processCoroId(CII, AA, DT); 422 423 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); 424 } 425 426 namespace { 427 struct CoroElideLegacy : FunctionPass { 428 static char ID; 429 CoroElideLegacy() : FunctionPass(ID) { 430 initializeCoroElideLegacyPass(*PassRegistry::getPassRegistry()); 431 } 432 433 std::unique_ptr<Lowerer> L; 434 435 bool doInitialization(Module &M) override { 436 if (declaresCoroElideIntrinsics(M)) 437 L = std::make_unique<Lowerer>(M); 438 return false; 439 } 440 441 bool runOnFunction(Function &F) override { 442 if (!L) 443 return false; 444 445 bool Changed = false; 446 447 if (F.hasFnAttribute(CORO_PRESPLIT_ATTR)) 448 Changed = replaceDevirtTrigger(F); 449 450 L->CoroIds.clear(); 451 L->collectPostSplitCoroIds(&F); 452 // If we did not find any coro.id, there is nothing to do. 453 if (L->CoroIds.empty()) 454 return Changed; 455 456 AAResults &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); 457 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 458 459 for (auto *CII : L->CoroIds) 460 Changed |= L->processCoroId(CII, AA, DT); 461 462 return Changed; 463 } 464 void getAnalysisUsage(AnalysisUsage &AU) const override { 465 AU.addRequired<AAResultsWrapperPass>(); 466 AU.addRequired<DominatorTreeWrapperPass>(); 467 } 468 StringRef getPassName() const override { return "Coroutine Elision"; } 469 }; 470 } 471 472 char CoroElideLegacy::ID = 0; 473 INITIALIZE_PASS_BEGIN( 474 CoroElideLegacy, "coro-elide", 475 "Coroutine frame allocation elision and indirect calls replacement", false, 476 false) 477 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 478 INITIALIZE_PASS_END( 479 CoroElideLegacy, "coro-elide", 480 "Coroutine frame allocation elision and indirect calls replacement", false, 481 false) 482 483 Pass *llvm::createCoroElideLegacyPass() { return new CoroElideLegacy(); } 484