RSSPIRVWriter.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2016, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "RSSPIRVWriter.h"
  17. #include "Builtin.h"
  18. #include "Context.h"
  19. #include "GlobalAllocPass.h"
  20. #include "GlobalAllocSPIRITPass.h"
  21. #include "GlobalMergePass.h"
  22. #include "InlinePreparationPass.h"
  23. #include "RemoveNonkernelsPass.h"
  24. #include "SPIRVModule.h"
  25. #include "Wrapper.h"
  26. #include "bcinfo/MetadataExtractor.h"
  27. #include "pass_queue.h"
  28. #include "llvm/ADT/Triple.h"
  29. #include "llvm/IR/LegacyPassManager.h"
  30. #include "llvm/IR/Module.h"
  31. #include "llvm/Support/CommandLine.h"
  32. #include "llvm/Support/Debug.h"
  33. #include "llvm/Support/SPIRV.h"
  34. #include "llvm/Support/raw_ostream.h"
  35. #include "llvm/Transforms/IPO.h"
  36. #include "llvm/Transforms/Scalar.h"
  37. #define DEBUG_TYPE "rs2spirv-writer"
  38. using namespace llvm;
  39. using namespace SPIRV;
  40. namespace rs2spirv {
  41. void addPassesForRS2SPIRV(llvm::legacy::PassManager &PassMgr) {
  42. PassMgr.add(createGlobalMergePass());
  43. PassMgr.add(createInlinePreparationPass());
  44. PassMgr.add(createAlwaysInlinerPass());
  45. PassMgr.add(createRemoveNonkernelsPass());
  46. // Delete unreachable globals.
  47. PassMgr.add(createGlobalDCEPass());
  48. // Remove dead debug info.
  49. PassMgr.add(createStripDeadDebugInfoPass());
  50. // Remove dead func decls.
  51. PassMgr.add(createStripDeadPrototypesPass());
  52. // Transform global allocations and accessors (rs[GS]etElementAt)
  53. PassMgr.add(createGlobalAllocPass());
  54. // Removed dead MemCpys in 64-bit targets after global alloc pass
  55. PassMgr.add(createDeadStoreEliminationPass());
  56. PassMgr.add(createAggressiveDCEPass());
  57. // Delete unreachable globals (after removing global allocations)
  58. PassMgr.add(createRemoveAllGlobalAllocPass());
  59. PassMgr.add(createPromoteMemoryToRegisterPass());
  60. PassMgr.add(createTransOCLMD());
  61. // TODO: investigate removal of OCLTypeToSPIRV pass.
  62. PassMgr.add(createOCLTypeToSPIRV());
  63. PassMgr.add(createSPIRVRegularizeLLVM());
  64. PassMgr.add(createSPIRVLowerConstExpr());
  65. PassMgr.add(createSPIRVLowerBool());
  66. }
  67. bool WriteSPIRV(Context &Ctxt, Module *M,
  68. llvm::raw_ostream &OS, std::string &ErrMsg) {
  69. llvm::legacy::PassManager PassMgr;
  70. addPassesForRS2SPIRV(PassMgr);
  71. std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule());
  72. PassMgr.add(createLLVMToSPIRV(BM.get()));
  73. PassMgr.run(*M);
  74. DEBUG(M->dump());
  75. if (BM->getError(ErrMsg) != SPIRVEC_Success) {
  76. return false;
  77. }
  78. llvm::SmallString<4096> O;
  79. llvm::raw_svector_ostream SVOS(O);
  80. SVOS << *BM;
  81. llvm::StringRef str = SVOS.str();
  82. std::vector<uint32_t> words(str.size() / 4);
  83. memcpy(words.data(), str.data(), str.size());
  84. android::spirit::PassQueue spiritPasses;
  85. spiritPasses.append(CreateWrapperPass(*M));
  86. spiritPasses.append(CreateBuiltinPass());
  87. spiritPasses.append(CreateGAPass());
  88. int error;
  89. auto wordsOut = spiritPasses.run(words, &error);
  90. if (error != 0) {
  91. OS << *BM;
  92. ErrMsg = "Failed to generate wrappers for kernels";
  93. return false;
  94. }
  95. OS.write(reinterpret_cast<const char *>(wordsOut.data()),
  96. wordsOut.size() * 4);
  97. return true;
  98. }
  99. } // namespace rs2spirv