name_mapper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #include "source/name_mapper.h"
  15. #include <algorithm>
  16. #include <cassert>
  17. #include <iterator>
  18. #include <sstream>
  19. #include <string>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. #include "source/binary.h"
  23. #include "source/latest_version_spirv_header.h"
  24. #include "source/parsed_operand.h"
  25. #include "spirv-tools/libspirv.h"
  26. namespace spvtools {
  27. namespace {
  28. // Converts a uint32_t to its string decimal representation.
  29. std::string to_string(uint32_t id) {
  30. // Use stringstream, since some versions of Android compilers lack
  31. // std::to_string.
  32. std::stringstream os;
  33. os << id;
  34. return os.str();
  35. }
  36. } // anonymous namespace
  37. NameMapper GetTrivialNameMapper() { return to_string; }
  38. FriendlyNameMapper::FriendlyNameMapper(const spv_const_context context,
  39. const uint32_t* code,
  40. const size_t wordCount)
  41. : grammar_(AssemblyGrammar(context)) {
  42. spv_diagnostic diag = nullptr;
  43. // We don't care if the parse fails.
  44. spvBinaryParse(context, this, code, wordCount, nullptr,
  45. ParseInstructionForwarder, &diag);
  46. spvDiagnosticDestroy(diag);
  47. }
  48. std::string FriendlyNameMapper::NameForId(uint32_t id) {
  49. auto iter = name_for_id_.find(id);
  50. if (iter == name_for_id_.end()) {
  51. // It must have been an invalid module, so just return a trivial mapping.
  52. // We don't care about uniqueness.
  53. return to_string(id);
  54. } else {
  55. return iter->second;
  56. }
  57. }
  58. std::string FriendlyNameMapper::Sanitize(const std::string& suggested_name) {
  59. if (suggested_name.empty()) return "_";
  60. // Otherwise, replace invalid characters by '_'.
  61. std::string result;
  62. std::string valid =
  63. "abcdefghijklmnopqrstuvwxyz"
  64. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  65. "_0123456789";
  66. std::transform(suggested_name.begin(), suggested_name.end(),
  67. std::back_inserter(result), [&valid](const char c) {
  68. return (std::string::npos == valid.find(c)) ? '_' : c;
  69. });
  70. return result;
  71. }
  72. void FriendlyNameMapper::SaveName(uint32_t id,
  73. const std::string& suggested_name) {
  74. if (name_for_id_.find(id) != name_for_id_.end()) return;
  75. const std::string sanitized_suggested_name = Sanitize(suggested_name);
  76. std::string name = sanitized_suggested_name;
  77. auto inserted = used_names_.insert(name);
  78. if (!inserted.second) {
  79. const std::string base_name = sanitized_suggested_name + "_";
  80. for (uint32_t index = 0; !inserted.second; ++index) {
  81. name = base_name + to_string(index);
  82. inserted = used_names_.insert(name);
  83. }
  84. }
  85. name_for_id_[id] = name;
  86. }
  87. void FriendlyNameMapper::SaveBuiltInName(uint32_t target_id,
  88. uint32_t built_in) {
  89. #define GLCASE(name) \
  90. case spv::BuiltIn::name: \
  91. SaveName(target_id, "gl_" #name); \
  92. return;
  93. #define GLCASE2(name, suggested) \
  94. case spv::BuiltIn::name: \
  95. SaveName(target_id, "gl_" #suggested); \
  96. return;
  97. #define CASE(name) \
  98. case spv::BuiltIn::name: \
  99. SaveName(target_id, #name); \
  100. return;
  101. switch (spv::BuiltIn(built_in)) {
  102. GLCASE(Position)
  103. GLCASE(PointSize)
  104. GLCASE(ClipDistance)
  105. GLCASE(CullDistance)
  106. GLCASE2(VertexId, VertexID)
  107. GLCASE2(InstanceId, InstanceID)
  108. GLCASE2(PrimitiveId, PrimitiveID)
  109. GLCASE2(InvocationId, InvocationID)
  110. GLCASE(Layer)
  111. GLCASE(ViewportIndex)
  112. GLCASE(TessLevelOuter)
  113. GLCASE(TessLevelInner)
  114. GLCASE(TessCoord)
  115. GLCASE(PatchVertices)
  116. GLCASE(FragCoord)
  117. GLCASE(PointCoord)
  118. GLCASE(FrontFacing)
  119. GLCASE2(SampleId, SampleID)
  120. GLCASE(SamplePosition)
  121. GLCASE(SampleMask)
  122. GLCASE(FragDepth)
  123. GLCASE(HelperInvocation)
  124. GLCASE2(NumWorkgroups, NumWorkGroups)
  125. GLCASE2(WorkgroupSize, WorkGroupSize)
  126. GLCASE2(WorkgroupId, WorkGroupID)
  127. GLCASE2(LocalInvocationId, LocalInvocationID)
  128. GLCASE2(GlobalInvocationId, GlobalInvocationID)
  129. GLCASE(LocalInvocationIndex)
  130. CASE(WorkDim)
  131. CASE(GlobalSize)
  132. CASE(EnqueuedWorkgroupSize)
  133. CASE(GlobalOffset)
  134. CASE(GlobalLinearId)
  135. CASE(SubgroupSize)
  136. CASE(SubgroupMaxSize)
  137. CASE(NumSubgroups)
  138. CASE(NumEnqueuedSubgroups)
  139. CASE(SubgroupId)
  140. CASE(SubgroupLocalInvocationId)
  141. GLCASE(VertexIndex)
  142. GLCASE(InstanceIndex)
  143. GLCASE(BaseInstance)
  144. CASE(SubgroupEqMaskKHR)
  145. CASE(SubgroupGeMaskKHR)
  146. CASE(SubgroupGtMaskKHR)
  147. CASE(SubgroupLeMaskKHR)
  148. CASE(SubgroupLtMaskKHR)
  149. default:
  150. break;
  151. }
  152. #undef GLCASE
  153. #undef GLCASE2
  154. #undef CASE
  155. }
  156. spv_result_t FriendlyNameMapper::ParseInstruction(
  157. const spv_parsed_instruction_t& inst) {
  158. const auto result_id = inst.result_id;
  159. switch (spv::Op(inst.opcode)) {
  160. case spv::Op::OpName:
  161. SaveName(inst.words[1], spvDecodeLiteralStringOperand(inst, 1));
  162. break;
  163. case spv::Op::OpDecorate:
  164. // Decorations come after OpName. So OpName will take precedence over
  165. // decorations.
  166. //
  167. // In theory, we should also handle OpGroupDecorate. But that's unlikely
  168. // to occur.
  169. if (spv::Decoration(inst.words[2]) == spv::Decoration::BuiltIn) {
  170. assert(inst.num_words > 3);
  171. SaveBuiltInName(inst.words[1], inst.words[3]);
  172. }
  173. break;
  174. case spv::Op::OpTypeVoid:
  175. SaveName(result_id, "void");
  176. break;
  177. case spv::Op::OpTypeBool:
  178. SaveName(result_id, "bool");
  179. break;
  180. case spv::Op::OpTypeInt: {
  181. std::string signedness;
  182. std::string root;
  183. const auto bit_width = inst.words[2];
  184. switch (bit_width) {
  185. case 8:
  186. root = "char";
  187. break;
  188. case 16:
  189. root = "short";
  190. break;
  191. case 32:
  192. root = "int";
  193. break;
  194. case 64:
  195. root = "long";
  196. break;
  197. default:
  198. root = to_string(bit_width);
  199. signedness = "i";
  200. break;
  201. }
  202. if (0 == inst.words[3]) signedness = "u";
  203. SaveName(result_id, signedness + root);
  204. } break;
  205. case spv::Op::OpTypeFloat: {
  206. const auto bit_width = inst.words[2];
  207. switch (bit_width) {
  208. case 16:
  209. SaveName(result_id, "half");
  210. break;
  211. case 32:
  212. SaveName(result_id, "float");
  213. break;
  214. case 64:
  215. SaveName(result_id, "double");
  216. break;
  217. default:
  218. SaveName(result_id, std::string("fp") + to_string(bit_width));
  219. break;
  220. }
  221. } break;
  222. case spv::Op::OpTypeVector:
  223. SaveName(result_id, std::string("v") + to_string(inst.words[3]) +
  224. NameForId(inst.words[2]));
  225. break;
  226. case spv::Op::OpTypeMatrix:
  227. SaveName(result_id, std::string("mat") + to_string(inst.words[3]) +
  228. NameForId(inst.words[2]));
  229. break;
  230. case spv::Op::OpTypeArray:
  231. SaveName(result_id, std::string("_arr_") + NameForId(inst.words[2]) +
  232. "_" + NameForId(inst.words[3]));
  233. break;
  234. case spv::Op::OpTypeRuntimeArray:
  235. SaveName(result_id,
  236. std::string("_runtimearr_") + NameForId(inst.words[2]));
  237. break;
  238. case spv::Op::OpTypePointer:
  239. SaveName(result_id, std::string("_ptr_") +
  240. NameForEnumOperand(SPV_OPERAND_TYPE_STORAGE_CLASS,
  241. inst.words[2]) +
  242. "_" + NameForId(inst.words[3]));
  243. break;
  244. case spv::Op::OpTypePipe:
  245. SaveName(result_id,
  246. std::string("Pipe") +
  247. NameForEnumOperand(SPV_OPERAND_TYPE_ACCESS_QUALIFIER,
  248. inst.words[2]));
  249. break;
  250. case spv::Op::OpTypeEvent:
  251. SaveName(result_id, "Event");
  252. break;
  253. case spv::Op::OpTypeDeviceEvent:
  254. SaveName(result_id, "DeviceEvent");
  255. break;
  256. case spv::Op::OpTypeReserveId:
  257. SaveName(result_id, "ReserveId");
  258. break;
  259. case spv::Op::OpTypeQueue:
  260. SaveName(result_id, "Queue");
  261. break;
  262. case spv::Op::OpTypeOpaque:
  263. SaveName(result_id, std::string("Opaque_") +
  264. Sanitize(spvDecodeLiteralStringOperand(inst, 1)));
  265. break;
  266. case spv::Op::OpTypePipeStorage:
  267. SaveName(result_id, "PipeStorage");
  268. break;
  269. case spv::Op::OpTypeNamedBarrier:
  270. SaveName(result_id, "NamedBarrier");
  271. break;
  272. case spv::Op::OpTypeStruct:
  273. // Structs are mapped rather simplisitically. Just indicate that they
  274. // are a struct and then give the raw Id number.
  275. SaveName(result_id, std::string("_struct_") + to_string(result_id));
  276. break;
  277. case spv::Op::OpConstantTrue:
  278. SaveName(result_id, "true");
  279. break;
  280. case spv::Op::OpConstantFalse:
  281. SaveName(result_id, "false");
  282. break;
  283. case spv::Op::OpConstant: {
  284. std::ostringstream value;
  285. EmitNumericLiteral(&value, inst, inst.operands[2]);
  286. auto value_str = value.str();
  287. // Use 'n' to signify negative. Other invalid characters will be mapped
  288. // to underscore.
  289. for (auto& c : value_str)
  290. if (c == '-') c = 'n';
  291. SaveName(result_id, NameForId(inst.type_id) + "_" + value_str);
  292. } break;
  293. default:
  294. // If this instruction otherwise defines an Id, then save a mapping for
  295. // it. This is needed to ensure uniqueness in there is an OpName with
  296. // string something like "1" that might collide with this result_id.
  297. // We should only do this if a name hasn't already been registered by some
  298. // previous forward reference.
  299. if (result_id && name_for_id_.find(result_id) == name_for_id_.end())
  300. SaveName(result_id, to_string(result_id));
  301. break;
  302. }
  303. return SPV_SUCCESS;
  304. }
  305. std::string FriendlyNameMapper::NameForEnumOperand(spv_operand_type_t type,
  306. uint32_t word) {
  307. spv_operand_desc desc = nullptr;
  308. if (SPV_SUCCESS == grammar_.lookupOperand(type, word, &desc)) {
  309. return desc->name;
  310. } else {
  311. // Invalid input. Just give something.
  312. return std::string("StorageClass") + to_string(word);
  313. }
  314. }
  315. } // namespace spvtools