1 //===- NVVMReflect.cpp - NVVM Emulate conditional compilation -------------===// 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 // This pass replaces occurrences of __nvvm_reflect("string") with an 11 // integer based on -nvvm-reflect-list string=<int> option given to this pass. 12 // If an undefined string value is seen in a call to __nvvm_reflect("string"), 13 // a default value of 0 will be used. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "NVPTX.h" 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/StringMap.h" 21 #include "llvm/IR/Constants.h" 22 #include "llvm/IR/DerivedTypes.h" 23 #include "llvm/IR/Function.h" 24 #include "llvm/IR/Instructions.h" 25 #include "llvm/IR/Module.h" 26 #include "llvm/IR/Type.h" 27 #include "llvm/Pass.h" 28 #include "llvm/Support/CommandLine.h" 29 #include "llvm/Support/Debug.h" 30 #include "llvm/Support/raw_os_ostream.h" 31 #include "llvm/Transforms/Scalar.h" 32 #include <map> 33 #include <sstream> 34 #include <string> 35 #include <vector> 36 37 #define NVVM_REFLECT_FUNCTION "__nvvm_reflect" 38 39 using namespace llvm; 40 41 #define DEBUG_TYPE "nvptx-reflect" 42 43 namespace llvm { void initializeNVVMReflectPass(PassRegistry &); } 44 45 namespace { 46 class NVVMReflect : public ModulePass { 47 private: 48 StringMap<int> VarMap; 49 typedef DenseMap<std::string, int>::iterator VarMapIter; 50 Function *ReflectFunction; 51 52 public: 53 static char ID; 54 NVVMReflect() : ModulePass(ID), ReflectFunction(nullptr) { 55 initializeNVVMReflectPass(*PassRegistry::getPassRegistry()); 56 VarMap.clear(); 57 } 58 59 NVVMReflect(const StringMap<int> &Mapping) 60 : ModulePass(ID), ReflectFunction(nullptr) { 61 initializeNVVMReflectPass(*PassRegistry::getPassRegistry()); 62 for (StringMap<int>::const_iterator I = Mapping.begin(), E = Mapping.end(); 63 I != E; ++I) { 64 VarMap[(*I).getKey()] = (*I).getValue(); 65 } 66 } 67 68 void getAnalysisUsage(AnalysisUsage &AU) const override { 69 AU.setPreservesAll(); 70 } 71 bool runOnModule(Module &) override; 72 73 void setVarMap(); 74 }; 75 } 76 77 ModulePass *llvm::createNVVMReflectPass() { 78 return new NVVMReflect(); 79 } 80 81 ModulePass *llvm::createNVVMReflectPass(const StringMap<int>& Mapping) { 82 return new NVVMReflect(Mapping); 83 } 84 85 static cl::opt<bool> 86 NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden, 87 cl::desc("NVVM reflection, enabled by default")); 88 89 char NVVMReflect::ID = 0; 90 INITIALIZE_PASS(NVVMReflect, "nvvm-reflect", 91 "Replace occurrences of __nvvm_reflect() calls with 0/1", false, 92 false) 93 94 static cl::list<std::string> 95 ReflectList("nvvm-reflect-list", cl::value_desc("name=<int>"), cl::Hidden, 96 cl::desc("A list of string=num assignments"), 97 cl::ValueRequired); 98 99 /// The command line can look as follows : 100 /// -nvvm-reflect-list a=1,b=2 -nvvm-reflect-list c=3,d=0 -R e=2 101 /// The strings "a=1,b=2", "c=3,d=0", "e=2" are available in the 102 /// ReflectList vector. First, each of ReflectList[i] is 'split' 103 /// using "," as the delimiter. Then each of this part is split 104 /// using "=" as the delimiter. 105 void NVVMReflect::setVarMap() { 106 for (unsigned i = 0, e = ReflectList.size(); i != e; ++i) { 107 DEBUG(dbgs() << "Option : " << ReflectList[i] << "\n"); 108 SmallVector<StringRef, 4> NameValList; 109 StringRef(ReflectList[i]).split(NameValList, ","); 110 for (unsigned j = 0, ej = NameValList.size(); j != ej; ++j) { 111 SmallVector<StringRef, 2> NameValPair; 112 NameValList[j].split(NameValPair, "="); 113 assert(NameValPair.size() == 2 && "name=val expected"); 114 std::stringstream ValStream(NameValPair[1]); 115 int Val; 116 ValStream >> Val; 117 assert((!(ValStream.fail())) && "integer value expected"); 118 VarMap[NameValPair[0]] = Val; 119 } 120 } 121 } 122 123 bool NVVMReflect::runOnModule(Module &M) { 124 if (!NVVMReflectEnabled) 125 return false; 126 127 setVarMap(); 128 129 ReflectFunction = M.getFunction(NVVM_REFLECT_FUNCTION); 130 131 // If reflect function is not used, then there will be 132 // no entry in the module. 133 if (!ReflectFunction) 134 return false; 135 136 // Validate _reflect function 137 assert(ReflectFunction->isDeclaration() && 138 "_reflect function should not have a body"); 139 assert(ReflectFunction->getReturnType()->isIntegerTy() && 140 "_reflect's return type should be integer"); 141 142 std::vector<Instruction *> ToRemove; 143 144 // Go through the uses of ReflectFunction in this Function. 145 // Each of them should a CallInst with a ConstantArray argument. 146 // First validate that. If the c-string corresponding to the 147 // ConstantArray can be found successfully, see if it can be 148 // found in VarMap. If so, replace the uses of CallInst with the 149 // value found in VarMap. If not, replace the use with value 0. 150 for (User *U : ReflectFunction->users()) { 151 assert(isa<CallInst>(U) && "Only a call instruction can use _reflect"); 152 CallInst *Reflect = cast<CallInst>(U); 153 154 assert((Reflect->getNumOperands() == 2) && 155 "Only one operand expect for _reflect function"); 156 // In cuda, we will have an extra constant-to-generic conversion of 157 // the string. 158 const Value *conv = Reflect->getArgOperand(0); 159 assert(isa<CallInst>(conv) && "Expected a const-to-gen conversion"); 160 const CallInst *ConvCall = cast<CallInst>(conv); 161 const Value *str = ConvCall->getArgOperand(0); 162 assert(isa<ConstantExpr>(str) && 163 "Format of _reflect function not recognized"); 164 const ConstantExpr *GEP = cast<ConstantExpr>(str); 165 166 const Value *Sym = GEP->getOperand(0); 167 assert(isa<Constant>(Sym) && "Format of _reflect function not recognized"); 168 169 const Constant *SymStr = cast<Constant>(Sym); 170 171 assert(isa<ConstantDataSequential>(SymStr->getOperand(0)) && 172 "Format of _reflect function not recognized"); 173 174 assert(cast<ConstantDataSequential>(SymStr->getOperand(0))->isCString() && 175 "Format of _reflect function not recognized"); 176 177 std::string ReflectArg = 178 cast<ConstantDataSequential>(SymStr->getOperand(0))->getAsString(); 179 180 ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1); 181 DEBUG(dbgs() << "Arg of _reflect : " << ReflectArg << "\n"); 182 183 int ReflectVal = 0; // The default value is 0 184 if (VarMap.find(ReflectArg) != VarMap.end()) { 185 ReflectVal = VarMap[ReflectArg]; 186 } 187 Reflect->replaceAllUsesWith( 188 ConstantInt::get(Reflect->getType(), ReflectVal)); 189 ToRemove.push_back(Reflect); 190 } 191 if (ToRemove.size() == 0) 192 return false; 193 194 for (unsigned i = 0, e = ToRemove.size(); i != e; ++i) 195 ToRemove[i]->eraseFromParent(); 196 return true; 197 } 198