LLVM 18.0.0git
CoroCleanup.cpp
Go to the documentation of this file.
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
10#include "CoroInternal.h"
11#include "llvm/IR/IRBuilder.h"
13#include "llvm/IR/PassManager.h"
14#include "llvm/IR/Function.h"
16
17using namespace llvm;
18
19#define DEBUG_TYPE "coro-cleanup"
20
21namespace {
22// Created on demand if CoroCleanup pass has work to do.
23struct Lowerer : coro::LowererBase {
25 Lowerer(Module &M) : LowererBase(M), Builder(Context) {}
26 bool lower(Function &F);
27};
28}
29
30static void lowerSubFn(IRBuilder<> &Builder, CoroSubFnInst *SubFn) {
31 Builder.SetInsertPoint(SubFn);
32 Value *FrameRaw = SubFn->getFrame();
33 int Index = SubFn->getIndex();
34
35 auto *FrameTy = StructType::get(
36 SubFn->getContext(), {Builder.getInt8PtrTy(), Builder.getInt8PtrTy()});
37 PointerType *FramePtrTy = FrameTy->getPointerTo();
38
39 Builder.SetInsertPoint(SubFn);
40 auto *FramePtr = Builder.CreateBitCast(FrameRaw, FramePtrTy);
41 auto *Gep = Builder.CreateConstInBoundsGEP2_32(FrameTy, FramePtr, 0, Index);
42 auto *Load = Builder.CreateLoad(FrameTy->getElementType(Index), Gep);
43
44 SubFn->replaceAllUsesWith(Load);
45}
46
47bool Lowerer::lower(Function &F) {
48 bool IsPrivateAndUnprocessed = F.isPresplitCoroutine() && F.hasLocalLinkage();
49 bool Changed = false;
50
52 if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
53 switch (II->getIntrinsicID()) {
54 default:
55 continue;
56 case Intrinsic::coro_begin:
57 II->replaceAllUsesWith(II->getArgOperand(1));
58 break;
59 case Intrinsic::coro_free:
60 II->replaceAllUsesWith(II->getArgOperand(1));
61 break;
62 case Intrinsic::coro_alloc:
63 II->replaceAllUsesWith(ConstantInt::getTrue(Context));
64 break;
65 case Intrinsic::coro_async_resume:
66 II->replaceAllUsesWith(
67 ConstantPointerNull::get(cast<PointerType>(I.getType())));
68 break;
69 case Intrinsic::coro_id:
70 case Intrinsic::coro_id_retcon:
71 case Intrinsic::coro_id_retcon_once:
72 case Intrinsic::coro_id_async:
73 II->replaceAllUsesWith(ConstantTokenNone::get(Context));
74 break;
75 case Intrinsic::coro_subfn_addr:
76 lowerSubFn(Builder, cast<CoroSubFnInst>(II));
77 break;
78 case Intrinsic::coro_end:
79 case Intrinsic::coro_suspend_retcon:
80 if (IsPrivateAndUnprocessed) {
81 II->replaceAllUsesWith(UndefValue::get(II->getType()));
82 } else
83 continue;
84 break;
85 case Intrinsic::coro_async_size_replace:
86 auto *Target = cast<ConstantStruct>(
87 cast<GlobalVariable>(II->getArgOperand(0)->stripPointerCasts())
88 ->getInitializer());
89 auto *Source = cast<ConstantStruct>(
90 cast<GlobalVariable>(II->getArgOperand(1)->stripPointerCasts())
91 ->getInitializer());
92 auto *TargetSize = Target->getOperand(1);
93 auto *SourceSize = Source->getOperand(1);
94 if (TargetSize->isElementWiseEqual(SourceSize)) {
95 break;
96 }
97 auto *TargetRelativeFunOffset = Target->getOperand(0);
98 auto *NewFuncPtrStruct = ConstantStruct::get(
99 Target->getType(), TargetRelativeFunOffset, SourceSize);
100 Target->replaceAllUsesWith(NewFuncPtrStruct);
101 break;
102 }
103 II->eraseFromParent();
104 Changed = true;
105 }
106 }
107
108 return Changed;
109}
110
113 M, {"llvm.coro.alloc", "llvm.coro.begin", "llvm.coro.subfn.addr",
114 "llvm.coro.free", "llvm.coro.id", "llvm.coro.id.retcon",
115 "llvm.coro.id.async", "llvm.coro.id.retcon.once",
116 "llvm.coro.async.size.replace", "llvm.coro.async.resume"});
117}
118
122 return PreservedAnalyses::all();
123
126
129
130 PreservedAnalyses FuncPA;
131 FuncPA.preserveSet<CFGAnalyses>();
132
133 Lowerer L(M);
134 for (auto &F : M) {
135 if (L.lower(F)) {
136 FAM.invalidate(F, FuncPA);
137 FPM.run(F, FAM);
138 }
139 }
140
142}
assume Assume Builder
static bool declaresCoroCleanupIntrinsics(const Module &M)
static void lowerSubFn(IRBuilder<> &Builder, CoroSubFnInst *SubFn)
Definition: CoroCleanup.cpp:30
Select target instructions out of generic instructions
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
LLVMContext & Context
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
This header defines various interfaces for pass management in LLVM.
This file provides the interface for the pass responsible for both simplifying and canonicalizing the...
static const unsigned FramePtr
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:774
Represents analyses that only rely on functions' control flow.
Definition: PassManager.h:113
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:833
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1691
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1300
static ConstantTokenNone * get(LLVMContext &Context)
Return the ConstantTokenNone.
Definition: Constants.cpp:1415
This class represents the llvm.coro.subfn.addr instruction.
Definition: CoroInstr.h:35
Value * getFrame() const
Definition: CoroInstr.h:48
ResumeKind getIndex() const
Definition: CoroInstr.h:49
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2625
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:933
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same< PassT, PassManager >::value > addPass(PassT &&Pass)
Definition: PassManager.h:544
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs)
Run all of the passes in this manager over the given unit of IR.
Definition: PassManager.h:498
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:188
A pass to simplify and canonicalize the CFG of a function.
Definition: SimplifyCFG.h:29
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:374
Target - Wrapper for Target specific information.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1724
LLVM Value Representation.
Definition: Value.h:74
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:535
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1069
bool declaresIntrinsics(const Module &M, const std::initializer_list< StringRef >)
Definition: Coroutines.cpp:117
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:666
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)