llvm.org GIT mirror llvm / 7a28de0
[NVPTX] Add reflect intrinsic (better than matching by function name) Also clean up some of the logic in NVVMReflect.cpp while we're messing around in there. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211948 91177308-0d34-0410-b5e6-96231b3b80d8 Justin Holewinski 6 years ago
3 changed file(s) with 64 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
886886 Intrinsic<[], [llvm_anyptr_ty], [], "llvm.nvvm.compiler.error">;
887887 def int_nvvm_compiler_warn :
888888 Intrinsic<[], [llvm_anyptr_ty], [], "llvm.nvvm.compiler.warn">;
889
890 def int_nvvm_reflect :
891 Intrinsic<[llvm_i32_ty], [llvm_anyptr_ty], [IntrNoMem], "llvm.nvvm.reflect">;
889892
890893 // isspacep.{const, global, local, shared}
891894 def int_nvvm_isspacep_const
2121 #include "llvm/IR/DerivedTypes.h"
2222 #include "llvm/IR/Function.h"
2323 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Intrinsics.h"
2425 #include "llvm/IR/Module.h"
2526 #include "llvm/IR/Type.h"
2627 #include "llvm/Pass.h"
4647 private:
4748 StringMap VarMap;
4849 typedef DenseMap::iterator VarMapIter;
49 Function *ReflectFunction;
5050
5151 public:
5252 static char ID;
53 NVVMReflect() : ModulePass(ID), ReflectFunction(nullptr) {
53 NVVMReflect() : ModulePass(ID) {
5454 initializeNVVMReflectPass(*PassRegistry::getPassRegistry());
5555 VarMap.clear();
5656 }
5757
5858 NVVMReflect(const StringMap &Mapping)
59 : ModulePass(ID), ReflectFunction(nullptr) {
59 : ModulePass(ID) {
6060 initializeNVVMReflectPass(*PassRegistry::getPassRegistry());
6161 for (StringMap::const_iterator I = Mapping.begin(), E = Mapping.end();
6262 I != E; ++I) {
6969 }
7070 bool runOnModule(Module &) override;
7171
72 private:
73 bool handleFunction(Function *ReflectFunction);
7274 void setVarMap();
7375 };
7476 }
119121 }
120122 }
121123
122 bool NVVMReflect::runOnModule(Module &M) {
123 if (!NVVMReflectEnabled)
124 return false;
125
126 setVarMap();
127
128 ReflectFunction = M.getFunction(NVVM_REFLECT_FUNCTION);
129
130 // If reflect function is not used, then there will be
131 // no entry in the module.
132 if (!ReflectFunction)
133 return false;
134
124 bool NVVMReflect::handleFunction(Function *ReflectFunction) {
135125 // Validate _reflect function
136126 assert(ReflectFunction->isDeclaration() &&
137127 "_reflect function should not have a body");
154144 "Only one operand expect for _reflect function");
155145 // In cuda, we will have an extra constant-to-generic conversion of
156146 // the string.
157 const Value *conv = Reflect->getArgOperand(0);
158 assert(isa(conv) && "Expected a const-to-gen conversion");
159 const CallInst *ConvCall = cast(conv);
160 const Value *str = ConvCall->getArgOperand(0);
161 assert(isa(str) &&
147 const Value *Str = Reflect->getArgOperand(0);
148 if (isa(Str)) {
149 // CUDA path
150 const CallInst *ConvCall = cast(Str);
151 Str = ConvCall->getArgOperand(0);
152 }
153 assert(isa(Str) &&
162154 "Format of _reflect function not recognized");
163 const ConstantExpr *GEP = cast(str);
155 const ConstantExpr *GEP = cast(Str);
164156
165157 const Value *Sym = GEP->getOperand(0);
166158 assert(isa(Sym) && "Format of _reflect function not recognized");
194186 ToRemove[i]->eraseFromParent();
195187 return true;
196188 }
189
190 bool NVVMReflect::runOnModule(Module &M) {
191 if (!NVVMReflectEnabled)
192 return false;
193
194 setVarMap();
195
196
197 bool Res = false;
198 std::string Name;
199 Type *Tys[1];
200 Type *I8Ty = Type::getInt8Ty(M.getContext());
201 Function *ReflectFunction;
202
203 // Check for standard overloaded versions of llvm.nvvm.reflect
204
205 for (unsigned i = 0; i != 5; ++i) {
206 Tys[0] = PointerType::get(I8Ty, i);
207 Name = Intrinsic::getName(Intrinsic::nvvm_reflect, Tys);
208 ReflectFunction = M.getFunction(Name);
209 if(ReflectFunction != 0) {
210 Res |= handleFunction(ReflectFunction);
211 }
212 }
213
214 ReflectFunction = M.getFunction(NVVM_REFLECT_FUNCTION);
215 // If reflect function is not used, then there will be
216 // no entry in the module.
217 if (ReflectFunction != 0)
218 Res |= handleFunction(ReflectFunction);
219
220 return Res;
221 }
3131 %ret = phi float [%ret1, %use_mul], [%ret2, %use_add]
3232 ret float %ret
3333 }
34
35 declare i32 @llvm.nvvm.reflect.p0i8(i8*)
36
37 ; USE_MUL_0: define i32 @intrinsic
38 ; USE_MUL_1: define i32 @intrinsic
39 define i32 @intrinsic() {
40 ; USE_MUL_0-NOT: call i32 @llvm.nvvm.reflect
41 ; USE_MUL_0: ret i32 0
42 ; USE_MUL_1-NOT: call i32 @llvm.nvvm.reflect
43 ; USE_MUL_1: ret i32 1
44 %ptr = tail call i8* @llvm.nvvm.ptr.constant.to.gen.p0i8.p4i8(i8 addrspace(4)* getelementptr inbounds ([8 x i8] addrspace(4)* @str, i32 0, i32 0))
45 %reflect = tail call i32 @llvm.nvvm.reflect.p0i8(i8* %ptr)
46 ret i32 %reflect
47 }