• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
NVVMReflect()57   NVVMReflect() : NVVMReflect(StringMap<int>()) {}
58 
NVVMReflect(const StringMap<int> & Mapping)59   NVVMReflect(const StringMap<int> &Mapping)
60       : FunctionPass(ID), VarMap(Mapping) {
61     initializeNVVMReflectPass(*PassRegistry::getPassRegistry());
62     setVarMap();
63   }
64 
65   bool runOnFunction(Function &) override;
66 
67 private:
68   bool handleFunction(Function *ReflectFunction);
69   void setVarMap();
70 };
71 }
72 
createNVVMReflectPass()73 FunctionPass *llvm::createNVVMReflectPass() { return new NVVMReflect(); }
createNVVMReflectPass(const StringMap<int> & Mapping)74 FunctionPass *llvm::createNVVMReflectPass(const StringMap<int> &Mapping) {
75   return new NVVMReflect(Mapping);
76 }
77 
78 static cl::opt<bool>
79 NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden,
80                    cl::desc("NVVM reflection, enabled by default"));
81 
82 char NVVMReflect::ID = 0;
83 INITIALIZE_PASS(NVVMReflect, "nvvm-reflect",
84                 "Replace occurrences of __nvvm_reflect() calls with 0/1", false,
85                 false)
86 
87 static cl::list<std::string>
88 ReflectList("nvvm-reflect-list", cl::value_desc("name=<int>"), cl::Hidden,
89             cl::desc("A list of string=num assignments"),
90             cl::ValueRequired);
91 
92 /// The command line can look as follows :
93 /// -nvvm-reflect-list a=1,b=2 -nvvm-reflect-list c=3,d=0 -R e=2
94 /// The strings "a=1,b=2", "c=3,d=0", "e=2" are available in the
95 /// ReflectList vector. First, each of ReflectList[i] is 'split'
96 /// using "," as the delimiter. Then each of this part is split
97 /// using "=" as the delimiter.
setVarMap()98 void NVVMReflect::setVarMap() {
99   for (unsigned i = 0, e = ReflectList.size(); i != e; ++i) {
100     DEBUG(dbgs() << "Option : "  << ReflectList[i] << "\n");
101     SmallVector<StringRef, 4> NameValList;
102     StringRef(ReflectList[i]).split(NameValList, ',');
103     for (unsigned j = 0, ej = NameValList.size(); j != ej; ++j) {
104       SmallVector<StringRef, 2> NameValPair;
105       NameValList[j].split(NameValPair, '=');
106       assert(NameValPair.size() == 2 && "name=val expected");
107       std::stringstream ValStream(NameValPair[1]);
108       int Val;
109       ValStream >> Val;
110       assert((!(ValStream.fail())) && "integer value expected");
111       VarMap[NameValPair[0]] = Val;
112     }
113   }
114 }
115 
runOnFunction(Function & F)116 bool NVVMReflect::runOnFunction(Function &F) {
117   if (!NVVMReflectEnabled)
118     return false;
119 
120   if (F.getName() == NVVM_REFLECT_FUNCTION) {
121     assert(F.isDeclaration() && "_reflect function should not have a body");
122     assert(F.getReturnType()->isIntegerTy() &&
123            "_reflect's return type should be integer");
124     return false;
125   }
126 
127   SmallVector<Instruction *, 4> ToRemove;
128 
129   // Go through the calls in this function.  Each call to __nvvm_reflect or
130   // llvm.nvvm.reflect should be a CallInst with a ConstantArray argument.
131   // First validate that. If the c-string corresponding to the ConstantArray can
132   // be found successfully, see if it can be found in VarMap. If so, replace the
133   // uses of CallInst with the value found in VarMap. If not, replace the use
134   // with value 0.
135 
136   // The IR for __nvvm_reflect calls differs between CUDA versions.
137   //
138   // CUDA 6.5 and earlier uses this sequence:
139   //    %ptr = tail call i8* @llvm.nvvm.ptr.constant.to.gen.p0i8.p4i8
140   //        (i8 addrspace(4)* getelementptr inbounds
141   //           ([8 x i8], [8 x i8] addrspace(4)* @str, i32 0, i32 0))
142   //    %reflect = tail call i32 @__nvvm_reflect(i8* %ptr)
143   //
144   // The value returned by Sym->getOperand(0) is a Constant with a
145   // ConstantDataSequential operand which can be converted to string and used
146   // for lookup.
147   //
148   // CUDA 7.0 does it slightly differently:
149   //   %reflect = call i32 @__nvvm_reflect(i8* addrspacecast
150   //        (i8 addrspace(1)* getelementptr inbounds
151   //           ([8 x i8], [8 x i8] addrspace(1)* @str, i32 0, i32 0) to i8*))
152   //
153   // In this case, we get a Constant with a GlobalVariable operand and we need
154   // to dig deeper to find its initializer with the string we'll use for lookup.
155   for (Instruction &I : instructions(F)) {
156     CallInst *Call = dyn_cast<CallInst>(&I);
157     if (!Call)
158       continue;
159     Function *Callee = Call->getCalledFunction();
160     if (!Callee || (Callee->getName() != NVVM_REFLECT_FUNCTION &&
161                     Callee->getIntrinsicID() != Intrinsic::nvvm_reflect))
162       continue;
163 
164     // FIXME: Improve error handling here and elsewhere in this pass.
165     assert(Call->getNumOperands() == 2 &&
166            "Wrong number of operands to __nvvm_reflect function");
167 
168     // In cuda 6.5 and earlier, we will have an extra constant-to-generic
169     // conversion of the string.
170     const Value *Str = Call->getArgOperand(0);
171     if (const CallInst *ConvCall = dyn_cast<CallInst>(Str)) {
172       // FIXME: Add assertions about ConvCall.
173       Str = ConvCall->getArgOperand(0);
174     }
175     assert(isa<ConstantExpr>(Str) &&
176            "Format of __nvvm__reflect function not recognized");
177     const ConstantExpr *GEP = cast<ConstantExpr>(Str);
178 
179     const Value *Sym = GEP->getOperand(0);
180     assert(isa<Constant>(Sym) &&
181            "Format of __nvvm_reflect function not recognized");
182 
183     const Value *Operand = cast<Constant>(Sym)->getOperand(0);
184     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) {
185       // For CUDA-7.0 style __nvvm_reflect calls, we need to find the operand's
186       // initializer.
187       assert(GV->hasInitializer() &&
188              "Format of _reflect function not recognized");
189       const Constant *Initializer = GV->getInitializer();
190       Operand = Initializer;
191     }
192 
193     assert(isa<ConstantDataSequential>(Operand) &&
194            "Format of _reflect function not recognized");
195     assert(cast<ConstantDataSequential>(Operand)->isCString() &&
196            "Format of _reflect function not recognized");
197 
198     StringRef ReflectArg = cast<ConstantDataSequential>(Operand)->getAsString();
199     ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1);
200     DEBUG(dbgs() << "Arg of _reflect : " << ReflectArg << "\n");
201 
202     int ReflectVal = 0; // The default value is 0
203     auto Iter = VarMap.find(ReflectArg);
204     if (Iter != VarMap.end())
205       ReflectVal = Iter->second;
206     else if (ReflectArg == "__CUDA_FTZ") {
207       // Try to pull __CUDA_FTZ from the nvvm-reflect-ftz module flag.
208       if (auto *Flag = mdconst::extract_or_null<ConstantInt>(
209               F.getParent()->getModuleFlag("nvvm-reflect-ftz")))
210         ReflectVal = Flag->getSExtValue();
211     }
212     Call->replaceAllUsesWith(ConstantInt::get(Call->getType(), ReflectVal));
213     ToRemove.push_back(Call);
214   }
215 
216   for (Instruction *I : ToRemove)
217     I->eraseFromParent();
218 
219   return ToRemove.size() > 0;
220 }
221