LLVM 18.0.0git
AMDGPUCtorDtorLowering.cpp
Go to the documentation of this file.
1//===-- AMDGPUCtorDtorLowering.cpp - Handle global ctors and dtors --------===//
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/// \file
10/// This pass creates a unified init and fini kernel with the required metadata
11//===----------------------------------------------------------------------===//
12
14#include "AMDGPU.h"
15#include "llvm/IR/Constants.h"
16#include "llvm/IR/Function.h"
18#include "llvm/IR/IRBuilder.h"
19#include "llvm/IR/Module.h"
20#include "llvm/IR/Value.h"
21#include "llvm/Pass.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "amdgpu-lower-ctor-dtor"
27
28namespace {
29
30static Function *createInitOrFiniKernelFunction(Module &M, bool IsCtor) {
31 StringRef InitOrFiniKernelName = "amdgcn.device.init";
32 if (!IsCtor)
33 InitOrFiniKernelName = "amdgcn.device.fini";
34 if (M.getFunction(InitOrFiniKernelName))
35 return nullptr;
36
37 Function *InitOrFiniKernel = Function::createWithDefaultAttr(
38 FunctionType::get(Type::getVoidTy(M.getContext()), false),
39 GlobalValue::WeakODRLinkage, 0, InitOrFiniKernelName, &M);
41 InitOrFiniKernel->addFnAttr("amdgpu-flat-work-group-size", "1,1");
42 if (IsCtor)
43 InitOrFiniKernel->addFnAttr("device-init");
44 else
45 InitOrFiniKernel->addFnAttr("device-fini");
46 return InitOrFiniKernel;
47}
48
49// The linker will provide the associated symbols to allow us to traverse the
50// global constructors / destructors in priority order. We create the IR
51// required to call each callback in this section. This is equivalent to the
52// following code.
53//
54// extern "C" void * __init_array_start[];
55// extern "C" void * __init_array_end[];
56//
57// using InitCallback = void();
58//
59// void call_init_array_callbacks() {
60// for (auto start = __init_array_start; start != __init_array_end; ++start)
61// reinterpret_cast<InitCallback *>(*start)();
62// }
63static void createInitOrFiniCalls(Function &F, bool IsCtor) {
64 Module &M = *F.getParent();
65 LLVMContext &C = M.getContext();
66
67 IRBuilder<> IRB(BasicBlock::Create(C, "entry", &F));
68 auto *LoopBB = BasicBlock::Create(C, "while.entry", &F);
69 auto *ExitBB = BasicBlock::Create(C, "while.end", &F);
71
72 auto *Begin = M.getOrInsertGlobal(
73 IsCtor ? "__init_array_start" : "__fini_array_start",
74 ArrayType::get(PtrTy, 0), [&]() {
75 return new GlobalVariable(
76 M, ArrayType::get(PtrTy, 0),
77 /*isConstant=*/true, GlobalValue::ExternalLinkage,
78 /*Initializer=*/nullptr,
79 IsCtor ? "__init_array_start" : "__fini_array_start",
80 /*InsertBefore=*/nullptr, GlobalVariable::NotThreadLocal,
81 /*AddressSpace=*/1);
82 });
83 auto *End = M.getOrInsertGlobal(
84 IsCtor ? "__init_array_end" : "__fini_array_end",
85 ArrayType::get(PtrTy, 0), [&]() {
86 return new GlobalVariable(
87 M, ArrayType::get(PtrTy, 0),
88 /*isConstant=*/true, GlobalValue::ExternalLinkage,
89 /*Initializer=*/nullptr,
90 IsCtor ? "__init_array_end" : "__fini_array_end",
91 /*InsertBefore=*/nullptr, GlobalVariable::NotThreadLocal,
92 /*AddressSpace=*/1);
93 });
94
95 // The constructor type is suppoed to allow using the argument vectors, but
96 // for now we just call them with no arguments.
97 auto *CallBackTy = FunctionType::get(IRB.getVoidTy(), {});
98
99 IRB.CreateCondBr(IRB.CreateICmpNE(Begin, End), LoopBB, ExitBB);
100 IRB.SetInsertPoint(LoopBB);
101 auto *CallBackPHI = IRB.CreatePHI(PtrTy, 2, "ptr");
102 auto *CallBack = IRB.CreateLoad(CallBackTy->getPointerTo(F.getAddressSpace()),
103 CallBackPHI, "callback");
104 IRB.CreateCall(CallBackTy, CallBack);
105 auto *NewCallBack = IRB.CreateConstGEP1_64(PtrTy, CallBackPHI, 1, "next");
106 auto *EndCmp = IRB.CreateICmpEQ(NewCallBack, End, "end");
107 CallBackPHI->addIncoming(Begin, &F.getEntryBlock());
108 CallBackPHI->addIncoming(NewCallBack, LoopBB);
109 IRB.CreateCondBr(EndCmp, ExitBB, LoopBB);
110 IRB.SetInsertPoint(ExitBB);
111 IRB.CreateRetVoid();
112}
113
114static bool createInitOrFiniKernel(Module &M, StringRef GlobalName,
115 bool IsCtor) {
116 GlobalVariable *GV = M.getGlobalVariable(GlobalName);
117 if (!GV || !GV->hasInitializer())
118 return false;
119 ConstantArray *GA = dyn_cast<ConstantArray>(GV->getInitializer());
120 if (!GA || GA->getNumOperands() == 0)
121 return false;
122
123 Function *InitOrFiniKernel = createInitOrFiniKernelFunction(M, IsCtor);
124 if (!InitOrFiniKernel)
125 return false;
126
127 createInitOrFiniCalls(*InitOrFiniKernel, IsCtor);
128
129 appendToUsed(M, {InitOrFiniKernel});
130 return true;
131}
132
133static bool lowerCtorsAndDtors(Module &M) {
134 bool Modified = false;
135 Modified |= createInitOrFiniKernel(M, "llvm.global_ctors", /*IsCtor =*/true);
136 Modified |= createInitOrFiniKernel(M, "llvm.global_dtors", /*IsCtor =*/false);
137 return Modified;
138}
139
140class AMDGPUCtorDtorLoweringLegacy final : public ModulePass {
141public:
142 static char ID;
143 AMDGPUCtorDtorLoweringLegacy() : ModulePass(ID) {}
144 bool runOnModule(Module &M) override { return lowerCtorsAndDtors(M); }
145};
146
147} // End anonymous namespace
148
151 return lowerCtorsAndDtors(M) ? PreservedAnalyses::none()
153}
154
155char AMDGPUCtorDtorLoweringLegacy::ID = 0;
157 AMDGPUCtorDtorLoweringLegacy::ID;
158INITIALIZE_PASS(AMDGPUCtorDtorLoweringLegacy, DEBUG_TYPE,
159 "Lower ctors and dtors for AMDGPU", false, false)
160
162 return new AMDGPUCtorDtorLoweringLegacy();
163}
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool End
Definition: ELF_riscv.cpp:469
#define DEBUG_TYPE
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:105
ConstantArray - Constant Array Declarations.
Definition: Constants.h:408
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
Definition: Function.cpp:555
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 applied.
Definition: Function.cpp:337
void setCallingConv(CallingConv::ID CC)
Definition: Function.h:243
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:53
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
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="")
Definition: IRBuilder.h:1923
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2204
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2356
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2200
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
Definition: IRBuilder.h:1111
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition: IRBuilder.h:1786
ReturnInst * CreateRetVoid()
Create a 'ret void' instruction.
Definition: IRBuilder.h:1081
PointerType * getPtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer.
Definition: IRBuilder.h:555
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:180
Type * getVoidTy()
Fetch the type representing void.
Definition: IRBuilder.h:550
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2371
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2625
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
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
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static Type * getVoidTy(LLVMContext &C)
unsigned getNumOperands() const
Definition: User.h:191
@ GLOBAL_ADDRESS
Address space for global memory (RAT0, VTX0).
Definition: AMDGPU.h:391
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
Definition: CallingConv.h:197
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
char & AMDGPUCtorDtorLoweringLegacyPassID
ModulePass * createAMDGPUCtorDtorLoweringLegacyPass()
void appendToUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.used list.