0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===- NVVMReflect.cpp - NVVM Emulate conditional compilation -------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 // The LLVM Compiler Infrastructure
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 // This file is distributed under the University of Illinois Open Source
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 // License. See LICENSE.TXT for details.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 //
|
120
|
10 // This pass replaces occurrences of __nvvm_reflect("foo") and llvm.nvvm.reflect
|
|
11 // with an integer.
|
|
12 //
|
121
|
13 // We choose the value we use by looking at metadata in the module itself. Note
|
|
14 // that we intentionally only have one way to choose these values, because other
|
|
15 // parts of LLVM (particularly, InstCombineCall) rely on being able to predict
|
|
16 // the values chosen by this pass.
|
120
|
17 //
|
|
18 // If we see an unknown string, we replace its call with 0.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 #include "NVPTX.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 #include "llvm/ADT/SmallVector.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 #include "llvm/ADT/StringMap.h"
|
77
|
25 #include "llvm/IR/Constants.h"
|
|
26 #include "llvm/IR/DerivedTypes.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 #include "llvm/IR/Function.h"
|
120
|
28 #include "llvm/IR/InstIterator.h"
|
77
|
29 #include "llvm/IR/Instructions.h"
|
|
30 #include "llvm/IR/Intrinsics.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 #include "llvm/IR/Module.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 #include "llvm/IR/Type.h"
|
77
|
33 #include "llvm/Pass.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 #include "llvm/Support/CommandLine.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 #include "llvm/Support/Debug.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 #include "llvm/Support/raw_os_ostream.h"
|
95
|
37 #include "llvm/Support/raw_ostream.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 #include "llvm/Transforms/Scalar.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 #include <sstream>
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 #include <string>
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 #define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44
|
77
|
45 #define DEBUG_TYPE "nvptx-reflect"
|
|
46
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 namespace llvm { void initializeNVVMReflectPass(PassRegistry &); }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 namespace {
|
120
|
50 class NVVMReflect : public FunctionPass {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 public:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 static char ID;
|
121
|
53 NVVMReflect() : FunctionPass(ID) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 initializeNVVMReflectPass(*PassRegistry::getPassRegistry());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56
|
120
|
57 bool runOnFunction(Function &) override;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 };
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60
|
120
|
61 FunctionPass *llvm::createNVVMReflectPass() { return new NVVMReflect(); }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
63 static cl::opt<bool>
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
64 NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65 cl::desc("NVVM reflection, enabled by default"));
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
67 char NVVMReflect::ID = 0;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 INITIALIZE_PASS(NVVMReflect, "nvvm-reflect",
|
77
|
69 "Replace occurrences of __nvvm_reflect() calls with 0/1", false,
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 false)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
71
|
120
|
72 bool NVVMReflect::runOnFunction(Function &F) {
|
|
73 if (!NVVMReflectEnabled)
|
|
74 return false;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75
|
120
|
76 if (F.getName() == NVVM_REFLECT_FUNCTION) {
|
|
77 assert(F.isDeclaration() && "_reflect function should not have a body");
|
|
78 assert(F.getReturnType()->isIntegerTy() &&
|
|
79 "_reflect's return type should be integer");
|
|
80 return false;
|
|
81 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82
|
120
|
83 SmallVector<Instruction *, 4> ToRemove;
|
95
|
84
|
120
|
85 // Go through the calls in this function. Each call to __nvvm_reflect or
|
|
86 // llvm.nvvm.reflect should be a CallInst with a ConstantArray argument.
|
|
87 // First validate that. If the c-string corresponding to the ConstantArray can
|
|
88 // be found successfully, see if it can be found in VarMap. If so, replace the
|
|
89 // uses of CallInst with the value found in VarMap. If not, replace the use
|
|
90 // with value 0.
|
|
91
|
|
92 // The IR for __nvvm_reflect calls differs between CUDA versions.
|
|
93 //
|
95
|
94 // CUDA 6.5 and earlier uses this sequence:
|
|
95 // %ptr = tail call i8* @llvm.nvvm.ptr.constant.to.gen.p0i8.p4i8
|
|
96 // (i8 addrspace(4)* getelementptr inbounds
|
|
97 // ([8 x i8], [8 x i8] addrspace(4)* @str, i32 0, i32 0))
|
|
98 // %reflect = tail call i32 @__nvvm_reflect(i8* %ptr)
|
|
99 //
|
120
|
100 // The value returned by Sym->getOperand(0) is a Constant with a
|
95
|
101 // ConstantDataSequential operand which can be converted to string and used
|
|
102 // for lookup.
|
|
103 //
|
|
104 // CUDA 7.0 does it slightly differently:
|
|
105 // %reflect = call i32 @__nvvm_reflect(i8* addrspacecast
|
|
106 // (i8 addrspace(1)* getelementptr inbounds
|
|
107 // ([8 x i8], [8 x i8] addrspace(1)* @str, i32 0, i32 0) to i8*))
|
|
108 //
|
|
109 // In this case, we get a Constant with a GlobalVariable operand and we need
|
|
110 // to dig deeper to find its initializer with the string we'll use for lookup.
|
120
|
111 for (Instruction &I : instructions(F)) {
|
|
112 CallInst *Call = dyn_cast<CallInst>(&I);
|
|
113 if (!Call)
|
|
114 continue;
|
|
115 Function *Callee = Call->getCalledFunction();
|
|
116 if (!Callee || (Callee->getName() != NVVM_REFLECT_FUNCTION &&
|
|
117 Callee->getIntrinsicID() != Intrinsic::nvvm_reflect))
|
|
118 continue;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
119
|
120
|
120 // FIXME: Improve error handling here and elsewhere in this pass.
|
|
121 assert(Call->getNumOperands() == 2 &&
|
|
122 "Wrong number of operands to __nvvm_reflect function");
|
|
123
|
|
124 // In cuda 6.5 and earlier, we will have an extra constant-to-generic
|
|
125 // conversion of the string.
|
|
126 const Value *Str = Call->getArgOperand(0);
|
|
127 if (const CallInst *ConvCall = dyn_cast<CallInst>(Str)) {
|
|
128 // FIXME: Add assertions about ConvCall.
|
77
|
129 Str = ConvCall->getArgOperand(0);
|
|
130 }
|
|
131 assert(isa<ConstantExpr>(Str) &&
|
120
|
132 "Format of __nvvm__reflect function not recognized");
|
77
|
133 const ConstantExpr *GEP = cast<ConstantExpr>(Str);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
134
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
135 const Value *Sym = GEP->getOperand(0);
|
120
|
136 assert(isa<Constant>(Sym) &&
|
|
137 "Format of __nvvm_reflect function not recognized");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
138
|
95
|
139 const Value *Operand = cast<Constant>(Sym)->getOperand(0);
|
|
140 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) {
|
120
|
141 // For CUDA-7.0 style __nvvm_reflect calls, we need to find the operand's
|
95
|
142 // initializer.
|
|
143 assert(GV->hasInitializer() &&
|
|
144 "Format of _reflect function not recognized");
|
|
145 const Constant *Initializer = GV->getInitializer();
|
|
146 Operand = Initializer;
|
|
147 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
148
|
95
|
149 assert(isa<ConstantDataSequential>(Operand) &&
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
150 "Format of _reflect function not recognized");
|
95
|
151 assert(cast<ConstantDataSequential>(Operand)->isCString() &&
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
152 "Format of _reflect function not recognized");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
153
|
120
|
154 StringRef ReflectArg = cast<ConstantDataSequential>(Operand)->getAsString();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
155 ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
156 DEBUG(dbgs() << "Arg of _reflect : " << ReflectArg << "\n");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
157
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
158 int ReflectVal = 0; // The default value is 0
|
121
|
159 if (ReflectArg == "__CUDA_FTZ") {
|
|
160 // Try to pull __CUDA_FTZ from the nvvm-reflect-ftz module flag. Our
|
|
161 // choice here must be kept in sync with AutoUpgrade, which uses the same
|
|
162 // technique to detect whether ftz is enabled.
|
120
|
163 if (auto *Flag = mdconst::extract_or_null<ConstantInt>(
|
|
164 F.getParent()->getModuleFlag("nvvm-reflect-ftz")))
|
|
165 ReflectVal = Flag->getSExtValue();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
166 }
|
120
|
167 Call->replaceAllUsesWith(ConstantInt::get(Call->getType(), ReflectVal));
|
|
168 ToRemove.push_back(Call);
|
77
|
169 }
|
|
170
|
120
|
171 for (Instruction *I : ToRemove)
|
|
172 I->eraseFromParent();
|
77
|
173
|
120
|
174 return ToRemove.size() > 0;
|
77
|
175 }
|