DxilExportMap.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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() {}
  37. void clear();
  38. bool empty() const;
  39. // Iterate export map by string name
  40. iterator begin() { return m_ExportMap.begin(); }
  41. const_iterator begin() const { return m_ExportMap.begin(); }
  42. iterator end() { return m_ExportMap.end(); }
  43. const_iterator end() const { return m_ExportMap.end(); }
  44. // Initialize export map from option strings
  45. bool ParseExports(const std::vector<std::string> &exportOpts, llvm::raw_ostream &errors);
  46. // Add one export to the export map
  47. void Add(llvm::StringRef exportName, llvm::StringRef internalName = llvm::StringRef());
  48. // Return true if export is present, or m_ExportMap is empty
  49. bool IsExported(llvm::StringRef original) const;
  50. // Retrieve export entry by name. If Name is mangled, it will fallback to
  51. // search for unmangled version if exact match fails.
  52. // If result == end(), no matching export was found.
  53. ExportMapByString::const_iterator GetExportsByName(llvm::StringRef Name) const;
  54. // Call before processing functions for renaming and cloning validation
  55. void BeginProcessing();
  56. // Called for each function to be processed
  57. // In order to avoid intermediate name collisions during renaming,
  58. // if collisionAvoidanceRenaming is true:
  59. // non-exported functions will be renamed internal.<name>
  60. // functions exported with a different name will be renamed temp.<name>
  61. // returns true if function is exported
  62. bool ProcessFunction(llvm::Function *F, bool collisionAvoidanceRenaming);
  63. // Add function to exports without checking export map or renaming
  64. // (useful for patch constant functions used by exported HS)
  65. void RegisterExportedFunction(llvm::Function *F);
  66. // Called to mark an internal name as used (remove from unused set)
  67. void UseExport(llvm::StringRef internalName);
  68. // Called to add an exported (full) name (for collision detection)
  69. void ExportName(llvm::StringRef exportName);
  70. // Called after functions are processed.
  71. // Returns true if no name collisions or unused exports are present.
  72. bool EndProcessing() const;
  73. const NameSet& GetNameCollisions() const { return m_NameCollisions; }
  74. const NameSet& GetUnusedExports() const { return m_UnusedExports; }
  75. // GetRenames gets the map of mangled renames by function pointer
  76. const RenameMap &GetFunctionRenames() const { return m_RenameMap; }
  77. private:
  78. // {"internalname": ("export1", "export2", ...), ...}
  79. ExportMapByString m_ExportMap;
  80. StringStore m_StringStorage;
  81. llvm::StringRef StoreString(llvm::StringRef str);
  82. // Renaming/Validation state
  83. RenameMap m_RenameMap;
  84. NameSet m_ExportNames;
  85. NameSet m_NameCollisions;
  86. NameSet m_UnusedExports;
  87. };
  88. }
  89. }