Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 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 | // |
Justin Lebar | efcc81c | 2016-04-01 01:09:07 +0000 | [diff] [blame] | 10 | // This pass replaces occurrences of __nvvm_reflect("foo") and llvm.nvvm.reflect |
| 11 | // with an integer. |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 12 | // |
Justin Lebar | efcc81c | 2016-04-01 01:09:07 +0000 | [diff] [blame] | 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. |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Justin Holewinski | 18f3a1f | 2013-05-20 16:42:16 +0000 | [diff] [blame] | 23 | #include "NVPTX.h" |
Justin Holewinski | a922c7e | 2013-04-02 12:37:11 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallVector.h" |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Constants.h" |
| 27 | #include "llvm/IR/DerivedTypes.h" |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Function.h" |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 29 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Instructions.h" |
Justin Holewinski | a0d531f | 2014-06-27 18:36:11 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Intrinsics.h" |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Module.h" |
| 33 | #include "llvm/IR/Type.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 34 | #include "llvm/Pass.h" |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
| 36 | #include "llvm/Support/Debug.h" |
| 37 | #include "llvm/Support/raw_os_ostream.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 39 | #include "llvm/Transforms/Scalar.h" |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 40 | #include <sstream> |
| 41 | #include <string> |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 42 | #define NVVM_REFLECT_FUNCTION "__nvvm_reflect" |
| 43 | |
| 44 | using namespace llvm; |
| 45 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 46 | #define DEBUG_TYPE "nvptx-reflect" |
| 47 | |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 48 | namespace llvm { void initializeNVVMReflectPass(PassRegistry &); } |
| 49 | |
| 50 | namespace { |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 51 | class NVVMReflect : public FunctionPass { |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 52 | private: |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 53 | StringMap<int> VarMap; |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 54 | |
| 55 | public: |
| 56 | static char ID; |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 57 | NVVMReflect() : NVVMReflect(StringMap<int>()) {} |
Justin Holewinski | 18f3a1f | 2013-05-20 16:42:16 +0000 | [diff] [blame] | 58 | |
Justin Lebar | efcc81c | 2016-04-01 01:09:07 +0000 | [diff] [blame] | 59 | NVVMReflect(const StringMap<int> &Mapping) |
| 60 | : FunctionPass(ID), VarMap(Mapping) { |
Justin Holewinski | 18f3a1f | 2013-05-20 16:42:16 +0000 | [diff] [blame] | 61 | initializeNVVMReflectPass(*PassRegistry::getPassRegistry()); |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 62 | setVarMap(); |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 65 | bool runOnFunction(Function &) override; |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 66 | |
Justin Holewinski | a0d531f | 2014-06-27 18:36:11 +0000 | [diff] [blame] | 67 | private: |
| 68 | bool handleFunction(Function *ReflectFunction); |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 69 | void setVarMap(); |
| 70 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 71 | } |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 72 | |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 73 | FunctionPass *llvm::createNVVMReflectPass() { return new NVVMReflect(); } |
| 74 | FunctionPass *llvm::createNVVMReflectPass(const StringMap<int> &Mapping) { |
Justin Holewinski | 18f3a1f | 2013-05-20 16:42:16 +0000 | [diff] [blame] | 75 | return new NVVMReflect(Mapping); |
| 76 | } |
| 77 | |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 78 | static cl::opt<bool> |
Nadav Rotem | 7f27e0b | 2013-10-18 23:38:13 +0000 | [diff] [blame] | 79 | NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden, |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 80 | cl::desc("NVVM reflection, enabled by default")); |
| 81 | |
| 82 | char NVVMReflect::ID = 0; |
| 83 | INITIALIZE_PASS(NVVMReflect, "nvvm-reflect", |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 84 | "Replace occurrences of __nvvm_reflect() calls with 0/1", false, |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 85 | false) |
| 86 | |
| 87 | static cl::list<std::string> |
Nadav Rotem | 7f27e0b | 2013-10-18 23:38:13 +0000 | [diff] [blame] | 88 | ReflectList("nvvm-reflect-list", cl::value_desc("name=<int>"), cl::Hidden, |
Justin Holewinski | a922c7e | 2013-04-02 12:37:11 +0000 | [diff] [blame] | 89 | cl::desc("A list of string=num assignments"), |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 90 | cl::ValueRequired); |
| 91 | |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 92 | /// The command line can look as follows : |
Justin Holewinski | a922c7e | 2013-04-02 12:37:11 +0000 | [diff] [blame] | 93 | /// -nvvm-reflect-list a=1,b=2 -nvvm-reflect-list c=3,d=0 -R e=2 |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 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. |
| 98 | void NVVMReflect::setVarMap() { |
| 99 | for (unsigned i = 0, e = ReflectList.size(); i != e; ++i) { |
Justin Holewinski | a922c7e | 2013-04-02 12:37:11 +0000 | [diff] [blame] | 100 | DEBUG(dbgs() << "Option : " << ReflectList[i] << "\n"); |
| 101 | SmallVector<StringRef, 4> NameValList; |
Chandler Carruth | e4405e9 | 2015-09-10 06:12:31 +0000 | [diff] [blame] | 102 | StringRef(ReflectList[i]).split(NameValList, ','); |
Justin Holewinski | a922c7e | 2013-04-02 12:37:11 +0000 | [diff] [blame] | 103 | for (unsigned j = 0, ej = NameValList.size(); j != ej; ++j) { |
| 104 | SmallVector<StringRef, 2> NameValPair; |
Chandler Carruth | e4405e9 | 2015-09-10 06:12:31 +0000 | [diff] [blame] | 105 | NameValList[j].split(NameValPair, '='); |
Justin Holewinski | a922c7e | 2013-04-02 12:37:11 +0000 | [diff] [blame] | 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; |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 116 | bool NVVMReflect::runOnFunction(Function &F) { |
| 117 | if (!NVVMReflectEnabled) |
| 118 | return false; |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 119 | |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 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 | } |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 126 | |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 127 | SmallVector<Instruction *, 4> ToRemove; |
Artem Belevich | 9e8a039 | 2015-03-19 17:05:35 +0000 | [diff] [blame] | 128 | |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 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 | // |
Artem Belevich | 9e8a039 | 2015-03-19 17:05:35 +0000 | [diff] [blame] | 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 | // |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 144 | // The value returned by Sym->getOperand(0) is a Constant with a |
Artem Belevich | 9e8a039 | 2015-03-19 17:05:35 +0000 | [diff] [blame] | 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. |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 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; |
Artem Belevich | 9e8a039 | 2015-03-19 17:05:35 +0000 | [diff] [blame] | 163 | |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 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"); |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 167 | |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 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. |
Justin Holewinski | a0d531f | 2014-06-27 18:36:11 +0000 | [diff] [blame] | 173 | Str = ConvCall->getArgOperand(0); |
| 174 | } |
| 175 | assert(isa<ConstantExpr>(Str) && |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 176 | "Format of __nvvm__reflect function not recognized"); |
Justin Holewinski | a0d531f | 2014-06-27 18:36:11 +0000 | [diff] [blame] | 177 | const ConstantExpr *GEP = cast<ConstantExpr>(Str); |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 178 | |
Justin Holewinski | a922c7e | 2013-04-02 12:37:11 +0000 | [diff] [blame] | 179 | const Value *Sym = GEP->getOperand(0); |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 180 | assert(isa<Constant>(Sym) && |
| 181 | "Format of __nvvm_reflect function not recognized"); |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 182 | |
Artem Belevich | 9e8a039 | 2015-03-19 17:05:35 +0000 | [diff] [blame] | 183 | const Value *Operand = cast<Constant>(Sym)->getOperand(0); |
| 184 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) { |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 185 | // For CUDA-7.0 style __nvvm_reflect calls, we need to find the operand's |
Artem Belevich | 9e8a039 | 2015-03-19 17:05:35 +0000 | [diff] [blame] | 186 | // initializer. |
| 187 | assert(GV->hasInitializer() && |
| 188 | "Format of _reflect function not recognized"); |
| 189 | const Constant *Initializer = GV->getInitializer(); |
| 190 | Operand = Initializer; |
| 191 | } |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 192 | |
Artem Belevich | 9e8a039 | 2015-03-19 17:05:35 +0000 | [diff] [blame] | 193 | assert(isa<ConstantDataSequential>(Operand) && |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 194 | "Format of _reflect function not recognized"); |
Artem Belevich | 9e8a039 | 2015-03-19 17:05:35 +0000 | [diff] [blame] | 195 | assert(cast<ConstantDataSequential>(Operand)->isCString() && |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 196 | "Format of _reflect function not recognized"); |
| 197 | |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 198 | StringRef ReflectArg = cast<ConstantDataSequential>(Operand)->getAsString(); |
Justin Holewinski | a922c7e | 2013-04-02 12:37:11 +0000 | [diff] [blame] | 199 | ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1); |
| 200 | DEBUG(dbgs() << "Arg of _reflect : " << ReflectArg << "\n"); |
Justin Holewinski | b94bd05 | 2013-03-30 14:29:25 +0000 | [diff] [blame] | 201 | |
Justin Holewinski | a922c7e | 2013-04-02 12:37:11 +0000 | [diff] [blame] | 202 | int ReflectVal = 0; // The default value is 0 |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 203 | auto Iter = VarMap.find(ReflectArg); |
| 204 | if (Iter != VarMap.end()) |
| 205 | ReflectVal = Iter->second; |
Justin Lebar | efcc81c | 2016-04-01 01:09:07 +0000 | [diff] [blame] | 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 | } |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 212 | Call->replaceAllUsesWith(ConstantInt::get(Call->getType(), ReflectVal)); |
| 213 | ToRemove.push_back(Call); |
Justin Holewinski | a0d531f | 2014-06-27 18:36:11 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 216 | for (Instruction *I : ToRemove) |
| 217 | I->eraseFromParent(); |
Justin Holewinski | a0d531f | 2014-06-27 18:36:11 +0000 | [diff] [blame] | 218 | |
Justin Lebar | e3804cc | 2016-03-30 20:40:11 +0000 | [diff] [blame] | 219 | return ToRemove.size() > 0; |
Justin Holewinski | a0d531f | 2014-06-27 18:36:11 +0000 | [diff] [blame] | 220 | } |