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