1 //===-- GenericToNVVM.cpp - Convert generic module to NVVM module - C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Convert generic global variables into either .global or .const access based
11 // on the variable's "constant" qualifier.
13 //===----------------------------------------------------------------------===//
16 #include "MCTargetDesc/NVPTXBaseInfo.h"
17 #include "NVPTXUtilities.h"
18 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
19 #include "llvm/CodeGen/ValueTypes.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Intrinsics.h"
25 #include "llvm/IR/LegacyPassManager.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/Operator.h"
28 #include "llvm/IR/ValueMap.h"
29 #include "llvm/Transforms/Utils/ValueMapper.h"
34 void initializeGenericToNVVMPass(PassRegistry &);
38 class GenericToNVVM : public ModulePass {
42 GenericToNVVM() : ModulePass(ID) {}
44 bool runOnModule(Module &M) override;
46 void getAnalysisUsage(AnalysisUsage &AU) const override {}
49 Value *getOrInsertCVTA(Module *M, Function *F, GlobalVariable *GV,
50 IRBuilder<> &Builder);
51 Value *remapConstant(Module *M, Function *F, Constant *C,
52 IRBuilder<> &Builder);
53 Value *remapConstantVectorOrConstantAggregate(Module *M, Function *F,
55 IRBuilder<> &Builder);
56 Value *remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
57 IRBuilder<> &Builder);
58 void remapNamedMDNode(ValueToValueMapTy &VM, NamedMDNode *N);
60 typedef ValueMap<GlobalVariable *, GlobalVariable *> GVMapTy;
61 typedef ValueMap<Constant *, Value *> ConstantToValueMapTy;
63 ConstantToValueMapTy ConstantToValueMap;
67 char GenericToNVVM::ID = 0;
69 ModulePass *llvm::createGenericToNVVMPass() { return new GenericToNVVM(); }
72 GenericToNVVM, "generic-to-nvvm",
73 "Ensure that the global variables are in the global address space", false,
76 bool GenericToNVVM::runOnModule(Module &M) {
77 // Create a clone of each global variable that has the default address space.
78 // The clone is created with the global address space specifier, and the pair
79 // of original global variable and its clone is placed in the GVMap for later
82 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
84 GlobalVariable *GV = I++;
85 if (GV->getType()->getAddressSpace() == llvm::ADDRESS_SPACE_GENERIC &&
86 !llvm::isTexture(*GV) && !llvm::isSurface(*GV) &&
87 !llvm::isSampler(*GV) && !GV->getName().startswith("llvm.")) {
88 GlobalVariable *NewGV = new GlobalVariable(
89 M, GV->getType()->getElementType(), GV->isConstant(),
91 GV->hasInitializer() ? GV->getInitializer() : nullptr,
92 "", GV, GV->getThreadLocalMode(), llvm::ADDRESS_SPACE_GLOBAL);
93 NewGV->copyAttributesFrom(GV);
98 // Return immediately, if every global variable has a specific address space
104 // Walk through the instructions in function defitinions, and replace any use
105 // of original global variables in GVMap with a use of the corresponding
106 // copies in GVMap. If necessary, promote constants to instructions.
107 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
108 if (I->isDeclaration()) {
111 IRBuilder<> Builder(I->getEntryBlock().getFirstNonPHIOrDbg());
112 for (Function::iterator BBI = I->begin(), BBE = I->end(); BBI != BBE;
114 for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE;
116 for (unsigned i = 0, e = II->getNumOperands(); i < e; ++i) {
117 Value *Operand = II->getOperand(i);
118 if (isa<Constant>(Operand)) {
120 i, remapConstant(&M, I, cast<Constant>(Operand), Builder));
125 ConstantToValueMap.clear();
128 // Copy GVMap over to a standard value map.
129 ValueToValueMapTy VM;
130 for (auto I = GVMap.begin(), E = GVMap.end(); I != E; ++I)
131 VM[I->first] = I->second;
133 // Walk through the metadata section and update the debug information
134 // associated with the global variables in the default address space.
135 for (Module::named_metadata_iterator I = M.named_metadata_begin(),
136 E = M.named_metadata_end();
138 remapNamedMDNode(VM, I);
141 // Walk through the global variable initializers, and replace any use of
142 // original global variables in GVMap with a use of the corresponding copies
143 // in GVMap. The copies need to be bitcast to the original global variable
144 // types, as we cannot use cvta in global variable initializers.
145 for (GVMapTy::iterator I = GVMap.begin(), E = GVMap.end(); I != E;) {
146 GlobalVariable *GV = I->first;
147 GlobalVariable *NewGV = I->second;
149 // Remove GV from the map so that it can be RAUWed. Note that
150 // DenseMap::erase() won't invalidate any iterators but this one.
151 auto Next = std::next(I);
155 Constant *BitCastNewGV = ConstantExpr::getPointerCast(NewGV, GV->getType());
156 // At this point, the remaining uses of GV should be found only in global
157 // variable initializers, as other uses have been already been removed
158 // while walking through the instructions in function definitions.
159 GV->replaceAllUsesWith(BitCastNewGV);
160 std::string Name = GV->getName();
161 GV->eraseFromParent();
162 NewGV->setName(Name);
164 assert(GVMap.empty() && "Expected it to be empty by now");
169 Value *GenericToNVVM::getOrInsertCVTA(Module *M, Function *F,
171 IRBuilder<> &Builder) {
172 PointerType *GVType = GV->getType();
173 Value *CVTA = nullptr;
175 // See if the address space conversion requires the operand to be bitcast
176 // to i8 addrspace(n)* first.
177 EVT ExtendedGVType = EVT::getEVT(GVType->getElementType(), true);
178 if (!ExtendedGVType.isInteger() && !ExtendedGVType.isFloatingPoint()) {
179 // A bitcast to i8 addrspace(n)* on the operand is needed.
180 LLVMContext &Context = M->getContext();
181 unsigned int AddrSpace = GVType->getAddressSpace();
182 Type *DestTy = PointerType::get(Type::getInt8Ty(Context), AddrSpace);
183 CVTA = Builder.CreateBitCast(GV, DestTy, "cvta");
184 // Insert the address space conversion.
186 PointerType::get(Type::getInt8Ty(Context), llvm::ADDRESS_SPACE_GENERIC);
187 SmallVector<Type *, 2> ParamTypes;
188 ParamTypes.push_back(ResultType);
189 ParamTypes.push_back(DestTy);
190 Function *CVTAFunction = Intrinsic::getDeclaration(
191 M, Intrinsic::nvvm_ptr_global_to_gen, ParamTypes);
192 CVTA = Builder.CreateCall(CVTAFunction, CVTA, "cvta");
193 // Another bitcast from i8 * to <the element type of GVType> * is
196 PointerType::get(GVType->getElementType(), llvm::ADDRESS_SPACE_GENERIC);
197 CVTA = Builder.CreateBitCast(CVTA, DestTy, "cvta");
199 // A simple CVTA is enough.
200 SmallVector<Type *, 2> ParamTypes;
201 ParamTypes.push_back(PointerType::get(GVType->getElementType(),
202 llvm::ADDRESS_SPACE_GENERIC));
203 ParamTypes.push_back(GVType);
204 Function *CVTAFunction = Intrinsic::getDeclaration(
205 M, Intrinsic::nvvm_ptr_global_to_gen, ParamTypes);
206 CVTA = Builder.CreateCall(CVTAFunction, GV, "cvta");
212 Value *GenericToNVVM::remapConstant(Module *M, Function *F, Constant *C,
213 IRBuilder<> &Builder) {
214 // If the constant C has been converted already in the given function F, just
215 // return the converted value.
216 ConstantToValueMapTy::iterator CTII = ConstantToValueMap.find(C);
217 if (CTII != ConstantToValueMap.end()) {
222 if (isa<GlobalVariable>(C)) {
223 // If the constant C is a global variable and is found in GVMap, generate a
224 // set set of instructions that convert the clone of C with the global
225 // address space specifier to a generic pointer.
226 // The constant C cannot be used here, as it will be erased from the
227 // module eventually. And the clone of C with the global address space
228 // specifier cannot be used here either, as it will affect the types of
229 // other instructions in the function. Hence, this address space conversion
231 GVMapTy::iterator I = GVMap.find(cast<GlobalVariable>(C));
232 if (I != GVMap.end()) {
233 NewValue = getOrInsertCVTA(M, F, I->second, Builder);
235 } else if (isa<ConstantVector>(C) || isa<ConstantArray>(C) ||
236 isa<ConstantStruct>(C)) {
237 // If any element in the constant vector or aggregate C is or uses a global
238 // variable in GVMap, the constant C needs to be reconstructed, using a set
240 NewValue = remapConstantVectorOrConstantAggregate(M, F, C, Builder);
241 } else if (isa<ConstantExpr>(C)) {
242 // If any operand in the constant expression C is or uses a global variable
243 // in GVMap, the constant expression C needs to be reconstructed, using a
244 // set of instructions.
245 NewValue = remapConstantExpr(M, F, cast<ConstantExpr>(C), Builder);
248 ConstantToValueMap[C] = NewValue;
252 Value *GenericToNVVM::remapConstantVectorOrConstantAggregate(
253 Module *M, Function *F, Constant *C, IRBuilder<> &Builder) {
254 bool OperandChanged = false;
255 SmallVector<Value *, 4> NewOperands;
256 unsigned NumOperands = C->getNumOperands();
258 // Check if any element is or uses a global variable in GVMap, and thus
259 // converted to another value.
260 for (unsigned i = 0; i < NumOperands; ++i) {
261 Value *Operand = C->getOperand(i);
262 Value *NewOperand = remapConstant(M, F, cast<Constant>(Operand), Builder);
263 OperandChanged |= Operand != NewOperand;
264 NewOperands.push_back(NewOperand);
267 // If none of the elements has been modified, return C as it is.
268 if (!OperandChanged) {
272 // If any of the elements has been modified, construct the equivalent
273 // vector or aggregate value with a set instructions and the converted
275 Value *NewValue = UndefValue::get(C->getType());
276 if (isa<ConstantVector>(C)) {
277 for (unsigned i = 0; i < NumOperands; ++i) {
278 Value *Idx = ConstantInt::get(Type::getInt32Ty(M->getContext()), i);
279 NewValue = Builder.CreateInsertElement(NewValue, NewOperands[i], Idx);
282 for (unsigned i = 0; i < NumOperands; ++i) {
284 Builder.CreateInsertValue(NewValue, NewOperands[i], makeArrayRef(i));
291 Value *GenericToNVVM::remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
292 IRBuilder<> &Builder) {
293 bool OperandChanged = false;
294 SmallVector<Value *, 4> NewOperands;
295 unsigned NumOperands = C->getNumOperands();
297 // Check if any operand is or uses a global variable in GVMap, and thus
298 // converted to another value.
299 for (unsigned i = 0; i < NumOperands; ++i) {
300 Value *Operand = C->getOperand(i);
301 Value *NewOperand = remapConstant(M, F, cast<Constant>(Operand), Builder);
302 OperandChanged |= Operand != NewOperand;
303 NewOperands.push_back(NewOperand);
306 // If none of the operands has been modified, return C as it is.
307 if (!OperandChanged) {
311 // If any of the operands has been modified, construct the instruction with
312 // the converted operands.
313 unsigned Opcode = C->getOpcode();
315 case Instruction::ICmp:
316 // CompareConstantExpr (icmp)
317 return Builder.CreateICmp(CmpInst::Predicate(C->getPredicate()),
318 NewOperands[0], NewOperands[1]);
319 case Instruction::FCmp:
320 // CompareConstantExpr (fcmp)
321 assert(false && "Address space conversion should have no effect "
322 "on float point CompareConstantExpr (fcmp)!");
324 case Instruction::ExtractElement:
325 // ExtractElementConstantExpr
326 return Builder.CreateExtractElement(NewOperands[0], NewOperands[1]);
327 case Instruction::InsertElement:
328 // InsertElementConstantExpr
329 return Builder.CreateInsertElement(NewOperands[0], NewOperands[1],
331 case Instruction::ShuffleVector:
333 return Builder.CreateShuffleVector(NewOperands[0], NewOperands[1],
335 case Instruction::ExtractValue:
336 // ExtractValueConstantExpr
337 return Builder.CreateExtractValue(NewOperands[0], C->getIndices());
338 case Instruction::InsertValue:
339 // InsertValueConstantExpr
340 return Builder.CreateInsertValue(NewOperands[0], NewOperands[1],
342 case Instruction::GetElementPtr:
343 // GetElementPtrConstantExpr
344 return cast<GEPOperator>(C)->isInBounds()
346 cast<GEPOperator>(C)->getSourceElementType(),
348 makeArrayRef(&NewOperands[1], NumOperands - 1))
349 : Builder.CreateInBoundsGEP(
350 cast<GEPOperator>(C)->getSourceElementType(),
352 makeArrayRef(&NewOperands[1], NumOperands - 1));
353 case Instruction::Select:
354 // SelectConstantExpr
355 return Builder.CreateSelect(NewOperands[0], NewOperands[1], NewOperands[2]);
357 // BinaryConstantExpr
358 if (Instruction::isBinaryOp(Opcode)) {
359 return Builder.CreateBinOp(Instruction::BinaryOps(C->getOpcode()),
360 NewOperands[0], NewOperands[1]);
363 if (Instruction::isCast(Opcode)) {
364 return Builder.CreateCast(Instruction::CastOps(C->getOpcode()),
365 NewOperands[0], C->getType());
367 assert(false && "GenericToNVVM encountered an unsupported ConstantExpr");
372 void GenericToNVVM::remapNamedMDNode(ValueToValueMapTy &VM, NamedMDNode *N) {
374 bool OperandChanged = false;
375 SmallVector<MDNode *, 16> NewOperands;
376 unsigned NumOperands = N->getNumOperands();
378 // Check if any operand is or contains a global variable in GVMap, and thus
379 // converted to another value.
380 for (unsigned i = 0; i < NumOperands; ++i) {
381 MDNode *Operand = N->getOperand(i);
382 MDNode *NewOperand = MapMetadata(Operand, VM);
383 OperandChanged |= Operand != NewOperand;
384 NewOperands.push_back(NewOperand);
387 // If none of the operands has been modified, return immediately.
388 if (!OperandChanged) {
392 // Replace the old operands with the new operands.
393 N->dropAllReferences();
394 for (SmallVectorImpl<MDNode *>::iterator I = NewOperands.begin(),
395 E = NewOperands.end();