DxilExportMap.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilExportMap.h //
  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. // dxilutil::ExportMap for handling -exports option. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. // TODO: Refactor to separate name export verification part from
  12. // llvm/Function part so first part may be have shared use without llvm
  13. #pragma once
  14. #include <vector>
  15. #include <set>
  16. #include <unordered_set>
  17. #include <string>
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/ADT/StringSet.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/MapVector.h"
  22. namespace llvm {
  23. class Function;
  24. class raw_ostream;
  25. }
  26. namespace hlsl {
  27. namespace dxilutil {
  28. class ExportMap {
  29. public:
  30. typedef std::unordered_set<std::string> StringStore;
  31. typedef std::set<llvm::StringRef> NameSet;
  32. typedef llvm::MapVector< llvm::Function*, NameSet > RenameMap;
  33. typedef llvm::StringMap< llvm::StringSet<> > ExportMapByString;
  34. typedef ExportMapByString::iterator iterator;
  35. typedef ExportMapByString::const_iterator const_iterator;
  36. ExportMap():m_ExportShadersOnly(false) {}
  37. void clear();
  38. bool empty() const;
  39. void setExportShadersOnly(bool v) { m_ExportShadersOnly = v; }
  40. bool isExportShadersOnly() const { return m_ExportShadersOnly; }
  41. // Iterate export map by string name
  42. iterator begin() { return m_ExportMap.begin(); }
  43. const_iterator begin() const { return m_ExportMap.begin(); }
  44. iterator end() { return m_ExportMap.end(); }
  45. const_iterator end() const { return m_ExportMap.end(); }
  46. // Initialize export map from option strings
  47. bool ParseExports(const std::vector<std::string> &exportOpts, llvm::raw_ostream &errors);
  48. // Add one export to the export map
  49. void Add(llvm::StringRef exportName, llvm::StringRef internalName = llvm::StringRef());
  50. // Return true if export is present, or m_ExportMap is empty
  51. bool IsExported(llvm::StringRef original) const;
  52. // Retrieve export entry by name. If Name is mangled, it will fallback to
  53. // search for unmangled version if exact match fails.
  54. // If result == end(), no matching export was found.
  55. ExportMapByString::const_iterator GetExportsByName(llvm::StringRef Name) const;
  56. // Call before processing functions for renaming and cloning validation
  57. void BeginProcessing();
  58. // Called for each function to be processed
  59. // In order to avoid intermediate name collisions during renaming,
  60. // if collisionAvoidanceRenaming is true:
  61. // non-exported functions will be renamed internal.<name>
  62. // functions exported with a different name will be renamed temp.<name>
  63. // returns true if function is exported
  64. bool ProcessFunction(llvm::Function *F, bool collisionAvoidanceRenaming);
  65. // Add function to exports without checking export map or renaming
  66. // (useful for patch constant functions used by exported HS)
  67. void RegisterExportedFunction(llvm::Function *F);
  68. // Called to mark an internal name as used (remove from unused set)
  69. void UseExport(llvm::StringRef internalName);
  70. // Called to add an exported (full) name (for collision detection)
  71. void ExportName(llvm::StringRef exportName);
  72. // Called after functions are processed.
  73. // Returns true if no name collisions or unused exports are present.
  74. bool EndProcessing() const;
  75. const NameSet& GetNameCollisions() const { return m_NameCollisions; }
  76. const NameSet& GetUnusedExports() const { return m_UnusedExports; }
  77. // GetRenames gets the map of mangled renames by function pointer
  78. const RenameMap &GetFunctionRenames() const { return m_RenameMap; }
  79. private:
  80. // {"internalname": ("export1", "export2", ...), ...}
  81. ExportMapByString m_ExportMap;
  82. StringStore m_StringStorage;
  83. llvm::StringRef StoreString(llvm::StringRef str);
  84. // Renaming/Validation state
  85. RenameMap m_RenameMap;
  86. NameSet m_ExportNames;
  87. NameSet m_NameCollisions;
  88. NameSet m_UnusedExports;
  89. bool m_ExportShadersOnly;
  90. };
  91. }
  92. }