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_id: 74 case Intrinsic::coro_id_retcon: 75 case Intrinsic::coro_id_retcon_once: 76 case Intrinsic::coro_id_async: 77 II->replaceAllUsesWith(ConstantTokenNone::get(Context)); 78 break; 79 case Intrinsic::coro_subfn_addr: 80 lowerSubFn(Builder, cast<CoroSubFnInst>(II)); 81 break; 82 case Intrinsic::coro_async_size_replace: 83 auto *Target = cast<ConstantStruct>( 84 cast<GlobalVariable>(II->getArgOperand(0)->stripPointerCasts()) 85 ->getInitializer()); 86 auto *Source = cast<ConstantStruct>( 87 cast<GlobalVariable>(II->getArgOperand(1)->stripPointerCasts()) 88 ->getInitializer()); 89 auto *TargetSize = Target->getOperand(1); 90 auto *SourceSize = Source->getOperand(1); 91 if (TargetSize->isElementWiseEqual(SourceSize)) { 92 break; 93 } 94 auto *TargetRelativeFunOffset = Target->getOperand(0); 95 auto *NewFuncPtrStruct = ConstantStruct::get( 96 Target->getType(), TargetRelativeFunOffset, SourceSize); 97 Target->replaceAllUsesWith(NewFuncPtrStruct); 98 break; 99 } 100 II->eraseFromParent(); 101 Changed = true; 102 } 103 } 104 105 if (Changed) { 106 // After replacement were made we can cleanup the function body a little. 107 simplifyCFG(F); 108 } 109 110 return Changed; 111 } 112 113 static bool declaresCoroCleanupIntrinsics(const Module &M) { 114 return coro::declaresIntrinsics( 115 M, {"llvm.coro.alloc", "llvm.coro.begin", "llvm.coro.subfn.addr", 116 "llvm.coro.free", "llvm.coro.id", "llvm.coro.id.retcon", 117 "llvm.coro.id.retcon.once", "llvm.coro.async.size.replace"}); 118 } 119 120 PreservedAnalyses CoroCleanupPass::run(Function &F, 121 FunctionAnalysisManager &AM) { 122 auto &M = *F.getParent(); 123 if (!declaresCoroCleanupIntrinsics(M) || 124 !Lowerer(M).lowerRemainingCoroIntrinsics(F)) 125 return PreservedAnalyses::all(); 126 127 return PreservedAnalyses::none(); 128 } 129 130 namespace { 131 132 struct CoroCleanupLegacy : FunctionPass { 133 static char ID; // Pass identification, replacement for typeid 134 135 CoroCleanupLegacy() : FunctionPass(ID) { 136 initializeCoroCleanupLegacyPass(*PassRegistry::getPassRegistry()); 137 } 138 139 std::unique_ptr<Lowerer> L; 140 141 // This pass has work to do only if we find intrinsics we are going to lower 142 // in the module. 143 bool doInitialization(Module &M) override { 144 if (declaresCoroCleanupIntrinsics(M)) 145 L = std::make_unique<Lowerer>(M); 146 return false; 147 } 148 149 bool runOnFunction(Function &F) override { 150 if (L) 151 return L->lowerRemainingCoroIntrinsics(F); 152 return false; 153 } 154 void getAnalysisUsage(AnalysisUsage &AU) const override { 155 if (!L) 156 AU.setPreservesAll(); 157 } 158 StringRef getPassName() const override { return "Coroutine Cleanup"; } 159 }; 160 } 161 162 char CoroCleanupLegacy::ID = 0; 163 INITIALIZE_PASS(CoroCleanupLegacy, "coro-cleanup", 164 "Lower all coroutine related intrinsics", false, false) 165 166 Pass *llvm::createCoroCleanupLegacyPass() { return new CoroCleanupLegacy(); } 167