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_SPIRV_STATS_H_ 16 #define LIBSPIRV_SPIRV_STATS_H_ 17 18 #include <string> 19 #include <unordered_map> 20 #include <vector> 21 22 #include "spirv-tools/libspirv.hpp" 23 24 namespace libspirv { 25 26 struct SpirvStats { 27 // Version histogram, version_word -> count. 28 std::unordered_map<uint32_t, uint32_t> version_hist; 29 30 // Generator histogram, generator_word -> count. 31 std::unordered_map<uint32_t, uint32_t> generator_hist; 32 33 // Capability histogram, SpvCapabilityXXX -> count. 34 std::unordered_map<uint32_t, uint32_t> capability_hist; 35 36 // Extension histogram, extension_string -> count. 37 std::unordered_map<std::string, uint32_t> extension_hist; 38 39 // Opcode histogram, SpvOpXXX -> count. 40 std::unordered_map<uint32_t, uint32_t> opcode_hist; 41 42 // OpConstant u16 histogram, value -> count. 43 std::unordered_map<uint16_t, uint32_t> u16_constant_hist; 44 45 // OpConstant u32 histogram, value -> count. 46 std::unordered_map<uint32_t, uint32_t> u32_constant_hist; 47 48 // OpConstant u64 histogram, value -> count. 49 std::unordered_map<uint64_t, uint32_t> u64_constant_hist; 50 51 // OpConstant s16 histogram, value -> count. 52 std::unordered_map<int16_t, uint32_t> s16_constant_hist; 53 54 // OpConstant s32 histogram, value -> count. 55 std::unordered_map<int32_t, uint32_t> s32_constant_hist; 56 57 // OpConstant s64 histogram, value -> count. 58 std::unordered_map<int64_t, uint32_t> s64_constant_hist; 59 60 // OpConstant f32 histogram, value -> count. 61 std::unordered_map<float, uint32_t> f32_constant_hist; 62 63 // OpConstant f64 histogram, value -> count. 64 std::unordered_map<double, uint32_t> f64_constant_hist; 65 66 // Used to collect statistics on opcodes triggering other opcodes. 67 // Container scheme: gap between instructions -> cue opcode -> later opcode 68 // -> count. 69 // For example opcode_markov_hist[2][OpFMul][OpFAdd] corresponds to 70 // the number of times an OpMul appears, followed by 2 other instructions, 71 // followed by OpFAdd. 72 // opcode_markov_hist[0][OpFMul][OpFAdd] corresponds to how many times 73 // OpFMul appears, directly followed by OpFAdd. 74 // The size of the outer std::vector also serves as an input parameter, 75 // determining how many steps will be collected. 76 // I.e. do opcode_markov_hist.resize(1) to collect data for one step only. 77 std::vector<std::unordered_map<uint32_t, 78 std::unordered_map<uint32_t, uint32_t>>> opcode_markov_hist; 79 }; 80 81 // Aggregates existing |stats| with new stats extracted from |binary|. 82 spv_result_t AggregateStats( 83 const spv_context_t& context, const uint32_t* words, const size_t num_words, 84 spv_diagnostic* pDiagnostic, SpirvStats* stats); 85 86 } // namespace libspirv 87 88 #endif // LIBSPIRV_SPIRV_STATS_H_ 89