1 //===- NVVMReflect.cpp - NVVM Emulate conditional compilation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
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"
37 #define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
41 #define DEBUG_TYPE "nvptx-reflect"
43 namespace llvm { void initializeNVVMReflectPass(PassRegistry &); }
46 class NVVMReflect : public ModulePass {
48 StringMap<int> VarMap;
49 typedef DenseMap<std::string, int>::iterator VarMapIter;
50 Function *ReflectFunction;
54 NVVMReflect() : ModulePass(ID), ReflectFunction(0) {
55 initializeNVVMReflectPass(*PassRegistry::getPassRegistry());
59 NVVMReflect(const StringMap<int> &Mapping)
60 : ModulePass(ID), ReflectFunction(0) {
61 initializeNVVMReflectPass(*PassRegistry::getPassRegistry());
62 for (StringMap<int>::const_iterator I = Mapping.begin(), E = Mapping.end();
64 VarMap[(*I).getKey()] = (*I).getValue();
68 void getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); }
69 virtual bool runOnModule(Module &);
75 ModulePass *llvm::createNVVMReflectPass() {
76 return new NVVMReflect();
79 ModulePass *llvm::createNVVMReflectPass(const StringMap<int>& Mapping) {
80 return new NVVMReflect(Mapping);
84 NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden,
85 cl::desc("NVVM reflection, enabled by default"));
87 char NVVMReflect::ID = 0;
88 INITIALIZE_PASS(NVVMReflect, "nvvm-reflect",
89 "Replace occurrences of __nvvm_reflect() calls with 0/1", false,
92 static cl::list<std::string>
93 ReflectList("nvvm-reflect-list", cl::value_desc("name=<int>"), cl::Hidden,
94 cl::desc("A list of string=num assignments"),
97 /// The command line can look as follows :
98 /// -nvvm-reflect-list a=1,b=2 -nvvm-reflect-list c=3,d=0 -R e=2
99 /// The strings "a=1,b=2", "c=3,d=0", "e=2" are available in the
100 /// ReflectList vector. First, each of ReflectList[i] is 'split'
101 /// using "," as the delimiter. Then each of this part is split
102 /// using "=" as the delimiter.
103 void NVVMReflect::setVarMap() {
104 for (unsigned i = 0, e = ReflectList.size(); i != e; ++i) {
105 DEBUG(dbgs() << "Option : " << ReflectList[i] << "\n");
106 SmallVector<StringRef, 4> NameValList;
107 StringRef(ReflectList[i]).split(NameValList, ",");
108 for (unsigned j = 0, ej = NameValList.size(); j != ej; ++j) {
109 SmallVector<StringRef, 2> NameValPair;
110 NameValList[j].split(NameValPair, "=");
111 assert(NameValPair.size() == 2 && "name=val expected");
112 std::stringstream ValStream(NameValPair[1]);
115 assert((!(ValStream.fail())) && "integer value expected");
116 VarMap[NameValPair[0]] = Val;
121 bool NVVMReflect::runOnModule(Module &M) {
122 if (!NVVMReflectEnabled)
127 ReflectFunction = M.getFunction(NVVM_REFLECT_FUNCTION);
129 // If reflect function is not used, then there will be
130 // no entry in the module.
131 if (ReflectFunction == 0)
134 // Validate _reflect function
135 assert(ReflectFunction->isDeclaration() &&
136 "_reflect function should not have a body");
137 assert(ReflectFunction->getReturnType()->isIntegerTy() &&
138 "_reflect's return type should be integer");
140 std::vector<Instruction *> ToRemove;
142 // Go through the uses of ReflectFunction in this Function.
143 // Each of them should a CallInst with a ConstantArray argument.
144 // First validate that. If the c-string corresponding to the
145 // ConstantArray can be found successfully, see if it can be
146 // found in VarMap. If so, replace the uses of CallInst with the
147 // value found in VarMap. If not, replace the use with value 0.
148 for (User *U : ReflectFunction->users()) {
149 assert(isa<CallInst>(U) && "Only a call instruction can use _reflect");
150 CallInst *Reflect = cast<CallInst>(U);
152 assert((Reflect->getNumOperands() == 2) &&
153 "Only one operand expect for _reflect function");
154 // In cuda, we will have an extra constant-to-generic conversion of
156 const Value *conv = Reflect->getArgOperand(0);
157 assert(isa<CallInst>(conv) && "Expected a const-to-gen conversion");
158 const CallInst *ConvCall = cast<CallInst>(conv);
159 const Value *str = ConvCall->getArgOperand(0);
160 assert(isa<ConstantExpr>(str) &&
161 "Format of _reflect function not recognized");
162 const ConstantExpr *GEP = cast<ConstantExpr>(str);
164 const Value *Sym = GEP->getOperand(0);
165 assert(isa<Constant>(Sym) && "Format of _reflect function not recognized");
167 const Constant *SymStr = cast<Constant>(Sym);
169 assert(isa<ConstantDataSequential>(SymStr->getOperand(0)) &&
170 "Format of _reflect function not recognized");
172 assert(cast<ConstantDataSequential>(SymStr->getOperand(0))->isCString() &&
173 "Format of _reflect function not recognized");
175 std::string ReflectArg =
176 cast<ConstantDataSequential>(SymStr->getOperand(0))->getAsString();
178 ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1);
179 DEBUG(dbgs() << "Arg of _reflect : " << ReflectArg << "\n");
181 int ReflectVal = 0; // The default value is 0
182 if (VarMap.find(ReflectArg) != VarMap.end()) {
183 ReflectVal = VarMap[ReflectArg];
185 Reflect->replaceAllUsesWith(
186 ConstantInt::get(Reflect->getType(), ReflectVal));
187 ToRemove.push_back(Reflect);
189 if (ToRemove.size() == 0)
192 for (unsigned i = 0, e = ToRemove.size(); i != e; ++i)
193 ToRemove[i]->eraseFromParent();