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("foo") and llvm.nvvm.reflect
11 // with an integer.
12 //
13 // We choose the value we use by looking, in this order, at:
14 //
15 //  * the -nvvm-reflect-list flag, which has the format "foo=1,bar=42",
16 //  * the StringMap passed to the pass's constructor, and
17 //  * metadata in the module itself.
18 //
19 // If we see an unknown string, we replace its call with 0.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 #include "NVPTX.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringMap.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/InstIterator.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/Intrinsics.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/Type.h"
34 #include "llvm/Pass.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/raw_os_ostream.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/Transforms/Scalar.h"
40 #include <sstream>
41 #include <string>
42 #define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
43 
44 using namespace llvm;
45 
46 #define DEBUG_TYPE "nvptx-reflect"
47 
48 namespace llvm { void initializeNVVMReflectPass(PassRegistry &); }
49 
50 namespace {
51 class NVVMReflect : public FunctionPass {
52 private:
53   StringMap<int> VarMap;
54 
55 public:
56   static char ID;
57   NVVMReflect() : NVVMReflect(StringMap<int>()) {}
58 
59   NVVMReflect(const StringMap<int> &Mapping)
60       : FunctionPass(ID), VarMap(Mapping) {
61     initializeNVVMReflectPass(*PassRegistry::getPassRegistry());
62     setVarMap();
63   }
64 
65   void getAnalysisUsage(AnalysisUsage &AU) const override {
66     AU.setPreservesAll();
67   }
68   bool runOnFunction(Function &) override;
69 
70 private:
71   bool handleFunction(Function *ReflectFunction);
72   void setVarMap();
73 };
74 }
75 
76 FunctionPass *llvm::createNVVMReflectPass() { return new NVVMReflect(); }
77 FunctionPass *llvm::createNVVMReflectPass(const StringMap<int> &Mapping) {
78   return new NVVMReflect(Mapping);
79 }
80 
81 static cl::opt<bool>
82 NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden,
83                    cl::desc("NVVM reflection, enabled by default"));
84 
85 char NVVMReflect::ID = 0;
86 INITIALIZE_PASS(NVVMReflect, "nvvm-reflect",
87                 "Replace occurrences of __nvvm_reflect() calls with 0/1", false,
88                 false)
89 
90 static cl::list<std::string>
91 ReflectList("nvvm-reflect-list", cl::value_desc("name=<int>"), cl::Hidden,
92             cl::desc("A list of string=num assignments"),
93             cl::ValueRequired);
94 
95 /// The command line can look as follows :
96 /// -nvvm-reflect-list a=1,b=2 -nvvm-reflect-list c=3,d=0 -R e=2
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) {
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;
115     }
116   }
117 }
118 
119 bool NVVMReflect::runOnFunction(Function &F) {
120   if (!NVVMReflectEnabled)
121     return false;
122 
123   if (F.getName() == NVVM_REFLECT_FUNCTION) {
124     assert(F.isDeclaration() && "_reflect function should not have a body");
125     assert(F.getReturnType()->isIntegerTy() &&
126            "_reflect's return type should be integer");
127     return false;
128   }
129 
130   SmallVector<Instruction *, 4> ToRemove;
131 
132   // Go through the calls in this function.  Each call to __nvvm_reflect or
133   // llvm.nvvm.reflect should be a CallInst with a ConstantArray argument.
134   // First validate that. If the c-string corresponding to the ConstantArray can
135   // be found successfully, see if it can be found in VarMap. If so, replace the
136   // uses of CallInst with the value found in VarMap. If not, replace the use
137   // with value 0.
138 
139   // The IR for __nvvm_reflect calls differs between CUDA versions.
140   //
141   // CUDA 6.5 and earlier uses this sequence:
142   //    %ptr = tail call i8* @llvm.nvvm.ptr.constant.to.gen.p0i8.p4i8
143   //        (i8 addrspace(4)* getelementptr inbounds
144   //           ([8 x i8], [8 x i8] addrspace(4)* @str, i32 0, i32 0))
145   //    %reflect = tail call i32 @__nvvm_reflect(i8* %ptr)
146   //
147   // The value returned by Sym->getOperand(0) is a Constant with a
148   // ConstantDataSequential operand which can be converted to string and used
149   // for lookup.
150   //
151   // CUDA 7.0 does it slightly differently:
152   //   %reflect = call i32 @__nvvm_reflect(i8* addrspacecast
153   //        (i8 addrspace(1)* getelementptr inbounds
154   //           ([8 x i8], [8 x i8] addrspace(1)* @str, i32 0, i32 0) to i8*))
155   //
156   // In this case, we get a Constant with a GlobalVariable operand and we need
157   // to dig deeper to find its initializer with the string we'll use for lookup.
158   for (Instruction &I : instructions(F)) {
159     CallInst *Call = dyn_cast<CallInst>(&I);
160     if (!Call)
161       continue;
162     Function *Callee = Call->getCalledFunction();
163     if (!Callee || (Callee->getName() != NVVM_REFLECT_FUNCTION &&
164                     Callee->getIntrinsicID() != Intrinsic::nvvm_reflect))
165       continue;
166 
167     // FIXME: Improve error handling here and elsewhere in this pass.
168     assert(Call->getNumOperands() == 2 &&
169            "Wrong number of operands to __nvvm_reflect function");
170 
171     // In cuda 6.5 and earlier, we will have an extra constant-to-generic
172     // conversion of the string.
173     const Value *Str = Call->getArgOperand(0);
174     if (const CallInst *ConvCall = dyn_cast<CallInst>(Str)) {
175       // FIXME: Add assertions about ConvCall.
176       Str = ConvCall->getArgOperand(0);
177     }
178     assert(isa<ConstantExpr>(Str) &&
179            "Format of __nvvm__reflect function not recognized");
180     const ConstantExpr *GEP = cast<ConstantExpr>(Str);
181 
182     const Value *Sym = GEP->getOperand(0);
183     assert(isa<Constant>(Sym) &&
184            "Format of __nvvm_reflect function not recognized");
185 
186     const Value *Operand = cast<Constant>(Sym)->getOperand(0);
187     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) {
188       // For CUDA-7.0 style __nvvm_reflect calls, we need to find the operand's
189       // initializer.
190       assert(GV->hasInitializer() &&
191              "Format of _reflect function not recognized");
192       const Constant *Initializer = GV->getInitializer();
193       Operand = Initializer;
194     }
195 
196     assert(isa<ConstantDataSequential>(Operand) &&
197            "Format of _reflect function not recognized");
198     assert(cast<ConstantDataSequential>(Operand)->isCString() &&
199            "Format of _reflect function not recognized");
200 
201     StringRef ReflectArg = cast<ConstantDataSequential>(Operand)->getAsString();
202     ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1);
203     DEBUG(dbgs() << "Arg of _reflect : " << ReflectArg << "\n");
204 
205     int ReflectVal = 0; // The default value is 0
206     auto Iter = VarMap.find(ReflectArg);
207     if (Iter != VarMap.end())
208       ReflectVal = Iter->second;
209     else if (ReflectArg == "__CUDA_FTZ") {
210       // Try to pull __CUDA_FTZ from the nvvm-reflect-ftz module flag.
211       if (auto *Flag = mdconst::extract_or_null<ConstantInt>(
212               F.getParent()->getModuleFlag("nvvm-reflect-ftz")))
213         ReflectVal = Flag->getSExtValue();
214     }
215     Call->replaceAllUsesWith(ConstantInt::get(Call->getType(), ReflectVal));
216     ToRemove.push_back(Call);
217   }
218 
219   for (Instruction *I : ToRemove)
220     I->eraseFromParent();
221 
222   return ToRemove.size() > 0;
223 }
224