1 // Copyright (c) 2017 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 15 #ifndef LIBSPIRV_ID_DESCRIPTOR_H_ 16 #define LIBSPIRV_ID_DESCRIPTOR_H_ 17 18 #include <unordered_map> 19 #include <vector> 20 21 #include "spirv-tools/libspirv.hpp" 22 23 namespace libspirv { 24 25 // Computes and stores id descriptors. 26 // 27 // Descriptors are computed as hash of all words in the instruction where ids 28 // were substituted with previously computed descriptors. 29 class IdDescriptorCollection { 30 public: 31 IdDescriptorCollection() { 32 words_.reserve(16); 33 } 34 35 // Computes descriptor for the result id of the given instruction and 36 // registers it in id_to_descriptor_. Returns the computed descriptor. 37 // This function needs to be sequentially called for every instruction in the 38 // module. 39 uint32_t ProcessInstruction(const spv_parsed_instruction_t& inst); 40 41 // Returns a previously computed descriptor id. 42 uint32_t GetDescriptor(uint32_t id) const { 43 const auto it = id_to_descriptor_.find(id); 44 if (it == id_to_descriptor_.end()) 45 return 0; 46 return it->second; 47 } 48 49 private: 50 std::unordered_map<uint32_t, uint32_t> id_to_descriptor_; 51 52 // Scratch buffer used for hashing. Class member to optimize on allocation. 53 std::vector<uint32_t> words_; 54 }; 55 56 } // namespace libspirv 57 58 #endif // LIBSPIRV_ID_DESCRIPTOR_H_ 59 60