LLVM  3.7.0
NVVMReflect.cpp
Go to the documentation of this file.
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("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.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "NVPTX.h"
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/Intrinsics.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/Type.h"
28 #include "llvm/Pass.h"
30 #include "llvm/Support/Debug.h"
33 #include "llvm/Transforms/Scalar.h"
34 #include <map>
35 #include <sstream>
36 #include <string>
37 #include <vector>
38 
39 #define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
40 
41 using namespace llvm;
42 
43 #define DEBUG_TYPE "nvptx-reflect"
44 
45 namespace llvm { void initializeNVVMReflectPass(PassRegistry &); }
46 
47 namespace {
48 class NVVMReflect : public ModulePass {
49 private:
50  StringMap<int> VarMap;
51  typedef DenseMap<std::string, int>::iterator VarMapIter;
52 
53 public:
54  static char ID;
55  NVVMReflect() : ModulePass(ID) {
57  VarMap.clear();
58  }
59 
60  NVVMReflect(const StringMap<int> &Mapping)
61  : ModulePass(ID) {
63  for (StringMap<int>::const_iterator I = Mapping.begin(), E = Mapping.end();
64  I != E; ++I) {
65  VarMap[(*I).getKey()] = (*I).getValue();
66  }
67  }
68 
69  void getAnalysisUsage(AnalysisUsage &AU) const override {
70  AU.setPreservesAll();
71  }
72  bool runOnModule(Module &) override;
73 
74 private:
75  bool handleFunction(Function *ReflectFunction);
76  void setVarMap();
77 };
78 }
79 
81  return new NVVMReflect();
82 }
83 
85  return new NVVMReflect(Mapping);
86 }
87 
88 static cl::opt<bool>
89 NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden,
90  cl::desc("NVVM reflection, enabled by default"));
91 
92 char NVVMReflect::ID = 0;
93 INITIALIZE_PASS(NVVMReflect, "nvvm-reflect",
94  "Replace occurrences of __nvvm_reflect() calls with 0/1", false,
95  false)
96 
97 static cl::list<std::string>
98 ReflectList("nvvm-reflect-list", cl::value_desc("name=<int>"), cl::Hidden,
99  cl::desc("A list of string=num assignments"),
100  cl::ValueRequired);
101 
102 /// The command line can look as follows :
103 /// -nvvm-reflect-list a=1,b=2 -nvvm-reflect-list c=3,d=0 -R e=2
104 /// The strings "a=1,b=2", "c=3,d=0", "e=2" are available in the
105 /// ReflectList vector. First, each of ReflectList[i] is 'split'
106 /// using "," as the delimiter. Then each of this part is split
107 /// using "=" as the delimiter.
108 void NVVMReflect::setVarMap() {
109  for (unsigned i = 0, e = ReflectList.size(); i != e; ++i) {
110  DEBUG(dbgs() << "Option : " << ReflectList[i] << "\n");
111  SmallVector<StringRef, 4> NameValList;
112  StringRef(ReflectList[i]).split(NameValList, ",");
113  for (unsigned j = 0, ej = NameValList.size(); j != ej; ++j) {
114  SmallVector<StringRef, 2> NameValPair;
115  NameValList[j].split(NameValPair, "=");
116  assert(NameValPair.size() == 2 && "name=val expected");
117  std::stringstream ValStream(NameValPair[1]);
118  int Val;
119  ValStream >> Val;
120  assert((!(ValStream.fail())) && "integer value expected");
121  VarMap[NameValPair[0]] = Val;
122  }
123  }
124 }
125 
126 bool NVVMReflect::handleFunction(Function *ReflectFunction) {
127  // Validate _reflect function
128  assert(ReflectFunction->isDeclaration() &&
129  "_reflect function should not have a body");
130  assert(ReflectFunction->getReturnType()->isIntegerTy() &&
131  "_reflect's return type should be integer");
132 
133  std::vector<Instruction *> ToRemove;
134 
135  // Go through the uses of ReflectFunction in this Function.
136  // Each of them should a CallInst with a ConstantArray argument.
137  // First validate that. If the c-string corresponding to the
138  // ConstantArray can be found successfully, see if it can be
139  // found in VarMap. If so, replace the uses of CallInst with the
140  // value found in VarMap. If not, replace the use with value 0.
141 
142  // IR for __nvvm_reflect calls differs between CUDA versions:
143  // CUDA 6.5 and earlier uses this sequence:
144  // %ptr = tail call i8* @llvm.nvvm.ptr.constant.to.gen.p0i8.p4i8
145  // (i8 addrspace(4)* getelementptr inbounds
146  // ([8 x i8], [8 x i8] addrspace(4)* @str, i32 0, i32 0))
147  // %reflect = tail call i32 @__nvvm_reflect(i8* %ptr)
148  //
149  // Value returned by Sym->getOperand(0) is a Constant with a
150  // ConstantDataSequential operand which can be converted to string and used
151  // for lookup.
152  //
153  // CUDA 7.0 does it slightly differently:
154  // %reflect = call i32 @__nvvm_reflect(i8* addrspacecast
155  // (i8 addrspace(1)* getelementptr inbounds
156  // ([8 x i8], [8 x i8] addrspace(1)* @str, i32 0, i32 0) to i8*))
157  //
158  // In this case, we get a Constant with a GlobalVariable operand and we need
159  // to dig deeper to find its initializer with the string we'll use for lookup.
160 
161  for (User *U : ReflectFunction->users()) {
162  assert(isa<CallInst>(U) && "Only a call instruction can use _reflect");
163  CallInst *Reflect = cast<CallInst>(U);
164 
165  assert((Reflect->getNumOperands() == 2) &&
166  "Only one operand expect for _reflect function");
167  // In cuda, we will have an extra constant-to-generic conversion of
168  // the string.
169  const Value *Str = Reflect->getArgOperand(0);
170  if (isa<CallInst>(Str)) {
171  // CUDA path
172  const CallInst *ConvCall = cast<CallInst>(Str);
173  Str = ConvCall->getArgOperand(0);
174  }
175  assert(isa<ConstantExpr>(Str) &&
176  "Format of _reflect function not recognized");
177  const ConstantExpr *GEP = cast<ConstantExpr>(Str);
178 
179  const Value *Sym = GEP->getOperand(0);
180  assert(isa<Constant>(Sym) && "Format of _reflect function not recognized");
181 
182  const Value *Operand = cast<Constant>(Sym)->getOperand(0);
183  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) {
184  // For CUDA-7.0 style __nvvm_reflect calls we need to find operand's
185  // initializer.
186  assert(GV->hasInitializer() &&
187  "Format of _reflect function not recognized");
188  const Constant *Initializer = GV->getInitializer();
189  Operand = Initializer;
190  }
191 
192  assert(isa<ConstantDataSequential>(Operand) &&
193  "Format of _reflect function not recognized");
194  assert(cast<ConstantDataSequential>(Operand)->isCString() &&
195  "Format of _reflect function not recognized");
196 
197  std::string ReflectArg =
198  cast<ConstantDataSequential>(Operand)->getAsString();
199 
200  ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1);
201  DEBUG(dbgs() << "Arg of _reflect : " << ReflectArg << "\n");
202 
203  int ReflectVal = 0; // The default value is 0
204  if (VarMap.find(ReflectArg) != VarMap.end()) {
205  ReflectVal = VarMap[ReflectArg];
206  }
207  Reflect->replaceAllUsesWith(
208  ConstantInt::get(Reflect->getType(), ReflectVal));
209  ToRemove.push_back(Reflect);
210  }
211  if (ToRemove.size() == 0)
212  return false;
213 
214  for (unsigned i = 0, e = ToRemove.size(); i != e; ++i)
215  ToRemove[i]->eraseFromParent();
216  return true;
217 }
218 
219 bool NVVMReflect::runOnModule(Module &M) {
220  if (!NVVMReflectEnabled)
221  return false;
222 
223  setVarMap();
224 
225 
226  bool Res = false;
227  std::string Name;
228  Type *Tys[1];
229  Type *I8Ty = Type::getInt8Ty(M.getContext());
230  Function *ReflectFunction;
231 
232  // Check for standard overloaded versions of llvm.nvvm.reflect
233 
234  for (unsigned i = 0; i != 5; ++i) {
235  Tys[0] = PointerType::get(I8Ty, i);
236  Name = Intrinsic::getName(Intrinsic::nvvm_reflect, Tys);
237  ReflectFunction = M.getFunction(Name);
238  if(ReflectFunction != 0) {
239  Res |= handleFunction(ReflectFunction);
240  }
241  }
242 
243  ReflectFunction = M.getFunction(NVVM_REFLECT_FUNCTION);
244  // If reflect function is not used, then there will be
245  // no entry in the module.
246  if (ReflectFunction != 0)
247  Res |= handleFunction(ReflectFunction);
248 
249  return Res;
250 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
unsigned getNumOperands() const
Definition: User.h:138
CallInst - This class represents a function call, abstracting a target machine's calling convention...
static PointerType * get(Type *ElementType, unsigned AddressSpace)
PointerType::get - This constructs a pointer to an object of the specified type in a numbered address...
Definition: Type.cpp:738
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:450
Type * getReturnType() const
Definition: Function.cpp:233
Hexagon Common GEP
static cl::opt< bool > NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden, cl::desc("NVVM reflection, enabled by default"))
ConstantExpr - a constant value that is initialized with an expression using other constant values...
Definition: Constants.h:852
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:188
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:325
void initializeNVVMReflectPass(PassRegistry &)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Represent the analysis usage information of a pass.
#define NVVM_REFLECT_FUNCTION
Definition: NVVMReflect.cpp:39
Value * getOperand(unsigned i) const
Definition: User.h:118
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
ModulePass * createNVVMReflectPass()
Definition: NVVMReflect.cpp:80
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:582
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:214
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
Definition: Type.h:193
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator_range< user_iterator > users()
Definition: Value.h:300
std::string getName(ID id, ArrayRef< Type * > Tys=None)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:500
iterator begin()
Definition: StringMap.h:252
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:128
#define I(x, y, z)
Definition: MD5.cpp:54
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
LLVM Value Representation.
Definition: Value.h:69
static const char * name
#define DEBUG(X)
Definition: Debug.h:92
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:41
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:237
iterator end()
Definition: StringMap.h:255
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:265