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