1 | //===-- NVPTXCtorDtorLowering.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 | |
13 | #include "NVPTXCtorDtorLowering.h" |
14 | #include "MCTargetDesc/NVPTXBaseInfo.h" |
15 | #include "NVPTX.h" |
16 | #include "llvm/ADT/StringExtras.h" |
17 | #include "llvm/IR/Constants.h" |
18 | #include "llvm/IR/Function.h" |
19 | #include "llvm/IR/GlobalVariable.h" |
20 | #include "llvm/IR/IRBuilder.h" |
21 | #include "llvm/IR/Module.h" |
22 | #include "llvm/IR/Value.h" |
23 | #include "llvm/Pass.h" |
24 | #include "llvm/Support/CommandLine.h" |
25 | #include "llvm/Support/MD5.h" |
26 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
27 | |
28 | using namespace llvm; |
29 | |
30 | #define DEBUG_TYPE "nvptx-lower-ctor-dtor" |
31 | |
32 | static cl::opt<std::string> |
33 | GlobalStr("nvptx-lower-global-ctor-dtor-id" , |
34 | cl::desc("Override unique ID of ctor/dtor globals." ), |
35 | cl::init(Val: "" ), cl::Hidden); |
36 | |
37 | static cl::opt<bool> |
38 | CreateKernels("nvptx-emit-init-fini-kernel" , |
39 | cl::desc("Emit kernels to call ctor/dtor globals." ), |
40 | cl::init(Val: true), cl::Hidden); |
41 | |
42 | namespace { |
43 | |
44 | static std::string getHash(StringRef Str) { |
45 | llvm::MD5 Hasher; |
46 | llvm::MD5::MD5Result Hash; |
47 | Hasher.update(Str); |
48 | Hasher.final(Result&: Hash); |
49 | return llvm::utohexstr(X: Hash.low(), /*LowerCase=*/true); |
50 | } |
51 | |
52 | static void addKernelMetadata(Module &M, GlobalValue *GV) { |
53 | llvm::LLVMContext &Ctx = M.getContext(); |
54 | |
55 | // Get "nvvm.annotations" metadata node. |
56 | llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata(Name: "nvvm.annotations" ); |
57 | |
58 | llvm::Metadata *KernelMDVals[] = { |
59 | llvm::ConstantAsMetadata::get(C: GV), llvm::MDString::get(Context&: Ctx, Str: "kernel" ), |
60 | llvm::ConstantAsMetadata::get( |
61 | C: llvm::ConstantInt::get(Ty: llvm::Type::getInt32Ty(C&: Ctx), V: 1))}; |
62 | |
63 | // This kernel is only to be called single-threaded. |
64 | llvm::Metadata *ThreadXMDVals[] = { |
65 | llvm::ConstantAsMetadata::get(C: GV), llvm::MDString::get(Context&: Ctx, Str: "maxntidx" ), |
66 | llvm::ConstantAsMetadata::get( |
67 | C: llvm::ConstantInt::get(Ty: llvm::Type::getInt32Ty(C&: Ctx), V: 1))}; |
68 | llvm::Metadata *ThreadYMDVals[] = { |
69 | llvm::ConstantAsMetadata::get(C: GV), llvm::MDString::get(Context&: Ctx, Str: "maxntidy" ), |
70 | llvm::ConstantAsMetadata::get( |
71 | C: llvm::ConstantInt::get(Ty: llvm::Type::getInt32Ty(C&: Ctx), V: 1))}; |
72 | llvm::Metadata *ThreadZMDVals[] = { |
73 | llvm::ConstantAsMetadata::get(C: GV), llvm::MDString::get(Context&: Ctx, Str: "maxntidz" ), |
74 | llvm::ConstantAsMetadata::get( |
75 | C: llvm::ConstantInt::get(Ty: llvm::Type::getInt32Ty(C&: Ctx), V: 1))}; |
76 | |
77 | llvm::Metadata *BlockMDVals[] = { |
78 | llvm::ConstantAsMetadata::get(C: GV), |
79 | llvm::MDString::get(Context&: Ctx, Str: "maxclusterrank" ), |
80 | llvm::ConstantAsMetadata::get( |
81 | C: llvm::ConstantInt::get(Ty: llvm::Type::getInt32Ty(C&: Ctx), V: 1))}; |
82 | |
83 | // Append metadata to nvvm.annotations. |
84 | MD->addOperand(M: llvm::MDNode::get(Context&: Ctx, MDs: KernelMDVals)); |
85 | MD->addOperand(M: llvm::MDNode::get(Context&: Ctx, MDs: ThreadXMDVals)); |
86 | MD->addOperand(M: llvm::MDNode::get(Context&: Ctx, MDs: ThreadYMDVals)); |
87 | MD->addOperand(M: llvm::MDNode::get(Context&: Ctx, MDs: ThreadZMDVals)); |
88 | MD->addOperand(M: llvm::MDNode::get(Context&: Ctx, MDs: BlockMDVals)); |
89 | } |
90 | |
91 | static Function *createInitOrFiniKernelFunction(Module &M, bool IsCtor) { |
92 | StringRef InitOrFiniKernelName = |
93 | IsCtor ? "nvptx$device$init" : "nvptx$device$fini" ; |
94 | if (M.getFunction(Name: InitOrFiniKernelName)) |
95 | return nullptr; |
96 | |
97 | Function *InitOrFiniKernel = Function::createWithDefaultAttr( |
98 | Ty: FunctionType::get(Result: Type::getVoidTy(C&: M.getContext()), isVarArg: false), |
99 | Linkage: GlobalValue::WeakODRLinkage, AddrSpace: 0, N: InitOrFiniKernelName, M: &M); |
100 | addKernelMetadata(M, GV: InitOrFiniKernel); |
101 | |
102 | return InitOrFiniKernel; |
103 | } |
104 | |
105 | // We create the IR required to call each callback in this section. This is |
106 | // equivalent to the following code. Normally, the linker would provide us with |
107 | // the definitions of the init and fini array sections. The 'nvlink' linker does |
108 | // not do this so initializing these values is done by the runtime. |
109 | // |
110 | // extern "C" void **__init_array_start = nullptr; |
111 | // extern "C" void **__init_array_end = nullptr; |
112 | // extern "C" void **__fini_array_start = nullptr; |
113 | // extern "C" void **__fini_array_end = nullptr; |
114 | // |
115 | // using InitCallback = void(); |
116 | // using FiniCallback = void(); |
117 | // |
118 | // void call_init_array_callbacks() { |
119 | // for (auto start = __init_array_start; start != __init_array_end; ++start) |
120 | // reinterpret_cast<InitCallback *>(*start)(); |
121 | // } |
122 | // |
123 | // void call_init_array_callbacks() { |
124 | // size_t fini_array_size = __fini_array_end - __fini_array_start; |
125 | // for (size_t i = fini_array_size; i > 0; --i) |
126 | // reinterpret_cast<FiniCallback *>(__fini_array_start[i - 1])(); |
127 | // } |
128 | static void createInitOrFiniCalls(Function &F, bool IsCtor) { |
129 | Module &M = *F.getParent(); |
130 | LLVMContext &C = M.getContext(); |
131 | |
132 | IRBuilder<> IRB(BasicBlock::Create(Context&: C, Name: "entry" , Parent: &F)); |
133 | auto *LoopBB = BasicBlock::Create(Context&: C, Name: "while.entry" , Parent: &F); |
134 | auto *ExitBB = BasicBlock::Create(Context&: C, Name: "while.end" , Parent: &F); |
135 | Type *PtrTy = IRB.getPtrTy(AddrSpace: llvm::ADDRESS_SPACE_GLOBAL); |
136 | |
137 | auto *Begin = M.getOrInsertGlobal( |
138 | Name: IsCtor ? "__init_array_start" : "__fini_array_start" , |
139 | Ty: PointerType::get(C, AddressSpace: 0), CreateGlobalCallback: [&]() { |
140 | auto *GV = new GlobalVariable( |
141 | M, PointerType::get(C, AddressSpace: 0), |
142 | /*isConstant=*/false, GlobalValue::WeakAnyLinkage, |
143 | Constant::getNullValue(Ty: PointerType::get(C, AddressSpace: 0)), |
144 | IsCtor ? "__init_array_start" : "__fini_array_start" , |
145 | /*InsertBefore=*/nullptr, GlobalVariable::NotThreadLocal, |
146 | /*AddressSpace=*/llvm::ADDRESS_SPACE_GLOBAL); |
147 | GV->setVisibility(GlobalVariable::ProtectedVisibility); |
148 | return GV; |
149 | }); |
150 | auto *End = M.getOrInsertGlobal( |
151 | Name: IsCtor ? "__init_array_end" : "__fini_array_end" , Ty: PointerType::get(C, AddressSpace: 0), |
152 | CreateGlobalCallback: [&]() { |
153 | auto *GV = new GlobalVariable( |
154 | M, PointerType::get(C, AddressSpace: 0), |
155 | /*isConstant=*/false, GlobalValue::WeakAnyLinkage, |
156 | Constant::getNullValue(Ty: PointerType::get(C, AddressSpace: 0)), |
157 | IsCtor ? "__init_array_end" : "__fini_array_end" , |
158 | /*InsertBefore=*/nullptr, GlobalVariable::NotThreadLocal, |
159 | /*AddressSpace=*/llvm::ADDRESS_SPACE_GLOBAL); |
160 | GV->setVisibility(GlobalVariable::ProtectedVisibility); |
161 | return GV; |
162 | }); |
163 | |
164 | // The constructor type is suppoed to allow using the argument vectors, but |
165 | // for now we just call them with no arguments. |
166 | auto *CallBackTy = FunctionType::get(Result: IRB.getVoidTy(), isVarArg: {}); |
167 | |
168 | // The destructor array must be called in reverse order. Get an expression to |
169 | // the end of the array and iterate backwards in that case. |
170 | Value *BeginVal = IRB.CreateLoad(Ty: Begin->getType(), Ptr: Begin, Name: "begin" ); |
171 | Value *EndVal = IRB.CreateLoad(Ty: Begin->getType(), Ptr: End, Name: "stop" ); |
172 | if (!IsCtor) { |
173 | auto *BeginInt = IRB.CreatePtrToInt(V: BeginVal, DestTy: IntegerType::getInt64Ty(C)); |
174 | auto *EndInt = IRB.CreatePtrToInt(V: EndVal, DestTy: IntegerType::getInt64Ty(C)); |
175 | auto *SubInst = IRB.CreateSub(LHS: EndInt, RHS: BeginInt); |
176 | auto *Offset = IRB.CreateAShr( |
177 | LHS: SubInst, RHS: ConstantInt::get(Ty: IntegerType::getInt64Ty(C), V: 3), Name: "offset" , |
178 | /*IsExact=*/isExact: true); |
179 | auto *ValuePtr = IRB.CreateGEP(Ty: PointerType::get(C, AddressSpace: 0), Ptr: BeginVal, |
180 | IdxList: ArrayRef<Value *>({Offset})); |
181 | EndVal = BeginVal; |
182 | BeginVal = IRB.CreateInBoundsGEP( |
183 | Ty: PointerType::get(C, AddressSpace: 0), Ptr: ValuePtr, |
184 | IdxList: ArrayRef<Value *>(ConstantInt::get(Ty: IntegerType::getInt64Ty(C), V: -1)), |
185 | Name: "start" ); |
186 | } |
187 | IRB.CreateCondBr( |
188 | Cond: IRB.CreateCmp(Pred: IsCtor ? ICmpInst::ICMP_NE : ICmpInst::ICMP_UGT, LHS: BeginVal, |
189 | RHS: EndVal), |
190 | True: LoopBB, False: ExitBB); |
191 | IRB.SetInsertPoint(LoopBB); |
192 | auto *CallBackPHI = IRB.CreatePHI(Ty: PtrTy, NumReservedValues: 2, Name: "ptr" ); |
193 | auto *CallBack = IRB.CreateLoad(Ty: IRB.getPtrTy(AddrSpace: F.getAddressSpace()), |
194 | Ptr: CallBackPHI, Name: "callback" ); |
195 | IRB.CreateCall(FTy: CallBackTy, Callee: CallBack); |
196 | auto *NewCallBack = |
197 | IRB.CreateConstGEP1_64(Ty: PtrTy, Ptr: CallBackPHI, Idx0: IsCtor ? 1 : -1, Name: "next" ); |
198 | auto *EndCmp = IRB.CreateCmp(Pred: IsCtor ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_ULT, |
199 | LHS: NewCallBack, RHS: EndVal, Name: "end" ); |
200 | CallBackPHI->addIncoming(V: BeginVal, BB: &F.getEntryBlock()); |
201 | CallBackPHI->addIncoming(V: NewCallBack, BB: LoopBB); |
202 | IRB.CreateCondBr(Cond: EndCmp, True: ExitBB, False: LoopBB); |
203 | IRB.SetInsertPoint(ExitBB); |
204 | IRB.CreateRetVoid(); |
205 | } |
206 | |
207 | static bool createInitOrFiniGlobals(Module &M, GlobalVariable *GV, |
208 | bool IsCtor) { |
209 | ConstantArray *GA = dyn_cast<ConstantArray>(Val: GV->getInitializer()); |
210 | if (!GA || GA->getNumOperands() == 0) |
211 | return false; |
212 | |
213 | // NVPTX has no way to emit variables at specific sections or support for |
214 | // the traditional constructor sections. Instead, we emit mangled global |
215 | // names so the runtime can build the list manually. |
216 | for (Value *V : GA->operands()) { |
217 | auto *CS = cast<ConstantStruct>(Val: V); |
218 | auto *F = cast<Constant>(Val: CS->getOperand(i_nocapture: 1)); |
219 | uint64_t Priority = cast<ConstantInt>(Val: CS->getOperand(i_nocapture: 0))->getSExtValue(); |
220 | std::string PriorityStr = "." + std::to_string(val: Priority); |
221 | // We append a semi-unique hash and the priority to the global name. |
222 | std::string GlobalID = |
223 | !GlobalStr.empty() ? GlobalStr : getHash(Str: M.getSourceFileName()); |
224 | std::string NameStr = |
225 | ((IsCtor ? "__init_array_object_" : "__fini_array_object_" ) + |
226 | F->getName() + "_" + GlobalID + "_" + std::to_string(val: Priority)) |
227 | .str(); |
228 | // PTX does not support exported names with '.' in them. |
229 | llvm::transform(Range&: NameStr, d_first: NameStr.begin(), |
230 | F: [](char c) { return c == '.' ? '_' : c; }); |
231 | |
232 | auto *GV = new GlobalVariable(M, F->getType(), /*IsConstant=*/true, |
233 | GlobalValue::ExternalLinkage, F, NameStr, |
234 | nullptr, GlobalValue::NotThreadLocal, |
235 | /*AddressSpace=*/4); |
236 | // This isn't respected by Nvidia, simply put here for clarity. |
237 | GV->setSection(IsCtor ? ".init_array" + PriorityStr |
238 | : ".fini_array" + PriorityStr); |
239 | GV->setVisibility(GlobalVariable::ProtectedVisibility); |
240 | appendToUsed(M, Values: {GV}); |
241 | } |
242 | |
243 | return true; |
244 | } |
245 | |
246 | static bool createInitOrFiniKernel(Module &M, StringRef GlobalName, |
247 | bool IsCtor) { |
248 | GlobalVariable *GV = M.getGlobalVariable(Name: GlobalName); |
249 | if (!GV || !GV->hasInitializer()) |
250 | return false; |
251 | |
252 | if (!createInitOrFiniGlobals(M, GV, IsCtor)) |
253 | return false; |
254 | |
255 | if (!CreateKernels) |
256 | return true; |
257 | |
258 | Function *InitOrFiniKernel = createInitOrFiniKernelFunction(M, IsCtor); |
259 | if (!InitOrFiniKernel) |
260 | return false; |
261 | |
262 | createInitOrFiniCalls(F&: *InitOrFiniKernel, IsCtor); |
263 | |
264 | GV->eraseFromParent(); |
265 | return true; |
266 | } |
267 | |
268 | static bool lowerCtorsAndDtors(Module &M) { |
269 | bool Modified = false; |
270 | Modified |= createInitOrFiniKernel(M, GlobalName: "llvm.global_ctors" , /*IsCtor =*/true); |
271 | Modified |= createInitOrFiniKernel(M, GlobalName: "llvm.global_dtors" , /*IsCtor =*/false); |
272 | return Modified; |
273 | } |
274 | |
275 | class NVPTXCtorDtorLoweringLegacy final : public ModulePass { |
276 | public: |
277 | static char ID; |
278 | NVPTXCtorDtorLoweringLegacy() : ModulePass(ID) {} |
279 | bool runOnModule(Module &M) override { return lowerCtorsAndDtors(M); } |
280 | }; |
281 | |
282 | } // End anonymous namespace |
283 | |
284 | PreservedAnalyses NVPTXCtorDtorLoweringPass::run(Module &M, |
285 | ModuleAnalysisManager &AM) { |
286 | return lowerCtorsAndDtors(M) ? PreservedAnalyses::none() |
287 | : PreservedAnalyses::all(); |
288 | } |
289 | |
290 | char NVPTXCtorDtorLoweringLegacy::ID = 0; |
291 | char &llvm::NVPTXCtorDtorLoweringLegacyPassID = NVPTXCtorDtorLoweringLegacy::ID; |
292 | INITIALIZE_PASS(NVPTXCtorDtorLoweringLegacy, DEBUG_TYPE, |
293 | "Lower ctors and dtors for NVPTX" , false, false) |
294 | |
295 | ModulePass *llvm::createNVPTXCtorDtorLoweringLegacyPass() { |
296 | return new NVPTXCtorDtorLoweringLegacy(); |
297 | } |
298 | |