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