NVPTXGenericToNVVM.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //===-- GenericToNVVM.cpp - Convert generic module to NVVM module - C++ -*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Convert generic global variables into either .global or .const access based
  10. // on the variable's "constant" qualifier.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "MCTargetDesc/NVPTXBaseInfo.h"
  14. #include "NVPTX.h"
  15. #include "NVPTXUtilities.h"
  16. #include "llvm/CodeGen/ValueTypes.h"
  17. #include "llvm/IR/Constants.h"
  18. #include "llvm/IR/DerivedTypes.h"
  19. #include "llvm/IR/IRBuilder.h"
  20. #include "llvm/IR/Instructions.h"
  21. #include "llvm/IR/Intrinsics.h"
  22. #include "llvm/IR/LegacyPassManager.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/IR/Operator.h"
  25. #include "llvm/IR/ValueMap.h"
  26. #include "llvm/Transforms/Utils/ValueMapper.h"
  27. using namespace llvm;
  28. namespace llvm {
  29. void initializeGenericToNVVMPass(PassRegistry &);
  30. }
  31. namespace {
  32. class GenericToNVVM : public ModulePass {
  33. public:
  34. static char ID;
  35. GenericToNVVM() : ModulePass(ID) {}
  36. bool runOnModule(Module &M) override;
  37. void getAnalysisUsage(AnalysisUsage &AU) const override {}
  38. private:
  39. Value *remapConstant(Module *M, Function *F, Constant *C,
  40. IRBuilder<> &Builder);
  41. Value *remapConstantVectorOrConstantAggregate(Module *M, Function *F,
  42. Constant *C,
  43. IRBuilder<> &Builder);
  44. Value *remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
  45. IRBuilder<> &Builder);
  46. typedef ValueMap<GlobalVariable *, GlobalVariable *> GVMapTy;
  47. typedef ValueMap<Constant *, Value *> ConstantToValueMapTy;
  48. GVMapTy GVMap;
  49. ConstantToValueMapTy ConstantToValueMap;
  50. };
  51. } // end namespace
  52. char GenericToNVVM::ID = 0;
  53. ModulePass *llvm::createGenericToNVVMPass() { return new GenericToNVVM(); }
  54. INITIALIZE_PASS(
  55. GenericToNVVM, "generic-to-nvvm",
  56. "Ensure that the global variables are in the global address space", false,
  57. false)
  58. bool GenericToNVVM::runOnModule(Module &M) {
  59. // Create a clone of each global variable that has the default address space.
  60. // The clone is created with the global address space specifier, and the pair
  61. // of original global variable and its clone is placed in the GVMap for later
  62. // use.
  63. for (GlobalVariable &GV : llvm::make_early_inc_range(M.globals())) {
  64. if (GV.getType()->getAddressSpace() == llvm::ADDRESS_SPACE_GENERIC &&
  65. !llvm::isTexture(GV) && !llvm::isSurface(GV) && !llvm::isSampler(GV) &&
  66. !GV.getName().startswith("llvm.")) {
  67. GlobalVariable *NewGV = new GlobalVariable(
  68. M, GV.getValueType(), GV.isConstant(), GV.getLinkage(),
  69. GV.hasInitializer() ? GV.getInitializer() : nullptr, "", &GV,
  70. GV.getThreadLocalMode(), llvm::ADDRESS_SPACE_GLOBAL);
  71. NewGV->copyAttributesFrom(&GV);
  72. NewGV->copyMetadata(&GV, /*Offset=*/0);
  73. GVMap[&GV] = NewGV;
  74. }
  75. }
  76. // Return immediately, if every global variable has a specific address space
  77. // specifier.
  78. if (GVMap.empty()) {
  79. return false;
  80. }
  81. // Walk through the instructions in function defitinions, and replace any use
  82. // of original global variables in GVMap with a use of the corresponding
  83. // copies in GVMap. If necessary, promote constants to instructions.
  84. for (Function &F : M) {
  85. if (F.isDeclaration()) {
  86. continue;
  87. }
  88. IRBuilder<> Builder(F.getEntryBlock().getFirstNonPHIOrDbg());
  89. for (BasicBlock &BB : F) {
  90. for (Instruction &II : BB) {
  91. for (unsigned i = 0, e = II.getNumOperands(); i < e; ++i) {
  92. Value *Operand = II.getOperand(i);
  93. if (isa<Constant>(Operand)) {
  94. II.setOperand(
  95. i, remapConstant(&M, &F, cast<Constant>(Operand), Builder));
  96. }
  97. }
  98. }
  99. }
  100. ConstantToValueMap.clear();
  101. }
  102. // Copy GVMap over to a standard value map.
  103. ValueToValueMapTy VM;
  104. for (auto I = GVMap.begin(), E = GVMap.end(); I != E; ++I)
  105. VM[I->first] = I->second;
  106. // Walk through the global variable initializers, and replace any use of
  107. // original global variables in GVMap with a use of the corresponding copies
  108. // in GVMap. The copies need to be bitcast to the original global variable
  109. // types, as we cannot use cvta in global variable initializers.
  110. for (GVMapTy::iterator I = GVMap.begin(), E = GVMap.end(); I != E;) {
  111. GlobalVariable *GV = I->first;
  112. GlobalVariable *NewGV = I->second;
  113. // Remove GV from the map so that it can be RAUWed. Note that
  114. // DenseMap::erase() won't invalidate any iterators but this one.
  115. auto Next = std::next(I);
  116. GVMap.erase(I);
  117. I = Next;
  118. Constant *BitCastNewGV = ConstantExpr::getPointerCast(NewGV, GV->getType());
  119. // At this point, the remaining uses of GV should be found only in global
  120. // variable initializers, as other uses have been already been removed
  121. // while walking through the instructions in function definitions.
  122. GV->replaceAllUsesWith(BitCastNewGV);
  123. std::string Name = std::string(GV->getName());
  124. GV->eraseFromParent();
  125. NewGV->setName(Name);
  126. }
  127. assert(GVMap.empty() && "Expected it to be empty by now");
  128. return true;
  129. }
  130. Value *GenericToNVVM::remapConstant(Module *M, Function *F, Constant *C,
  131. IRBuilder<> &Builder) {
  132. // If the constant C has been converted already in the given function F, just
  133. // return the converted value.
  134. ConstantToValueMapTy::iterator CTII = ConstantToValueMap.find(C);
  135. if (CTII != ConstantToValueMap.end()) {
  136. return CTII->second;
  137. }
  138. Value *NewValue = C;
  139. if (isa<GlobalVariable>(C)) {
  140. // If the constant C is a global variable and is found in GVMap, substitute
  141. //
  142. // addrspacecast GVMap[C] to addrspace(0)
  143. //
  144. // for our use of C.
  145. GVMapTy::iterator I = GVMap.find(cast<GlobalVariable>(C));
  146. if (I != GVMap.end()) {
  147. GlobalVariable *GV = I->second;
  148. NewValue = Builder.CreateAddrSpaceCast(
  149. GV,
  150. PointerType::get(GV->getValueType(), llvm::ADDRESS_SPACE_GENERIC));
  151. }
  152. } else if (isa<ConstantAggregate>(C)) {
  153. // If any element in the constant vector or aggregate C is or uses a global
  154. // variable in GVMap, the constant C needs to be reconstructed, using a set
  155. // of instructions.
  156. NewValue = remapConstantVectorOrConstantAggregate(M, F, C, Builder);
  157. } else if (isa<ConstantExpr>(C)) {
  158. // If any operand in the constant expression C is or uses a global variable
  159. // in GVMap, the constant expression C needs to be reconstructed, using a
  160. // set of instructions.
  161. NewValue = remapConstantExpr(M, F, cast<ConstantExpr>(C), Builder);
  162. }
  163. ConstantToValueMap[C] = NewValue;
  164. return NewValue;
  165. }
  166. Value *GenericToNVVM::remapConstantVectorOrConstantAggregate(
  167. Module *M, Function *F, Constant *C, IRBuilder<> &Builder) {
  168. bool OperandChanged = false;
  169. SmallVector<Value *, 4> NewOperands;
  170. unsigned NumOperands = C->getNumOperands();
  171. // Check if any element is or uses a global variable in GVMap, and thus
  172. // converted to another value.
  173. for (unsigned i = 0; i < NumOperands; ++i) {
  174. Value *Operand = C->getOperand(i);
  175. Value *NewOperand = remapConstant(M, F, cast<Constant>(Operand), Builder);
  176. OperandChanged |= Operand != NewOperand;
  177. NewOperands.push_back(NewOperand);
  178. }
  179. // If none of the elements has been modified, return C as it is.
  180. if (!OperandChanged) {
  181. return C;
  182. }
  183. // If any of the elements has been modified, construct the equivalent
  184. // vector or aggregate value with a set instructions and the converted
  185. // elements.
  186. Value *NewValue = PoisonValue::get(C->getType());
  187. if (isa<ConstantVector>(C)) {
  188. for (unsigned i = 0; i < NumOperands; ++i) {
  189. Value *Idx = ConstantInt::get(Type::getInt32Ty(M->getContext()), i);
  190. NewValue = Builder.CreateInsertElement(NewValue, NewOperands[i], Idx);
  191. }
  192. } else {
  193. for (unsigned i = 0; i < NumOperands; ++i) {
  194. NewValue =
  195. Builder.CreateInsertValue(NewValue, NewOperands[i], ArrayRef(i));
  196. }
  197. }
  198. return NewValue;
  199. }
  200. Value *GenericToNVVM::remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
  201. IRBuilder<> &Builder) {
  202. bool OperandChanged = false;
  203. SmallVector<Value *, 4> NewOperands;
  204. unsigned NumOperands = C->getNumOperands();
  205. // Check if any operand is or uses a global variable in GVMap, and thus
  206. // converted to another value.
  207. for (unsigned i = 0; i < NumOperands; ++i) {
  208. Value *Operand = C->getOperand(i);
  209. Value *NewOperand = remapConstant(M, F, cast<Constant>(Operand), Builder);
  210. OperandChanged |= Operand != NewOperand;
  211. NewOperands.push_back(NewOperand);
  212. }
  213. // If none of the operands has been modified, return C as it is.
  214. if (!OperandChanged) {
  215. return C;
  216. }
  217. // If any of the operands has been modified, construct the instruction with
  218. // the converted operands.
  219. unsigned Opcode = C->getOpcode();
  220. switch (Opcode) {
  221. case Instruction::ICmp:
  222. // CompareConstantExpr (icmp)
  223. return Builder.CreateICmp(CmpInst::Predicate(C->getPredicate()),
  224. NewOperands[0], NewOperands[1]);
  225. case Instruction::FCmp:
  226. // CompareConstantExpr (fcmp)
  227. llvm_unreachable("Address space conversion should have no effect "
  228. "on float point CompareConstantExpr (fcmp)!");
  229. case Instruction::ExtractElement:
  230. // ExtractElementConstantExpr
  231. return Builder.CreateExtractElement(NewOperands[0], NewOperands[1]);
  232. case Instruction::InsertElement:
  233. // InsertElementConstantExpr
  234. return Builder.CreateInsertElement(NewOperands[0], NewOperands[1],
  235. NewOperands[2]);
  236. case Instruction::ShuffleVector:
  237. // ShuffleVector
  238. return Builder.CreateShuffleVector(NewOperands[0], NewOperands[1],
  239. NewOperands[2]);
  240. case Instruction::GetElementPtr:
  241. // GetElementPtrConstantExpr
  242. return Builder.CreateGEP(cast<GEPOperator>(C)->getSourceElementType(),
  243. NewOperands[0],
  244. ArrayRef(&NewOperands[1], NumOperands - 1), "",
  245. cast<GEPOperator>(C)->isInBounds());
  246. case Instruction::Select:
  247. // SelectConstantExpr
  248. return Builder.CreateSelect(NewOperands[0], NewOperands[1], NewOperands[2]);
  249. default:
  250. // BinaryConstantExpr
  251. if (Instruction::isBinaryOp(Opcode)) {
  252. return Builder.CreateBinOp(Instruction::BinaryOps(C->getOpcode()),
  253. NewOperands[0], NewOperands[1]);
  254. }
  255. // UnaryConstantExpr
  256. if (Instruction::isCast(Opcode)) {
  257. return Builder.CreateCast(Instruction::CastOps(C->getOpcode()),
  258. NewOperands[0], C->getType());
  259. }
  260. llvm_unreachable("GenericToNVVM encountered an unsupported ConstantExpr");
  261. }
  262. }