1 //===- CoroCleanup.cpp - Coroutine Cleanup 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/CoroCleanup.h" 10 #include "CoroInternal.h" 11 #include "llvm/IR/IRBuilder.h" 12 #include "llvm/IR/InstIterator.h" 13 #include "llvm/IR/LegacyPassManager.h" 14 #include "llvm/Pass.h" 15 #include "llvm/Transforms/Scalar.h" 16 17 using namespace llvm; 18 19 #define DEBUG_TYPE "coro-cleanup" 20 21 namespace { 22 // Created on demand if CoroCleanup pass has work to do. 23 struct Lowerer : coro::LowererBase { 24 IRBuilder<> Builder; 25 Lowerer(Module &M) : LowererBase(M), Builder(Context) {} 26 bool lowerRemainingCoroIntrinsics(Function &F); 27 }; 28 } 29 30 static void simplifyCFG(Function &F) { 31 llvm::legacy::FunctionPassManager FPM(F.getParent()); 32 FPM.add(createCFGSimplificationPass()); 33 34 FPM.doInitialization(); 35 FPM.run(F); 36 FPM.doFinalization(); 37 } 38 39 static void lowerSubFn(IRBuilder<> &Builder, CoroSubFnInst *SubFn) { 40 Builder.SetInsertPoint(SubFn); 41 Value *FrameRaw = SubFn->getFrame(); 42 int Index = SubFn->getIndex(); 43 44 auto *FrameTy = StructType::get( 45 SubFn->getContext(), {Builder.getInt8PtrTy(), Builder.getInt8PtrTy()}); 46 PointerType *FramePtrTy = FrameTy->getPointerTo(); 47 48 Builder.SetInsertPoint(SubFn); 49 auto *FramePtr = Builder.CreateBitCast(FrameRaw, FramePtrTy); 50 auto *Gep = Builder.CreateConstInBoundsGEP2_32(FrameTy, FramePtr, 0, Index); 51 auto *Load = Builder.CreateLoad(FrameTy->getElementType(Index), Gep); 52 53 SubFn->replaceAllUsesWith(Load); 54 } 55 56 bool Lowerer::lowerRemainingCoroIntrinsics(Function &F) { 57 bool Changed = false; 58 59 for (Instruction &I : llvm::make_early_inc_range(instructions(F))) { 60 if (auto *II = dyn_cast<IntrinsicInst>(&I)) { 61 switch (II->getIntrinsicID()) { 62 default: 63 continue; 64 case Intrinsic::coro_begin: 65 II->replaceAllUsesWith(II->getArgOperand(1)); 66 break; 67 case Intrinsic::coro_free: 68 II->replaceAllUsesWith(II->getArgOperand(1)); 69 break; 70 case Intrinsic::coro_alloc: 71 II->replaceAllUsesWith(ConstantInt::getTrue(Context)); 72 break; 73 case Intrinsic::coro_async_resume: 74 II->replaceAllUsesWith( 75 ConstantPointerNull::get(cast<PointerType>(I.getType()))); 76 break; 77 case Intrinsic::coro_id: 78 case Intrinsic::coro_id_retcon: 79 case Intrinsic::coro_id_retcon_once: 80 case Intrinsic::coro_id_async: 81 II->replaceAllUsesWith(ConstantTokenNone::get(Context)); 82 break; 83 case Intrinsic::coro_subfn_addr: 84 lowerSubFn(Builder, cast<CoroSubFnInst>(II)); 85 break; 86 case Intrinsic::coro_async_size_replace: 87 auto *Target = cast<ConstantStruct>( 88 cast<GlobalVariable>(II->getArgOperand(0)->stripPointerCasts()) 89 ->getInitializer()); 90 auto *Source = cast<ConstantStruct>( 91 cast<GlobalVariable>(II->getArgOperand(1)->stripPointerCasts()) 92 ->getInitializer()); 93 auto *TargetSize = Target->getOperand(1); 94 auto *SourceSize = Source->getOperand(1); 95 if (TargetSize->isElementWiseEqual(SourceSize)) { 96 break; 97 } 98 auto *TargetRelativeFunOffset = Target->getOperand(0); 99 auto *NewFuncPtrStruct = ConstantStruct::get( 100 Target->getType(), TargetRelativeFunOffset, SourceSize); 101 Target->replaceAllUsesWith(NewFuncPtrStruct); 102 break; 103 } 104 II->eraseFromParent(); 105 Changed = true; 106 } 107 } 108 109 if (Changed) { 110 // After replacement were made we can cleanup the function body a little. 111 simplifyCFG(F); 112 } 113 114 return Changed; 115 } 116 117 static bool declaresCoroCleanupIntrinsics(const Module &M) { 118 return coro::declaresIntrinsics( 119 M, {"llvm.coro.alloc", "llvm.coro.begin", "llvm.coro.subfn.addr", 120 "llvm.coro.free", "llvm.coro.id", "llvm.coro.id.retcon", 121 "llvm.coro.id.retcon.once", "llvm.coro.async.size.replace", 122 "llvm.coro.async.resume"}); 123 } 124 125 PreservedAnalyses CoroCleanupPass::run(Function &F, 126 FunctionAnalysisManager &AM) { 127 auto &M = *F.getParent(); 128 if (!declaresCoroCleanupIntrinsics(M) || 129 !Lowerer(M).lowerRemainingCoroIntrinsics(F)) 130 return PreservedAnalyses::all(); 131 132 return PreservedAnalyses::none(); 133 } 134 135 namespace { 136 137 struct CoroCleanupLegacy : FunctionPass { 138 static char ID; // Pass identification, replacement for typeid 139 140 CoroCleanupLegacy() : FunctionPass(ID) { 141 initializeCoroCleanupLegacyPass(*PassRegistry::getPassRegistry()); 142 } 143 144 std::unique_ptr<Lowerer> L; 145 146 // This pass has work to do only if we find intrinsics we are going to lower 147 // in the module. 148 bool doInitialization(Module &M) override { 149 if (declaresCoroCleanupIntrinsics(M)) 150 L = std::make_unique<Lowerer>(M); 151 return false; 152 } 153 154 bool runOnFunction(Function &F) override { 155 if (L) 156 return L->lowerRemainingCoroIntrinsics(F); 157 return false; 158 } 159 void getAnalysisUsage(AnalysisUsage &AU) const override { 160 if (!L) 161 AU.setPreservesAll(); 162 } 163 StringRef getPassName() const override { return "Coroutine Cleanup"; } 164 }; 165 } 166 167 char CoroCleanupLegacy::ID = 0; 168 INITIALIZE_PASS(CoroCleanupLegacy, "coro-cleanup", 169 "Lower all coroutine related intrinsics", false, false) 170 171 Pass *llvm::createCoroCleanupLegacyPass() { return new CoroCleanupLegacy(); } 172