summaryrefslogtreecommitdiff
path: root/tools/comp/markv_model_shader.cpp
blob: 6f64ec0f05274d058d563c91cfa4991297346358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "tools/comp/markv_model_shader.h"

#include <algorithm>
#include <map>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using spvtools::utils::HuffmanCodec;

namespace spvtools {
namespace comp {
namespace {

// Signals that the value is not in the coding scheme and a fallback method
// needs to be used.
const uint64_t kMarkvNoneOfTheAbove = MarkvModel::GetMarkvNoneOfTheAbove();

inline uint32_t CombineOpcodeAndNumOperands(uint32_t opcode,
                                            uint32_t num_operands) {
  return opcode | (num_operands << 16);
}

// The following file contains autogenerated statistical coding rules.
// Can be generated by running spirv-stats on representative corpus of shaders
// with flags:
// --codegen_opcode_and_num_operands_hist
// --codegen_opcode_and_num_operands_markov_huffman_codecs
// --codegen_literal_string_huffman_codecs
// --codegen_non_id_word_huffman_codecs
// --codegen_id_descriptor_huffman_codecs
//
// Example:
// find <SHADER_CORPUS_DIR> -type f -print0 | xargs -0 -s 2000000
// ~/SPIRV-Tools/build/tools/spirv-stats -v
// --codegen_opcode_and_num_operands_hist
// --codegen_opcode_and_num_operands_markov_huffman_codecs
// --codegen_literal_string_huffman_codecs --codegen_non_id_word_huffman_codecs
// --codegen_id_descriptor_huffman_codecs -o
// ~/SPIRV-Tools/source/comp/markv_autogen.inc
#include "tools/comp/markv_model_shader_default_autogen.inc"

}  // namespace

MarkvModelShaderLite::MarkvModelShaderLite() {
  const uint16_t kVersionNumber = 1;
  SetModelVersion(kVersionNumber);

  opcode_and_num_operands_huffman_codec_.reset(
      new HuffmanCodec<uint64_t>(GetOpcodeAndNumOperandsHist()));

  id_fallback_strategy_ = IdFallbackStrategy::kShortDescriptor;
}

MarkvModelShaderMid::MarkvModelShaderMid() {
  const uint16_t kVersionNumber = 1;
  SetModelVersion(kVersionNumber);

  opcode_and_num_operands_huffman_codec_.reset(
      new HuffmanCodec<uint64_t>(GetOpcodeAndNumOperandsHist()));
  non_id_word_huffman_codecs_ = GetNonIdWordHuffmanCodecs();
  id_descriptor_huffman_codecs_ = GetIdDescriptorHuffmanCodecs();
  descriptors_with_coding_scheme_ = GetDescriptorsWithCodingScheme();
  literal_string_huffman_codecs_ = GetLiteralStringHuffmanCodecs();

  id_fallback_strategy_ = IdFallbackStrategy::kShortDescriptor;
}

MarkvModelShaderMax::MarkvModelShaderMax() {
  const uint16_t kVersionNumber = 1;
  SetModelVersion(kVersionNumber);

  opcode_and_num_operands_huffman_codec_.reset(
      new HuffmanCodec<uint64_t>(GetOpcodeAndNumOperandsHist()));
  opcode_and_num_operands_markov_huffman_codecs_ =
      GetOpcodeAndNumOperandsMarkovHuffmanCodecs();
  non_id_word_huffman_codecs_ = GetNonIdWordHuffmanCodecs();
  id_descriptor_huffman_codecs_ = GetIdDescriptorHuffmanCodecs();
  descriptors_with_coding_scheme_ = GetDescriptorsWithCodingScheme();
  literal_string_huffman_codecs_ = GetLiteralStringHuffmanCodecs();

  id_fallback_strategy_ = IdFallbackStrategy::kRuleBased;
}

}  // namespace comp
}  // namespace spvtools