name_mapper.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2016 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_NAME_MAPPER_H_
  15. #define SOURCE_NAME_MAPPER_H_
  16. #include <functional>
  17. #include <string>
  18. #include <unordered_map>
  19. #include <unordered_set>
  20. #include "source/assembly_grammar.h"
  21. #include "spirv-tools/libspirv.h"
  22. namespace spvtools {
  23. // A NameMapper maps SPIR-V Id values to names. Each name is valid to use in
  24. // SPIR-V assembly. The mapping is one-to-one, i.e. no two Ids map to the same
  25. // name.
  26. using NameMapper = std::function<std::string(uint32_t)>;
  27. // Returns a NameMapper which always maps an Id to its decimal representation.
  28. NameMapper GetTrivialNameMapper();
  29. // A FriendlyNameMapper parses a module upon construction. If the parse is
  30. // successful, then the NameForId method maps an Id to a friendly name
  31. // while also satisfying the constraints on a NameMapper.
  32. //
  33. // The mapping is friendly in the following sense:
  34. // - If an Id has a debug name (via OpName), then that will be used when
  35. // possible.
  36. // - Well known scalar types map to friendly names. For example,
  37. // OpTypeVoid should be %void. Scalar types map to their names in OpenCL
  38. // when
  39. // there is a correspondence, and otherwise as follows:
  40. // - unsigned integer type of n bits map to "u" followed by n
  41. // - signed integer type of n bits map to "i" followed by n
  42. // - floating point type of n bits map to "fp" followed by n
  43. // - Vector type names map to "v" followed by the number of components,
  44. // followed by the friendly name for the base type.
  45. // - Matrix type names map to "mat" followed by the number of columns,
  46. // followed by the friendly name for the base vector type.
  47. // - Pointer types map to "_ptr_", then the name of the storage class, then the
  48. // name for the pointee type.
  49. // - Exotic types like event, pipe, opaque, queue, reserve-id map to their own
  50. // human readable names.
  51. // - A struct type maps to "_struct_" followed by the raw Id number. That's
  52. // pretty simplistic, but workable.
  53. // - A built-in variable maps to its GLSL variable name.
  54. // - Numeric literals in OpConstant map to a human-friendly name.
  55. class FriendlyNameMapper {
  56. public:
  57. // Construct a friendly name mapper, and determine friendly names for each
  58. // defined Id in the specified module. The module is specified by the code
  59. // wordCount, and should be parseable in the specified context.
  60. FriendlyNameMapper(const spv_const_context context, const uint32_t* code,
  61. const size_t wordCount);
  62. // Returns a NameMapper which maps ids to the friendly names parsed from the
  63. // module provided to the constructor.
  64. NameMapper GetNameMapper() {
  65. return [this](uint32_t id) { return this->NameForId(id); };
  66. }
  67. // Returns the friendly name for the given id. If the module parsed during
  68. // construction is valid, then the mapping satisfies the rules for a
  69. // NameMapper.
  70. std::string NameForId(uint32_t id);
  71. private:
  72. // Transforms the given string so that it is acceptable as an Id name in
  73. // assembly language. Two distinct inputs can map to the same output.
  74. std::string Sanitize(const std::string& suggested_name);
  75. // Records a name for the given id. If this id already has a name, then
  76. // this is a no-op. If the id doesn't have a name, use the given
  77. // suggested_name if it hasn't already been taken, and otherwise generate
  78. // a new (unused) name based on the suggested name.
  79. void SaveName(uint32_t id, const std::string& suggested_name);
  80. // Records a built-in variable name for target_id. If target_id already
  81. // has a name then this is a no-op.
  82. void SaveBuiltInName(uint32_t target_id, uint32_t built_in);
  83. // Collects information from the given parsed instruction to populate
  84. // name_for_id_. Returns SPV_SUCCESS;
  85. spv_result_t ParseInstruction(const spv_parsed_instruction_t& inst);
  86. // Forwards a parsed-instruction callback from the binary parser into the
  87. // FriendlyNameMapper hidden inside the user_data parameter.
  88. static spv_result_t ParseInstructionForwarder(
  89. void* user_data, const spv_parsed_instruction_t* parsed_instruction) {
  90. return reinterpret_cast<FriendlyNameMapper*>(user_data)->ParseInstruction(
  91. *parsed_instruction);
  92. }
  93. // Returns the friendly name for an enumerant.
  94. std::string NameForEnumOperand(spv_operand_type_t type, uint32_t word);
  95. // Maps an id to its friendly name. This will have an entry for each Id
  96. // defined in the module.
  97. std::unordered_map<uint32_t, std::string> name_for_id_;
  98. // The set of names that have a mapping in name_for_id_;
  99. std::unordered_set<std::string> used_names_;
  100. // The assembly grammar for the current context.
  101. const AssemblyGrammar grammar_;
  102. };
  103. } // namespace spvtools
  104. #endif // SOURCE_NAME_MAPPER_H_