DxilRenameResourcesPass.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilRenameResourcesPass.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include "llvm/Pass.h"
  10. #include "llvm/IR/Function.h"
  11. #include "llvm/IR/Instruction.h"
  12. #include "llvm/IR/Instructions.h"
  13. #include "llvm/IR/LLVMContext.h"
  14. #include "llvm/IR/Module.h"
  15. #include "dxc/DXIL/DxilModule.h"
  16. #include "dxc/HLSL/DxilGenerationPass.h"
  17. using namespace llvm;
  18. using namespace hlsl;
  19. // Rename resources with prefix
  20. namespace {
  21. class DxilRenameResources : public ModulePass {
  22. public:
  23. static char ID; // Pass identification, replacement for typeid
  24. explicit DxilRenameResources()
  25. : ModulePass(ID) {}
  26. void applyOptions(PassOptions O) override {
  27. GetPassOptionBool(O, "from-binding", &m_bFromBinding, false);
  28. GetPassOptionBool(O, "keep-name", &m_bKeepName, true);
  29. StringRef prefix;
  30. GetPassOption(O, "prefix", &prefix);
  31. m_Prefix = prefix.str();
  32. }
  33. const char *getPassName() const override {
  34. return "DXIL rename resources";
  35. }
  36. bool runOnModule(Module &M) override {
  37. DxilModule &DM = M.GetOrCreateDxilModule();
  38. bool bChanged = false;
  39. if (m_bFromBinding) {
  40. bChanged = DM.RenameResourceGlobalsWithBinding(m_bKeepName);
  41. }
  42. if (!m_Prefix.empty()) {
  43. bChanged |= DM.RenameResourcesWithPrefix(m_Prefix);
  44. }
  45. if (bChanged) {
  46. DM.ReEmitDxilResources();
  47. }
  48. return bChanged;
  49. }
  50. private:
  51. bool m_bFromBinding;
  52. bool m_bKeepName;
  53. std::string m_Prefix;
  54. };
  55. char DxilRenameResources::ID = 0;
  56. }
  57. ModulePass *llvm::createDxilRenameResourcesPass() {
  58. return new DxilRenameResources();
  59. }
  60. INITIALIZE_PASS(DxilRenameResources,
  61. "dxil-rename-resources",
  62. "DXIL rename resources", false, false)