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