1 //===-- GenericToNVVM.cpp - Convert generic module to NVVM module - C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Convert generic global variables into either .global or .const access based
11 // on the variable's "constant" qualifier.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "NVPTX.h"
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"
30 
31 using namespace llvm;
32 
33 namespace llvm {
34 void initializeGenericToNVVMPass(PassRegistry &);
35 }
36 
37 namespace {
38 class GenericToNVVM : public ModulePass {
39 public:
40   static char ID;
41 
GenericToNVVM()42   GenericToNVVM() : ModulePass(ID) {}
43 
44   bool runOnModule(Module &M) override;
45 
getAnalysisUsage(AnalysisUsage & AU) const46   void getAnalysisUsage(AnalysisUsage &AU) const override {}
47 
48 private:
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,
54                                                 Constant *C,
55                                                 IRBuilder<> &Builder);
56   Value *remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
57                            IRBuilder<> &Builder);
58   void remapNamedMDNode(ValueToValueMapTy &VM, NamedMDNode *N);
59 
60   typedef ValueMap<GlobalVariable *, GlobalVariable *> GVMapTy;
61   typedef ValueMap<Constant *, Value *> ConstantToValueMapTy;
62   GVMapTy GVMap;
63   ConstantToValueMapTy ConstantToValueMap;
64 };
65 } // end namespace
66 
67 char GenericToNVVM::ID = 0;
68 
createGenericToNVVMPass()69 ModulePass *llvm::createGenericToNVVMPass() { return new GenericToNVVM(); }
70 
71 INITIALIZE_PASS(
72     GenericToNVVM, "generic-to-nvvm",
73     "Ensure that the global variables are in the global address space", false,
74     false)
75 
runOnModule(Module & M)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
80   // use.
81 
82   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
83        I != E;) {
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(),
90           GV->getLinkage(),
91           GV->hasInitializer() ? GV->getInitializer() : nullptr,
92           "", GV, GV->getThreadLocalMode(), llvm::ADDRESS_SPACE_GLOBAL);
93       NewGV->copyAttributesFrom(GV);
94       GVMap[GV] = NewGV;
95     }
96   }
97 
98   // Return immediately, if every global variable has a specific address space
99   // specifier.
100   if (GVMap.empty()) {
101     return false;
102   }
103 
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()) {
109       continue;
110     }
111     IRBuilder<> Builder(I->getEntryBlock().getFirstNonPHIOrDbg());
112     for (Function::iterator BBI = I->begin(), BBE = I->end(); BBI != BBE;
113          ++BBI) {
114       for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE;
115            ++II) {
116         for (unsigned i = 0, e = II->getNumOperands(); i < e; ++i) {
117           Value *Operand = II->getOperand(i);
118           if (isa<Constant>(Operand)) {
119             II->setOperand(
120                 i, remapConstant(&M, I, cast<Constant>(Operand), Builder));
121           }
122         }
123       }
124     }
125     ConstantToValueMap.clear();
126   }
127 
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;
132 
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();
137        I != E; I++) {
138     remapNamedMDNode(VM, I);
139   }
140 
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;
148 
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);
152     GVMap.erase(I);
153     I = Next;
154 
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);
163   }
164   assert(GVMap.empty() && "Expected it to be empty by now");
165 
166   return true;
167 }
168 
getOrInsertCVTA(Module * M,Function * F,GlobalVariable * GV,IRBuilder<> & Builder)169 Value *GenericToNVVM::getOrInsertCVTA(Module *M, Function *F,
170                                       GlobalVariable *GV,
171                                       IRBuilder<> &Builder) {
172   PointerType *GVType = GV->getType();
173   Value *CVTA = nullptr;
174 
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.
185     Type *ResultType =
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
194     // required.
195     DestTy =
196         PointerType::get(GVType->getElementType(), llvm::ADDRESS_SPACE_GENERIC);
197     CVTA = Builder.CreateBitCast(CVTA, DestTy, "cvta");
198   } else {
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");
207   }
208 
209   return CVTA;
210 }
211 
remapConstant(Module * M,Function * F,Constant * C,IRBuilder<> & Builder)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()) {
218     return CTII->second;
219   }
220 
221   Value *NewValue = C;
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
230     // is required.
231     GVMapTy::iterator I = GVMap.find(cast<GlobalVariable>(C));
232     if (I != GVMap.end()) {
233       NewValue = getOrInsertCVTA(M, F, I->second, Builder);
234     }
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
239     // of instructions.
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);
246   }
247 
248   ConstantToValueMap[C] = NewValue;
249   return NewValue;
250 }
251 
remapConstantVectorOrConstantAggregate(Module * M,Function * F,Constant * C,IRBuilder<> & Builder)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();
257 
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);
265   }
266 
267   // If none of the elements has been modified, return C as it is.
268   if (!OperandChanged) {
269     return C;
270   }
271 
272   // If any of the elements has been  modified, construct the equivalent
273   // vector or aggregate value with a set instructions and the converted
274   // elements.
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);
280     }
281   } else {
282     for (unsigned i = 0; i < NumOperands; ++i) {
283       NewValue =
284           Builder.CreateInsertValue(NewValue, NewOperands[i], makeArrayRef(i));
285     }
286   }
287 
288   return NewValue;
289 }
290 
remapConstantExpr(Module * M,Function * F,ConstantExpr * C,IRBuilder<> & Builder)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();
296 
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);
304   }
305 
306   // If none of the operands has been modified, return C as it is.
307   if (!OperandChanged) {
308     return C;
309   }
310 
311   // If any of the operands has been modified, construct the instruction with
312   // the converted operands.
313   unsigned Opcode = C->getOpcode();
314   switch (Opcode) {
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)!");
323     return C;
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],
330                                        NewOperands[2]);
331   case Instruction::ShuffleVector:
332     // ShuffleVector
333     return Builder.CreateShuffleVector(NewOperands[0], NewOperands[1],
334                                        NewOperands[2]);
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],
341                                      C->getIndices());
342   case Instruction::GetElementPtr:
343     // GetElementPtrConstantExpr
344     return cast<GEPOperator>(C)->isInBounds()
345                ? Builder.CreateGEP(
346                      cast<GEPOperator>(C)->getSourceElementType(),
347                      NewOperands[0],
348                      makeArrayRef(&NewOperands[1], NumOperands - 1))
349                : Builder.CreateInBoundsGEP(
350                      cast<GEPOperator>(C)->getSourceElementType(),
351                      NewOperands[0],
352                      makeArrayRef(&NewOperands[1], NumOperands - 1));
353   case Instruction::Select:
354     // SelectConstantExpr
355     return Builder.CreateSelect(NewOperands[0], NewOperands[1], NewOperands[2]);
356   default:
357     // BinaryConstantExpr
358     if (Instruction::isBinaryOp(Opcode)) {
359       return Builder.CreateBinOp(Instruction::BinaryOps(C->getOpcode()),
360                                  NewOperands[0], NewOperands[1]);
361     }
362     // UnaryConstantExpr
363     if (Instruction::isCast(Opcode)) {
364       return Builder.CreateCast(Instruction::CastOps(C->getOpcode()),
365                                 NewOperands[0], C->getType());
366     }
367     assert(false && "GenericToNVVM encountered an unsupported ConstantExpr");
368     return C;
369   }
370 }
371 
remapNamedMDNode(ValueToValueMapTy & VM,NamedMDNode * N)372 void GenericToNVVM::remapNamedMDNode(ValueToValueMapTy &VM, NamedMDNode *N) {
373 
374   bool OperandChanged = false;
375   SmallVector<MDNode *, 16> NewOperands;
376   unsigned NumOperands = N->getNumOperands();
377 
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);
385   }
386 
387   // If none of the operands has been modified, return immediately.
388   if (!OperandChanged) {
389     return;
390   }
391 
392   // Replace the old operands with the new operands.
393   N->dropAllReferences();
394   for (SmallVectorImpl<MDNode *>::iterator I = NewOperands.begin(),
395                                            E = NewOperands.end();
396        I != E; ++I) {
397     N->addOperand(*I);
398   }
399 }
400