26#define DEBUG_TYPE "amdgpu-lower-ctor-dtor"
30static Function *createInitOrFiniKernelFunction(
Module &M,
bool IsCtor) {
31 StringRef InitOrFiniKernelName =
"amdgcn.device.init";
33 InitOrFiniKernelName =
"amdgcn.device.fini";
34 if (M.getFunction(InitOrFiniKernelName))
41 InitOrFiniKernel->
addFnAttr(
"amdgpu-flat-work-group-size",
"1,1");
43 InitOrFiniKernel->
addFnAttr(
"device-init");
45 InitOrFiniKernel->
addFnAttr(
"device-fini");
46 return InitOrFiniKernel;
72static void createInitOrFiniCalls(
Function &
F,
bool IsCtor) {
82 auto *Begin = M.getOrInsertGlobal(
83 IsCtor ?
"__init_array_start" :
"__fini_array_start", PtrArrayTy, [&]() {
88 IsCtor ?
"__init_array_start" :
"__fini_array_start",
92 auto *End = M.getOrInsertGlobal(
93 IsCtor ?
"__init_array_end" :
"__fini_array_end", PtrArrayTy, [&]() {
98 IsCtor ?
"__init_array_end" :
"__fini_array_end",
107 Value *Start = Begin;
122 auto *CallBackPHI = IRB.
CreatePHI(PtrTy, 2,
"ptr");
123 auto *CallBack = IRB.
CreateLoad(
F.getType(), CallBackPHI,
"callback");
128 NewCallBack, Stop,
"end");
129 CallBackPHI->addIncoming(Start, &
F.getEntryBlock());
130 CallBackPHI->addIncoming(NewCallBack, LoopBB);
145 Function *InitOrFiniKernel = createInitOrFiniKernelFunction(M, IsCtor);
146 if (!InitOrFiniKernel)
149 createInitOrFiniCalls(*InitOrFiniKernel, IsCtor);
155static bool lowerCtorsAndDtors(
Module &M) {
157 Modified |= createInitOrFiniKernel(M,
"llvm.global_ctors",
true);
158 Modified |= createInitOrFiniKernel(M,
"llvm.global_dtors",
false);
162class AMDGPUCtorDtorLoweringLegacy final :
public ModulePass {
165 AMDGPUCtorDtorLoweringLegacy() :
ModulePass(ID) {}
166 bool runOnModule(
Module &M)
override {
return lowerCtorsAndDtors(M); }
177char AMDGPUCtorDtorLoweringLegacy::ID = 0;
179 AMDGPUCtorDtorLoweringLegacy::ID;
181 "Lower ctors and dtors for AMDGPU",
false,
false)
184 return new AMDGPUCtorDtorLoweringLegacy();
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
@ ICMP_UGE
unsigned greater or equal
@ ICMP_ULT
unsigned less than
ConstantArray - Constant Array Declarations.
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
static Function * createWithDefaultAttr(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Creates a function with some attributes recorded in llvm.module.flags and the LLVMContext applied.
void setCallingConv(CallingConv::ID CC)
@ WeakODRLinkage
Same, but only replaced by something equivalent.
@ ExternalLinkage
Externally visible function.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool hasInitializer() const
Definitions have initializers, declarations don't.
Value * CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, const Twine &Name="")
CondBrInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Value * CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
ReturnInst * CreateRetVoid()
Create a 'ret void' instruction.
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
PointerType * getPtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Type * getVoidTy()
Fetch the type representing void.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
This is an important class for using LLVM in a threaded context.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
A Module instance is used to store all the information related to an LLVM module.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Represent a constant reference to a string, i.e.
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
unsigned getNumOperands() const
LLVM Value Representation.
@ GLOBAL_ADDRESS
Address space for global memory (RAT0, VTX0).
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
This is an optimization pass for GlobalISel generic memory operations.
char & AMDGPUCtorDtorLoweringLegacyPassID
ModulePass * createAMDGPUCtorDtorLoweringLegacyPass()
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI void appendToUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.used list.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.