NVPTXImageOptimizer.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //===-- NVPTXImageOptimizer.cpp - Image optimization pass -----------------===//
  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. // This pass implements IR-level optimizations of image access code,
  10. // including:
  11. //
  12. // 1. Eliminate istypep intrinsics when image access qualifier is known
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "NVPTX.h"
  16. #include "NVPTXUtilities.h"
  17. #include "llvm/Analysis/ConstantFolding.h"
  18. #include "llvm/IR/Instructions.h"
  19. #include "llvm/IR/Intrinsics.h"
  20. #include "llvm/IR/IntrinsicsNVPTX.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/Pass.h"
  23. using namespace llvm;
  24. namespace {
  25. class NVPTXImageOptimizer : public FunctionPass {
  26. private:
  27. static char ID;
  28. SmallVector<Instruction*, 4> InstrToDelete;
  29. public:
  30. NVPTXImageOptimizer();
  31. bool runOnFunction(Function &F) override;
  32. private:
  33. bool replaceIsTypePSampler(Instruction &I);
  34. bool replaceIsTypePSurface(Instruction &I);
  35. bool replaceIsTypePTexture(Instruction &I);
  36. Value *cleanupValue(Value *V);
  37. void replaceWith(Instruction *From, ConstantInt *To);
  38. };
  39. }
  40. char NVPTXImageOptimizer::ID = 0;
  41. NVPTXImageOptimizer::NVPTXImageOptimizer()
  42. : FunctionPass(ID) {}
  43. bool NVPTXImageOptimizer::runOnFunction(Function &F) {
  44. if (skipFunction(F))
  45. return false;
  46. bool Changed = false;
  47. InstrToDelete.clear();
  48. // Look for call instructions in the function
  49. for (BasicBlock &BB : F) {
  50. for (Instruction &Instr : BB) {
  51. if (CallInst *CI = dyn_cast<CallInst>(&Instr)) {
  52. Function *CalledF = CI->getCalledFunction();
  53. if (CalledF && CalledF->isIntrinsic()) {
  54. // This is an intrinsic function call, check if its an istypep
  55. switch (CalledF->getIntrinsicID()) {
  56. default: break;
  57. case Intrinsic::nvvm_istypep_sampler:
  58. Changed |= replaceIsTypePSampler(Instr);
  59. break;
  60. case Intrinsic::nvvm_istypep_surface:
  61. Changed |= replaceIsTypePSurface(Instr);
  62. break;
  63. case Intrinsic::nvvm_istypep_texture:
  64. Changed |= replaceIsTypePTexture(Instr);
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. // Delete any istypep instances we replaced in the IR
  72. for (Instruction *I : InstrToDelete)
  73. I->eraseFromParent();
  74. return Changed;
  75. }
  76. bool NVPTXImageOptimizer::replaceIsTypePSampler(Instruction &I) {
  77. Value *TexHandle = cleanupValue(I.getOperand(0));
  78. if (isSampler(*TexHandle)) {
  79. // This is an OpenCL sampler, so it must be a samplerref
  80. replaceWith(&I, ConstantInt::getTrue(I.getContext()));
  81. return true;
  82. } else if (isImage(*TexHandle)) {
  83. // This is an OpenCL image, so it cannot be a samplerref
  84. replaceWith(&I, ConstantInt::getFalse(I.getContext()));
  85. return true;
  86. } else {
  87. // The image type is unknown, so we cannot eliminate the intrinsic
  88. return false;
  89. }
  90. }
  91. bool NVPTXImageOptimizer::replaceIsTypePSurface(Instruction &I) {
  92. Value *TexHandle = cleanupValue(I.getOperand(0));
  93. if (isImageReadWrite(*TexHandle) ||
  94. isImageWriteOnly(*TexHandle)) {
  95. // This is an OpenCL read-only/read-write image, so it must be a surfref
  96. replaceWith(&I, ConstantInt::getTrue(I.getContext()));
  97. return true;
  98. } else if (isImageReadOnly(*TexHandle) ||
  99. isSampler(*TexHandle)) {
  100. // This is an OpenCL read-only/ imageor sampler, so it cannot be
  101. // a surfref
  102. replaceWith(&I, ConstantInt::getFalse(I.getContext()));
  103. return true;
  104. } else {
  105. // The image type is unknown, so we cannot eliminate the intrinsic
  106. return false;
  107. }
  108. }
  109. bool NVPTXImageOptimizer::replaceIsTypePTexture(Instruction &I) {
  110. Value *TexHandle = cleanupValue(I.getOperand(0));
  111. if (isImageReadOnly(*TexHandle)) {
  112. // This is an OpenCL read-only image, so it must be a texref
  113. replaceWith(&I, ConstantInt::getTrue(I.getContext()));
  114. return true;
  115. } else if (isImageWriteOnly(*TexHandle) ||
  116. isImageReadWrite(*TexHandle) ||
  117. isSampler(*TexHandle)) {
  118. // This is an OpenCL read-write/write-only image or a sampler, so it
  119. // cannot be a texref
  120. replaceWith(&I, ConstantInt::getFalse(I.getContext()));
  121. return true;
  122. } else {
  123. // The image type is unknown, so we cannot eliminate the intrinsic
  124. return false;
  125. }
  126. }
  127. void NVPTXImageOptimizer::replaceWith(Instruction *From, ConstantInt *To) {
  128. // We implement "poor man's DCE" here to make sure any code that is no longer
  129. // live is actually unreachable and can be trivially eliminated by the
  130. // unreachable block elimination pass.
  131. for (Use &U : From->uses()) {
  132. if (BranchInst *BI = dyn_cast<BranchInst>(U)) {
  133. if (BI->isUnconditional()) continue;
  134. BasicBlock *Dest;
  135. if (To->isZero())
  136. // Get false block
  137. Dest = BI->getSuccessor(1);
  138. else
  139. // Get true block
  140. Dest = BI->getSuccessor(0);
  141. BranchInst::Create(Dest, BI);
  142. InstrToDelete.push_back(BI);
  143. }
  144. }
  145. From->replaceAllUsesWith(To);
  146. InstrToDelete.push_back(From);
  147. }
  148. Value *NVPTXImageOptimizer::cleanupValue(Value *V) {
  149. if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
  150. return cleanupValue(EVI->getAggregateOperand());
  151. }
  152. return V;
  153. }
  154. FunctionPass *llvm::createNVPTXImageOptimizerPass() {
  155. return new NVPTXImageOptimizer();
  156. }